mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-13 13:48:50 +00:00
More robust highlighting
This commit is contained in:
parent
010075be6a
commit
8f5330cb07
4 changed files with 38 additions and 3 deletions
|
@ -134,6 +134,14 @@ export function activate(context: vscode.ExtensionContext) {
|
||||||
textDocumentContentProvider.eventEmitter.fire(uris.syntaxTree)
|
textDocumentContentProvider.eventEmitter.fire(uris.syntaxTree)
|
||||||
})
|
})
|
||||||
}, null, context.subscriptions)
|
}, null, context.subscriptions)
|
||||||
|
vscode.window.onDidChangeActiveTextEditor(async (editor) => {
|
||||||
|
if (!editor || editor.document.languageId != 'rust') return
|
||||||
|
let params: lc.TextDocumentIdentifier = {
|
||||||
|
uri: editor.document.uri.toString()
|
||||||
|
}
|
||||||
|
let decorations = await client.sendRequest<Decoration[]>("m/decorationsRequest", params)
|
||||||
|
setHighlights(editor, decorations)
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// We need to order this after LS updates, but there's no API for that.
|
// We need to order this after LS updates, but there's no API for that.
|
||||||
|
|
|
@ -7,7 +7,7 @@ use languageserver_types::{
|
||||||
CompletionItem,
|
CompletionItem,
|
||||||
};
|
};
|
||||||
use serde_json::{to_value, from_value};
|
use serde_json::{to_value, from_value};
|
||||||
use libanalysis::{Query, QuickFix};
|
use libanalysis::{Query, QuickFix, FileId};
|
||||||
use libeditor;
|
use libeditor;
|
||||||
use libsyntax2::{
|
use libsyntax2::{
|
||||||
TextUnit,
|
TextUnit,
|
||||||
|
@ -401,18 +401,33 @@ pub fn publish_diagnostics(
|
||||||
Ok(req::PublishDiagnosticsParams { uri, diagnostics })
|
Ok(req::PublishDiagnosticsParams { uri, diagnostics })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn handle_decorations(
|
||||||
|
world: ServerWorld,
|
||||||
|
params: TextDocumentIdentifier,
|
||||||
|
) -> Result<Vec<Decoration>> {
|
||||||
|
let file_id = params.try_conv_with(&world)?;
|
||||||
|
highlight(&world, file_id)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn publish_decorations(
|
pub fn publish_decorations(
|
||||||
world: ServerWorld,
|
world: ServerWorld,
|
||||||
uri: Url
|
uri: Url
|
||||||
) -> Result<req::PublishDecorationsParams> {
|
) -> Result<req::PublishDecorationsParams> {
|
||||||
let file_id = world.uri_to_file_id(&uri)?;
|
let file_id = world.uri_to_file_id(&uri)?;
|
||||||
|
Ok(req::PublishDecorationsParams {
|
||||||
|
uri,
|
||||||
|
decorations: highlight(&world, file_id)?
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn highlight(world: &ServerWorld, file_id: FileId) -> Result<Vec<Decoration>> {
|
||||||
let file = world.analysis().file_syntax(file_id)?;
|
let file = world.analysis().file_syntax(file_id)?;
|
||||||
let line_index = world.analysis().file_line_index(file_id)?;
|
let line_index = world.analysis().file_line_index(file_id)?;
|
||||||
let decorations = libeditor::highlight(&file)
|
let res = libeditor::highlight(&file)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|h| Decoration {
|
.map(|h| Decoration {
|
||||||
range: h.range.conv_with(&line_index),
|
range: h.range.conv_with(&line_index),
|
||||||
tag: h.tag,
|
tag: h.tag,
|
||||||
}).collect();
|
}).collect();
|
||||||
Ok(req::PublishDecorationsParams { uri, decorations })
|
Ok(res)
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,7 @@ use {
|
||||||
handle_join_lines,
|
handle_join_lines,
|
||||||
handle_completion,
|
handle_completion,
|
||||||
handle_runnables,
|
handle_runnables,
|
||||||
|
handle_decorations,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -157,6 +158,9 @@ fn on_request(
|
||||||
handle_request_on_threadpool::<req::JoinLines>(
|
handle_request_on_threadpool::<req::JoinLines>(
|
||||||
&mut req, pool, world, sender, handle_join_lines,
|
&mut req, pool, world, sender, handle_join_lines,
|
||||||
)?;
|
)?;
|
||||||
|
handle_request_on_threadpool::<req::DecorationsRequest>(
|
||||||
|
&mut req, pool, world, sender, handle_decorations,
|
||||||
|
)?;
|
||||||
dispatch::handle_request::<req::ExecuteCommand, _>(&mut req, |params, resp| {
|
dispatch::handle_request::<req::ExecuteCommand, _>(&mut req, |params, resp| {
|
||||||
io.send(RawMsg::Response(resp.into_response(Ok(None))?));
|
io.send(RawMsg::Response(resp.into_response(Ok(None))?));
|
||||||
|
|
||||||
|
|
|
@ -84,6 +84,14 @@ pub struct FindMatchingBraceParams {
|
||||||
pub offsets: Vec<Position>,
|
pub offsets: Vec<Position>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub enum DecorationsRequest {}
|
||||||
|
|
||||||
|
impl Request for DecorationsRequest {
|
||||||
|
type Params = TextDocumentIdentifier;
|
||||||
|
type Result = Vec<Decoration>;
|
||||||
|
const METHOD: &'static str = "m/decorationsRequest";
|
||||||
|
}
|
||||||
|
|
||||||
pub enum PublishDecorations {}
|
pub enum PublishDecorations {}
|
||||||
|
|
||||||
impl Notification for PublishDecorations {
|
impl Notification for PublishDecorations {
|
||||||
|
|
Loading…
Reference in a new issue