Skip to content

Commit 98e20da

Browse files
bors[bot]matklad
andauthored
Merge #2699
2699: Switch impure functional style to pure imperative r=matklad a=matklad Co-authored-by: Aleksey Kladov <aleksey.kladov@gmail.com>
2 parents 44d6ab2 + f984ef2 commit 98e20da

File tree

5 files changed

+182
-221
lines changed

5 files changed

+182
-221
lines changed

editors/code/src/config.ts

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

54
const RA_LSP_DEBUG = process.env.__RA_LSP_SERVER_DEBUG;
@@ -60,7 +59,6 @@ export class Config {
6059
if (config.has('highlightingOn')) {
6160
this.highlightingOn = config.get('highlightingOn') as boolean;
6261
if (this.highlightingOn) {
63-
scopes.load();
6462
scopesMapper.load();
6563
}
6664
}

editors/code/src/ctx.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export class Ctx {
6565
async sendRequestWithRetry<R>(
6666
method: string,
6767
param: any,
68-
token: vscode.CancellationToken,
68+
token?: vscode.CancellationToken,
6969
): Promise<R> {
7070
await this.client.onReady();
7171
for (const delay of [2, 4, 6, 8, 10, null]) {

editors/code/src/highlighting.ts

Lines changed: 108 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +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 * as scopes from './scopes';
6+
import { loadThemeColors, TextMateRuleSettings } from './scopes';
77
import * as scopesMapper from './scopes_mapper';
88

99
import { Ctx } from './ctx';
@@ -47,7 +47,7 @@ export function activateHighlighting(ctx: Ctx) {
4747
const params: lc.TextDocumentIdentifier = {
4848
uri: editor.document.uri.toString(),
4949
};
50-
const decorations = await ctx.client.sendRequest<Decoration[]>(
50+
const decorations = await ctx.sendRequestWithRetry<Decoration[]>(
5151
'rust-analyzer/decorationsRequest',
5252
params,
5353
);
@@ -62,7 +62,7 @@ interface PublishDecorationsParams {
6262
decorations: Decoration[];
6363
}
6464

65-
export interface Decoration {
65+
interface Decoration {
6666
range: lc.Range;
6767
tag: string;
6868
bindingHash?: string;
@@ -81,116 +81,17 @@ function fancify(seed: string, shade: 'light' | 'dark') {
8181
return `hsl(${h},${s}%,${l}%)`;
8282
}
8383

84-
function createDecorationFromTextmate(
85-
themeStyle: scopes.TextMateRuleSettings,
86-
): vscode.TextEditorDecorationType {
87-
const decorationOptions: vscode.DecorationRenderOptions = {};
88-
decorationOptions.rangeBehavior = vscode.DecorationRangeBehavior.OpenOpen;
89-
90-
if (themeStyle.foreground) {
91-
decorationOptions.color = themeStyle.foreground;
92-
}
93-
94-
if (themeStyle.background) {
95-
decorationOptions.backgroundColor = themeStyle.background;
96-
}
97-
98-
if (themeStyle.fontStyle) {
99-
const parts: string[] = themeStyle.fontStyle.split(' ');
100-
parts.forEach(part => {
101-
switch (part) {
102-
case 'italic':
103-
decorationOptions.fontStyle = 'italic';
104-
break;
105-
case 'bold':
106-
decorationOptions.fontWeight = 'bold';
107-
break;
108-
case 'underline':
109-
decorationOptions.textDecoration = 'underline';
110-
break;
111-
default:
112-
break;
113-
}
114-
});
115-
}
116-
return vscode.window.createTextEditorDecorationType(decorationOptions);
117-
}
118-
11984
class Highlighter {
12085
private ctx: Ctx;
121-
122-
constructor(ctx: Ctx) {
123-
this.ctx = ctx;
124-
}
125-
126-
private static initDecorations(): Map<
127-
string,
128-
vscode.TextEditorDecorationType
129-
> {
130-
const decoration = (
131-
tag: string,
132-
textDecoration?: string,
133-
): [string, vscode.TextEditorDecorationType] => {
134-
const rule = scopesMapper.toRule(tag, scopes.find);
135-
136-
if (rule) {
137-
const decor = createDecorationFromTextmate(rule);
138-
return [tag, decor];
139-
} else {
140-
const fallBackTag = 'ralsp.' + tag;
141-
// console.log(' ');
142-
// console.log('Missing theme for: <"' + tag + '"> for following mapped scopes:');
143-
// console.log(scopesMapper.find(tag));
144-
// console.log('Falling back to values defined in: ' + fallBackTag);
145-
// console.log(' ');
146-
const color = new vscode.ThemeColor(fallBackTag);
147-
const decor = vscode.window.createTextEditorDecorationType({
148-
color,
149-
textDecoration,
150-
});
151-
return [tag, decor];
152-
}
153-
};
154-
155-
const decorations: Iterable<[
156-
string,
157-
vscode.TextEditorDecorationType,
158-
]> = [
159-
decoration('comment'),
160-
decoration('string'),
161-
decoration('keyword'),
162-
decoration('keyword.control'),
163-
decoration('keyword.unsafe'),
164-
decoration('function'),
165-
decoration('parameter'),
166-
decoration('constant'),
167-
decoration('type.builtin'),
168-
decoration('type.generic'),
169-
decoration('type.lifetime'),
170-
decoration('type.param'),
171-
decoration('type.self'),
172-
decoration('type'),
173-
decoration('text'),
174-
decoration('attribute'),
175-
decoration('literal'),
176-
decoration('literal.numeric'),
177-
decoration('literal.char'),
178-
decoration('literal.byte'),
179-
decoration('macro'),
180-
decoration('variable'),
181-
decoration('variable.mut', 'underline'),
182-
decoration('field'),
183-
decoration('module'),
184-
];
185-
186-
return new Map<string, vscode.TextEditorDecorationType>(decorations);
187-
}
188-
18986
private decorations: Map<
19087
string,
19188
vscode.TextEditorDecorationType
19289
> | null = null;
19390

91+
constructor(ctx: Ctx) {
92+
this.ctx = ctx;
93+
}
94+
19495
public removeHighlights() {
19596
if (this.decorations == null) {
19697
return;
@@ -210,7 +111,7 @@ class Highlighter {
210111
// Note: decoration objects need to be kept around so we can dispose them
211112
// if the user disables syntax highlighting
212113
if (this.decorations == null) {
213-
this.decorations = Highlighter.initDecorations();
114+
this.decorations = initDecorations();
214115
}
215116

216117
const byTag: Map<string, vscode.Range[]> = new Map();
@@ -266,3 +167,103 @@ class Highlighter {
266167
}
267168
}
268169
}
170+
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);
234+
}
235+
236+
function createDecorationFromTextmate(
237+
themeStyle: TextMateRuleSettings,
238+
): vscode.TextEditorDecorationType {
239+
const decorationOptions: vscode.DecorationRenderOptions = {};
240+
decorationOptions.rangeBehavior = vscode.DecorationRangeBehavior.OpenOpen;
241+
242+
if (themeStyle.foreground) {
243+
decorationOptions.color = themeStyle.foreground;
244+
}
245+
246+
if (themeStyle.background) {
247+
decorationOptions.backgroundColor = themeStyle.background;
248+
}
249+
250+
if (themeStyle.fontStyle) {
251+
const parts: string[] = themeStyle.fontStyle.split(' ');
252+
parts.forEach(part => {
253+
switch (part) {
254+
case 'italic':
255+
decorationOptions.fontStyle = 'italic';
256+
break;
257+
case 'bold':
258+
decorationOptions.fontWeight = 'bold';
259+
break;
260+
case 'underline':
261+
decorationOptions.textDecoration = 'underline';
262+
break;
263+
default:
264+
break;
265+
}
266+
});
267+
}
268+
return vscode.window.createTextEditorDecorationType(decorationOptions);
269+
}

0 commit comments

Comments
 (0)