From 8346bdc04d5bbaad133c6377bf8863a2d298d55a Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 31 Dec 2019 02:17:50 +0100 Subject: Rearrange code --- editors/code/src/ctx.ts | 2 +- editors/code/src/highlighting.ts | 211 +++++++++++++++++++-------------------- 2 files changed, 106 insertions(+), 107 deletions(-) (limited to 'editors') diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 39eddfcbd..30dd9811c 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts @@ -65,7 +65,7 @@ export class Ctx { async sendRequestWithRetry( method: string, param: any, - token: vscode.CancellationToken, + token?: vscode.CancellationToken, ): Promise { await this.client.onReady(); for (const delay of [2, 4, 6, 8, 10, null]) { diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index 247673b5c..4c2e7f67b 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts @@ -47,7 +47,7 @@ export function activateHighlighting(ctx: Ctx) { const params: lc.TextDocumentIdentifier = { uri: editor.document.uri.toString(), }; - const decorations = await ctx.client.sendRequest( + const decorations = await ctx.sendRequestWithRetry( 'rust-analyzer/decorationsRequest', params, ); @@ -62,7 +62,7 @@ interface PublishDecorationsParams { decorations: Decoration[]; } -export interface Decoration { +interface Decoration { range: lc.Range; tag: string; bindingHash?: string; @@ -81,116 +81,17 @@ function fancify(seed: string, shade: 'light' | 'dark') { return `hsl(${h},${s}%,${l}%)`; } -function createDecorationFromTextmate( - themeStyle: scopes.TextMateRuleSettings, -): vscode.TextEditorDecorationType { - const decorationOptions: vscode.DecorationRenderOptions = {}; - decorationOptions.rangeBehavior = vscode.DecorationRangeBehavior.OpenOpen; - - if (themeStyle.foreground) { - decorationOptions.color = themeStyle.foreground; - } - - if (themeStyle.background) { - decorationOptions.backgroundColor = themeStyle.background; - } - - if (themeStyle.fontStyle) { - const parts: string[] = themeStyle.fontStyle.split(' '); - parts.forEach(part => { - switch (part) { - case 'italic': - decorationOptions.fontStyle = 'italic'; - break; - case 'bold': - decorationOptions.fontWeight = 'bold'; - break; - case 'underline': - decorationOptions.textDecoration = 'underline'; - break; - default: - break; - } - }); - } - return vscode.window.createTextEditorDecorationType(decorationOptions); -} - class Highlighter { private ctx: Ctx; - - constructor(ctx: Ctx) { - this.ctx = ctx; - } - - private static initDecorations(): Map< - string, - vscode.TextEditorDecorationType - > { - const decoration = ( - tag: string, - textDecoration?: string, - ): [string, vscode.TextEditorDecorationType] => { - const rule = scopesMapper.toRule(tag, scopes.find); - - if (rule) { - const decor = createDecorationFromTextmate(rule); - return [tag, decor]; - } else { - const fallBackTag = 'ralsp.' + tag; - // console.log(' '); - // console.log('Missing theme for: <"' + tag + '"> for following mapped scopes:'); - // console.log(scopesMapper.find(tag)); - // console.log('Falling back to values defined in: ' + fallBackTag); - // console.log(' '); - const color = new vscode.ThemeColor(fallBackTag); - const decor = vscode.window.createTextEditorDecorationType({ - color, - textDecoration, - }); - return [tag, decor]; - } - }; - - const decorations: Iterable<[ - string, - vscode.TextEditorDecorationType, - ]> = [ - decoration('comment'), - decoration('string'), - decoration('keyword'), - decoration('keyword.control'), - decoration('keyword.unsafe'), - decoration('function'), - decoration('parameter'), - decoration('constant'), - decoration('type.builtin'), - decoration('type.generic'), - decoration('type.lifetime'), - decoration('type.param'), - decoration('type.self'), - decoration('type'), - decoration('text'), - decoration('attribute'), - decoration('literal'), - decoration('literal.numeric'), - decoration('literal.char'), - decoration('literal.byte'), - decoration('macro'), - decoration('variable'), - decoration('variable.mut', 'underline'), - decoration('field'), - decoration('module'), - ]; - - return new Map(decorations); - } - private decorations: Map< string, vscode.TextEditorDecorationType > | null = null; + constructor(ctx: Ctx) { + this.ctx = ctx; + } + public removeHighlights() { if (this.decorations == null) { return; @@ -210,7 +111,7 @@ class Highlighter { // Note: decoration objects need to be kept around so we can dispose them // if the user disables syntax highlighting if (this.decorations == null) { - this.decorations = Highlighter.initDecorations(); + this.decorations = initDecorations(); } const byTag: Map = new Map(); @@ -266,3 +167,101 @@ class Highlighter { } } } + +function initDecorations(): Map< + string, + vscode.TextEditorDecorationType +> { + const decoration = ( + tag: string, + textDecoration?: string, + ): [string, vscode.TextEditorDecorationType] => { + const rule = scopesMapper.toRule(tag, scopes.find); + + if (rule) { + const decor = createDecorationFromTextmate(rule); + return [tag, decor]; + } else { + const fallBackTag = 'ralsp.' + tag; + // console.log(' '); + // console.log('Missing theme for: <"' + tag + '"> for following mapped scopes:'); + // console.log(scopesMapper.find(tag)); + // console.log('Falling back to values defined in: ' + fallBackTag); + // console.log(' '); + const color = new vscode.ThemeColor(fallBackTag); + const decor = vscode.window.createTextEditorDecorationType({ + color, + textDecoration, + }); + return [tag, decor]; + } + }; + + const decorations: Iterable<[ + string, + vscode.TextEditorDecorationType, + ]> = [ + decoration('comment'), + decoration('string'), + decoration('keyword'), + decoration('keyword.control'), + decoration('keyword.unsafe'), + decoration('function'), + decoration('parameter'), + decoration('constant'), + decoration('type.builtin'), + decoration('type.generic'), + decoration('type.lifetime'), + decoration('type.param'), + decoration('type.self'), + decoration('type'), + decoration('text'), + decoration('attribute'), + decoration('literal'), + decoration('literal.numeric'), + decoration('literal.char'), + decoration('literal.byte'), + decoration('macro'), + decoration('variable'), + decoration('variable.mut', 'underline'), + decoration('field'), + decoration('module'), + ]; + + return new Map(decorations); +} + +function createDecorationFromTextmate( + themeStyle: scopes.TextMateRuleSettings, +): vscode.TextEditorDecorationType { + const decorationOptions: vscode.DecorationRenderOptions = {}; + decorationOptions.rangeBehavior = vscode.DecorationRangeBehavior.OpenOpen; + + if (themeStyle.foreground) { + decorationOptions.color = themeStyle.foreground; + } + + if (themeStyle.background) { + decorationOptions.backgroundColor = themeStyle.background; + } + + if (themeStyle.fontStyle) { + const parts: string[] = themeStyle.fontStyle.split(' '); + parts.forEach(part => { + switch (part) { + case 'italic': + decorationOptions.fontStyle = 'italic'; + break; + case 'bold': + decorationOptions.fontWeight = 'bold'; + break; + case 'underline': + decorationOptions.textDecoration = 'underline'; + break; + default: + break; + } + }); + } + return vscode.window.createTextEditorDecorationType(decorationOptions); +} -- cgit v1.2.3