Skip to content

Commit 4695298

Browse files
bors[bot]matklad
andauthored
Merge #2702
2702: Drop support for legacy colorization r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 7c4d4e1 + 26bd7a8 commit 4695298

File tree

8 files changed

+187
-271
lines changed

8 files changed

+187
-271
lines changed

crates/ra_ide/src/snapshots/highlighting.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,12 @@
3838
<span class="keyword">fn</span> <span class="function">main</span>() {
3939
<span class="macro">println</span><span class="macro">!</span>(<span class="string">"Hello, {}!"</span>, <span class="literal.numeric">92</span>);
4040

41-
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable.mut">vec</span> = <span class="text">Vec</span>::<span class="text">new</span>();
41+
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable.mut">vec</span> = Vec::new();
4242
<span class="keyword.control">if</span> <span class="keyword">true</span> {
4343
<span class="keyword">let</span> <span class="variable">x</span> = <span class="literal.numeric">92</span>;
44-
<span class="variable.mut">vec</span>.<span class="text">push</span>(<span class="type">Foo</span> { <span class="field">x</span>, <span class="field">y</span>: <span class="literal.numeric">1</span> });
44+
<span class="variable.mut">vec</span>.push(<span class="type">Foo</span> { <span class="field">x</span>, <span class="field">y</span>: <span class="literal.numeric">1</span> });
4545
}
46-
<span class="keyword.unsafe">unsafe</span> { <span class="variable.mut">vec</span>.<span class="text">set_len</span>(<span class="literal.numeric">0</span>); }
46+
<span class="keyword.unsafe">unsafe</span> { <span class="variable.mut">vec</span>.set_len(<span class="literal.numeric">0</span>); }
4747

4848
<span class="keyword">let</span> <span class="keyword">mut</span> <span class="variable.mut">x</span> = <span class="literal.numeric">42</span>;
4949
<span class="keyword">let</span> <span class="variable.mut">y</span> = &<span class="keyword">mut</span> <span class="variable.mut">x</span>;

crates/ra_ide/src/snapshots/rainbow_highlighting.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@
2525
</style>
2626
<pre><code><span class="keyword">fn</span> <span class="function">main</span>() {
2727
<span class="keyword">let</span> <span class="variable" data-binding-hash="8723171760279909834" style="color: hsl(307,91%,75%);">hello</span> = <span class="string">"hello"</span>;
28-
<span class="keyword">let</span> <span class="variable" data-binding-hash="14702933417323009544" style="color: hsl(108,90%,49%);">x</span> = <span class="variable" data-binding-hash="8723171760279909834" style="color: hsl(307,91%,75%);">hello</span>.<span class="text">to_string</span>();
29-
<span class="keyword">let</span> <span class="variable" data-binding-hash="5443150872754369068" style="color: hsl(215,43%,43%);">y</span> = <span class="variable" data-binding-hash="8723171760279909834" style="color: hsl(307,91%,75%);">hello</span>.<span class="text">to_string</span>();
28+
<span class="keyword">let</span> <span class="variable" data-binding-hash="14702933417323009544" style="color: hsl(108,90%,49%);">x</span> = <span class="variable" data-binding-hash="8723171760279909834" style="color: hsl(307,91%,75%);">hello</span>.to_string();
29+
<span class="keyword">let</span> <span class="variable" data-binding-hash="5443150872754369068" style="color: hsl(215,43%,43%);">y</span> = <span class="variable" data-binding-hash="8723171760279909834" style="color: hsl(307,91%,75%);">hello</span>.to_string();
3030

3131
<span class="keyword">let</span> <span class="variable" data-binding-hash="17358108296605513516" style="color: hsl(331,46%,60%);">x</span> = <span class="string">"other color please!"</span>;
32-
<span class="keyword">let</span> <span class="variable" data-binding-hash="2073121142529774969" style="color: hsl(320,43%,74%);">y</span> = <span class="variable" data-binding-hash="17358108296605513516" style="color: hsl(331,46%,60%);">x</span>.<span class="text">to_string</span>();
32+
<span class="keyword">let</span> <span class="variable" data-binding-hash="2073121142529774969" style="color: hsl(320,43%,74%);">y</span> = <span class="variable" data-binding-hash="17358108296605513516" style="color: hsl(331,46%,60%);">x</span>.to_string();
3333
}
3434

3535
<span class="keyword">fn</span> <span class="function">bar</span>() {

crates/ra_ide/src/syntax_highlighting.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,13 @@ pub mod tags {
2020
pub(crate) const FIELD: &str = "field";
2121
pub(crate) const FUNCTION: &str = "function";
2222
pub(crate) const MODULE: &str = "module";
23-
pub(crate) const TYPE: &str = "type";
2423
pub(crate) const CONSTANT: &str = "constant";
2524
pub(crate) const MACRO: &str = "macro";
25+
2626
pub(crate) const VARIABLE: &str = "variable";
2727
pub(crate) const VARIABLE_MUT: &str = "variable.mut";
28-
pub(crate) const TEXT: &str = "text";
2928

29+
pub(crate) const TYPE: &str = "type";
3030
pub(crate) const TYPE_BUILTIN: &str = "type.builtin";
3131
pub(crate) const TYPE_SELF: &str = "type.self";
3232
pub(crate) const TYPE_PARAM: &str = "type.param";
@@ -35,13 +35,14 @@ pub mod tags {
3535
pub(crate) const LITERAL_BYTE: &str = "literal.byte";
3636
pub(crate) const LITERAL_NUMERIC: &str = "literal.numeric";
3737
pub(crate) const LITERAL_CHAR: &str = "literal.char";
38+
3839
pub(crate) const LITERAL_COMMENT: &str = "comment";
3940
pub(crate) const LITERAL_STRING: &str = "string";
4041
pub(crate) const LITERAL_ATTRIBUTE: &str = "attribute";
4142

43+
pub(crate) const KEYWORD: &str = "keyword";
4244
pub(crate) const KEYWORD_UNSAFE: &str = "keyword.unsafe";
4345
pub(crate) const KEYWORD_CONTROL: &str = "keyword.control";
44-
pub(crate) const KEYWORD: &str = "keyword";
4546
}
4647

4748
#[derive(Debug)]
@@ -109,15 +110,21 @@ pub(crate) fn highlight(db: &RootDatabase, file_id: FileId) -> Vec<HighlightedRa
109110
let name_ref = node.as_node().cloned().and_then(ast::NameRef::cast).unwrap();
110111
let name_kind =
111112
classify_name_ref(db, InFile::new(file_id.into(), &name_ref)).map(|d| d.kind);
113+
match name_kind {
114+
Some(name_kind) => {
115+
if let Local(local) = &name_kind {
116+
if let Some(name) = local.name(db) {
117+
let shadow_count =
118+
bindings_shadow_count.entry(name.clone()).or_default();
119+
binding_hash =
120+
Some(calc_binding_hash(file_id, &name, *shadow_count))
121+
}
122+
};
112123

113-
if let Some(Local(local)) = &name_kind {
114-
if let Some(name) = local.name(db) {
115-
let shadow_count = bindings_shadow_count.entry(name.clone()).or_default();
116-
binding_hash = Some(calc_binding_hash(file_id, &name, *shadow_count))
124+
highlight_name(db, name_kind)
117125
}
118-
};
119-
120-
name_kind.map_or(tags::TEXT, |it| highlight_name(db, it))
126+
_ => continue,
127+
}
121128
}
122129
NAME => {
123130
let name = node.as_node().cloned().and_then(ast::Name::cast).unwrap();

editors/code/src/color_theme.ts

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import * as fs from 'fs';
2+
import * as jsonc from 'jsonc-parser';
3+
import * as path from 'path';
4+
import * as vscode from 'vscode';
5+
6+
export interface TextMateRuleSettings {
7+
foreground?: string;
8+
background?: string;
9+
fontStyle?: string;
10+
}
11+
12+
export class ColorTheme {
13+
private rules: Map<string, TextMateRuleSettings> = new Map();
14+
15+
static load(): ColorTheme {
16+
// Find out current color theme
17+
const themeName = vscode.workspace
18+
.getConfiguration('workbench')
19+
.get('colorTheme');
20+
21+
if (typeof themeName !== 'string') {
22+
// console.warn('workbench.colorTheme is', themeName)
23+
return new ColorTheme();
24+
}
25+
return loadThemeNamed(themeName);
26+
}
27+
28+
static fromRules(rules: TextMateRule[]): ColorTheme {
29+
const res = new ColorTheme();
30+
for (const rule of rules) {
31+
const scopes = typeof rule.scope === 'string'
32+
? [rule.scope]
33+
: rule.scope;
34+
for (const scope of scopes) {
35+
res.rules.set(scope, rule.settings)
36+
}
37+
}
38+
return res
39+
}
40+
41+
lookup(scopes: string[]): TextMateRuleSettings {
42+
let res: TextMateRuleSettings = {}
43+
for (const scope of scopes) {
44+
this.rules.forEach((value, key) => {
45+
if (scope.startsWith(key)) {
46+
res = mergeRuleSettings(res, value)
47+
}
48+
})
49+
}
50+
return res
51+
}
52+
53+
mergeFrom(other: ColorTheme) {
54+
other.rules.forEach((value, key) => {
55+
const merged = mergeRuleSettings(this.rules.get(key), value)
56+
this.rules.set(key, merged)
57+
})
58+
}
59+
}
60+
61+
function loadThemeNamed(themeName: string): ColorTheme {
62+
function isTheme(extension: vscode.Extension<any>): boolean {
63+
return (
64+
extension.extensionKind === vscode.ExtensionKind.UI &&
65+
extension.packageJSON.contributes &&
66+
extension.packageJSON.contributes.themes
67+
);
68+
}
69+
70+
let themePaths = vscode.extensions.all
71+
.filter(isTheme)
72+
.flatMap(ext => {
73+
return ext.packageJSON.contributes.themes
74+
.filter((it: any) => (it.id || it.label) === themeName)
75+
.map((it: any) => path.join(ext.extensionPath, it.path));
76+
})
77+
78+
const res = new ColorTheme();
79+
for (const themePath of themePaths) {
80+
res.mergeFrom(loadThemeFile(themePath))
81+
}
82+
83+
const customizations: any = vscode.workspace.getConfiguration('editor').get('tokenColorCustomizations');
84+
res.mergeFrom(ColorTheme.fromRules(customizations?.textMateRules ?? []))
85+
86+
return res;
87+
}
88+
89+
function loadThemeFile(themePath: string): ColorTheme {
90+
let text;
91+
try {
92+
text = fs.readFileSync(themePath, 'utf8')
93+
} catch {
94+
return new ColorTheme();
95+
}
96+
const obj = jsonc.parse(text);
97+
const tokenColors = obj?.tokenColors ?? [];
98+
const res = ColorTheme.fromRules(tokenColors);
99+
100+
for (const include in obj?.include ?? []) {
101+
const includePath = path.join(path.dirname(themePath), include);
102+
const tmp = loadThemeFile(includePath);
103+
res.mergeFrom(tmp);
104+
}
105+
106+
return res;
107+
}
108+
109+
interface TextMateRule {
110+
scope: string | string[];
111+
settings: TextMateRuleSettings;
112+
}
113+
114+
function mergeRuleSettings(
115+
defaultSetting: TextMateRuleSettings | undefined,
116+
override: TextMateRuleSettings,
117+
): TextMateRuleSettings {
118+
return {
119+
foreground: override.foreground ?? defaultSetting?.foreground,
120+
background: override.background ?? defaultSetting?.background,
121+
fontStyle: override.fontStyle ?? defaultSetting?.fontStyle,
122+
}
123+
}

editors/code/src/config.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import * as vscode from 'vscode';
2-
import * as scopesMapper from './scopes_mapper';
32

43
const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG;
54

@@ -58,9 +57,6 @@ export class Config {
5857

5958
if (config.has('highlightingOn')) {
6059
this.highlightingOn = config.get('highlightingOn') as boolean;
61-
if (this.highlightingOn) {
62-
scopesMapper.load();
63-
}
6460
}
6561

6662
if (config.has('rainbowHighlightingOn')) {

editors/code/src/highlighting.ts

Lines changed: 41 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,7 @@ import * as lc from 'vscode-languageclient';
33
import * as seedrandom_ from 'seedrandom';
44
const seedrandom = seedrandom_; // https://github.com/jvandemo/generator-angular2-library/issues/221#issuecomment-355945207
55

6-
import { loadThemeColors, TextMateRuleSettings } from './scopes';
7-
import * as scopesMapper from './scopes_mapper';
6+
import { ColorTheme, TextMateRuleSettings } from './color_theme';
87

98
import { Ctx } from './ctx';
109

@@ -168,69 +167,16 @@ class Highlighter {
168167
}
169168
}
170169

171-
function initDecorations(): Map<
172-
string,
173-
vscode.TextEditorDecorationType
174-
> {
175-
const themeColors = loadThemeColors();
176-
177-
const decoration = (
178-
tag: string,
179-
textDecoration?: string,
180-
): [string, vscode.TextEditorDecorationType] => {
181-
const rule = scopesMapper.toRule(tag, it => themeColors.get(it));
182-
183-
if (rule) {
184-
const decor = createDecorationFromTextmate(rule);
185-
return [tag, decor];
186-
} else {
187-
const fallBackTag = 'ralsp.' + tag;
188-
// console.log(' ');
189-
// console.log('Missing theme for: <"' + tag + '"> for following mapped scopes:');
190-
// console.log(scopesMapper.find(tag));
191-
// console.log('Falling back to values defined in: ' + fallBackTag);
192-
// console.log(' ');
193-
const color = new vscode.ThemeColor(fallBackTag);
194-
const decor = vscode.window.createTextEditorDecorationType({
195-
color,
196-
textDecoration,
197-
});
198-
return [tag, decor];
199-
}
200-
};
201-
202-
const decorations: Iterable<[
203-
string,
204-
vscode.TextEditorDecorationType,
205-
]> = [
206-
decoration('comment'),
207-
decoration('string'),
208-
decoration('keyword'),
209-
decoration('keyword.control'),
210-
decoration('keyword.unsafe'),
211-
decoration('function'),
212-
decoration('parameter'),
213-
decoration('constant'),
214-
decoration('type.builtin'),
215-
decoration('type.generic'),
216-
decoration('type.lifetime'),
217-
decoration('type.param'),
218-
decoration('type.self'),
219-
decoration('type'),
220-
decoration('text'),
221-
decoration('attribute'),
222-
decoration('literal'),
223-
decoration('literal.numeric'),
224-
decoration('literal.char'),
225-
decoration('literal.byte'),
226-
decoration('macro'),
227-
decoration('variable'),
228-
decoration('variable.mut', 'underline'),
229-
decoration('field'),
230-
decoration('module'),
231-
];
232-
233-
return new Map<string, vscode.TextEditorDecorationType>(decorations);
170+
function initDecorations(): Map<string, vscode.TextEditorDecorationType> {
171+
const theme = ColorTheme.load();
172+
const res = new Map()
173+
TAG_TO_SCOPES.forEach((scopes, tag) => {
174+
if (!scopes) throw `unmapped tag: ${tag}`
175+
let rule = theme.lookup(scopes)
176+
const decor = createDecorationFromTextmate(rule);
177+
res.set(tag, decor)
178+
})
179+
return res;
234180
}
235181

236182
function createDecorationFromTextmate(
@@ -267,3 +213,33 @@ function createDecorationFromTextmate(
267213
}
268214
return vscode.window.createTextEditorDecorationType(decorationOptions);
269215
}
216+
217+
// sync with tags from `syntax_highlighting.rs`.
218+
const TAG_TO_SCOPES = new Map<string, string[]>([
219+
["field", ["entity.name.field"]],
220+
["function", ["entity.name.function"]],
221+
["module", ["entity.name.module"]],
222+
["constant", ["entity.name.constant"]],
223+
["macro", ["entity.name.macro"]],
224+
225+
["variable", ["variable"]],
226+
["variable.mut", ["variable", "meta.mutable"]],
227+
228+
["type", ["entity.name.type"]],
229+
["type.builtin", ["entity.name.type", "support.type.primitive"]],
230+
["type.self", ["entity.name.type.parameter.self"]],
231+
["type.param", ["entity.name.type.parameter"]],
232+
["type.lifetime", ["entity.name.type.lifetime"]],
233+
234+
["literal.byte", ["constant.character.byte"]],
235+
["literal.char", ["constant.character"]],
236+
["literal.numeric", ["constant.numeric"]],
237+
238+
["comment", ["comment"]],
239+
["string", ["string.quoted"]],
240+
["attribute", ["meta.attribute"]],
241+
242+
["keyword", ["keyword"]],
243+
["keyword.unsafe", ["keyword.other.unsafe"]],
244+
["keyword.control", ["keyword.control"]],
245+
]);

0 commit comments

Comments
 (0)