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