From 7646dc046eb562276231de8ec6a4df1bc691cafc Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 30 Dec 2019 20:29:21 +0100 Subject: Encapsulate highlighting activation --- .../code/src/events/change_active_text_editor.ts | 25 ---------------------- editors/code/src/events/index.ts | 3 --- editors/code/src/highlighting.ts | 22 ++++++++++++++++++- editors/code/src/main.ts | 9 +++----- 4 files changed, 24 insertions(+), 35 deletions(-) delete mode 100644 editors/code/src/events/change_active_text_editor.ts delete mode 100644 editors/code/src/events/index.ts diff --git a/editors/code/src/events/change_active_text_editor.ts b/editors/code/src/events/change_active_text_editor.ts deleted file mode 100644 index 4384ee567..000000000 --- a/editors/code/src/events/change_active_text_editor.ts +++ /dev/null @@ -1,25 +0,0 @@ -import { TextEditor } from 'vscode'; -import { TextDocumentIdentifier } from 'vscode-languageclient'; -import { Decoration } from '../highlighting'; -import { Server } from '../server'; - -export function makeHandler() { - return async function handle(editor: TextEditor | undefined) { - if (!editor || editor.document.languageId !== 'rust') { - return; - } - - if (!Server.config.highlightingOn) { - return; - } - - const params: TextDocumentIdentifier = { - uri: editor.document.uri.toString(), - }; - const decorations = await Server.client.sendRequest( - 'rust-analyzer/decorationsRequest', - params, - ); - Server.highlighter.setHighlights(editor, decorations); - }; -} diff --git a/editors/code/src/events/index.ts b/editors/code/src/events/index.ts deleted file mode 100644 index be135474d..000000000 --- a/editors/code/src/events/index.ts +++ /dev/null @@ -1,3 +0,0 @@ -import * as changeActiveTextEditor from './change_active_text_editor'; - -export { changeActiveTextEditor }; diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index 4e224a54c..ced78adc1 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts @@ -1,10 +1,30 @@ -import seedrandom = require('seedrandom'); import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; +import * as seedrandom from 'seedrandom'; + import * as scopes from './scopes'; import * as scopesMapper from './scopes_mapper'; import { Server } from './server'; +import { Ctx } from './ctx'; + +export function activateHighlighting(ctx: Ctx) { + vscode.window.onDidChangeActiveTextEditor( + async (editor: vscode.TextEditor | undefined) => { + if (!editor || editor.document.languageId !== 'rust') return; + if (!Server.config.highlightingOn) return; + + const params: lc.TextDocumentIdentifier = { + uri: editor.document.uri.toString(), + }; + const decorations = await ctx.client.sendRequest( + 'rust-analyzer/decorationsRequest', + params, + ); + Server.highlighter.setHighlights(editor, decorations); + }, + ); +} export interface Decoration { range: lc.Range; diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 7e63a9cac..45657532e 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts @@ -4,10 +4,10 @@ import * as lc from 'vscode-languageclient'; import * as commands from './commands'; import { activateInlayHints } from './inlay_hints'; import { StatusDisplay } from './status_display'; -import * as events from './events'; import * as notifications from './notifications'; import { Server } from './server'; import { Ctx } from './ctx'; +import { activateHighlighting } from './highlighting'; let ctx!: Ctx; @@ -37,6 +37,8 @@ export async function activate(context: vscode.ExtensionContext) { ); ctx.pushCleanup(watchStatus); + activateHighlighting(ctx); + // Notifications are events triggered by the language server const allNotifications: [string, lc.GenericNotificationHandler][] = [ [ @@ -49,11 +51,6 @@ export async function activate(context: vscode.ExtensionContext) { ], ]; - // The events below are plain old javascript events, triggered and handled by vscode - vscode.window.onDidChangeActiveTextEditor( - events.changeActiveTextEditor.makeHandler(), - ); - const startServer = () => Server.start(allNotifications); const reloadCommand = () => reloadServer(startServer); -- cgit v1.2.3