diff options
author | Aleksey Kladov <[email protected]> | 2019-12-31 01:17:50 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-12-31 01:17:50 +0000 |
commit | 8346bdc04d5bbaad133c6377bf8863a2d298d55a (patch) | |
tree | c7f7e1d0aed4a2a5514c132f229e6d78a3a06f99 | |
parent | 44d6ab2650bce0faac87b87ef279674d6f63f8ec (diff) |
Rearrange code
-rw-r--r-- | editors/code/src/ctx.ts | 2 | ||||
-rw-r--r-- | editors/code/src/highlighting.ts | 211 |
2 files changed, 106 insertions, 107 deletions
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 39eddfcbd..30dd9811c 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts | |||
@@ -65,7 +65,7 @@ export class Ctx { | |||
65 | async sendRequestWithRetry<R>( | 65 | async sendRequestWithRetry<R>( |
66 | method: string, | 66 | method: string, |
67 | param: any, | 67 | param: any, |
68 | token: vscode.CancellationToken, | 68 | token?: vscode.CancellationToken, |
69 | ): Promise<R> { | 69 | ): Promise<R> { |
70 | await this.client.onReady(); | 70 | await this.client.onReady(); |
71 | for (const delay of [2, 4, 6, 8, 10, null]) { | 71 | for (const delay of [2, 4, 6, 8, 10, null]) { |
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index 247673b5c..4c2e7f67b 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts | |||
@@ -47,7 +47,7 @@ export function activateHighlighting(ctx: Ctx) { | |||
47 | const params: lc.TextDocumentIdentifier = { | 47 | const params: lc.TextDocumentIdentifier = { |
48 | uri: editor.document.uri.toString(), | 48 | uri: editor.document.uri.toString(), |
49 | }; | 49 | }; |
50 | const decorations = await ctx.client.sendRequest<Decoration[]>( | 50 | const decorations = await ctx.sendRequestWithRetry<Decoration[]>( |
51 | 'rust-analyzer/decorationsRequest', | 51 | 'rust-analyzer/decorationsRequest', |
52 | params, | 52 | params, |
53 | ); | 53 | ); |
@@ -62,7 +62,7 @@ interface PublishDecorationsParams { | |||
62 | decorations: Decoration[]; | 62 | decorations: Decoration[]; |
63 | } | 63 | } |
64 | 64 | ||
65 | export interface Decoration { | 65 | interface Decoration { |
66 | range: lc.Range; | 66 | range: lc.Range; |
67 | tag: string; | 67 | tag: string; |
68 | bindingHash?: string; | 68 | bindingHash?: string; |
@@ -81,116 +81,17 @@ function fancify(seed: string, shade: 'light' | 'dark') { | |||
81 | return `hsl(${h},${s}%,${l}%)`; | 81 | return `hsl(${h},${s}%,${l}%)`; |
82 | } | 82 | } |
83 | 83 | ||
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 | |||
119 | class Highlighter { | 84 | class Highlighter { |
120 | private ctx: Ctx; | 85 | 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 | |||
189 | private decorations: Map< | 86 | private decorations: Map< |
190 | string, | 87 | string, |
191 | vscode.TextEditorDecorationType | 88 | vscode.TextEditorDecorationType |
192 | > | null = null; | 89 | > | null = null; |
193 | 90 | ||
91 | constructor(ctx: Ctx) { | ||
92 | this.ctx = ctx; | ||
93 | } | ||
94 | |||
194 | public removeHighlights() { | 95 | public removeHighlights() { |
195 | if (this.decorations == null) { | 96 | if (this.decorations == null) { |
196 | return; | 97 | return; |
@@ -210,7 +111,7 @@ class Highlighter { | |||
210 | // Note: decoration objects need to be kept around so we can dispose them | 111 | // Note: decoration objects need to be kept around so we can dispose them |
211 | // if the user disables syntax highlighting | 112 | // if the user disables syntax highlighting |
212 | if (this.decorations == null) { | 113 | if (this.decorations == null) { |
213 | this.decorations = Highlighter.initDecorations(); | 114 | this.decorations = initDecorations(); |
214 | } | 115 | } |
215 | 116 | ||
216 | const byTag: Map<string, vscode.Range[]> = new Map(); | 117 | const byTag: Map<string, vscode.Range[]> = new Map(); |
@@ -266,3 +167,101 @@ class Highlighter { | |||
266 | } | 167 | } |
267 | } | 168 | } |
268 | } | 169 | } |
170 | |||
171 | function initDecorations(): Map< | ||
172 | string, | ||
173 | vscode.TextEditorDecorationType | ||
174 | > { | ||
175 | const decoration = ( | ||
176 | tag: string, | ||
177 | textDecoration?: string, | ||
178 | ): [string, vscode.TextEditorDecorationType] => { | ||
179 | const rule = scopesMapper.toRule(tag, scopes.find); | ||
180 | |||
181 | if (rule) { | ||
182 | const decor = createDecorationFromTextmate(rule); | ||
183 | return [tag, decor]; | ||
184 | } else { | ||
185 | const fallBackTag = 'ralsp.' + tag; | ||
186 | // console.log(' '); | ||
187 | // console.log('Missing theme for: <"' + tag + '"> for following mapped scopes:'); | ||
188 | // console.log(scopesMapper.find(tag)); | ||
189 | // console.log('Falling back to values defined in: ' + fallBackTag); | ||
190 | // console.log(' '); | ||
191 | const color = new vscode.ThemeColor(fallBackTag); | ||
192 | const decor = vscode.window.createTextEditorDecorationType({ | ||
193 | color, | ||
194 | textDecoration, | ||
195 | }); | ||
196 | return [tag, decor]; | ||
197 | } | ||
198 | }; | ||
199 | |||
200 | const decorations: Iterable<[ | ||
201 | string, | ||
202 | vscode.TextEditorDecorationType, | ||
203 | ]> = [ | ||
204 | decoration('comment'), | ||
205 | decoration('string'), | ||
206 | decoration('keyword'), | ||
207 | decoration('keyword.control'), | ||
208 | decoration('keyword.unsafe'), | ||
209 | decoration('function'), | ||
210 | decoration('parameter'), | ||
211 | decoration('constant'), | ||
212 | decoration('type.builtin'), | ||
213 | decoration('type.generic'), | ||
214 | decoration('type.lifetime'), | ||
215 | decoration('type.param'), | ||
216 | decoration('type.self'), | ||
217 | decoration('type'), | ||
218 | decoration('text'), | ||
219 | decoration('attribute'), | ||
220 | decoration('literal'), | ||
221 | decoration('literal.numeric'), | ||
222 | decoration('literal.char'), | ||
223 | decoration('literal.byte'), | ||
224 | decoration('macro'), | ||
225 | decoration('variable'), | ||
226 | decoration('variable.mut', 'underline'), | ||
227 | decoration('field'), | ||
228 | decoration('module'), | ||
229 | ]; | ||
230 | |||
231 | return new Map<string, vscode.TextEditorDecorationType>(decorations); | ||
232 | } | ||
233 | |||
234 | function createDecorationFromTextmate( | ||
235 | themeStyle: scopes.TextMateRuleSettings, | ||
236 | ): vscode.TextEditorDecorationType { | ||
237 | const decorationOptions: vscode.DecorationRenderOptions = {}; | ||
238 | decorationOptions.rangeBehavior = vscode.DecorationRangeBehavior.OpenOpen; | ||
239 | |||
240 | if (themeStyle.foreground) { | ||
241 | decorationOptions.color = themeStyle.foreground; | ||
242 | } | ||
243 | |||
244 | if (themeStyle.background) { | ||
245 | decorationOptions.backgroundColor = themeStyle.background; | ||
246 | } | ||
247 | |||
248 | if (themeStyle.fontStyle) { | ||
249 | const parts: string[] = themeStyle.fontStyle.split(' '); | ||
250 | parts.forEach(part => { | ||
251 | switch (part) { | ||
252 | case 'italic': | ||
253 | decorationOptions.fontStyle = 'italic'; | ||
254 | break; | ||
255 | case 'bold': | ||
256 | decorationOptions.fontWeight = 'bold'; | ||
257 | break; | ||
258 | case 'underline': | ||
259 | decorationOptions.textDecoration = 'underline'; | ||
260 | break; | ||
261 | default: | ||
262 | break; | ||
263 | } | ||
264 | }); | ||
265 | } | ||
266 | return vscode.window.createTextEditorDecorationType(decorationOptions); | ||
267 | } | ||