Document highlights
Put the cursor on a name and a language server highlights every occurrence of
it: textDocument/documentHighlight. Each occurrence is a text, read or
write highlight, and an editor draws them as a background. They are not
semantic tokens, but marks can show them all the same: highlight is the token,
its modifiers say what kind of occurrence it is, and the cursor modifier draws
the cursor on the first character of the token.
```rust,lsp
let mut §highlight.write.cursor|total§ = 0;
§highlight.write|total§ += value;
```
Read occurrences are blue, write occurrences red and plain text occurrences yellow, from the palette of whichever theme the book is shown in. Like every other token, they can be restyled with CSS.
fn running_total(values: &[u32]) -> u32 {
let mut total = 0;
for value in values {
total += value;
if total > 100 {
return total;
}
}
total
}
struct Job {
ready: bool,
}
fn next(queue: &mut Vec<Job>) -> Option<Job> {
let position = queue.iter().position(|job: &Job| job.ready)?;
Some(queue.remove(position))
}
let found = loop {
match queue.pop() {
Some(job) if job.ready => break Some(job),
Some(job) => retry.push(job),
None => break None,
}
};
On a keyword the occurrences are the places control flow leaves: with the cursor
on loop, rust-analyzer highlights each break that ends it.