From cb41ffbbbdea66d3a0abae4d270da1224a5de91c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 31 Dec 2019 18:50:32 +0100 Subject: Fix NPEs --- editors/code/src/commands/matching_brace.ts | 12 ++++++------ editors/code/src/commands/on_enter.ts | 6 ++++-- editors/code/src/commands/parent_module.ts | 11 ++++++----- editors/code/src/commands/runnables.ts | 7 ++++--- editors/code/src/commands/syntax_tree.ts | 9 ++++----- 5 files changed, 24 insertions(+), 21 deletions(-) (limited to 'editors') diff --git a/editors/code/src/commands/matching_brace.ts b/editors/code/src/commands/matching_brace.ts index 59c253f88..7c58bb7e7 100644 --- a/editors/code/src/commands/matching_brace.ts +++ b/editors/code/src/commands/matching_brace.ts @@ -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( + const response = await client.sendRequest( '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; diff --git a/editors/code/src/commands/on_enter.ts b/editors/code/src/commands/on_enter.ts index 8324060e8..6f61883cd 100644 --- a/editors/code/src/commands/on_enter.ts +++ b/editors/code/src/commands/on_enter.ts @@ -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( + const change = await client.sendRequest( 'rust-analyzer/onEnter', request, ); diff --git a/editors/code/src/commands/parent_module.ts b/editors/code/src/commands/parent_module.ts index 258b61b21..bf40b4021 100644 --- a/editors/code/src/commands/parent_module.ts +++ b/editors/code/src/commands/parent_module.ts @@ -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( + const response = await client.sendRequest( '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); diff --git a/editors/code/src/commands/runnables.ts b/editors/code/src/commands/runnables.ts index 8cd86c21e..7919997ce 100644 --- a/editors/code/src/commands/runnables.ts +++ b/editors/code/src/commands/runnables.ts @@ -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( + const runnables = await client.sendRequest( 'rust-analyzer/runnables', params, ); diff --git a/editors/code/src/commands/syntax_tree.ts b/editors/code/src/commands/syntax_tree.ts index 5b8f6e4d9..2ee80f910 100644 --- a/editors/code/src/commands/syntax_tree.ts +++ b/editors/code/src/commands/syntax_tree.ts @@ -76,7 +76,8 @@ class TextDocumentContentProvider provideTextDocumentContent(uri: vscode.Uri): vscode.ProviderResult { 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( + return client.sendRequest( 'rust-analyzer/syntaxTree', request, ); -- cgit v1.2.3