diff options
Diffstat (limited to 'editors')
-rw-r--r-- | editors/code/package.json | 8 | ||||
-rw-r--r-- | editors/code/src/cargo.ts | 12 | ||||
-rw-r--r-- | editors/code/src/client.ts | 65 | ||||
-rw-r--r-- | editors/code/src/commands/index.ts | 56 | ||||
-rw-r--r-- | editors/code/src/commands/join_lines.ts | 12 | ||||
-rw-r--r-- | editors/code/src/commands/on_enter.ts | 5 | ||||
-rw-r--r-- | editors/code/src/commands/ssr.ts | 6 | ||||
-rw-r--r-- | editors/code/src/main.ts | 1 | ||||
-rw-r--r-- | editors/code/src/rust-analyzer-api.ts | 8 |
9 files changed, 146 insertions, 27 deletions
diff --git a/editors/code/package.json b/editors/code/package.json index d899f60e3..78f647baa 100644 --- a/editors/code/package.json +++ b/editors/code/package.json | |||
@@ -313,22 +313,22 @@ | |||
313 | "rust-analyzer.inlayHints.enable": { | 313 | "rust-analyzer.inlayHints.enable": { |
314 | "type": "boolean", | 314 | "type": "boolean", |
315 | "default": true, | 315 | "default": true, |
316 | "description": "Disable all inlay hints" | 316 | "description": "Whether to show inlay hints" |
317 | }, | 317 | }, |
318 | "rust-analyzer.inlayHints.typeHints": { | 318 | "rust-analyzer.inlayHints.typeHints": { |
319 | "type": "boolean", | 319 | "type": "boolean", |
320 | "default": true, | 320 | "default": true, |
321 | "description": "Whether to show inlay type hints" | 321 | "description": "Whether to show inlay type hints for variables." |
322 | }, | 322 | }, |
323 | "rust-analyzer.inlayHints.chainingHints": { | 323 | "rust-analyzer.inlayHints.chainingHints": { |
324 | "type": "boolean", | 324 | "type": "boolean", |
325 | "default": true, | 325 | "default": true, |
326 | "description": "Whether to show inlay type hints for method chains" | 326 | "description": "Whether to show inlay type hints for method chains." |
327 | }, | 327 | }, |
328 | "rust-analyzer.inlayHints.parameterHints": { | 328 | "rust-analyzer.inlayHints.parameterHints": { |
329 | "type": "boolean", | 329 | "type": "boolean", |
330 | "default": true, | 330 | "default": true, |
331 | "description": "Whether to show function parameter name inlay hints at the call site" | 331 | "description": "Whether to show function parameter name inlay hints at the call site." |
332 | }, | 332 | }, |
333 | "rust-analyzer.inlayHints.maxLength": { | 333 | "rust-analyzer.inlayHints.maxLength": { |
334 | "type": [ | 334 | "type": [ |
diff --git a/editors/code/src/cargo.ts b/editors/code/src/cargo.ts index 28c7de992..6a41873d0 100644 --- a/editors/code/src/cargo.ts +++ b/editors/code/src/cargo.ts | |||
@@ -51,10 +51,14 @@ export class Cargo { | |||
51 | 51 | ||
52 | // arguments for a runnable from the quick pick should be updated. | 52 | // arguments for a runnable from the quick pick should be updated. |
53 | // see crates\rust-analyzer\src\main_loop\handlers.rs, handle_code_lens | 53 | // see crates\rust-analyzer\src\main_loop\handlers.rs, handle_code_lens |
54 | if (cargoArgs[0] === "run") { | 54 | switch (cargoArgs[0]) { |
55 | cargoArgs[0] = "build"; | 55 | case "run": cargoArgs[0] = "build"; break; |
56 | } else if (cargoArgs.indexOf("--no-run") === -1) { | 56 | case "test": { |
57 | cargoArgs.push("--no-run"); | 57 | if (cargoArgs.indexOf("--no-run") === -1) { |
58 | cargoArgs.push("--no-run"); | ||
59 | } | ||
60 | break; | ||
61 | } | ||
58 | } | 62 | } |
59 | 63 | ||
60 | let artifacts = await this.artifactsFromArgs(cargoArgs); | 64 | let artifacts = await this.artifactsFromArgs(cargoArgs); |
diff --git a/editors/code/src/client.ts b/editors/code/src/client.ts index cffdcf11a..fac1a0be3 100644 --- a/editors/code/src/client.ts +++ b/editors/code/src/client.ts | |||
@@ -31,24 +31,79 @@ export function createClient(serverPath: string, cwd: string): lc.LanguageClient | |||
31 | const res = await next(document, token); | 31 | const res = await next(document, token); |
32 | if (res === undefined) throw new Error('busy'); | 32 | if (res === undefined) throw new Error('busy'); |
33 | return res; | 33 | return res; |
34 | }, | ||
35 | async provideCodeActions(document: vscode.TextDocument, range: vscode.Range, context: vscode.CodeActionContext, token: vscode.CancellationToken, _next: lc.ProvideCodeActionsSignature) { | ||
36 | const params: lc.CodeActionParams = { | ||
37 | textDocument: client.code2ProtocolConverter.asTextDocumentIdentifier(document), | ||
38 | range: client.code2ProtocolConverter.asRange(range), | ||
39 | context: client.code2ProtocolConverter.asCodeActionContext(context) | ||
40 | }; | ||
41 | return client.sendRequest(lc.CodeActionRequest.type, params, token).then((values) => { | ||
42 | if (values === null) return undefined; | ||
43 | const result: (vscode.CodeAction | vscode.Command)[] = []; | ||
44 | for (const item of values) { | ||
45 | if (lc.CodeAction.is(item)) { | ||
46 | const action = client.protocol2CodeConverter.asCodeAction(item); | ||
47 | if (isSnippetEdit(item)) { | ||
48 | action.command = { | ||
49 | command: "rust-analyzer.applySnippetWorkspaceEdit", | ||
50 | title: "", | ||
51 | arguments: [action.edit], | ||
52 | }; | ||
53 | action.edit = undefined; | ||
54 | } | ||
55 | result.push(action); | ||
56 | } else { | ||
57 | const command = client.protocol2CodeConverter.asCommand(item); | ||
58 | result.push(command); | ||
59 | } | ||
60 | } | ||
61 | return result; | ||
62 | }, | ||
63 | (_error) => undefined | ||
64 | ); | ||
34 | } | 65 | } |
66 | |||
35 | } as any | 67 | } as any |
36 | }; | 68 | }; |
37 | 69 | ||
38 | const res = new lc.LanguageClient( | 70 | const client = new lc.LanguageClient( |
39 | 'rust-analyzer', | 71 | 'rust-analyzer', |
40 | 'Rust Analyzer Language Server', | 72 | 'Rust Analyzer Language Server', |
41 | serverOptions, | 73 | serverOptions, |
42 | clientOptions, | 74 | clientOptions, |
43 | ); | 75 | ); |
44 | 76 | ||
45 | // To turn on all proposed features use: res.registerProposedFeatures(); | 77 | // To turn on all proposed features use: client.registerProposedFeatures(); |
46 | // Here we want to enable CallHierarchyFeature and SemanticTokensFeature | 78 | // Here we want to enable CallHierarchyFeature and SemanticTokensFeature |
47 | // since they are available on stable. | 79 | // since they are available on stable. |
48 | // Note that while these features are stable in vscode their LSP protocol | 80 | // Note that while these features are stable in vscode their LSP protocol |
49 | // implementations are still in the "proposed" category for 3.16. | 81 | // implementations are still in the "proposed" category for 3.16. |
50 | res.registerFeature(new CallHierarchyFeature(res)); | 82 | client.registerFeature(new CallHierarchyFeature(client)); |
51 | res.registerFeature(new SemanticTokensFeature(res)); | 83 | client.registerFeature(new SemanticTokensFeature(client)); |
84 | client.registerFeature(new SnippetTextEditFeature()); | ||
85 | |||
86 | return client; | ||
87 | } | ||
52 | 88 | ||
53 | return res; | 89 | class SnippetTextEditFeature implements lc.StaticFeature { |
90 | fillClientCapabilities(capabilities: lc.ClientCapabilities): void { | ||
91 | const caps: any = capabilities.experimental ?? {}; | ||
92 | caps.snippetTextEdit = true; | ||
93 | capabilities.experimental = caps; | ||
94 | } | ||
95 | initialize(_capabilities: lc.ServerCapabilities<any>, _documentSelector: lc.DocumentSelector | undefined): void { | ||
96 | } | ||
97 | } | ||
98 | |||
99 | function isSnippetEdit(action: lc.CodeAction): boolean { | ||
100 | const documentChanges = action.edit?.documentChanges ?? []; | ||
101 | for (const edit of documentChanges) { | ||
102 | if (lc.TextDocumentEdit.is(edit)) { | ||
103 | if (edit.edits.some((indel) => (indel as any).insertTextFormat === lc.InsertTextFormat.Snippet)) { | ||
104 | return true; | ||
105 | } | ||
106 | } | ||
107 | } | ||
108 | return false; | ||
54 | } | 109 | } |
diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index bdb7fc3b0..e5ed77e32 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts | |||
@@ -4,6 +4,7 @@ import * as ra from '../rust-analyzer-api'; | |||
4 | 4 | ||
5 | import { Ctx, Cmd } from '../ctx'; | 5 | import { Ctx, Cmd } from '../ctx'; |
6 | import * as sourceChange from '../source_change'; | 6 | import * as sourceChange from '../source_change'; |
7 | import { assert } from '../util'; | ||
7 | 8 | ||
8 | export * from './analyzer_status'; | 9 | export * from './analyzer_status'; |
9 | export * from './matching_brace'; | 10 | export * from './matching_brace'; |
@@ -51,3 +52,58 @@ export function selectAndApplySourceChange(ctx: Ctx): Cmd { | |||
51 | } | 52 | } |
52 | }; | 53 | }; |
53 | } | 54 | } |
55 | |||
56 | export function applySnippetWorkspaceEditCommand(_ctx: Ctx): Cmd { | ||
57 | return async (edit: vscode.WorkspaceEdit) => { | ||
58 | await applySnippetWorkspaceEdit(edit); | ||
59 | }; | ||
60 | } | ||
61 | |||
62 | export async function applySnippetWorkspaceEdit(edit: vscode.WorkspaceEdit) { | ||
63 | assert(edit.entries().length === 1, `bad ws edit: ${JSON.stringify(edit)}`); | ||
64 | const [uri, edits] = edit.entries()[0]; | ||
65 | |||
66 | const editor = vscode.window.visibleTextEditors.find((it) => it.document.uri.toString() === uri.toString()); | ||
67 | if (!editor) return; | ||
68 | |||
69 | let selection: vscode.Selection | undefined = undefined; | ||
70 | let lineDelta = 0; | ||
71 | await editor.edit((builder) => { | ||
72 | for (const indel of edits) { | ||
73 | const parsed = parseSnippet(indel.newText); | ||
74 | if (parsed) { | ||
75 | const [newText, [placeholderStart, placeholderLength]] = parsed; | ||
76 | const prefix = newText.substr(0, placeholderStart); | ||
77 | const lastNewline = prefix.lastIndexOf('\n'); | ||
78 | |||
79 | const startLine = indel.range.start.line + lineDelta + countLines(prefix); | ||
80 | const startColumn = lastNewline === -1 ? | ||
81 | indel.range.start.character + placeholderStart | ||
82 | : prefix.length - lastNewline - 1; | ||
83 | const endColumn = startColumn + placeholderLength; | ||
84 | selection = new vscode.Selection( | ||
85 | new vscode.Position(startLine, startColumn), | ||
86 | new vscode.Position(startLine, endColumn), | ||
87 | ); | ||
88 | builder.replace(indel.range, newText); | ||
89 | } else { | ||
90 | lineDelta = countLines(indel.newText) - (indel.range.end.line - indel.range.start.line); | ||
91 | builder.replace(indel.range, indel.newText); | ||
92 | } | ||
93 | } | ||
94 | }); | ||
95 | if (selection) editor.selection = selection; | ||
96 | } | ||
97 | |||
98 | function parseSnippet(snip: string): [string, [number, number]] | undefined { | ||
99 | const m = snip.match(/\$(0|\{0:([^}]*)\})/); | ||
100 | if (!m) return undefined; | ||
101 | const placeholder = m[2] ?? ""; | ||
102 | const range: [number, number] = [m.index!!, placeholder.length]; | ||
103 | const insert = snip.replace(m[0], placeholder); | ||
104 | return [insert, range]; | ||
105 | } | ||
106 | |||
107 | function countLines(text: string): number { | ||
108 | return (text.match(/\n/g) || []).length; | ||
109 | } | ||
diff --git a/editors/code/src/commands/join_lines.ts b/editors/code/src/commands/join_lines.ts index de0614653..0bf1ee6e6 100644 --- a/editors/code/src/commands/join_lines.ts +++ b/editors/code/src/commands/join_lines.ts | |||
@@ -1,7 +1,7 @@ | |||
1 | import * as ra from '../rust-analyzer-api'; | 1 | import * as ra from '../rust-analyzer-api'; |
2 | import * as lc from 'vscode-languageclient'; | ||
2 | 3 | ||
3 | import { Ctx, Cmd } from '../ctx'; | 4 | import { Ctx, Cmd } from '../ctx'; |
4 | import { applySourceChange } from '../source_change'; | ||
5 | 5 | ||
6 | export function joinLines(ctx: Ctx): Cmd { | 6 | export function joinLines(ctx: Ctx): Cmd { |
7 | return async () => { | 7 | return async () => { |
@@ -9,10 +9,14 @@ export function joinLines(ctx: Ctx): Cmd { | |||
9 | const client = ctx.client; | 9 | const client = ctx.client; |
10 | if (!editor || !client) return; | 10 | if (!editor || !client) return; |
11 | 11 | ||
12 | const change = await client.sendRequest(ra.joinLines, { | 12 | const items: lc.TextEdit[] = await client.sendRequest(ra.joinLines, { |
13 | range: client.code2ProtocolConverter.asRange(editor.selection), | 13 | ranges: editor.selections.map((it) => client.code2ProtocolConverter.asRange(it)), |
14 | textDocument: { uri: editor.document.uri.toString() }, | 14 | textDocument: { uri: editor.document.uri.toString() }, |
15 | }); | 15 | }); |
16 | await applySourceChange(ctx, change); | 16 | editor.edit((builder) => { |
17 | client.protocol2CodeConverter.asTextEdits(items).forEach((edit) => { | ||
18 | builder.replace(edit.range, edit.newText); | ||
19 | }); | ||
20 | }); | ||
17 | }; | 21 | }; |
18 | } | 22 | } |
diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts index 285849db7..a7871c31e 100644 --- a/editors/code/src/commands/on_enter.ts +++ b/editors/code/src/commands/on_enter.ts | |||
@@ -1,8 +1,8 @@ | |||
1 | import * as vscode from 'vscode'; | 1 | import * as vscode from 'vscode'; |
2 | import * as ra from '../rust-analyzer-api'; | 2 | import * as ra from '../rust-analyzer-api'; |
3 | 3 | ||
4 | import { applySourceChange } from '../source_change'; | ||
5 | import { Cmd, Ctx } from '../ctx'; | 4 | import { Cmd, Ctx } from '../ctx'; |
5 | import { applySnippetWorkspaceEdit } from '.'; | ||
6 | 6 | ||
7 | async function handleKeypress(ctx: Ctx) { | 7 | async function handleKeypress(ctx: Ctx) { |
8 | const editor = ctx.activeRustEditor; | 8 | const editor = ctx.activeRustEditor; |
@@ -21,7 +21,8 @@ async function handleKeypress(ctx: Ctx) { | |||
21 | }); | 21 | }); |
22 | if (!change) return false; | 22 | if (!change) return false; |
23 | 23 | ||
24 | await applySourceChange(ctx, change); | 24 | const workspaceEdit = client.protocol2CodeConverter.asWorkspaceEdit(change); |
25 | await applySnippetWorkspaceEdit(workspaceEdit); | ||
25 | return true; | 26 | return true; |
26 | } | 27 | } |
27 | 28 | ||
diff --git a/editors/code/src/commands/ssr.ts b/editors/code/src/commands/ssr.ts index 4ef8cdf04..5d40a64d2 100644 --- a/editors/code/src/commands/ssr.ts +++ b/editors/code/src/commands/ssr.ts | |||
@@ -2,7 +2,6 @@ import * as vscode from 'vscode'; | |||
2 | import * as ra from "../rust-analyzer-api"; | 2 | import * as ra from "../rust-analyzer-api"; |
3 | 3 | ||
4 | import { Ctx, Cmd } from '../ctx'; | 4 | import { Ctx, Cmd } from '../ctx'; |
5 | import { applySourceChange } from '../source_change'; | ||
6 | 5 | ||
7 | export function ssr(ctx: Ctx): Cmd { | 6 | export function ssr(ctx: Ctx): Cmd { |
8 | return async () => { | 7 | return async () => { |
@@ -22,11 +21,10 @@ export function ssr(ctx: Ctx): Cmd { | |||
22 | } | 21 | } |
23 | }; | 22 | }; |
24 | const request = await vscode.window.showInputBox(options); | 23 | const request = await vscode.window.showInputBox(options); |
25 | |||
26 | if (!request) return; | 24 | if (!request) return; |
27 | 25 | ||
28 | const change = await client.sendRequest(ra.ssr, { query: request, parseOnly: false }); | 26 | const edit = await client.sendRequest(ra.ssr, { query: request, parseOnly: false }); |
29 | 27 | ||
30 | await applySourceChange(ctx, change); | 28 | await vscode.workspace.applyEdit(client.protocol2CodeConverter.asWorkspaceEdit(edit)); |
31 | }; | 29 | }; |
32 | } | 30 | } |
diff --git a/editors/code/src/main.ts b/editors/code/src/main.ts index c015460b8..8b0a9d870 100644 --- a/editors/code/src/main.ts +++ b/editors/code/src/main.ts | |||
@@ -91,6 +91,7 @@ export async function activate(context: vscode.ExtensionContext) { | |||
91 | ctx.registerCommand('debugSingle', commands.debugSingle); | 91 | ctx.registerCommand('debugSingle', commands.debugSingle); |
92 | ctx.registerCommand('showReferences', commands.showReferences); | 92 | ctx.registerCommand('showReferences', commands.showReferences); |
93 | ctx.registerCommand('applySourceChange', commands.applySourceChange); | 93 | ctx.registerCommand('applySourceChange', commands.applySourceChange); |
94 | ctx.registerCommand('applySnippetWorkspaceEdit', commands.applySnippetWorkspaceEditCommand); | ||
94 | ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); | 95 | ctx.registerCommand('selectAndApplySourceChange', commands.selectAndApplySourceChange); |
95 | 96 | ||
96 | ctx.pushCleanup(activateTaskProvider(workspaceFolder)); | 97 | ctx.pushCleanup(activateTaskProvider(workspaceFolder)); |
diff --git a/editors/code/src/rust-analyzer-api.ts b/editors/code/src/rust-analyzer-api.ts index 400ac3714..73f36432f 100644 --- a/editors/code/src/rust-analyzer-api.ts +++ b/editors/code/src/rust-analyzer-api.ts | |||
@@ -64,12 +64,12 @@ export const parentModule = request<lc.TextDocumentPositionParams, Vec<lc.Locati | |||
64 | 64 | ||
65 | export interface JoinLinesParams { | 65 | export interface JoinLinesParams { |
66 | textDocument: lc.TextDocumentIdentifier; | 66 | textDocument: lc.TextDocumentIdentifier; |
67 | range: lc.Range; | 67 | ranges: lc.Range[]; |
68 | } | 68 | } |
69 | export const joinLines = request<JoinLinesParams, SourceChange>("joinLines"); | 69 | export const joinLines = new lc.RequestType<JoinLinesParams, lc.TextEdit[], unknown>('experimental/joinLines'); |
70 | 70 | ||
71 | 71 | ||
72 | export const onEnter = request<lc.TextDocumentPositionParams, Option<SourceChange>>("onEnter"); | 72 | export const onEnter = request<lc.TextDocumentPositionParams, Option<lc.WorkspaceEdit>>("onEnter"); |
73 | 73 | ||
74 | export interface RunnablesParams { | 74 | export interface RunnablesParams { |
75 | textDocument: lc.TextDocumentIdentifier; | 75 | textDocument: lc.TextDocumentIdentifier; |
@@ -112,7 +112,7 @@ export interface SsrParams { | |||
112 | query: string; | 112 | query: string; |
113 | parseOnly: boolean; | 113 | parseOnly: boolean; |
114 | } | 114 | } |
115 | export const ssr = request<SsrParams, SourceChange>("ssr"); | 115 | export const ssr = new lc.RequestType<SsrParams, lc.WorkspaceEdit, unknown>('experimental/ssr'); |
116 | 116 | ||
117 | 117 | ||
118 | export const publishDecorations = notification<PublishDecorationsParams>("publishDecorations"); | 118 | export const publishDecorations = notification<PublishDecorationsParams>("publishDecorations"); |