aboutsummaryrefslogtreecommitdiff
path: root/editors/code
diff options
context:
space:
mode:
authorVeetaha <[email protected]>2020-02-24 22:49:54 +0000
committerVeetaha <[email protected]>2020-02-24 22:49:54 +0000
commit8c4409b3bb6dcdc439c7ea98dfb89c0181969323 (patch)
tree28ed5a364c6c04692157fb52a4ed5d030fbe902b /editors/code
parent21ab13396672a4ad75b93bbb73af02d019ef918b (diff)
vscode: migrate highlighting to rust-analyzer-api.ts
Diffstat (limited to 'editors/code')
-rw-r--r--editors/code/src/highlighting.ts60
1 files changed, 21 insertions, 39 deletions
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts
index 77b4a1a68..3e0cbdc56 100644
--- a/editors/code/src/highlighting.ts
+++ b/editors/code/src/highlighting.ts
@@ -1,5 +1,5 @@
1import * as vscode from 'vscode'; 1import * as vscode from 'vscode';
2import * as lc from 'vscode-languageclient'; 2import * as ra from './rust-analyzer-api';
3 3
4import { ColorTheme, TextMateRuleSettings } from './color_theme'; 4import { ColorTheme, TextMateRuleSettings } from './color_theme';
5 5
@@ -8,29 +8,25 @@ import { sendRequestWithRetry } from './util';
8 8
9export function activateHighlighting(ctx: Ctx) { 9export function activateHighlighting(ctx: Ctx) {
10 const highlighter = new Highlighter(ctx); 10 const highlighter = new Highlighter(ctx);
11 const client = ctx.client;
12 if (client != null) {
13 client.onNotification(
14 'rust-analyzer/publishDecorations',
15 (params: PublishDecorationsParams) => {
16 if (!ctx.config.highlightingOn) return;
17
18 const targetEditor = vscode.window.visibleTextEditors.find(
19 editor => {
20 const unescapedUri = unescape(
21 editor.document.uri.toString(),
22 );
23 // Unescaped URI looks like:
24 // file:///c:/Workspace/ra-test/src/main.rs
25 return unescapedUri === params.uri;
26 },
27 );
28 if (!targetEditor) return;
29 11
30 highlighter.setHighlights(targetEditor, params.decorations); 12 ctx.client.onNotification(ra.publishDecorations, params => {
13 if (!ctx.config.highlightingOn) return;
14
15 const targetEditor = vscode.window.visibleTextEditors.find(
16 editor => {
17 const unescapedUri = unescape(
18 editor.document.uri.toString(),
19 );
20 // Unescaped URI looks like:
21 // file:///c:/Workspace/ra-test/src/main.rs
22 return unescapedUri === params.uri;
31 }, 23 },
32 ); 24 );
33 } 25 if (!targetEditor) return;
26
27 highlighter.setHighlights(targetEditor, params.decorations);
28 });
29
34 30
35 vscode.workspace.onDidChangeConfiguration( 31 vscode.workspace.onDidChangeConfiguration(
36 _ => highlighter.removeHighlights(), 32 _ => highlighter.removeHighlights(),
@@ -45,13 +41,10 @@ export function activateHighlighting(ctx: Ctx) {
45 const client = ctx.client; 41 const client = ctx.client;
46 if (!client) return; 42 if (!client) return;
47 43
48 const params: lc.TextDocumentIdentifier = { 44 const decorations = await sendRequestWithRetry(
49 uri: editor.document.uri.toString(),
50 };
51 const decorations = await sendRequestWithRetry<Decoration[]>(
52 client, 45 client,
53 'rust-analyzer/decorationsRequest', 46 ra.decorationsRequest,
54 params, 47 { uri: editor.document.uri.toString() },
55 ); 48 );
56 highlighter.setHighlights(editor, decorations); 49 highlighter.setHighlights(editor, decorations);
57 }, 50 },
@@ -60,17 +53,6 @@ export function activateHighlighting(ctx: Ctx) {
60 ); 53 );
61} 54}
62 55
63interface PublishDecorationsParams {
64 uri: string;
65 decorations: Decoration[];
66}
67
68interface Decoration {
69 range: lc.Range;
70 tag: string;
71 bindingHash?: string;
72}
73
74// Based on this HSL-based color generator: https://gist.github.com/bendc/76c48ce53299e6078a76 56// Based on this HSL-based color generator: https://gist.github.com/bendc/76c48ce53299e6078a76
75function fancify(seed: string, shade: 'light' | 'dark') { 57function fancify(seed: string, shade: 'light' | 'dark') {
76 const random = randomU32Numbers(hashString(seed)); 58 const random = randomU32Numbers(hashString(seed));
@@ -108,7 +90,7 @@ class Highlighter {
108 this.decorations = null; 90 this.decorations = null;
109 } 91 }
110 92
111 public setHighlights(editor: vscode.TextEditor, highlights: Decoration[]) { 93 public setHighlights(editor: vscode.TextEditor, highlights: ra.Decoration[]) {
112 const client = this.ctx.client; 94 const client = this.ctx.client;
113 if (!client) return; 95 if (!client) return;
114 // Initialize decorations if necessary 96 // Initialize decorations if necessary