diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2019-12-30 22:24:39 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2019-12-30 22:24:39 +0000 |
commit | e6a1ccc1f49488af31ff7429ff977768d3d32485 (patch) | |
tree | 0e6c920b9bb1955de444dc360072b2256060d89b | |
parent | c3d74744cdae29aa6a6bfa0cd7ab64b8b251e287 (diff) | |
parent | 233f1dd2a850a7c8c6947c88c1ce06f7a945befd (diff) |
Merge #2695
2695: Privatize highlighting r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
-rw-r--r-- | editors/code/src/config.ts | 3 | ||||
-rw-r--r-- | editors/code/src/ctx.ts | 17 | ||||
-rw-r--r-- | editors/code/src/highlighting.ts | 40 | ||||
-rw-r--r-- | editors/code/src/inlay_hints.ts | 4 | ||||
-rw-r--r-- | editors/code/src/main.ts | 9 | ||||
-rw-r--r-- | editors/code/src/notifications/index.ts | 3 | ||||
-rw-r--r-- | editors/code/src/notifications/publish_decorations.ts | 24 | ||||
-rw-r--r-- | editors/code/src/server.ts | 2 |
8 files changed, 54 insertions, 48 deletions
diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index a88be6e35..2bd276958 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts | |||
@@ -1,7 +1,6 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
2 | import * as scopes from './scopes'; | 2 | import * as scopes from './scopes'; |
3 | import * as scopesMapper from './scopes_mapper'; | 3 | import * as scopesMapper from './scopes_mapper'; |
4 | import { Server } from './server'; | ||
5 | 4 | ||
6 | const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG; | 5 | const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG; |
7 | 6 | ||
@@ -56,8 +55,6 @@ export class Config { | |||
56 | public userConfigChanged() { | 55 | public userConfigChanged() { |
57 | const config = vscode.workspace.getConfiguration('rust-analyzer'); | 56 | const config = vscode.workspace.getConfiguration('rust-analyzer'); |
58 | 57 | ||
59 | Server.highlighter.removeHighlights(); | ||
60 | |||
61 | let requireReloadMessage = null; | 58 | let requireReloadMessage = null; |
62 | 59 | ||
63 | if (config.has('highlightingOn')) { | 60 | if (config.has('highlightingOn')) { |
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index d3ef27e43..39eddfcbd 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts | |||
@@ -62,23 +62,30 @@ export class Ctx { | |||
62 | this.extCtx.subscriptions.push(d); | 62 | this.extCtx.subscriptions.push(d); |
63 | } | 63 | } |
64 | 64 | ||
65 | async sendRequestWithRetry<R>(method: string, param: any, token: vscode.CancellationToken): Promise<R> { | 65 | async sendRequestWithRetry<R>( |
66 | method: string, | ||
67 | param: any, | ||
68 | token: vscode.CancellationToken, | ||
69 | ): Promise<R> { | ||
66 | await this.client.onReady(); | 70 | await this.client.onReady(); |
67 | for (const delay of [2, 4, 6, 8, 10, null]) { | 71 | for (const delay of [2, 4, 6, 8, 10, null]) { |
68 | try { | 72 | try { |
69 | return await this.client.sendRequest(method, param, token); | 73 | return await this.client.sendRequest(method, param, token); |
70 | } catch (e) { | 74 | } catch (e) { |
71 | if (e.code === lc.ErrorCodes.ContentModified && delay !== null) { | 75 | if ( |
72 | await sleep(10 * (1 << delay)) | 76 | e.code === lc.ErrorCodes.ContentModified && |
77 | delay !== null | ||
78 | ) { | ||
79 | await sleep(10 * (1 << delay)); | ||
73 | continue; | 80 | continue; |
74 | } | 81 | } |
75 | throw e; | 82 | throw e; |
76 | } | 83 | } |
77 | } | 84 | } |
78 | throw 'unreachable' | 85 | throw 'unreachable'; |
79 | } | 86 | } |
80 | } | 87 | } |
81 | 88 | ||
82 | export type Cmd = (...args: any[]) => any; | 89 | export type Cmd = (...args: any[]) => any; |
83 | 90 | ||
84 | const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)) | 91 | const sleep = (ms: number) => new Promise(resolve => setTimeout(resolve, ms)); |
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index 333319b85..c7ee8c0a1 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts | |||
@@ -10,6 +10,36 @@ import { Server } from './server'; | |||
10 | import { Ctx } from './ctx'; | 10 | import { Ctx } from './ctx'; |
11 | 11 | ||
12 | export function activateHighlighting(ctx: Ctx) { | 12 | export function activateHighlighting(ctx: Ctx) { |
13 | const highlighter = new Highlighter(); | ||
14 | |||
15 | ctx.client.onReady().then(() => { | ||
16 | ctx.client.onNotification( | ||
17 | 'rust-analyzer/publishDecorations', | ||
18 | (params: PublishDecorationsParams) => { | ||
19 | if (!ctx.config.highlightingOn) return; | ||
20 | |||
21 | const targetEditor = vscode.window.visibleTextEditors.find( | ||
22 | editor => { | ||
23 | const unescapedUri = unescape( | ||
24 | editor.document.uri.toString(), | ||
25 | ); | ||
26 | // Unescaped URI looks like: | ||
27 | // file:///c:/Workspace/ra-test/src/main.rs | ||
28 | return unescapedUri === params.uri; | ||
29 | }, | ||
30 | ); | ||
31 | if (!targetEditor) return; | ||
32 | |||
33 | highlighter.setHighlights(targetEditor, params.decorations); | ||
34 | }, | ||
35 | ); | ||
36 | }); | ||
37 | |||
38 | vscode.workspace.onDidChangeConfiguration( | ||
39 | _ => highlighter.removeHighlights(), | ||
40 | ctx.subscriptions, | ||
41 | ); | ||
42 | |||
13 | vscode.window.onDidChangeActiveTextEditor( | 43 | vscode.window.onDidChangeActiveTextEditor( |
14 | async (editor: vscode.TextEditor | undefined) => { | 44 | async (editor: vscode.TextEditor | undefined) => { |
15 | if (!editor || editor.document.languageId !== 'rust') return; | 45 | if (!editor || editor.document.languageId !== 'rust') return; |
@@ -22,11 +52,17 @@ export function activateHighlighting(ctx: Ctx) { | |||
22 | 'rust-analyzer/decorationsRequest', | 52 | 'rust-analyzer/decorationsRequest', |
23 | params, | 53 | params, |
24 | ); | 54 | ); |
25 | Server.highlighter.setHighlights(editor, decorations); | 55 | highlighter.setHighlights(editor, decorations); |
26 | }, | 56 | }, |
57 | ctx.subscriptions, | ||
27 | ); | 58 | ); |
28 | } | 59 | } |
29 | 60 | ||
61 | interface PublishDecorationsParams { | ||
62 | uri: string; | ||
63 | decorations: Decoration[]; | ||
64 | } | ||
65 | |||
30 | export interface Decoration { | 66 | export interface Decoration { |
31 | range: lc.Range; | 67 | range: lc.Range; |
32 | tag: string; | 68 | tag: string; |
@@ -81,7 +117,7 @@ function createDecorationFromTextmate( | |||
81 | return vscode.window.createTextEditorDecorationType(decorationOptions); | 117 | return vscode.window.createTextEditorDecorationType(decorationOptions); |
82 | } | 118 | } |
83 | 119 | ||
84 | export class Highlighter { | 120 | class Highlighter { |
85 | private static initDecorations(): Map< | 121 | private static initDecorations(): Map< |
86 | string, | 122 | string, |
87 | vscode.TextEditorDecorationType | 123 | vscode.TextEditorDecorationType |
diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts index d41297407..a7be97db8 100644 --- a/editors/code/src/inlay_hints.ts +++ b/editors/code/src/inlay_hints.ts | |||
@@ -102,7 +102,7 @@ class HintsUpdater { | |||
102 | }; | 102 | }; |
103 | let tokenSource = new vscode.CancellationTokenSource(); | 103 | let tokenSource = new vscode.CancellationTokenSource(); |
104 | let prev = this.pending.get(documentUri); | 104 | let prev = this.pending.get(documentUri); |
105 | if (prev) prev.cancel() | 105 | if (prev) prev.cancel(); |
106 | this.pending.set(documentUri, tokenSource); | 106 | this.pending.set(documentUri, tokenSource); |
107 | try { | 107 | try { |
108 | return await this.ctx.sendRequestWithRetry<InlayHint[] | null>( | 108 | return await this.ctx.sendRequestWithRetry<InlayHint[] | null>( |
@@ -112,7 +112,7 @@ class HintsUpdater { | |||
112 | ); | 112 | ); |
113 | } finally { | 113 | } finally { |
114 | if (!tokenSource.token.isCancellationRequested) { | 114 | if (!tokenSource.token.isCancellationRequested) { |
115 | this.pending.delete(documentUri) | 115 | this.pending.delete(documentUri); |
116 | } | 116 | } |
117 | } | 117 | } |
118 | } | 118 | } |
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 345ae0685..20a3ea119 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts | |||
@@ -4,7 +4,6 @@ import * as lc from 'vscode-languageclient'; | |||
4 | import * as commands from './commands'; | 4 | import * as commands from './commands'; |
5 | import { activateInlayHints } from './inlay_hints'; | 5 | import { activateInlayHints } from './inlay_hints'; |
6 | import { StatusDisplay } from './status_display'; | 6 | import { StatusDisplay } from './status_display'; |
7 | import * as notifications from './notifications'; | ||
8 | import { Server } from './server'; | 7 | import { Server } from './server'; |
9 | import { Ctx } from './ctx'; | 8 | import { Ctx } from './ctx'; |
10 | import { activateHighlighting } from './highlighting'; | 9 | import { activateHighlighting } from './highlighting'; |
@@ -35,15 +34,9 @@ export async function activate(context: vscode.ExtensionContext) { | |||
35 | const watchStatus = new StatusDisplay(ctx.config.cargoWatchOptions.command); | 34 | const watchStatus = new StatusDisplay(ctx.config.cargoWatchOptions.command); |
36 | ctx.pushCleanup(watchStatus); | 35 | ctx.pushCleanup(watchStatus); |
37 | 36 | ||
38 | activateHighlighting(ctx); | ||
39 | |||
40 | // Notifications are events triggered by the language server | 37 | // Notifications are events triggered by the language server |
41 | const allNotifications: [string, lc.GenericNotificationHandler][] = [ | 38 | const allNotifications: [string, lc.GenericNotificationHandler][] = [ |
42 | [ | 39 | [ |
43 | 'rust-analyzer/publishDecorations', | ||
44 | notifications.publishDecorations.handle, | ||
45 | ], | ||
46 | [ | ||
47 | '$/progress', | 40 | '$/progress', |
48 | params => watchStatus.handleProgressNotification(params), | 41 | params => watchStatus.handleProgressNotification(params), |
49 | ], | 42 | ], |
@@ -61,6 +54,8 @@ export async function activate(context: vscode.ExtensionContext) { | |||
61 | vscode.window.showErrorMessage(e.message); | 54 | vscode.window.showErrorMessage(e.message); |
62 | } | 55 | } |
63 | 56 | ||
57 | activateHighlighting(ctx); | ||
58 | |||
64 | if (ctx.config.displayInlayHints) { | 59 | if (ctx.config.displayInlayHints) { |
65 | activateInlayHints(ctx); | 60 | activateInlayHints(ctx); |
66 | } | 61 | } |
diff --git a/editors/code/src/notifications/index.ts b/editors/code/src/notifications/index.ts deleted file mode 100644 index 74c4c3563..000000000 --- a/editors/code/src/notifications/index.ts +++ /dev/null | |||
@@ -1,3 +0,0 @@ | |||
1 | import * as publishDecorations from './publish_decorations'; | ||
2 | |||
3 | export { publishDecorations }; | ||
diff --git a/editors/code/src/notifications/publish_decorations.ts b/editors/code/src/notifications/publish_decorations.ts deleted file mode 100644 index f23e286ad..000000000 --- a/editors/code/src/notifications/publish_decorations.ts +++ /dev/null | |||
@@ -1,24 +0,0 @@ | |||
1 | import * as vscode from 'vscode'; | ||
2 | |||
3 | import { Decoration } from '../highlighting'; | ||
4 | import { Server } from '../server'; | ||
5 | |||
6 | export interface PublishDecorationsParams { | ||
7 | uri: string; | ||
8 | decorations: Decoration[]; | ||
9 | } | ||
10 | |||
11 | export function handle(params: PublishDecorationsParams) { | ||
12 | const targetEditor = vscode.window.visibleTextEditors.find(editor => { | ||
13 | const unescapedUri = unescape(editor.document.uri.toString()); | ||
14 | // Unescaped URI looks like: | ||
15 | // file:///c:/Workspace/ra-test/src/main.rs | ||
16 | return unescapedUri === params.uri; | ||
17 | }); | ||
18 | |||
19 | if (!Server.config.highlightingOn || !targetEditor) { | ||
20 | return; | ||
21 | } | ||
22 | |||
23 | Server.highlighter.setHighlights(targetEditor, params.decorations); | ||
24 | } | ||
diff --git a/editors/code/src/server.ts b/editors/code/src/server.ts index 10dc079fb..2bb21da6b 100644 --- a/editors/code/src/server.ts +++ b/editors/code/src/server.ts | |||
@@ -3,7 +3,6 @@ import * as lc from 'vscode-languageclient'; | |||
3 | 3 | ||
4 | import { window, workspace } from 'vscode'; | 4 | import { window, workspace } from 'vscode'; |
5 | import { Config } from './config'; | 5 | import { Config } from './config'; |
6 | import { Highlighter } from './highlighting'; | ||
7 | 6 | ||
8 | function expandPathResolving(path: string) { | 7 | function expandPathResolving(path: string) { |
9 | if (path.startsWith('~/')) { | 8 | if (path.startsWith('~/')) { |
@@ -13,7 +12,6 @@ function expandPathResolving(path: string) { | |||
13 | } | 12 | } |
14 | 13 | ||
15 | export class Server { | 14 | export class Server { |
16 | public static highlighter = new Highlighter(); | ||
17 | public static config = new Config(); | 15 | public static config = new Config(); |
18 | public static client: lc.LanguageClient; | 16 | public static client: lc.LanguageClient; |
19 | 17 | ||