mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-16 17:58:16 +00:00
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:
parent
529b227d42
commit
83a33fbbea
6 changed files with 110 additions and 106 deletions
|
@ -550,4 +550,4 @@
|
|||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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'
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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<string, vscode.TextEditorDecorationType>(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();
|
||||
}
|
||||
|
|
|
@ -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 {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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.
|
||||
// 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);
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue