diff options
author | Aleksey Kladov <[email protected]> | 2018-08-10 22:55:32 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2018-08-10 22:55:32 +0100 |
commit | 9863b9161d8d702848516be70c5c8161b7f382e8 (patch) | |
tree | 3aa95abbacf0897e4e738e1fc8ccf10492b7c60c | |
parent | 836e0c1863eaea5dffdf76a658c2ee9d7bc22e6f (diff) |
decorations
-rw-r--r-- | codeless/src/extension.ts | 106 | ||||
-rw-r--r-- | crates/libanalysis/src/lib.rs | 4 | ||||
-rw-r--r-- | crates/server/Cargo.toml | 1 | ||||
-rw-r--r-- | crates/server/src/handlers.rs | 14 | ||||
-rw-r--r-- | crates/server/src/main.rs | 21 | ||||
-rw-r--r-- | crates/server/src/req.rs | 24 |
6 files changed, 143 insertions, 27 deletions
diff --git a/codeless/src/extension.ts b/codeless/src/extension.ts index 792af5a73..99cbfc78e 100644 --- a/codeless/src/extension.ts +++ b/codeless/src/extension.ts | |||
@@ -1,17 +1,9 @@ | |||
1 | 'use strict'; | 1 | 'use strict'; |
2 | import * as vscode from 'vscode'; | 2 | import * as vscode from 'vscode'; |
3 | import { | 3 | import * as lc from 'vscode-languageclient' |
4 | LanguageClient, | ||
5 | LanguageClientOptions, | ||
6 | ServerOptions, | ||
7 | TransportKind, | ||
8 | Executable, | ||
9 | TextDocumentIdentifier, | ||
10 | Range | ||
11 | } from 'vscode-languageclient'; | ||
12 | 4 | ||
13 | 5 | ||
14 | let client: LanguageClient; | 6 | let client: lc.LanguageClient; |
15 | 7 | ||
16 | let uris = { | 8 | let uris = { |
17 | syntaxTree: vscode.Uri.parse('libsyntax-rust://syntaxtree') | 9 | syntaxTree: vscode.Uri.parse('libsyntax-rust://syntaxtree') |
@@ -34,8 +26,7 @@ export function activate(context: vscode.ExtensionContext) { | |||
34 | let request: ExtendSelectionParams = { | 26 | let request: ExtendSelectionParams = { |
35 | textDocument: { uri: editor.document.uri.toString() }, | 27 | textDocument: { uri: editor.document.uri.toString() }, |
36 | selections: editor.selections.map((s) => { | 28 | selections: editor.selections.map((s) => { |
37 | let r: Range = { start: s.start, end: s.end } | 29 | return { start: s.start, end: s.end }; |
38 | return r; | ||
39 | }) | 30 | }) |
40 | } | 31 | } |
41 | let response = await client.sendRequest<ExtendSelectionResult>("m/extendSelection", request) | 32 | let response = await client.sendRequest<ExtendSelectionResult>("m/extendSelection", request) |
@@ -71,26 +62,46 @@ export function deactivate(): Thenable<void> { | |||
71 | } | 62 | } |
72 | 63 | ||
73 | function startServer() { | 64 | function startServer() { |
74 | let run: Executable = { | 65 | let run: lc.Executable = { |
75 | command: "cargo", | 66 | command: "cargo", |
76 | args: ["run", "--package", "m"], | 67 | args: ["run", "--package", "m"], |
77 | options: { cwd: "." } | 68 | options: { cwd: "." } |
78 | } | 69 | } |
79 | let serverOptions: ServerOptions = { | 70 | let serverOptions: lc.ServerOptions = { |
80 | run, | 71 | run, |
81 | debug: run | 72 | debug: run |
82 | }; | 73 | }; |
83 | 74 | ||
84 | let clientOptions: LanguageClientOptions = { | 75 | let clientOptions: lc.LanguageClientOptions = { |
85 | documentSelector: [{ scheme: 'file', language: 'rust' }], | 76 | documentSelector: [{ scheme: 'file', language: 'rust' }], |
86 | }; | 77 | }; |
87 | 78 | ||
88 | client = new LanguageClient( | 79 | client = new lc.LanguageClient( |
89 | 'm', | 80 | 'm', |
90 | 'm languge server', | 81 | 'm languge server', |
91 | serverOptions, | 82 | serverOptions, |
92 | clientOptions, | 83 | clientOptions, |
93 | ); | 84 | ); |
85 | client.onReady().then(() => { | ||
86 | client.onNotification( | ||
87 | new lc.NotificationType("m/publishDecorations"), | ||
88 | (params: PublishDecorationsParams) => { | ||
89 | console.log("A"); | ||
90 | console.log(params.uri); | ||
91 | console.log(vscode.window.activeTextEditor.document.uri.toString()); | ||
92 | console.log("B"); | ||
93 | |||
94 | let editor = vscode.window.visibleTextEditors.find( | ||
95 | (editor) => editor.document.uri.toString() == params.uri | ||
96 | ) | ||
97 | if (editor == null) return; | ||
98 | setHighlights( | ||
99 | editor, | ||
100 | params.decorations, | ||
101 | ) | ||
102 | } | ||
103 | ) | ||
104 | }) | ||
94 | client.start(); | 105 | client.start(); |
95 | } | 106 | } |
96 | 107 | ||
@@ -117,17 +128,72 @@ class TextDocumentContentProvider implements vscode.TextDocumentContentProvider | |||
117 | } | 128 | } |
118 | } | 129 | } |
119 | 130 | ||
131 | |||
132 | const decorations = (() => { | ||
133 | const decor = (obj) => vscode.window.createTextEditorDecorationType({ color: obj }) | ||
134 | return { | ||
135 | background: decor("#3F3F3F"), | ||
136 | error: vscode.window.createTextEditorDecorationType({ | ||
137 | borderColor: "red", | ||
138 | borderStyle: "none none dashed none", | ||
139 | }), | ||
140 | comment: decor("#7F9F7F"), | ||
141 | string: decor("#CC9393"), | ||
142 | keyword: decor("#F0DFAF"), | ||
143 | function: decor("#93E0E3"), | ||
144 | parameter: decor("#94BFF3"), | ||
145 | builtin: decor("#DD6718"), | ||
146 | text: decor("#DCDCCC"), | ||
147 | attribute: decor("#BFEBBF"), | ||
148 | literal: decor("#DFAF8F"), | ||
149 | } | ||
150 | })() | ||
151 | |||
152 | function setHighlights( | ||
153 | editor: vscode.TextEditor, | ||
154 | highlihgs: Array<Decoration> | ||
155 | ) { | ||
156 | let byTag = {} | ||
157 | for (let tag in decorations) { | ||
158 | byTag[tag] = [] | ||
159 | } | ||
160 | |||
161 | for (let d of highlihgs) { | ||
162 | if (!byTag[d.tag]) { | ||
163 | console.log(`unknown tag ${d.tag}`) | ||
164 | continue | ||
165 | } | ||
166 | byTag[d.tag].push(d.range) | ||
167 | } | ||
168 | |||
169 | for (let tag in byTag) { | ||
170 | let dec = decorations[tag] | ||
171 | let ranges = byTag[tag] | ||
172 | editor.setDecorations(dec, ranges) | ||
173 | } | ||
174 | } | ||
175 | |||
120 | interface SyntaxTreeParams { | 176 | interface SyntaxTreeParams { |
121 | textDocument: TextDocumentIdentifier; | 177 | textDocument: lc.TextDocumentIdentifier; |
122 | } | 178 | } |
123 | 179 | ||
124 | type SyntaxTreeResult = string | 180 | type SyntaxTreeResult = string |
125 | 181 | ||
126 | interface ExtendSelectionParams { | 182 | interface ExtendSelectionParams { |
127 | textDocument: TextDocumentIdentifier; | 183 | textDocument: lc.TextDocumentIdentifier; |
128 | selections: Range[]; | 184 | selections: lc.Range[]; |
129 | } | 185 | } |
130 | 186 | ||
131 | interface ExtendSelectionResult { | 187 | interface ExtendSelectionResult { |
132 | selections: Range[]; | 188 | selections: lc.Range[]; |
189 | } | ||
190 | |||
191 | interface PublishDecorationsParams { | ||
192 | uri: string, | ||
193 | decorations: Decoration[], | ||
194 | } | ||
195 | |||
196 | interface Decoration { | ||
197 | range: lc.Range, | ||
198 | tag: string, | ||
133 | } | 199 | } |
diff --git a/crates/libanalysis/src/lib.rs b/crates/libanalysis/src/lib.rs index 6a946a0b0..74f043a9b 100644 --- a/crates/libanalysis/src/lib.rs +++ b/crates/libanalysis/src/lib.rs | |||
@@ -24,6 +24,7 @@ pub struct WorldState { | |||
24 | data: Arc<WorldData> | 24 | data: Arc<WorldData> |
25 | } | 25 | } |
26 | 26 | ||
27 | #[derive(Clone, Debug)] | ||
27 | pub struct World { | 28 | pub struct World { |
28 | data: Arc<WorldData>, | 29 | data: Arc<WorldData>, |
29 | } | 30 | } |
@@ -119,12 +120,13 @@ impl World { | |||
119 | } | 120 | } |
120 | 121 | ||
121 | 122 | ||
122 | #[derive(Default)] | 123 | #[derive(Default, Debug)] |
123 | struct WorldData { | 124 | struct WorldData { |
124 | mem_map: HashMap<PathBuf, Arc<String>>, | 125 | mem_map: HashMap<PathBuf, Arc<String>>, |
125 | file_map: RwLock<HashMap<PathBuf, Arc<FileData>>>, | 126 | file_map: RwLock<HashMap<PathBuf, Arc<FileData>>>, |
126 | } | 127 | } |
127 | 128 | ||
129 | #[derive(Debug)] | ||
128 | struct FileData { | 130 | struct FileData { |
129 | text: Option<String>, | 131 | text: Option<String>, |
130 | syntax: OnceCell<ast::File>, | 132 | syntax: OnceCell<ast::File>, |
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" | |||
15 | flexi_logger = "0.9.0" | 15 | flexi_logger = "0.9.0" |
16 | log = "0.4.3" | 16 | log = "0.4.3" |
17 | url = "1.1.0" | 17 | url = "1.1.0" |
18 | url_serde = "0.2.0" | ||
18 | 19 | ||
19 | libeditor = { path = "../libeditor" } | 20 | libeditor = { path = "../libeditor" } |
20 | libanalysis = { path = "../libanalysis" } | 21 | 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; | |||
4 | use libeditor::{self, LineIndex, LineCol, TextRange, TextUnit}; | 4 | use libeditor::{self, LineIndex, LineCol, TextRange, TextUnit}; |
5 | 5 | ||
6 | use ::{ | 6 | use ::{ |
7 | req, Result, | 7 | req::{self, Decoration}, Result, |
8 | util::FilePath, | 8 | util::FilePath, |
9 | }; | 9 | }; |
10 | 10 | ||
@@ -51,6 +51,18 @@ pub fn publish_diagnostics(world: World, uri: Url) -> Result<req::PublishDiagnos | |||
51 | Ok(req::PublishDiagnosticsParams { uri, diagnostics }) | 51 | Ok(req::PublishDiagnosticsParams { uri, diagnostics }) |
52 | } | 52 | } |
53 | 53 | ||
54 | pub fn publish_decorations(world: World, uri: Url) -> Result<req::PublishDecorationsParams> { | ||
55 | let path = uri.file_path()?; | ||
56 | let file = world.file_syntax(&path)?; | ||
57 | let line_index = world.file_line_index(&path)?; | ||
58 | let decorations = libeditor::highlight(&file) | ||
59 | .into_iter() | ||
60 | .map(|h| Decoration { | ||
61 | range: to_vs_range(&line_index, h.range), | ||
62 | tag: h.tag, | ||
63 | }).collect(); | ||
64 | Ok(req::PublishDecorationsParams { uri, decorations }) | ||
65 | } | ||
54 | 66 | ||
55 | fn to_text_range(line_index: &LineIndex, range: Range) -> TextRange { | 67 | fn to_text_range(line_index: &LineIndex, range: Range) -> TextRange { |
56 | TextRange::from_to( | 68 | 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; | |||
12 | #[macro_use] | 12 | #[macro_use] |
13 | extern crate log; | 13 | extern crate log; |
14 | extern crate url; | 14 | extern crate url; |
15 | extern crate url_serde; | ||
15 | extern crate flexi_logger; | 16 | extern crate flexi_logger; |
16 | extern crate libeditor; | 17 | extern crate libeditor; |
17 | extern crate libanalysis; | 18 | extern crate libanalysis; |
@@ -31,7 +32,7 @@ use libanalysis::{WorldState, World}; | |||
31 | 32 | ||
32 | use ::{ | 33 | use ::{ |
33 | io::{Io, RawMsg, RawRequest}, | 34 | io::{Io, RawMsg, RawRequest}, |
34 | handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics}, | 35 | handlers::{handle_syntax_tree, handle_extend_selection, publish_diagnostics, publish_decorations}, |
35 | util::{FilePath, FnBox} | 36 | util::{FilePath, FnBox} |
36 | }; | 37 | }; |
37 | 38 | ||
@@ -198,7 +199,7 @@ fn main_loop( | |||
198 | dispatch::handle_notification::<req::DidOpenTextDocument, _>(&mut not, |params| { | 199 | dispatch::handle_notification::<req::DidOpenTextDocument, _>(&mut not, |params| { |
199 | let path = params.text_document.file_path()?; | 200 | let path = params.text_document.file_path()?; |
200 | world.change_overlay(path, Some(params.text_document.text)); | 201 | world.change_overlay(path, Some(params.text_document.text)); |
201 | update_diagnostics_on_threadpool( | 202 | update_file_notifications_on_threadpool( |
202 | pool, world.snapshot(), sender.clone(), params.text_document.uri, | 203 | pool, world.snapshot(), sender.clone(), params.text_document.uri, |
203 | ); | 204 | ); |
204 | Ok(()) | 205 | Ok(()) |
@@ -209,7 +210,7 @@ fn main_loop( | |||
209 | .ok_or_else(|| format_err!("empty changes"))? | 210 | .ok_or_else(|| format_err!("empty changes"))? |
210 | .text; | 211 | .text; |
211 | world.change_overlay(path, Some(text)); | 212 | world.change_overlay(path, Some(text)); |
212 | update_diagnostics_on_threadpool( | 213 | update_file_notifications_on_threadpool( |
213 | pool, world.snapshot(), sender.clone(), params.text_document.uri, | 214 | pool, world.snapshot(), sender.clone(), params.text_document.uri, |
214 | ); | 215 | ); |
215 | Ok(()) | 216 | Ok(()) |
@@ -254,14 +255,14 @@ fn handle_request_on_threadpool<R: req::ClientRequest>( | |||
254 | }) | 255 | }) |
255 | } | 256 | } |
256 | 257 | ||
257 | fn update_diagnostics_on_threadpool( | 258 | fn update_file_notifications_on_threadpool( |
258 | pool: &ThreadPool, | 259 | pool: &ThreadPool, |
259 | world: World, | 260 | world: World, |
260 | sender: Sender<Thunk>, | 261 | sender: Sender<Thunk>, |
261 | uri: Url, | 262 | uri: Url, |
262 | ) { | 263 | ) { |
263 | pool.execute(move || { | 264 | pool.execute(move || { |
264 | match publish_diagnostics(world, uri) { | 265 | match publish_diagnostics(world.clone(), uri.clone()) { |
265 | Err(e) => { | 266 | Err(e) => { |
266 | error!("failed to compute diagnostics: {:?}", e) | 267 | error!("failed to compute diagnostics: {:?}", e) |
267 | } | 268 | } |
@@ -271,5 +272,15 @@ fn update_diagnostics_on_threadpool( | |||
271 | })) | 272 | })) |
272 | } | 273 | } |
273 | } | 274 | } |
275 | match publish_decorations(world, uri) { | ||
276 | Err(e) => { | ||
277 | error!("failed to compute decortions: {:?}", e) | ||
278 | } | ||
279 | Ok(params) => { | ||
280 | sender.send(Box::new(|io: &mut Io| { | ||
281 | dispatch::send_notification::<req::PublishDecorations>(io, params) | ||
282 | })) | ||
283 | } | ||
284 | } | ||
274 | }); | 285 | }); |
275 | } | 286 | } |
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 @@ | |||
1 | use serde::{ser::Serialize, de::DeserializeOwned}; | 1 | use serde::{ser::Serialize, de::DeserializeOwned}; |
2 | use url::Url; | ||
2 | use languageserver_types::{TextDocumentIdentifier, Range}; | 3 | use languageserver_types::{TextDocumentIdentifier, Range}; |
4 | use url_serde; | ||
3 | 5 | ||
4 | pub use languageserver_types::{ | 6 | pub use languageserver_types::{ |
5 | request::*, notification::*, | 7 | request::*, notification::*, |
@@ -58,3 +60,25 @@ pub struct ExtendSelectionParams { | |||
58 | pub struct ExtendSelectionResult { | 60 | pub struct ExtendSelectionResult { |
59 | pub selections: Vec<Range>, | 61 | pub selections: Vec<Range>, |
60 | } | 62 | } |
63 | |||
64 | pub enum PublishDecorations {} | ||
65 | |||
66 | impl Notification for PublishDecorations { | ||
67 | type Params = PublishDecorationsParams; | ||
68 | const METHOD: &'static str = "m/publishDecorations"; | ||
69 | } | ||
70 | |||
71 | #[derive(Serialize, Debug)] | ||
72 | #[serde(rename_all = "camelCase")] | ||
73 | pub struct PublishDecorationsParams { | ||
74 | #[serde(with = "url_serde")] | ||
75 | pub uri: Url, | ||
76 | pub decorations: Vec<Decoration>, | ||
77 | } | ||
78 | |||
79 | #[derive(Serialize, Debug)] | ||
80 | #[serde(rename_all = "camelCase")] | ||
81 | pub struct Decoration { | ||
82 | pub range: Range, | ||
83 | pub tag: &'static str | ||
84 | } | ||