mirror of
https://github.com/rust-lang/rust-analyzer
synced 2024-11-10 15:14:32 +00:00
Fix NPEs
This commit is contained in:
parent
6368b40dd9
commit
cb41ffbbbd
5 changed files with 24 additions and 21 deletions
|
@ -6,21 +6,21 @@ import { Ctx, Cmd } from '../ctx';
|
|||
export function matchingBrace(ctx: Ctx): Cmd {
|
||||
return async () => {
|
||||
const editor = ctx.activeRustEditor;
|
||||
if (!editor) {
|
||||
return;
|
||||
}
|
||||
const client = ctx.client;
|
||||
if (!editor || !client) return;
|
||||
|
||||
const request: FindMatchingBraceParams = {
|
||||
textDocument: { uri: editor.document.uri.toString() },
|
||||
offsets: editor.selections.map(s =>
|
||||
ctx.client.code2ProtocolConverter.asPosition(s.active),
|
||||
client.code2ProtocolConverter.asPosition(s.active),
|
||||
),
|
||||
};
|
||||
const response = await ctx.client.sendRequest<lc.Position[]>(
|
||||
const response = await client.sendRequest<lc.Position[]>(
|
||||
'rust-analyzer/findMatchingBrace',
|
||||
request,
|
||||
);
|
||||
editor.selections = editor.selections.map((sel, idx) => {
|
||||
const active = ctx.client.protocol2CodeConverter.asPosition(
|
||||
const active = client.protocol2CodeConverter.asPosition(
|
||||
response[idx],
|
||||
);
|
||||
const anchor = sel.isEmpty ? active : sel.anchor;
|
||||
|
|
|
@ -6,15 +6,17 @@ import { Cmd, Ctx } from '../ctx';
|
|||
export function onEnter(ctx: Ctx): Cmd {
|
||||
return async (event: { text: string }) => {
|
||||
const editor = ctx.activeRustEditor;
|
||||
const client = ctx.client;
|
||||
if (!editor || event.text !== '\n') return false;
|
||||
if (!client) return false;
|
||||
|
||||
const request: lc.TextDocumentPositionParams = {
|
||||
textDocument: { uri: editor.document.uri.toString() },
|
||||
position: ctx.client.code2ProtocolConverter.asPosition(
|
||||
position: client.code2ProtocolConverter.asPosition(
|
||||
editor.selection.active,
|
||||
),
|
||||
};
|
||||
const change = await ctx.client.sendRequest<undefined | SourceChange>(
|
||||
const change = await client.sendRequest<undefined | SourceChange>(
|
||||
'rust-analyzer/onEnter',
|
||||
request,
|
||||
);
|
||||
|
|
|
@ -6,23 +6,24 @@ import { Ctx, Cmd } from '../ctx';
|
|||
export function parentModule(ctx: Ctx): Cmd {
|
||||
return async () => {
|
||||
const editor = ctx.activeRustEditor;
|
||||
if (!editor) return;
|
||||
const client = ctx.client;
|
||||
if (!editor || !client) return;
|
||||
|
||||
const request: lc.TextDocumentPositionParams = {
|
||||
textDocument: { uri: editor.document.uri.toString() },
|
||||
position: ctx.client.code2ProtocolConverter.asPosition(
|
||||
position: client.code2ProtocolConverter.asPosition(
|
||||
editor.selection.active,
|
||||
),
|
||||
};
|
||||
const response = await ctx.client.sendRequest<lc.Location[]>(
|
||||
const response = await client.sendRequest<lc.Location[]>(
|
||||
'rust-analyzer/parentModule',
|
||||
request,
|
||||
);
|
||||
const loc = response[0];
|
||||
if (loc == null) return;
|
||||
|
||||
const uri = ctx.client.protocol2CodeConverter.asUri(loc.uri);
|
||||
const range = ctx.client.protocol2CodeConverter.asRange(loc.range);
|
||||
const uri = client.protocol2CodeConverter.asUri(loc.uri);
|
||||
const range = client.protocol2CodeConverter.asRange(loc.range);
|
||||
|
||||
const doc = await vscode.workspace.openTextDocument(uri);
|
||||
const e = await vscode.window.showTextDocument(doc);
|
||||
|
|
|
@ -8,18 +8,19 @@ export function run(ctx: Ctx): Cmd {
|
|||
|
||||
return async () => {
|
||||
const editor = ctx.activeRustEditor;
|
||||
if (!editor) return;
|
||||
const client = ctx.client;
|
||||
if (!editor || !client) return;
|
||||
|
||||
const textDocument: lc.TextDocumentIdentifier = {
|
||||
uri: editor.document.uri.toString(),
|
||||
};
|
||||
const params: RunnablesParams = {
|
||||
textDocument,
|
||||
position: ctx.client.code2ProtocolConverter.asPosition(
|
||||
position: client.code2ProtocolConverter.asPosition(
|
||||
editor.selection.active,
|
||||
),
|
||||
};
|
||||
const runnables = await ctx.client.sendRequest<Runnable[]>(
|
||||
const runnables = await client.sendRequest<Runnable[]>(
|
||||
'rust-analyzer/runnables',
|
||||
params,
|
||||
);
|
||||
|
|
|
@ -76,7 +76,8 @@ class TextDocumentContentProvider
|
|||
|
||||
provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult<string> {
|
||||
const editor = vscode.window.activeTextEditor;
|
||||
if (editor == null) return '';
|
||||
const client = this.ctx.client
|
||||
if (!editor || !client) return '';
|
||||
|
||||
let range: lc.Range | undefined;
|
||||
|
||||
|
@ -84,16 +85,14 @@ class TextDocumentContentProvider
|
|||
if (uri.query === 'range=true') {
|
||||
range = editor.selection.isEmpty
|
||||
? undefined
|
||||
: this.ctx.client.code2ProtocolConverter.asRange(
|
||||
editor.selection,
|
||||
);
|
||||
: client.code2ProtocolConverter.asRange(editor.selection);
|
||||
}
|
||||
|
||||
const request: SyntaxTreeParams = {
|
||||
textDocument: { uri: editor.document.uri.toString() },
|
||||
range,
|
||||
};
|
||||
return this.ctx.client.sendRequest<string>(
|
||||
return client.sendRequest<string>(
|
||||
'rust-analyzer/syntaxTree',
|
||||
request,
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue