From c60f9bf4c6d6ddd341c673b228b6aa1add3da62b Mon Sep 17 00:00:00 2001 From: Seivan Heidari Date: Mon, 4 Nov 2019 23:59:11 +0100 Subject: * Adding scope mapping configuration manifest in `package.json` * Loading configurable scope mappings from settings. * Updating Readme with `rust-analyzer.scopeMappings`. `rust-analyzer.scopeMappings` -- a scheme backed JSON object to tweak Rust Analyzer scopes to TextMate scopes. ```jsonc { //Will autocomplete keys to available RA scopes. "keyword.unsafe": ["keyword", "keyword.control"], //Values are string | TextMateScope | [string | TextMateScope] "comments": "comment.block" } ``` --- editors/code/src/scopes.ts | 64 +++++++++++++++++++++++----------------------- 1 file changed, 32 insertions(+), 32 deletions(-) (limited to 'editors/code/src/scopes.ts') diff --git a/editors/code/src/scopes.ts b/editors/code/src/scopes.ts index 5d4395930..a6138fad0 100644 --- a/editors/code/src/scopes.ts +++ b/editors/code/src/scopes.ts @@ -1,41 +1,41 @@ -import * as fs from 'fs' -import * as path from 'path' -import * as vscode from 'vscode' +import * as fs from 'fs'; +import * as path from 'path'; +import * as vscode from 'vscode'; export interface TextMateRule { - scope: string | string[] - settings: TextMateRuleSettings + scope: string | string[]; + settings: TextMateRuleSettings; } export interface TextMateRuleSettings { - foreground: string | undefined - background: string | undefined - fontStyle: string | undefined + foreground: string | undefined; + background: string | undefined; + fontStyle: string | undefined; } // Current theme colors -const rules = new Map() +const rules = new Map(); export function find(scope: string): TextMateRuleSettings | undefined { - return rules.get(scope) + return rules.get(scope); } // Load all textmate scopes in the currently active theme export function load() { // Remove any previous theme - rules.clear() + 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) - return + return; } // Try to load colors from that theme try { - loadThemeNamed(themeName) + loadThemeNamed(themeName); } catch (e) { // console.warn('failed to load theme', themeName, e) } @@ -44,7 +44,7 @@ export function load() { function filterThemeExtensions(extension: vscode.Extension): boolean { return extension.extensionKind === vscode.ExtensionKind.UI && extension.packageJSON.contributes && - extension.packageJSON.contributes.themes + extension.packageJSON.contributes.themes; } @@ -59,17 +59,17 @@ function loadThemeNamed(themeName: string) { .filter((element: any) => (element.id || element.label) === themeName) .map((element: any) => path.join(extension.extensionPath, element.path)) .concat(list) - }, Array()) + }, Array()); - themePaths.forEach(loadThemeFile) + themePaths.forEach(loadThemeFile); const tokenColorCustomizations: [any] = [vscode.workspace.getConfiguration('editor').get('tokenColorCustomizations')] tokenColorCustomizations .filter(custom => custom && custom.textMateRules) .map(custom => custom.textMateRules) - .forEach(loadColors) + .forEach(loadColors); } @@ -79,26 +79,26 @@ function loadThemeFile(themePath: string) { .filter(isFile) .map(readFileText) .map(parseJSON) - .filter(theme => theme) + .filter(theme => theme); themeContent .filter(theme => theme.tokenColors) .map(theme => theme.tokenColors) - .forEach(loadColors) + .forEach(loadColors); themeContent .filter(theme => theme.include) .map(theme => path.join(path.dirname(themePath), theme.include)) - .forEach(loadThemeFile) + .forEach(loadThemeFile); } function mergeRuleSettings(defaultSetting: TextMateRuleSettings | undefined, override: TextMateRuleSettings): TextMateRuleSettings { - if (defaultSetting === undefined) { return override } - const mergedRule = defaultSetting + 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 + mergedRule.background = override.background || defaultSetting.background; + mergedRule.foreground = override.foreground || defaultSetting.foreground; + mergedRule.fontStyle = override.fontStyle || defaultSetting.foreground; return mergedRule } @@ -106,29 +106,29 @@ function mergeRuleSettings(defaultSetting: TextMateRuleSettings | undefined, ove function updateRules(scope: string, updatedSettings: TextMateRuleSettings): void { [rules.get(scope)] .map(settings => mergeRuleSettings(settings, updatedSettings)) - .forEach(settings => rules.set(scope, settings)) + .forEach(settings => rules.set(scope, settings)); } function loadColors(textMateRules: TextMateRule[]): void { textMateRules.forEach(rule => { if (typeof rule.scope === 'string') { - updateRules(rule.scope, rule.settings) + updateRules(rule.scope, rule.settings); } else if (rule.scope instanceof Array) { - rule.scope.forEach(scope => updateRules(scope, rule.settings)) + rule.scope.forEach(scope => updateRules(scope, rule.settings)); } }) } function isFile(filePath: string): boolean { - return [filePath].map(fs.statSync).every(stat => stat.isFile()) + return [filePath].map(fs.statSync).every(stat => stat.isFile()); } function readFileText(filePath: string): string { - return fs.readFileSync(filePath, 'utf8') + return fs.readFileSync(filePath, 'utf8'); } // Might need to replace with JSONC if a theme contains comments. function parseJSON(content: string): any { - return JSON.parse(content) + return JSON.parse(content); } \ No newline at end of file -- cgit v1.2.3