From 83a33fbbeae9cbec8bec855e9338b7ccd08bd3a0 Mon Sep 17 00:00:00 2001 From: Seivan Heidari Date: Sat, 9 Nov 2019 17:23:30 +0100 Subject: Vscode wasn't running the linter automatically so ran `npm run fix` - wonder if it's related to `tslint` being deprecated. --- editors/code/package.json | 2 +- editors/code/src/config.ts | 5 +-- editors/code/src/extension.ts | 10 ++--- editors/code/src/highlighting.ts | 81 ++++++++++++++------------------------- editors/code/src/scopes.ts | 60 +++++++++++++++++------------ editors/code/src/scopes_mapper.ts | 58 ++++++++++++++++++---------- 6 files changed, 110 insertions(+), 106 deletions(-) diff --git a/editors/code/package.json b/editors/code/package.json index 35f2f1e62..5c5be34db 100644 --- a/editors/code/package.json +++ b/editors/code/package.json @@ -550,4 +550,4 @@ } ] } -} \ No newline at end of file +} diff --git a/editors/code/src/config.ts b/editors/code/src/config.ts index 234a390ac..4cedbea46 100644 --- a/editors/code/src/config.ts +++ b/editors/code/src/config.ts @@ -46,14 +46,14 @@ export class Config { public userConfigChanged() { const config = vscode.workspace.getConfiguration('rust-analyzer'); - + Server.highlighter.removeHighlights(); if (config.has('highlightingOn')) { this.highlightingOn = config.get('highlightingOn') as boolean; if (this.highlightingOn) { scopes.load(); - scopesMapper.load(); + scopesMapper.load(); } } @@ -63,7 +63,6 @@ export class Config { ) as boolean; } - if (config.has('enableEnhancedTyping')) { this.enableEnhancedTyping = config.get( 'enableEnhancedTyping' diff --git a/editors/code/src/extension.ts b/editors/code/src/extension.ts index 07a5c59e8..c06928d12 100644 --- a/editors/code/src/extension.ts +++ b/editors/code/src/extension.ts @@ -91,11 +91,11 @@ export function activate(context: vscode.ExtensionContext) { const allNotifications: Iterable< [string, lc.GenericNotificationHandler] > = [ - [ - 'rust-analyzer/publishDecorations', - notifications.publishDecorations.handle - ] - ]; + [ + 'rust-analyzer/publishDecorations', + notifications.publishDecorations.handle + ] + ]; const syntaxTreeContentProvider = new SyntaxTreeContentProvider(); // The events below are plain old javascript events, triggered and handled by vscode diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index ee39ca64c..14199dbea 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts @@ -25,35 +25,15 @@ function fancify(seed: string, shade: 'light' | 'dark') { return `hsl(${h},${s}%,${l}%)`; } - -function createDecorationFromTextmate(themeStyle: scopes.TextMateRuleSettings): vscode.TextEditorDecorationType { - const options: vscode.DecorationRenderOptions = {}; - options.rangeBehavior = vscode.DecorationRangeBehavior.OpenOpen; - if (themeStyle.foreground) { - options.color = themeStyle.foreground; - } - if (themeStyle.background) { - options.backgroundColor = themeStyle.background; - } - if (themeStyle.fontStyle) { - const parts: string[] = themeStyle.fontStyle.split(' '); - parts.forEach((part) => { - switch (part) { - case 'italic': - options.fontStyle = 'italic'; - break; - case 'bold': - options.fontWeight = 'bold'; - break; - case 'underline': - options.textDecoration = 'underline'; - break; - default: - break; - } - }) - } - return vscode.window.createTextEditorDecorationType(options); +function createDecorationFromTextmate( + themeStyle: scopes.TextMateRuleSettings +): vscode.TextEditorDecorationType { + const decorationOptions: vscode.DecorationRenderOptions = {}; + decorationOptions.rangeBehavior = vscode.DecorationRangeBehavior.OpenOpen; + decorationOptions.color = themeStyle.foreground; + decorationOptions.backgroundColor = themeStyle.background; + decorationOptions.fontStyle = themeStyle.fontStyle; + return vscode.window.createTextEditorDecorationType(decorationOptions); } export class Highlighter { @@ -65,14 +45,12 @@ export class Highlighter { tag: string, textDecoration?: string ): [string, vscode.TextEditorDecorationType] => { - const rule = scopesMapper.toRule(tag, scopes.find); if (rule) { const decor = createDecorationFromTextmate(rule); return [tag, decor]; - } - else { + } else { const fallBackTag = 'ralsp.' + tag; // console.log(' '); // console.log('Missing theme for: <"' + tag + '"> for following mapped scopes:'); @@ -91,25 +69,25 @@ export class Highlighter { 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'), - decoration('builtin'), - decoration('text'), - decoration('attribute'), - decoration('literal'), - decoration('macro'), - decoration('variable'), - decoration('variable.mut', 'underline'), - decoration('field'), - decoration('module') - ]; + decoration('comment'), + decoration('string'), + decoration('keyword'), + decoration('keyword.control'), + decoration('keyword.unsafe'), + decoration('function'), + decoration('parameter'), + decoration('constant'), + decoration('type'), + decoration('builtin'), + decoration('text'), + decoration('attribute'), + decoration('literal'), + decoration('macro'), + decoration('variable'), + decoration('variable.mut', 'underline'), + decoration('field'), + decoration('module') + ]; return new Map(decorations); } @@ -138,7 +116,6 @@ export 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(); } diff --git a/editors/code/src/scopes.ts b/editors/code/src/scopes.ts index a6138fad0..98099872c 100644 --- a/editors/code/src/scopes.ts +++ b/editors/code/src/scopes.ts @@ -2,8 +2,6 @@ import * as fs from 'fs'; import * as path from 'path'; import * as vscode from 'vscode'; - - export interface TextMateRule { scope: string | string[]; settings: TextMateRuleSettings; @@ -27,7 +25,9 @@ export function load() { // Remove any previous theme rules.clear(); // Find out current color theme - const themeName = vscode.workspace.getConfiguration('workbench').get('colorTheme'); + const themeName = vscode.workspace + .getConfiguration('workbench') + .get('colorTheme'); if (typeof themeName !== 'string') { // console.warn('workbench.colorTheme is', themeName) @@ -42,38 +42,43 @@ export function load() { } function filterThemeExtensions(extension: vscode.Extension): boolean { - return extension.extensionKind === vscode.ExtensionKind.UI && + return ( + extension.extensionKind === vscode.ExtensionKind.UI && extension.packageJSON.contributes && - extension.packageJSON.contributes.themes; + extension.packageJSON.contributes.themes + ); } - - // Find current theme on disk function loadThemeNamed(themeName: string) { - const themePaths = vscode.extensions.all .filter(filterThemeExtensions) .reduce((list, extension) => { return extension.packageJSON.contributes.themes - .filter((element: any) => (element.id || element.label) === themeName) - .map((element: any) => path.join(extension.extensionPath, element.path)) - .concat(list) + .filter( + (element: any) => + (element.id || element.label) === themeName + ) + .map((element: any) => + path.join(extension.extensionPath, element.path) + ) + .concat(list); }, Array()); - themePaths.forEach(loadThemeFile); - const tokenColorCustomizations: [any] = [vscode.workspace.getConfiguration('editor').get('tokenColorCustomizations')] + const tokenColorCustomizations: [any] = [ + vscode.workspace + .getConfiguration('editor') + .get('tokenColorCustomizations') + ]; tokenColorCustomizations .filter(custom => custom && custom.textMateRules) .map(custom => custom.textMateRules) .forEach(loadColors); - } - function loadThemeFile(themePath: string) { const themeContent = [themePath] .filter(isFile) @@ -92,18 +97,26 @@ function loadThemeFile(themePath: string) { .forEach(loadThemeFile); } -function mergeRuleSettings(defaultSetting: TextMateRuleSettings | undefined, override: TextMateRuleSettings): TextMateRuleSettings { - if (defaultSetting === undefined) { return override; } +function mergeRuleSettings( + defaultSetting: TextMateRuleSettings | undefined, + override: TextMateRuleSettings +): TextMateRuleSettings { + if (defaultSetting === undefined) { + return override; + } const mergedRule = defaultSetting; mergedRule.background = override.background || defaultSetting.background; mergedRule.foreground = override.foreground || defaultSetting.foreground; mergedRule.fontStyle = override.fontStyle || defaultSetting.foreground; - return mergedRule + return mergedRule; } -function updateRules(scope: string, updatedSettings: TextMateRuleSettings): void { +function updateRules( + scope: string, + updatedSettings: TextMateRuleSettings +): void { [rules.get(scope)] .map(settings => mergeRuleSettings(settings, updatedSettings)) .forEach(settings => rules.set(scope, settings)); @@ -113,11 +126,10 @@ function loadColors(textMateRules: TextMateRule[]): void { textMateRules.forEach(rule => { if (typeof rule.scope === 'string') { updateRules(rule.scope, rule.settings); - } - else if (rule.scope instanceof Array) { + } else if (rule.scope instanceof Array) { rule.scope.forEach(scope => updateRules(scope, rule.settings)); } - }) + }); } function isFile(filePath: string): boolean { @@ -128,7 +140,7 @@ function readFileText(filePath: string): string { return fs.readFileSync(filePath, 'utf8'); } -// Might need to replace with JSONC if a theme contains comments. +// Might need to replace with JSONC if a theme contains comments. function parseJSON(content: string): any { return JSON.parse(content); -} \ No newline at end of file +} diff --git a/editors/code/src/scopes_mapper.ts b/editors/code/src/scopes_mapper.ts index dfb8bf217..85c791ff5 100644 --- a/editors/code/src/scopes_mapper.ts +++ b/editors/code/src/scopes_mapper.ts @@ -1,17 +1,25 @@ import * as vscode from 'vscode'; import { TextMateRuleSettings } from './scopes'; - - let mappings = new Map(); - const defaultMapping = new Map([ - ['comment', ['comment', 'comment.block', 'comment.line', 'comment.block.documentation']], + [ + 'comment', + [ + 'comment', + 'comment.block', + 'comment.line', + 'comment.block.documentation' + ] + ], ['string', ['string']], ['keyword', ['keyword']], ['keyword.control', ['keyword.control', 'keyword', 'keyword.other']], - ['keyword.unsafe', ['storage.modifier', 'keyword.other', 'keyword.control', 'keyword']], + [ + 'keyword.unsafe', + ['storage.modifier', 'keyword.other', 'keyword.control', 'keyword'] + ], ['function', ['entity.name.function']], ['parameter', ['variable.parameter']], ['constant', ['constant', 'variable']], @@ -23,21 +31,32 @@ const defaultMapping = new Map([ ['macro', ['support.other']], ['variable', ['variable']], ['variable.mut', ['variable', 'storage.modifier']], - ['field', ['variable.object.property', 'meta.field.declaration', 'meta.definition.property', 'variable.other',]], + [ + 'field', + [ + 'variable.object.property', + 'meta.field.declaration', + 'meta.definition.property', + 'variable.other' + ] + ], ['module', ['entity.name.section', 'entity.other']] -] -); +]); -// Temporary exported for debugging for now. +// Temporary exported for debugging for now. export function find(scope: string): string[] { return mappings.get(scope) || []; } -export function toRule(scope: string, intoRule: (scope: string) => TextMateRuleSettings | undefined): TextMateRuleSettings | undefined { - return find(scope).map(intoRule).filter(rule => rule !== undefined)[0]; +export function toRule( + scope: string, + intoRule: (scope: string) => TextMateRuleSettings | undefined +): TextMateRuleSettings | undefined { + return find(scope) + .map(intoRule) + .filter(rule => rule !== undefined)[0]; } - function isString(value: any): value is string { return typeof value === 'string'; } @@ -46,18 +65,15 @@ function isArrayOfString(value: any): value is string[] { return Array.isArray(value) && value.every(item => isString(item)); } - export function load() { - const rawConfig: { [key: string]: any } = vscode.workspace - .getConfiguration('rust-analyzer') - .get('scopeMappings') - || {}; + const rawConfig: { [key: string]: any } = + vscode.workspace + .getConfiguration('rust-analyzer') + .get('scopeMappings') || {}; - mappings = Object - .entries(rawConfig) + mappings = Object.entries(rawConfig) .filter(([_, value]) => isString(value) || isArrayOfString(value)) .reduce((list, [key, value]: [string, string | string[]]) => { return list.set(key, isString(value) ? [value] : value); }, defaultMapping); - -} \ No newline at end of file +} -- cgit v1.2.3