From 9863b9161d8d702848516be70c5c8161b7f382e8 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Sat, 11 Aug 2018 00:55:32 +0300 Subject: decorations --- crates/server/Cargo.toml | 1 + crates/server/src/handlers.rs | 14 +++++++++++++- crates/server/src/main.rs | 21 ++++++++++++++++----- crates/server/src/req.rs | 24 ++++++++++++++++++++++++ 4 files changed, 54 insertions(+), 6 deletions(-) (limited to 'crates/server') diff --git a/crates/server/Cargo.toml b/crates/server/Cargo.toml index b2a7ce5b4..b5e4e1926 100644 --- a/crates/server/Cargo.toml +++ b/crates/server/Cargo.toml @@ -15,6 +15,7 @@ threadpool = "1.7.1" flexi_logger = "0.9.0" log = "0.4.3" url = "1.1.0" +url_serde = "0.2.0" libeditor = { path = "../libeditor" } libanalysis = { path = "../libanalysis" } diff --git a/crates/server/src/handlers.rs b/crates/server/src/handlers.rs index de1fd557d..8b7e00c92 100644 --- a/crates/server/src/handlers.rs +++ b/crates/server/src/handlers.rs @@ -4,7 +4,7 @@ use libanalysis::World; use libeditor::{self, LineIndex, LineCol, TextRange, TextUnit}; use ::{ - req, Result, + req::{self, Decoration}, Result, util::FilePath, }; @@ -51,6 +51,18 @@ pub fn publish_diagnostics(world: World, uri: Url) -> Result Result { + let path = uri.file_path()?; + let file = world.file_syntax(&path)?; + let line_index = world.file_line_index(&path)?; + let decorations = libeditor::highlight(&file) + .into_iter() + .map(|h| Decoration { + range: to_vs_range(&line_index, h.range), + tag: h.tag, + }).collect(); + Ok(req::PublishDecorationsParams { uri, decorations }) +} fn to_text_range(line_index: &LineIndex, range: Range) -> TextRange { TextRange::from_to( diff --git a/crates/server/src/main.rs b/crates/server/src/main.rs index 0e4f5f86a..900ee555f 100644 --- a/crates/server/src/main.rs +++ b/crates/server/src/main.rs @@ -12,6 +12,7 @@ extern crate threadpool; #[macro_use] extern crate log; extern crate url; +extern crate url_serde; extern crate flexi_logger; extern crate libeditor; extern crate libanalysis; @@ -31,7 +32,7 @@ use libanalysis::{WorldState, World}; use ::{ io::{Io, RawMsg, RawRequest}, - handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics}, + handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics, publish_decorations}, util::{FilePath, FnBox} }; @@ -198,7 +199,7 @@ fn main_loop( dispatch::handle_notification::(&mut not, |params| { let path = params.text_document.file_path()?; world.change_overlay(path, Some(params.text_document.text)); - update_diagnostics_on_threadpool( + update_file_notifications_on_threadpool( pool, world.snapshot(), sender.clone(), params.text_document.uri, ); Ok(()) @@ -209,7 +210,7 @@ fn main_loop( .ok_or_else(|| format_err!("empty changes"))? .text; world.change_overlay(path, Some(text)); - update_diagnostics_on_threadpool( + update_file_notifications_on_threadpool( pool, world.snapshot(), sender.clone(), params.text_document.uri, ); Ok(()) @@ -254,14 +255,14 @@ fn handle_request_on_threadpool( }) } -fn update_diagnostics_on_threadpool( +fn update_file_notifications_on_threadpool( pool: &ThreadPool, world: World, sender: Sender, uri: Url, ) { pool.execute(move || { - match publish_diagnostics(world, uri) { + match publish_diagnostics(world.clone(), uri.clone()) { Err(e) => { error!("failed to compute diagnostics: {:?}", e) } @@ -271,5 +272,15 @@ fn update_diagnostics_on_threadpool( })) } } + match publish_decorations(world, uri) { + Err(e) => { + error!("failed to compute decortions: {:?}", e) + } + Ok(params) => { + sender.send(Box::new(|io: &mut Io| { + dispatch::send_notification::(io, params) + })) + } + } }); } diff --git a/crates/server/src/req.rs b/crates/server/src/req.rs index 645a17306..480fbabcd 100644 --- a/crates/server/src/req.rs +++ b/crates/server/src/req.rs @@ -1,5 +1,7 @@ use serde::{ser::Serialize, de::DeserializeOwned}; +use url::Url; use languageserver_types::{TextDocumentIdentifier, Range}; +use url_serde; pub use languageserver_types::{ request::*, notification::*, @@ -58,3 +60,25 @@ pub struct ExtendSelectionParams { pub struct ExtendSelectionResult { pub selections: Vec, } + +pub enum PublishDecorations {} + +impl Notification for PublishDecorations { + type Params = PublishDecorationsParams; + const METHOD: &'static str = "m/publishDecorations"; +} + +#[derive(Serialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct PublishDecorationsParams { + #[serde(with = "url_serde")] + pub uri: Url, + pub decorations: Vec, +} + +#[derive(Serialize, Debug)] +#[serde(rename_all = "camelCase")] +pub struct Decoration { + pub range: Range, + pub tag: &'static str +} -- cgit v1.2.3