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 /codeless/src/extension.ts | |
parent | 836e0c1863eaea5dffdf76a658c2ee9d7bc22e6f (diff) |
decorations
Diffstat (limited to 'codeless/src/extension.ts')
-rw-r--r-- | codeless/src/extension.ts | 106 |
1 files changed, 86 insertions, 20 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 | } |