From 39efb301ff7946592ac0d8a64749582daaa67b86 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 00:48:44 +0200 Subject: vscode: create rust-analyzer-api.ts --- editors/code/src/rust-analyzer-api.ts | 117 ++++++++++++++++++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 editors/code/src/rust-analyzer-api.ts (limited to 'editors/code') diff --git a/editors/code/src/rust-analyzer-api.ts b/editors/code/src/rust-analyzer-api.ts new file mode 100644 index 000000000..d2738fef3 --- /dev/null +++ b/editors/code/src/rust-analyzer-api.ts @@ -0,0 +1,117 @@ +/** + * This file mirrors `crates/rust-analyzer/src/req.rs` declarations. + */ + +import { RequestType, TextDocumentIdentifier, Position, Range, TextDocumentPositionParams, Location, NotificationType, WorkspaceEdit } from "vscode-languageclient"; + +type Option = null | T; +type Vec = T[]; +type FxHashMap = Record; + +function request(method: string) { + return new RequestType(`rust-analyzer/${method}`); +} +function notification(method: string) { + return new NotificationType(method); +} + + +export const analyzerStatus = request("analyzerStatus"); + + +export const collectGarbage = request("collectGarbage"); + + +export interface SyntaxTreeParams { + textDocument: TextDocumentIdentifier; + range: Option; +} +export const syntaxTree = request("syntaxTree"); + + +export interface ExpandMacroParams { + textDocument: TextDocumentIdentifier; + position: Option; +} +export interface ExpandedMacro { + name: string; + expansion: string; +} +export const expandMacro = request>("expandMacro"); + + +export interface FindMatchingBraceParams { + textDocument: TextDocumentIdentifier; + offsets: Vec; +} +export const findMatchingBrace = request>("findMatchingBrace"); + + +export interface PublishDecorationsParams { + uri: string; + decorations: Vec; +} +export interface Decoration { + range: Range; + tag: string; + bindingHash: Option; +} +export const decorationsRequest = request>("decorationsRequest"); + + +export const parentModule = request>("parentModule"); + + +export interface JoinLinesParams { + textDocument: TextDocumentIdentifier; + range: Range; +} +export const joinLines = request("joinLines"); + + +export const onEnter = request>("onEnter"); + +export interface RunnablesParams { + textDocument: TextDocumentIdentifier; + position: Option; +} +export interface Runnable { + range: Range; + label: string; + bin: string; + args: Vec; + env: FxHashMap; + cwd: Option; +} +export const runnables = request>("runnables"); + + +export const enum InlayKind { + TypeHint = "TypeHint", + ParameterHint = "ParameterHint", +} +export interface InlayHint { + range: Range; + kind: InlayKind; + label: string; +} +export interface InlayHintsParams { + textDocument: TextDocumentIdentifier; +} +export const inlayHints = request>("inlayHints"); + + +export interface SsrParams { + arg: string; +} +export const ssr = request("ssr"); + + +export const publishDecorations = notification("publishDecorations"); + + +export interface SourceChange { + label: string; + workspaceEdit: WorkspaceEdit; + cursorPosition: Option; +} -- cgit v1.2.3 From 21ab13396672a4ad75b93bbb73af02d019ef918b Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 00:49:19 +0200 Subject: vscode: migrate source_cnage.rs to rust-analyzer-api.rs --- editors/code/src/source_change.ts | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/source_change.ts b/editors/code/src/source_change.ts index a336269ba..399a150c6 100644 --- a/editors/code/src/source_change.ts +++ b/editors/code/src/source_change.ts @@ -1,15 +1,10 @@ import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; +import * as ra from './rust-analyzer-api'; import { Ctx } from './ctx'; -export interface SourceChange { - label: string; - workspaceEdit: lc.WorkspaceEdit; - cursorPosition?: lc.TextDocumentPositionParams; -} - -export async function applySourceChange(ctx: Ctx, change: SourceChange) { +export async function applySourceChange(ctx: Ctx, change: ra.SourceChange) { const client = ctx.client; if (!client) return; -- cgit v1.2.3 From 8c4409b3bb6dcdc439c7ea98dfb89c0181969323 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 00:49:54 +0200 Subject: vscode: migrate highlighting to rust-analyzer-api.ts --- editors/code/src/highlighting.ts | 60 ++++++++++++++-------------------------- 1 file changed, 21 insertions(+), 39 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/highlighting.ts b/editors/code/src/highlighting.ts index 77b4a1a68..3e0cbdc56 100644 --- a/editors/code/src/highlighting.ts +++ b/editors/code/src/highlighting.ts @@ -1,5 +1,5 @@ import * as vscode from 'vscode'; -import * as lc from 'vscode-languageclient'; +import * as ra from './rust-analyzer-api'; import { ColorTheme, TextMateRuleSettings } from './color_theme'; @@ -8,29 +8,25 @@ import { sendRequestWithRetry } from './util'; export function activateHighlighting(ctx: Ctx) { const highlighter = new Highlighter(ctx); - const client = ctx.client; - if (client != null) { - client.onNotification( - 'rust-analyzer/publishDecorations', - (params: PublishDecorationsParams) => { - if (!ctx.config.highlightingOn) return; - - const targetEditor = vscode.window.visibleTextEditors.find( - editor => { - const unescapedUri = unescape( - editor.document.uri.toString(), - ); - // Unescaped URI looks like: - // file:///c:/Workspace/ra-test/src/main.rs - return unescapedUri === params.uri; - }, - ); - if (!targetEditor) return; - highlighter.setHighlights(targetEditor, params.decorations); + ctx.client.onNotification(ra.publishDecorations, params => { + if (!ctx.config.highlightingOn) return; + + const targetEditor = vscode.window.visibleTextEditors.find( + editor => { + const unescapedUri = unescape( + editor.document.uri.toString(), + ); + // Unescaped URI looks like: + // file:///c:/Workspace/ra-test/src/main.rs + return unescapedUri === params.uri; }, ); - } + if (!targetEditor) return; + + highlighter.setHighlights(targetEditor, params.decorations); + }); + vscode.workspace.onDidChangeConfiguration( _ => highlighter.removeHighlights(), @@ -45,13 +41,10 @@ export function activateHighlighting(ctx: Ctx) { const client = ctx.client; if (!client) return; - const params: lc.TextDocumentIdentifier = { - uri: editor.document.uri.toString(), - }; - const decorations = await sendRequestWithRetry( + const decorations = await sendRequestWithRetry( client, - 'rust-analyzer/decorationsRequest', - params, + ra.decorationsRequest, + { uri: editor.document.uri.toString() }, ); highlighter.setHighlights(editor, decorations); }, @@ -60,17 +53,6 @@ export function activateHighlighting(ctx: Ctx) { ); } -interface PublishDecorationsParams { - uri: string; - decorations: Decoration[]; -} - -interface Decoration { - range: lc.Range; - tag: string; - bindingHash?: string; -} - // Based on this HSL-based color generator: https://gist.github.com/bendc/76c48ce53299e6078a76 function fancify(seed: string, shade: 'light' | 'dark') { const random = randomU32Numbers(hashString(seed)); @@ -108,7 +90,7 @@ class Highlighter { this.decorations = null; } - public setHighlights(editor: vscode.TextEditor, highlights: Decoration[]) { + public setHighlights(editor: vscode.TextEditor, highlights: ra.Decoration[]) { const client = this.ctx.client; if (!client) return; // Initialize decorations if necessary -- cgit v1.2.3 From 603bc71a57ce386d78f1b1c842714ff0dbeeaf9c Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 00:50:14 +0200 Subject: vscode: migrate analyzer_status to rust-analyzer-api.ts --- editors/code/src/commands/analyzer_status.ts | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/commands/analyzer_status.ts b/editors/code/src/commands/analyzer_status.ts index 6631e8db7..1c6ea399b 100644 --- a/editors/code/src/commands/analyzer_status.ts +++ b/editors/code/src/commands/analyzer_status.ts @@ -1,5 +1,6 @@ import * as vscode from 'vscode'; +import * as ra from '../rust-analyzer-api'; import { Ctx, Cmd } from '../ctx'; // Shows status of rust-analyzer (for debugging) @@ -50,10 +51,7 @@ class TextDocumentContentProvider const client = this.ctx.client; if (!editor || !client) return ''; - return client.sendRequest( - 'rust-analyzer/analyzerStatus', - null, - ); + return client.sendRequest(ra.analyzerStatus, null); } get onDidChange(): vscode.Event { -- cgit v1.2.3 From 31d9932d18151bffe94e4137ac3b5d10df37641f Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 00:50:36 +0200 Subject: vscode: migrate expand_macro to rust-analyzer-api.ts --- editors/code/src/commands/expand_macro.ts | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/commands/expand_macro.ts b/editors/code/src/commands/expand_macro.ts index edec9bbc1..23f2ef1d5 100644 --- a/editors/code/src/commands/expand_macro.ts +++ b/editors/code/src/commands/expand_macro.ts @@ -1,5 +1,5 @@ import * as vscode from 'vscode'; -import * as lc from 'vscode-languageclient'; +import * as ra from '../rust-analyzer-api'; import { Ctx, Cmd } from '../ctx'; @@ -26,12 +26,7 @@ export function expandMacro(ctx: Ctx): Cmd { }; } -interface ExpandedMacro { - name: string; - expansion: string; -} - -function codeFormat(expanded: ExpandedMacro): string { +function codeFormat(expanded: ra.ExpandedMacro): string { let result = `// Recursive expansion of ${expanded.name}! macro\n`; result += '// ' + '='.repeat(result.length - 3); result += '\n\n'; @@ -54,14 +49,11 @@ class TextDocumentContentProvider if (!editor || !client) return ''; const position = editor.selection.active; - const request: lc.TextDocumentPositionParams = { + + const expanded = await client.sendRequest(ra.expandMacro, { textDocument: { uri: editor.document.uri.toString() }, position, - }; - const expanded = await client.sendRequest( - 'rust-analyzer/expandMacro', - request, - ); + }); if (expanded == null) return 'Not available'; -- cgit v1.2.3 From c9a2fa1835a9b91d7e9332f7eceb8c899c727d32 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 00:50:57 +0200 Subject: vscode: migrate collectGarbage to rust-analyzer-api.ts --- editors/code/src/commands/index.ts | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/commands/index.ts b/editors/code/src/commands/index.ts index 839245f48..bdb7fc3b0 100644 --- a/editors/code/src/commands/index.ts +++ b/editors/code/src/commands/index.ts @@ -1,5 +1,6 @@ import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; +import * as ra from '../rust-analyzer-api'; import { Ctx, Cmd } from '../ctx'; import * as sourceChange from '../source_change'; @@ -16,9 +17,7 @@ export * from './ssr'; export * from './server_version'; export function collectGarbage(ctx: Ctx): Cmd { - return async () => { - await ctx.client?.sendRequest('rust-analyzer/collectGarbage', null); - }; + return async () => ctx.client.sendRequest(ra.collectGarbage, null); } export function showReferences(ctx: Ctx): Cmd { @@ -36,13 +35,13 @@ export function showReferences(ctx: Ctx): Cmd { } export function applySourceChange(ctx: Ctx): Cmd { - return async (change: sourceChange.SourceChange) => { + return async (change: ra.SourceChange) => { await sourceChange.applySourceChange(ctx, change); }; } export function selectAndApplySourceChange(ctx: Ctx): Cmd { - return async (changes: sourceChange.SourceChange[]) => { + return async (changes: ra.SourceChange[]) => { if (changes.length === 1) { await sourceChange.applySourceChange(ctx, changes[0]); } else if (changes.length > 0) { -- cgit v1.2.3 From 38d7945ec7d3522e09a105a92156d1aaf8651f46 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 00:54:50 +0200 Subject: vscode: migrate join_lines to rust-analyzer-api.ts --- editors/code/src/commands/join_lines.ts | 17 ++++------------- 1 file changed, 4 insertions(+), 13 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/commands/join_lines.ts b/editors/code/src/commands/join_lines.ts index 7b08c3255..de0614653 100644 --- a/editors/code/src/commands/join_lines.ts +++ b/editors/code/src/commands/join_lines.ts @@ -1,7 +1,7 @@ -import * as lc from 'vscode-languageclient'; +import * as ra from '../rust-analyzer-api'; import { Ctx, Cmd } from '../ctx'; -import { applySourceChange, SourceChange } from '../source_change'; +import { applySourceChange } from '../source_change'; export function joinLines(ctx: Ctx): Cmd { return async () => { @@ -9,19 +9,10 @@ export function joinLines(ctx: Ctx): Cmd { const client = ctx.client; if (!editor || !client) return; - const request: JoinLinesParams = { + const change = await client.sendRequest(ra.joinLines, { range: client.code2ProtocolConverter.asRange(editor.selection), textDocument: { uri: editor.document.uri.toString() }, - }; - const change = await client.sendRequest( - 'rust-analyzer/joinLines', - request, - ); + }); await applySourceChange(ctx, change); }; } - -interface JoinLinesParams { - textDocument: lc.TextDocumentIdentifier; - range: lc.Range; -} -- cgit v1.2.3 From 56d1ff65324d59623e8483c7cbf03672611cbcdf Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 00:55:13 +0200 Subject: vscode: migrate matching_brace to rust-analyzer-api.ts --- editors/code/src/commands/matching_brace.ts | 15 +++------------ 1 file changed, 3 insertions(+), 12 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/commands/matching_brace.ts b/editors/code/src/commands/matching_brace.ts index 7c58bb7e7..a60776e2d 100644 --- a/editors/code/src/commands/matching_brace.ts +++ b/editors/code/src/commands/matching_brace.ts @@ -1,5 +1,5 @@ import * as vscode from 'vscode'; -import * as lc from 'vscode-languageclient'; +import * as ra from '../rust-analyzer-api'; import { Ctx, Cmd } from '../ctx'; @@ -9,16 +9,12 @@ export function matchingBrace(ctx: Ctx): Cmd { const client = ctx.client; if (!editor || !client) return; - const request: FindMatchingBraceParams = { + const response = await client.sendRequest(ra.findMatchingBrace, { textDocument: { uri: editor.document.uri.toString() }, offsets: editor.selections.map(s => client.code2ProtocolConverter.asPosition(s.active), ), - }; - const response = await client.sendRequest( - 'rust-analyzer/findMatchingBrace', - request, - ); + }); editor.selections = editor.selections.map((sel, idx) => { const active = client.protocol2CodeConverter.asPosition( response[idx], @@ -29,8 +25,3 @@ export function matchingBrace(ctx: Ctx): Cmd { editor.revealRange(editor.selection); }; } - -interface FindMatchingBraceParams { - textDocument: lc.TextDocumentIdentifier; - offsets: lc.Position[]; -} -- cgit v1.2.3 From 8c6581dcc3db0e79a075d22ab930cb58a31dfe3c Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 00:55:48 +0200 Subject: vscode: migrate on_enter to rust-analyzer-api.ts --- editors/code/src/commands/on_enter.ts | 21 +++++++-------------- 1 file changed, 7 insertions(+), 14 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts index 27ae8ec23..285849db7 100644 --- a/editors/code/src/commands/on_enter.ts +++ b/editors/code/src/commands/on_enter.ts @@ -1,7 +1,7 @@ import * as vscode from 'vscode'; -import * as lc from 'vscode-languageclient'; +import * as ra from '../rust-analyzer-api'; -import { applySourceChange, SourceChange } from '../source_change'; +import { applySourceChange } from '../source_change'; import { Cmd, Ctx } from '../ctx'; async function handleKeypress(ctx: Ctx) { @@ -10,22 +10,15 @@ async function handleKeypress(ctx: Ctx) { if (!editor || !client) return false; - const request: lc.TextDocumentPositionParams = { + const change = await client.sendRequest(ra.onEnter, { textDocument: { uri: editor.document.uri.toString() }, position: client.code2ProtocolConverter.asPosition( editor.selection.active, ), - }; - const change = await client.sendRequest( - 'rust-analyzer/onEnter', - request, - ).catch( - (_error: any) => { - // FIXME: switch to the more modern (?) typed request infrastructure - // client.logFailedRequest(OnEnterRequest.type, error); - return Promise.resolve(null); - } - ); + }).catch(_error => { + // client.logFailedRequest(OnEnterRequest.type, error); + return null; + }); if (!change) return false; await applySourceChange(ctx, change); -- cgit v1.2.3 From d6a96a90f417d48e6391d2233abf752988c04f1a Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 00:56:19 +0200 Subject: vscode: migrate parent_module to rust-analyzer-api.ts --- editors/code/src/commands/parent_module.ts | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/commands/parent_module.ts b/editors/code/src/commands/parent_module.ts index bf40b4021..8f78ddd71 100644 --- a/editors/code/src/commands/parent_module.ts +++ b/editors/code/src/commands/parent_module.ts @@ -1,5 +1,5 @@ import * as vscode from 'vscode'; -import * as lc from 'vscode-languageclient'; +import * as ra from '../rust-analyzer-api'; import { Ctx, Cmd } from '../ctx'; @@ -9,16 +9,12 @@ export function parentModule(ctx: Ctx): Cmd { const client = ctx.client; if (!editor || !client) return; - const request: lc.TextDocumentPositionParams = { + const response = await client.sendRequest(ra.parentModule, { textDocument: { uri: editor.document.uri.toString() }, position: client.code2ProtocolConverter.asPosition( editor.selection.active, ), - }; - const response = await client.sendRequest( - 'rust-analyzer/parentModule', - request, - ); + }); const loc = response[0]; if (loc == null) return; -- cgit v1.2.3 From 8a8a4d08ef6c695cce04f5e306fad83cc9abddcd Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 00:56:38 +0200 Subject: vscode: migrate runnables to rust-analyzer-api.ts --- editors/code/src/commands/runnables.ts | 29 +++++++---------------------- 1 file changed, 7 insertions(+), 22 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts index 7919997ce..06b513466 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/commands/runnables.ts @@ -1,5 +1,6 @@ import * as vscode from 'vscode'; import * as lc from 'vscode-languageclient'; +import * as ra from '../rust-analyzer-api'; import { Ctx, Cmd } from '../ctx'; @@ -14,16 +15,13 @@ export function run(ctx: Ctx): Cmd { const textDocument: lc.TextDocumentIdentifier = { uri: editor.document.uri.toString(), }; - const params: RunnablesParams = { + + const runnables = await client.sendRequest(ra.runnables, { textDocument, position: client.code2ProtocolConverter.asPosition( editor.selection.active, ), - }; - const runnables = await client.sendRequest( - 'rust-analyzer/runnables', - params, - ); + }); const items: RunnableQuickPick[] = []; if (prevRunnable) { items.push(prevRunnable); @@ -48,7 +46,7 @@ export function run(ctx: Ctx): Cmd { } export function runSingle(ctx: Ctx): Cmd { - return async (runnable: Runnable) => { + return async (runnable: ra.Runnable) => { const editor = ctx.activeRustEditor; if (!editor) return; @@ -64,26 +62,13 @@ export function runSingle(ctx: Ctx): Cmd { }; } -interface RunnablesParams { - textDocument: lc.TextDocumentIdentifier; - position?: lc.Position; -} - -interface Runnable { - label: string; - bin: string; - args: string[]; - env: { [index: string]: string }; - cwd?: string; -} - class RunnableQuickPick implements vscode.QuickPickItem { public label: string; public description?: string | undefined; public detail?: string | undefined; public picked?: boolean | undefined; - constructor(public runnable: Runnable) { + constructor(public runnable: ra.Runnable) { this.label = runnable.label; } } @@ -96,7 +81,7 @@ interface CargoTaskDefinition extends vscode.TaskDefinition { env?: { [key: string]: string }; } -function createTask(spec: Runnable): vscode.Task { +function createTask(spec: ra.Runnable): vscode.Task { const TASK_SOURCE = 'Rust'; const definition: CargoTaskDefinition = { type: 'cargo', -- cgit v1.2.3 From 9ea63d5a86b8217c25f0db49a535105b345ceae1 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 00:56:57 +0200 Subject: vscode: migrate ssr to rust-analyzer-api.ts --- editors/code/src/commands/ssr.ts | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/commands/ssr.ts b/editors/code/src/commands/ssr.ts index 9b814612a..63c36ce80 100644 --- a/editors/code/src/commands/ssr.ts +++ b/editors/code/src/commands/ssr.ts @@ -1,6 +1,8 @@ -import { Ctx, Cmd } from '../ctx'; -import { applySourceChange, SourceChange } from '../source_change'; import * as vscode from 'vscode'; +import * as ra from "../rust-analyzer-api"; + +import { Ctx, Cmd } from '../ctx'; +import { applySourceChange } from '../source_change'; export function ssr(ctx: Ctx): Cmd { return async () => { @@ -21,16 +23,8 @@ export function ssr(ctx: Ctx): Cmd { if (!request) return; - const ssrRequest: SsrRequest = { arg: request }; - const change = await client.sendRequest( - 'rust-analyzer/ssr', - ssrRequest, - ); + const change = await client.sendRequest(ra.ssr, { arg: request },); await applySourceChange(ctx, change); }; } - -interface SsrRequest { - arg: string; -} -- cgit v1.2.3 From 8aea0ec511d141b5c53d419960c688b13bb6c061 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 00:57:14 +0200 Subject: vscode: migrate syntax_tree to rust-analyzer-api.ts --- editors/code/src/commands/syntax_tree.ts | 27 +++++++-------------------- 1 file changed, 7 insertions(+), 20 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/commands/syntax_tree.ts index 2887c96c8..7218bfb90 100644 --- a/editors/code/src/commands/syntax_tree.ts +++ b/editors/code/src/commands/syntax_tree.ts @@ -1,5 +1,5 @@ import * as vscode from 'vscode'; -import * as lc from 'vscode-languageclient'; +import * as ra from '../rust-analyzer-api'; import { Ctx, Cmd } from '../ctx'; @@ -61,13 +61,8 @@ function afterLs(f: () => void) { setTimeout(f, 10); } -interface SyntaxTreeParams { - textDocument: lc.TextDocumentIdentifier; - range?: lc.Range; -} -class TextDocumentContentProvider - implements vscode.TextDocumentContentProvider { +class TextDocumentContentProvider implements vscode.TextDocumentContentProvider { uri = vscode.Uri.parse('rust-analyzer://syntaxtree'); eventEmitter = new vscode.EventEmitter(); @@ -79,23 +74,15 @@ class TextDocumentContentProvider const client = this.ctx.client; if (!editor || !client) return ''; - let range: lc.Range | undefined; - // When the range based query is enabled we take the range of the selection - if (uri.query === 'range=true') { - range = editor.selection.isEmpty - ? undefined - : client.code2ProtocolConverter.asRange(editor.selection); - } + const range = uri.query === 'range=true' && !editor.selection.isEmpty + ? client.code2ProtocolConverter.asRange(editor.selection) + : null; - const request: SyntaxTreeParams = { + return client.sendRequest(ra.syntaxTree, { textDocument: { uri: editor.document.uri.toString() }, range, - }; - return client.sendRequest( - 'rust-analyzer/syntaxTree', - request, - ); + }); } get onDidChange(): vscode.Event { -- cgit v1.2.3 From c9230b88b4852faf1dac59e05fd4f4d8c1f0dfb0 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 00:57:49 +0200 Subject: vscode: migrate inlay_hints to rust-analyzer-api.ts --- editors/code/src/inlay_hints.ts | 31 ++++++++----------------------- editors/code/src/util.ts | 16 ++++++++-------- 2 files changed, 16 insertions(+), 31 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/inlay_hints.ts b/editors/code/src/inlay_hints.ts index 5f9229efb..5951cf1b4 100644 --- a/editors/code/src/inlay_hints.ts +++ b/editors/code/src/inlay_hints.ts @@ -1,5 +1,5 @@ import * as vscode from 'vscode'; -import * as lc from 'vscode-languageclient'; +import * as ra from './rust-analyzer-api'; import { Ctx } from './ctx'; import { log, sendRequestWithRetry } from './util'; @@ -39,16 +39,6 @@ export function activateInlayHints(ctx: Ctx) { void hintsUpdater.setEnabled(ctx.config.displayInlayHints); } -interface InlayHintsParams { - textDocument: lc.TextDocumentIdentifier; -} - -interface InlayHint { - range: vscode.Range; - kind: "TypeHint" | "ParameterHint"; - label: string; -} - const typeHintDecorationType = vscode.window.createTextEditorDecorationType({ after: { color: new vscode.ThemeColor('rust_analyzer.inlayHint'), @@ -107,9 +97,9 @@ class HintsUpdater { if (newHints == null) return; const newTypeDecorations = newHints - .filter(hint => hint.kind === 'TypeHint') + .filter(hint => hint.kind === ra.InlayKind.TypeHint) .map(hint => ({ - range: hint.range, + range: this.ctx.client.protocol2CodeConverter.asRange(hint.range), renderOptions: { after: { contentText: `: ${hint.label}`, @@ -119,9 +109,9 @@ class HintsUpdater { this.setTypeDecorations(editor, newTypeDecorations); const newParameterDecorations = newHints - .filter(hint => hint.kind === 'ParameterHint') + .filter(hint => hint.kind === ra.InlayKind.ParameterHint) .map(hint => ({ - range: hint.range, + range: this.ctx.client.protocol2CodeConverter.asRange(hint.range), renderOptions: { before: { contentText: `${hint.label}: `, @@ -151,20 +141,15 @@ class HintsUpdater { ); } - private async queryHints(documentUri: string): Promise { + private async queryHints(documentUri: string): Promise { this.pending.get(documentUri)?.cancel(); const tokenSource = new vscode.CancellationTokenSource(); this.pending.set(documentUri, tokenSource); - const request: InlayHintsParams = { textDocument: { uri: documentUri } }; + const request = { textDocument: { uri: documentUri } }; - return sendRequestWithRetry( - this.ctx.client, - 'rust-analyzer/inlayHints', - request, - tokenSource.token - ) + return sendRequestWithRetry(this.ctx.client, ra.inlayHints, request, tokenSource.token) .catch(_ => null) .finally(() => { if (!tokenSource.token.isCancellationRequested) { diff --git a/editors/code/src/util.ts b/editors/code/src/util.ts index 2f18f85a3..68c2a94d0 100644 --- a/editors/code/src/util.ts +++ b/editors/code/src/util.ts @@ -20,21 +20,21 @@ export const log = { } }; -export async function sendRequestWithRetry( +export async function sendRequestWithRetry( client: lc.LanguageClient, - method: string, - param: unknown, + reqType: lc.RequestType, + param: TParam, token?: vscode.CancellationToken, -): Promise { +): Promise { for (const delay of [2, 4, 6, 8, 10, null]) { try { return await (token - ? client.sendRequest(method, param, token) - : client.sendRequest(method, param) + ? client.sendRequest(reqType, param, token) + : client.sendRequest(reqType, param) ); } catch (error) { if (delay === null) { - log.error("LSP request timed out", { method, param, error }); + log.error("LSP request timed out", { method: reqType.method, param, error }); throw error; } @@ -43,7 +43,7 @@ export async function sendRequestWithRetry( } if (error.code !== lc.ErrorCodes.ContentModified) { - log.error("LSP request failed", { method, param, error }); + log.error("LSP request failed", { method: reqType.method, param, error }); throw error; } -- cgit v1.2.3 From 72e81dae71b8d2efbc418cba206b1988727766b7 Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 01:00:00 +0200 Subject: vscode: run fmt --- editors/code/src/commands/ssr.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'editors/code') diff --git a/editors/code/src/commands/ssr.ts b/editors/code/src/commands/ssr.ts index 63c36ce80..eee48c693 100644 --- a/editors/code/src/commands/ssr.ts +++ b/editors/code/src/commands/ssr.ts @@ -23,7 +23,7 @@ export function ssr(ctx: Ctx): Cmd { if (!request) return; - const change = await client.sendRequest(ra.ssr, { arg: request },); + const change = await client.sendRequest(ra.ssr, { arg: request }); await applySourceChange(ctx, change); }; -- cgit v1.2.3 From 18b97d9d367d5fc1533c48157ebca7bb18b62e3c Mon Sep 17 00:00:00 2001 From: Veetaha Date: Tue, 25 Feb 2020 01:43:52 +0200 Subject: vscode: migrate rust-analyzer-api to import * as lc as per matklad and kjeremy --- editors/code/src/rust-analyzer-api.ts | 46 +++++++++++++++++------------------ 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'editors/code') diff --git a/editors/code/src/rust-analyzer-api.ts b/editors/code/src/rust-analyzer-api.ts index d2738fef3..c5a010e94 100644 --- a/editors/code/src/rust-analyzer-api.ts +++ b/editors/code/src/rust-analyzer-api.ts @@ -2,17 +2,17 @@ * This file mirrors `crates/rust-analyzer/src/req.rs` declarations. */ -import { RequestType, TextDocumentIdentifier, Position, Range, TextDocumentPositionParams, Location, NotificationType, WorkspaceEdit } from "vscode-languageclient"; +import * as lc from "vscode-languageclient"; type Option = null | T; type Vec = T[]; type FxHashMap = Record; function request(method: string) { - return new RequestType(`rust-analyzer/${method}`); + return new lc.RequestType(`rust-analyzer/${method}`); } function notification(method: string) { - return new NotificationType(method); + return new lc.NotificationType(method); } @@ -23,15 +23,15 @@ export const collectGarbage = request("collectGarbage"); export interface SyntaxTreeParams { - textDocument: TextDocumentIdentifier; - range: Option; + textDocument: lc.TextDocumentIdentifier; + range: Option; } export const syntaxTree = request("syntaxTree"); export interface ExpandMacroParams { - textDocument: TextDocumentIdentifier; - position: Option; + textDocument: lc.TextDocumentIdentifier; + position: Option; } export interface ExpandedMacro { name: string; @@ -41,10 +41,10 @@ export const expandMacro = request>("ex export interface FindMatchingBraceParams { - textDocument: TextDocumentIdentifier; - offsets: Vec; + textDocument: lc.TextDocumentIdentifier; + offsets: Vec; } -export const findMatchingBrace = request>("findMatchingBrace"); +export const findMatchingBrace = request>("findMatchingBrace"); export interface PublishDecorationsParams { @@ -52,31 +52,31 @@ export interface PublishDecorationsParams { decorations: Vec; } export interface Decoration { - range: Range; + range: lc.Range; tag: string; bindingHash: Option; } -export const decorationsRequest = request>("decorationsRequest"); +export const decorationsRequest = request>("decorationsRequest"); -export const parentModule = request>("parentModule"); +export const parentModule = request>("parentModule"); export interface JoinLinesParams { - textDocument: TextDocumentIdentifier; - range: Range; + textDocument: lc.TextDocumentIdentifier; + range: lc.Range; } export const joinLines = request("joinLines"); -export const onEnter = request>("onEnter"); +export const onEnter = request>("onEnter"); export interface RunnablesParams { - textDocument: TextDocumentIdentifier; - position: Option; + textDocument: lc.TextDocumentIdentifier; + position: Option; } export interface Runnable { - range: Range; + range: lc.Range; label: string; bin: string; args: Vec; @@ -91,12 +91,12 @@ export const enum InlayKind { ParameterHint = "ParameterHint", } export interface InlayHint { - range: Range; + range: lc.Range; kind: InlayKind; label: string; } export interface InlayHintsParams { - textDocument: TextDocumentIdentifier; + textDocument: lc.TextDocumentIdentifier; } export const inlayHints = request>("inlayHints"); @@ -112,6 +112,6 @@ export const publishDecorations = notification("publis export interface SourceChange { label: string; - workspaceEdit: WorkspaceEdit; - cursorPosition: Option; + workspaceEdit: lc.WorkspaceEdit; + cursorPosition: Option; } -- cgit v1.2.3