Vscode wasn't running the linter automatically so ran npm run fix - wonder if it's related to tslint being deprecated.

This commit is contained in:
Seivan Heidari 2019-11-09 17:23:30 +01:00
parent 529b227d42
commit 83a33fbbea
6 changed files with 110 additions and 106 deletions

View file

@ -63,7 +63,6 @@ export class Config {
) as boolean;
}
if (config.has('enableEnhancedTyping')) {
this.enableEnhancedTyping = config.get(
'enableEnhancedTyping'

View file

@ -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:');
@ -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();
}

View file

@ -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<any>): 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<string>());
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 {

View file

@ -1,17 +1,25 @@
import * as vscode from 'vscode';
import { TextMateRuleSettings } from './scopes';
let mappings = new Map<string, string[]>();
const defaultMapping = new Map<string, string[]>([
['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<string, string[]>([
['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.
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
const rawConfig: { [key: string]: any } =
vscode.workspace
.getConfiguration('rust-analyzer')
.get('scopeMappings')
|| {};
.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);
}