Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions editors/code/src/commands/analyzer_status.ts
Original file line number Diff line number Diff line change
@@ -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)
Expand Down Expand Up @@ -50,10 +51,7 @@ class TextDocumentContentProvider
const client = this.ctx.client;
if (!editor || !client) return '';

return client.sendRequest<string>(
'rust-analyzer/analyzerStatus',
null,
);
return client.sendRequest(ra.analyzerStatus, null);
}

get onDidChange(): vscode.Event<vscode.Uri> {
Expand Down
18 changes: 5 additions & 13 deletions editors/code/src/commands/expand_macro.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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';
Expand All @@ -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<ExpandedMacro>(
'rust-analyzer/expandMacro',
request,
);
});

if (expanded == null) return 'Not available';

Expand Down
9 changes: 4 additions & 5 deletions editors/code/src/commands/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -16,9 +17,7 @@ export * from './ssr';
export * from './server_version';

export function collectGarbage(ctx: Ctx): Cmd {
return async () => {
await ctx.client?.sendRequest<null>('rust-analyzer/collectGarbage', null);
};
return async () => ctx.client.sendRequest(ra.collectGarbage, null);
}

export function showReferences(ctx: Ctx): Cmd {
Expand All @@ -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) {
Expand Down
17 changes: 4 additions & 13 deletions editors/code/src/commands/join_lines.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,18 @@
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 () => {
const editor = ctx.activeRustEditor;
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<SourceChange>(
'rust-analyzer/joinLines',
request,
);
});
await applySourceChange(ctx, change);
};
}

interface JoinLinesParams {
textDocument: lc.TextDocumentIdentifier;
range: lc.Range;
}
15 changes: 3 additions & 12 deletions editors/code/src/commands/matching_brace.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<lc.Position[]>(
'rust-analyzer/findMatchingBrace',
request,
);
});
editor.selections = editor.selections.map((sel, idx) => {
const active = client.protocol2CodeConverter.asPosition(
response[idx],
Expand All @@ -29,8 +25,3 @@ export function matchingBrace(ctx: Ctx): Cmd {
editor.revealRange(editor.selection);
};
}

interface FindMatchingBraceParams {
textDocument: lc.TextDocumentIdentifier;
offsets: lc.Position[];
}
21 changes: 7 additions & 14 deletions editors/code/src/commands/on_enter.ts
Original file line number Diff line number Diff line change
@@ -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) {
Expand All @@ -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<undefined | SourceChange>(
'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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI: @matklad there is no need to wrap the return value into a promise in the then(onfulfilled, onrejected)/catch(onrejected) handlers.

}
);
}).catch(_error => {
// client.logFailedRequest(OnEnterRequest.type, error);
return null;
});
if (!change) return false;

await applySourceChange(ctx, change);
Expand Down
10 changes: 3 additions & 7 deletions editors/code/src/commands/parent_module.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<lc.Location[]>(
'rust-analyzer/parentModule',
request,
);
});
const loc = response[0];
if (loc == null) return;

Expand Down
29 changes: 7 additions & 22 deletions editors/code/src/commands/runnables.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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<Runnable[]>(
'rust-analyzer/runnables',
params,
);
});
const items: RunnableQuickPick[] = [];
if (prevRunnable) {
items.push(prevRunnable);
Expand All @@ -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;

Expand All @@ -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;
}
}
Expand All @@ -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',
Expand Down
16 changes: 5 additions & 11 deletions editors/code/src/commands/ssr.ts
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand All @@ -21,16 +23,8 @@ export function ssr(ctx: Ctx): Cmd {

if (!request) return;

const ssrRequest: SsrRequest = { arg: request };
const change = await client.sendRequest<SourceChange>(
'rust-analyzer/ssr',
ssrRequest,
);
const change = await client.sendRequest(ra.ssr, { arg: request });

await applySourceChange(ctx, change);
};
}

interface SsrRequest {
arg: string;
}
27 changes: 7 additions & 20 deletions editors/code/src/commands/syntax_tree.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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<vscode.Uri>();

Expand All @@ -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<string>(
'rust-analyzer/syntaxTree',
request,
);
});
}

get onDidChange(): vscode.Event<vscode.Uri> {
Expand Down
Loading