diff options
Diffstat (limited to 'editors/code/src')
-rw-r--r-- | editors/code/src/commands/on_enter.ts | 45 | ||||
-rw-r--r-- | editors/code/src/ctx.ts | 24 | ||||
-rw-r--r-- | editors/code/src/highlighting.ts | 25 | ||||
-rw-r--r-- | editors/code/src/main.ts | 4 |
4 files changed, 49 insertions, 49 deletions
diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts index 6f61883cd..c636234da 100644 --- a/editors/code/src/commands/on_enter.ts +++ b/editors/code/src/commands/on_enter.ts | |||
@@ -1,28 +1,35 @@ | |||
1 | import * as vscode from 'vscode'; | ||
1 | import * as lc from 'vscode-languageclient'; | 2 | import * as lc from 'vscode-languageclient'; |
2 | 3 | ||
3 | import { applySourceChange, SourceChange } from '../source_change'; | 4 | import { applySourceChange, SourceChange } from '../source_change'; |
4 | import { Cmd, Ctx } from '../ctx'; | 5 | import { Cmd, Ctx } from '../ctx'; |
5 | 6 | ||
6 | export function onEnter(ctx: Ctx): Cmd { | 7 | async function handleKeypress(ctx: Ctx) { |
7 | return async (event: { text: string }) => { | 8 | const editor = ctx.activeRustEditor; |
8 | const editor = ctx.activeRustEditor; | 9 | const client = ctx.client; |
9 | const client = ctx.client; | 10 | if (!editor) return false; |
10 | if (!editor || event.text !== '\n') return false; | 11 | if (!editor || !client) return false; |
11 | if (!client) return false; | 12 | |
13 | const request: lc.TextDocumentPositionParams = { | ||
14 | textDocument: { uri: editor.document.uri.toString() }, | ||
15 | position: client.code2ProtocolConverter.asPosition( | ||
16 | editor.selection.active, | ||
17 | ), | ||
18 | }; | ||
19 | const change = await client.sendRequest<undefined | SourceChange>( | ||
20 | 'rust-analyzer/onEnter', | ||
21 | request, | ||
22 | ); | ||
23 | if (!change) return false; | ||
12 | 24 | ||
13 | const request: lc.TextDocumentPositionParams = { | 25 | await applySourceChange(ctx, change); |
14 | textDocument: { uri: editor.document.uri.toString() }, | 26 | return true; |
15 | position: client.code2ProtocolConverter.asPosition( | 27 | } |
16 | editor.selection.active, | 28 | |
17 | ), | 29 | export function onEnter(ctx: Ctx): Cmd { |
18 | }; | 30 | return async () => { |
19 | const change = await client.sendRequest<undefined | SourceChange>( | 31 | if (await handleKeypress(ctx)) return; |
20 | 'rust-analyzer/onEnter', | ||
21 | request, | ||
22 | ); | ||
23 | if (!change) return false; | ||
24 | 32 | ||
25 | await applySourceChange(ctx, change); | 33 | await vscode.commands.executeCommand('default:type', { text: '\n' }); |
26 | return true; | ||
27 | }; | 34 | }; |
28 | } | 35 | } |
diff --git a/editors/code/src/ctx.ts b/editors/code/src/ctx.ts index 05d21ae56..aa75943bf 100644 --- a/editors/code/src/ctx.ts +++ b/editors/code/src/ctx.ts | |||
@@ -50,30 +50,6 @@ export class Ctx { | |||
50 | this.pushCleanup(d); | 50 | this.pushCleanup(d); |
51 | } | 51 | } |
52 | 52 | ||
53 | overrideCommand(name: string, factory: (ctx: Ctx) => Cmd) { | ||
54 | const defaultCmd = `default:${name}`; | ||
55 | const override = factory(this); | ||
56 | const original = (...args: unknown[]) => | ||
57 | vscode.commands.executeCommand(defaultCmd, ...args); | ||
58 | try { | ||
59 | const d = vscode.commands.registerCommand( | ||
60 | name, | ||
61 | async (...args: unknown[]) => { | ||
62 | if (!(await override(...args))) { | ||
63 | return await original(...args); | ||
64 | } | ||
65 | }, | ||
66 | ); | ||
67 | this.pushCleanup(d); | ||
68 | } catch (_) { | ||
69 | vscode.window.showWarningMessage( | ||
70 | 'Enhanced typing feature is disabled because of incompatibility ' + | ||
71 | 'with VIM extension, consider turning off rust-analyzer.enableEnhancedTyping: ' + | ||
72 | 'https://github.com/rust-analyzer/rust-analyzer/blob/master/docs/user/README.md#settings', | ||
73 | ); | ||
74 | } | ||
75 | } | ||
76 | |||
77 | get subscriptions(): Disposable[] { | 53 | get subscriptions(): Disposable[] { |
78 | return this.extCtx.subscriptions; | 54 | return this.extCtx.subscriptions; |
79 | } | 55 | } |
diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index 3d190c3ad..66216e0fc 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts | |||
@@ -1,6 +1,5 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
2 | import * as lc from 'vscode-languageclient'; | 2 | import * as lc from 'vscode-languageclient'; |
3 | import seedrandom from 'seedrandom'; | ||
4 | 3 | ||
5 | import { ColorTheme, TextMateRuleSettings } from './color_theme'; | 4 | import { ColorTheme, TextMateRuleSettings } from './color_theme'; |
6 | 5 | ||
@@ -70,9 +69,9 @@ interface Decoration { | |||
70 | 69 | ||
71 | // Based on this HSL-based color generator: https://gist.github.com/bendc/76c48ce53299e6078a76 | 70 | // Based on this HSL-based color generator: https://gist.github.com/bendc/76c48ce53299e6078a76 |
72 | function fancify(seed: string, shade: 'light' | 'dark') { | 71 | function fancify(seed: string, shade: 'light' | 'dark') { |
73 | const random = seedrandom(seed); | 72 | const random = randomU32Numbers(hashString(seed)) |
74 | const randomInt = (min: number, max: number) => { | 73 | const randomInt = (min: number, max: number) => { |
75 | return Math.floor(random() * (max - min + 1)) + min; | 74 | return Math.abs(random()) % (max - min + 1) + min; |
76 | }; | 75 | }; |
77 | 76 | ||
78 | const h = randomInt(0, 360); | 77 | const h = randomInt(0, 360); |
@@ -246,3 +245,23 @@ const TAG_TO_SCOPES = new Map<string, string[]>([ | |||
246 | ["keyword.unsafe", ["keyword.other.unsafe"]], | 245 | ["keyword.unsafe", ["keyword.other.unsafe"]], |
247 | ["keyword.control", ["keyword.control"]], | 246 | ["keyword.control", ["keyword.control"]], |
248 | ]); | 247 | ]); |
248 | |||
249 | function randomU32Numbers(seed: number) { | ||
250 | let random = seed | 0; | ||
251 | return () => { | ||
252 | random ^= random << 13; | ||
253 | random ^= random >> 17; | ||
254 | random ^= random << 5; | ||
255 | random |= 0; | ||
256 | return random | ||
257 | } | ||
258 | } | ||
259 | |||
260 | function hashString(str: string): number { | ||
261 | let res = 0; | ||
262 | for (let i = 0; i < str.length; ++i) { | ||
263 | const c = str.codePointAt(i)!!; | ||
264 | res = (res * 31 + c) & ~0; | ||
265 | } | ||
266 | return res; | ||
267 | } | ||
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index 6813c3c4c..efc31b2e2 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts | |||
@@ -21,6 +21,7 @@ export async function activate(context: vscode.ExtensionContext) { | |||
21 | ctx.registerCommand('expandMacro', commands.expandMacro); | 21 | ctx.registerCommand('expandMacro', commands.expandMacro); |
22 | ctx.registerCommand('run', commands.run); | 22 | ctx.registerCommand('run', commands.run); |
23 | ctx.registerCommand('reload', commands.reload); | 23 | ctx.registerCommand('reload', commands.reload); |
24 | ctx.registerCommand('onEnter', commands.onEnter); | ||
24 | 25 | ||
25 | // Internal commands which are invoked by the server. | 26 | // Internal commands which are invoked by the server. |
26 | ctx.registerCommand('runSingle', commands.runSingle); | 27 | ctx.registerCommand('runSingle', commands.runSingle); |
@@ -28,9 +29,6 @@ export async function activate(context: vscode.ExtensionContext) { | |||
28 | ctx.registerCommand('applySourceChange', commands.applySourceChange); | 29 | ctx.registerCommand('applySourceChange', commands.applySourceChange); |
29 | ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); | 30 | ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); |
30 | 31 | ||
31 | if (ctx.config.enableEnhancedTyping) { | ||
32 | ctx.overrideCommand('type', commands.onEnter); | ||
33 | } | ||
34 | activateStatusDisplay(ctx); | 32 | activateStatusDisplay(ctx); |
35 | 33 | ||
36 | activateHighlighting(ctx); | 34 | activateHighlighting(ctx); |