Use more standard scheme across providers for getting TS response body

- Avoids extra checks when response cannot be null
This commit is contained in:
Matt Bierner
2018-07-25 18:50:16 -07:00
parent a1af04f571
commit e8b1ee0b4d
17 changed files with 64 additions and 69 deletions

View File

@@ -366,8 +366,8 @@ class TypeScriptCompletionItemProvider implements vscode.CompletionItemProvider
let details: Proto.CompletionEntryDetails[] | undefined;
try {
const response = await this.client.execute('completionEntryDetails', args, token);
details = response.body;
const { body } = await this.client.execute('completionEntryDetails', args, token);
details = body;
} catch {
return item;
}
@@ -529,9 +529,8 @@ class TypeScriptCompletionItemProvider implements vscode.CompletionItemProvider
// Workaround for https://github.com/Microsoft/TypeScript/issues/12677
// Don't complete function calls inside of destructive assigments or imports
try {
const infoResponse = await this.client.execute('quickinfo', typeConverters.Position.toFileLocationRequestArgs(filepath, position));
const info = infoResponse.body;
switch (info && info.kind) {
const { body } = await this.client.execute('quickinfo', typeConverters.Position.toFileLocationRequestArgs(filepath, position));
switch (body && body.kind) {
case 'var':
case 'let':
case 'const':

View File

@@ -26,15 +26,14 @@ class TypeScriptDocumentHighlightProvider implements vscode.DocumentHighlightPro
}
const args = typeConverters.Position.toFileLocationRequestArgs(file, position);
let items: Proto.OccurrencesResponseItem[] | undefined;
let items: Proto.OccurrencesResponseItem[];
try {
const response = await this.client.execute('occurrences', args, token);
items = response.body;
const { body } = await this.client.execute('occurrences', args, token);
if (!body) {
return [];
}
items = body;
} catch {
// noop
}
if (!items) {
return [];
}

View File

@@ -41,11 +41,14 @@ class TypeScriptDocumentSymbolProvider implements vscode.DocumentSymbolProvider
}
let tree: Proto.NavigationTree | undefined;
let tree: Proto.NavigationTree;
try {
const args: Proto.FileRequestArgs = { file };
const response = await this.client.execute('navtree', args, token);
tree = response.body;
const { body } = await this.client.execute('navtree', args, token);
if (!body) {
return undefined;
}
tree = body;
} catch {
return undefined;
}

View File

@@ -26,12 +26,12 @@ class TypeScriptFoldingProvider implements vscode.FoldingRangeProvider {
}
const args: Proto.FileRequestArgs = { file };
const response: Proto.OutliningSpansResponse = await this.client.execute('getOutliningSpans', args, token);
if (!response || !response.body) {
const { body } = await this.client.execute('getOutliningSpans', args, token);
if (!body) {
return;
}
return response.body
return body
.map(span => this.convertOutliningSpan(span, document))
.filter(foldingRange => !!foldingRange) as vscode.FoldingRange[];
}

View File

@@ -29,16 +29,19 @@ class TypeScriptFormattingProvider implements vscode.DocumentRangeFormattingEdit
await this.formattingOptionsManager.ensureConfigurationOptions(document, options, token);
let edits: Proto.CodeEdit[] | undefined;
let edits: Proto.CodeEdit[];
try {
const args = typeConverters.Range.toFormattingRequestArgs(file, range);
const response = await this.client.execute('format', args, token);
edits = response.body;
const { body } = await this.client.execute('format', args, token);
if (!body) {
return undefined;
}
edits = body;
} catch {
// noop
return undefined;
}
return (edits || []).map(typeConverters.TextEdit.fromCodeEdit);
return edits.map(typeConverters.TextEdit.fromCodeEdit);
}
public async provideOnTypeFormattingEdits(
@@ -60,8 +63,8 @@ class TypeScriptFormattingProvider implements vscode.DocumentRangeFormattingEdit
key: ch
};
try {
const response = await this.client.execute('formatonkey', args, token);
const edits = response.body;
const { body } = await this.client.execute('formatonkey', args, token);
const edits = body;
const result: vscode.TextEdit[] = [];
if (!edits) {
return result;

View File

@@ -27,12 +27,11 @@ class TypeScriptHoverProvider implements vscode.HoverProvider {
}
const args = typeConverters.Position.toFileLocationRequestArgs(filepath, position);
try {
const response = await this.client.execute('quickinfo', args, token);
if (response && response.body) {
const data = response.body;
const { body } = await this.client.execute('quickinfo', args, token);
if (body) {
return new vscode.Hover(
TypeScriptHoverProvider.getContents(data),
typeConverters.Range.fromTextSpan(data));
TypeScriptHoverProvider.getContents(body),
typeConverters.Range.fromTextSpan(body));
}
} catch (e) {
// noop

View File

@@ -23,9 +23,9 @@ export default class TypeScriptImplementationsCodeLensProvider extends TypeScrip
const codeLens = inputCodeLens as ReferencesCodeLens;
const args = typeConverters.Position.toFileLocationRequestArgs(codeLens.file, codeLens.range.start);
try {
const response = await this.client.execute('implementation', args, token);
if (response && response.body) {
const locations = response.body
const { body } = await this.client.execute('implementation', args, token);
if (body) {
const locations = body
.map(reference =>
// Only take first line on implementation: https://github.com/Microsoft/vscode/issues/23924
new vscode.Location(this.client.toResource(reference.file),

View File

@@ -45,12 +45,8 @@ class OrganizeImportsCommand implements Command {
}
}
};
const response = await this.client.execute('organizeImports', args);
if (!response || !response.success) {
return false;
}
const edits = typeconverts.WorkspaceEdit.fromFileCodeEdits(this.client, response.body);
const { body } = await this.client.execute('organizeImports', args);
const edits = typeconverts.WorkspaceEdit.fromFileCodeEdits(this.client, body);
return vscode.workspace.applyEdit(edits);
}
}

View File

@@ -47,8 +47,7 @@ class ApplyRefactoringCommand implements Command {
refactor,
action
};
const response = await this.client.execute('getEditsForRefactor', args);
const body = response && response.body;
const { body } = await this.client.execute('getEditsForRefactor', args);
if (!body || !body.edits.length) {
return false;
}
@@ -142,11 +141,11 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider {
const args: Proto.GetApplicableRefactorsRequestArgs = typeConverters.Range.toFileRangeRequestArgs(file, rangeOrSelection);
let refactorings: Proto.ApplicableRefactorInfo[];
try {
const response = await this.client.execute('getApplicableRefactors', args, token);
if (!response.body) {
const { body } = await this.client.execute('getApplicableRefactors', args, token);
if (!body) {
return undefined;
}
refactorings = response.body;
refactorings = body;
} catch {
return undefined;
}

View File

@@ -26,13 +26,13 @@ class TypeScriptReferenceSupport implements vscode.ReferenceProvider {
const args = typeConverters.Position.toFileLocationRequestArgs(filepath, position);
try {
const msg = await this.client.execute('references', args, token);
if (!msg.body) {
const { body } = await this.client.execute('references', args, token);
if (!body) {
return [];
}
const result: vscode.Location[] = [];
const has203Features = this.client.apiVersion.gte(API.v203);
for (const ref of msg.body.refs) {
for (const ref of body.refs) {
if (!options.includeDeclaration && has203Features && ref.isDefinition) {
continue;
}

View File

@@ -21,7 +21,7 @@ class TypeScriptReferencesCodeLensProvider extends TypeScriptBaseCodeLensProvide
const codeLens = inputCodeLens as ReferencesCodeLens;
const args = typeConverters.Position.toFileLocationRequestArgs(codeLens.file, codeLens.range.start);
return this.client.execute('references', args, token).then(response => {
if (!response || !response.body) {
if (!response.body) {
throw codeLens;
}

View File

@@ -32,17 +32,17 @@ class TypeScriptRenameProvider implements vscode.RenameProvider {
};
try {
const response = await this.client.execute('rename', args, token);
if (!response.body) {
const { body } = await this.client.execute('rename', args, token);
if (!body) {
return null;
}
const renameInfo = response.body.info;
const renameInfo = body.info;
if (!renameInfo.canRename) {
return Promise.reject<vscode.WorkspaceEdit>(renameInfo.localizedErrorMessage);
}
return this.toWorkspaceEdit(response.body.locs, newName);
return this.toWorkspaceEdit(body.locs, newName);
} catch {
// noop
}

View File

@@ -28,13 +28,13 @@ class TypeScriptSignatureHelpProvider implements vscode.SignatureHelpProvider {
}
const args: Proto.SignatureHelpRequestArgs = typeConverters.Position.toFileLocationRequestArgs(filepath, position);
let info: Proto.SignatureHelpItems | undefined = undefined;
let info: Proto.SignatureHelpItems;
try {
const response = await this.client.execute('signatureHelp', args, token);
info = response.body;
if (!info) {
const { body } = await this.client.execute('signatureHelp', args, token);
if (!body) {
return undefined;
}
info = body;
} catch {
return undefined;
}

View File

@@ -88,16 +88,16 @@ class TagClosing extends Disposable {
}
let position = new vscode.Position(rangeStart.line, rangeStart.character + lastChange.text.length);
let body: Proto.TextInsertion | undefined = undefined;
let insertion: Proto.TextInsertion;
const args: Proto.JsxClosingTagRequestArgs = typeConverters.Position.toFileLocationRequestArgs(filepath, position);
this._cancel = new vscode.CancellationTokenSource();
try {
const response = await this.client.execute('jsxClosingTag', args, this._cancel.token);
body = response && response.body;
const { body } = await this.client.execute('jsxClosingTag', args, this._cancel.token);
if (!body) {
return;
}
insertion = body;
} catch {
return;
}
@@ -114,7 +114,7 @@ class TagClosing extends Disposable {
const activeDocument = activeEditor.document;
if (document === activeDocument && activeDocument.version === version) {
activeEditor.insertSnippet(
this.getTagSnippet(body),
this.getTagSnippet(insertion),
this.getInsertionPositions(activeEditor, position));
}
}, 100);

View File

@@ -84,11 +84,11 @@ class UpdateImportsOnFileRenameHandler {
// Workaround for https://github.com/Microsoft/vscode/issues/52967
// Never attempt to update import paths if the file does not contain something the looks like an export
try {
const tree = await this.client.execute('navtree', { file: newFile });
const { body } = await this.client.execute('navtree', { file: newFile });
const hasExport = (node: Proto.NavigationTree): boolean => {
return !!node.kindModifiers.match(/\bexports?\b/g) || !!(node.childItems && node.childItems.some(hasExport));
};
if (!tree.body || !tree.body || !hasExport(tree.body)) {
if (!body || !hasExport(body)) {
return;
}
} catch {

View File

@@ -44,13 +44,13 @@ class TypeScriptWorkspaceSymbolProvider implements vscode.WorkspaceSymbolProvide
file: filepath,
searchValue: search
};
const response = await this.client.execute('navto', args, token);
if (!response.body) {
const { body } = await this.client.execute('navto', args, token);
if (!body) {
return [];
}
const result: vscode.SymbolInformation[] = [];
for (const item of response.body) {
for (const item of body) {
if (!item.containerName && item.kind === 'alias') {
continue;
}

View File

@@ -36,10 +36,7 @@ export async function applyCodeActionCommands(
): Promise<boolean> {
if (commands && commands.length) {
for (const command of commands) {
const response = await client.execute('applyCodeActionCommand', { command });
if (!response || !response.body) {
return false;
}
await client.execute('applyCodeActionCommand', { command });
}
}
return true;