From f1afe720873f8d11f2eff8d0c3c3fb8045d33c48 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Fri, 8 Feb 2019 18:06:08 -0800 Subject: [PATCH] Add getDocument helper method Many places in `extHostLanguageFeatures` were calling `getDocumentData` without checking if the result is undefined. Add an `getDocument` that cannot return undefined values and instead throws an error if there is no document --- src/vs/workbench/api/node/extHostComments.ts | 71 +++++-------------- src/vs/workbench/api/node/extHostDocuments.ts | 8 +++ .../api/node/extHostLanguageFeatures.ts | 44 ++++++------ 3 files changed, 46 insertions(+), 77 deletions(-) diff --git a/src/vs/workbench/api/node/extHostComments.ts b/src/vs/workbench/api/node/extHostComments.ts index 3e76d66a03e..547da7fe41f 100644 --- a/src/vs/workbench/api/node/extHostComments.ts +++ b/src/vs/workbench/api/node/extHostComments.ts @@ -111,107 +111,68 @@ export class ExtHostComments implements ExtHostCommentsShape { } $editComment(handle: number, uri: UriComponents, comment: modes.Comment, text: string): Promise { - const data = this._documents.getDocumentData(URI.revive(uri)); - - if (!data || !data.document) { - throw new Error('Unable to retrieve document from URI'); - } - + const document = this._documents.getDocument(URI.revive(uri)); const handlerData = this._documentProviders.get(handle); return asPromise(() => { - return handlerData.provider.editComment(data.document, convertFromComment(comment), text, CancellationToken.None); + return handlerData.provider.editComment(document, convertFromComment(comment), text, CancellationToken.None); }); } $deleteComment(handle: number, uri: UriComponents, comment: modes.Comment): Promise { - const data = this._documents.getDocumentData(URI.revive(uri)); - - if (!data || !data.document) { - throw new Error('Unable to retrieve document from URI'); - } - + const document = this._documents.getDocument(URI.revive(uri)); const handlerData = this._documentProviders.get(handle); return asPromise(() => { - return handlerData.provider.deleteComment(data.document, convertFromComment(comment), CancellationToken.None); + return handlerData.provider.deleteComment(document, convertFromComment(comment), CancellationToken.None); }); } $startDraft(handle: number, uri: UriComponents): Promise { - const data = this._documents.getDocumentData(URI.revive(uri)); - - if (!data || !data.document) { - throw new Error('Unable to retrieve document from URI'); - } + const document = this._documents.getDocument(URI.revive(uri)); const handlerData = this._documentProviders.get(handle); return asPromise(() => { - return handlerData.provider.startDraft(data.document, CancellationToken.None); + return handlerData.provider.startDraft(document, CancellationToken.None); }); } $deleteDraft(handle: number, uri: UriComponents): Promise { - const data = this._documents.getDocumentData(URI.revive(uri)); - - if (!data || !data.document) { - throw new Error('Unable to retrieve document from URI'); - } - + const document = this._documents.getDocument(URI.revive(uri)); const handlerData = this._documentProviders.get(handle); return asPromise(() => { - return handlerData.provider.deleteDraft(data.document, CancellationToken.None); + return handlerData.provider.deleteDraft(document, CancellationToken.None); }); } $finishDraft(handle: number, uri: UriComponents): Promise { - const data = this._documents.getDocumentData(URI.revive(uri)); - - if (!data || !data.document) { - throw new Error('Unable to retrieve document from URI'); - } - + const document = this._documents.getDocument(URI.revive(uri)); const handlerData = this._documentProviders.get(handle); return asPromise(() => { - return handlerData.provider.finishDraft(data.document, CancellationToken.None); + return handlerData.provider.finishDraft(document, CancellationToken.None); }); } $addReaction(handle: number, uri: UriComponents, comment: modes.Comment, reaction: modes.CommentReaction): Promise { - const data = this._documents.getDocumentData(URI.revive(uri)); - - if (!data || !data.document) { - throw new Error('Unable to retrieve document from URI'); - } - + const document = this._documents.getDocument(URI.revive(uri)); const handlerData = this._documentProviders.get(handle); return asPromise(() => { - return handlerData.provider.addReaction(data.document, convertFromComment(comment), reaction); + return handlerData.provider.addReaction(document, convertFromComment(comment), reaction); }); } $deleteReaction(handle: number, uri: UriComponents, comment: modes.Comment, reaction: modes.CommentReaction): Promise { - const data = this._documents.getDocumentData(URI.revive(uri)); - - if (!data || !data.document) { - throw new Error('Unable to retrieve document from URI'); - } - + const document = this._documents.getDocument(URI.revive(uri)); const handlerData = this._documentProviders.get(handle); - return asPromise(() => { - return handlerData.provider.deleteReaction(data.document, convertFromComment(comment), reaction); + return handlerData.provider.deleteReaction(document, convertFromComment(comment), reaction); }); } $provideDocumentComments(handle: number, uri: UriComponents): Promise { - const data = this._documents.getDocumentData(URI.revive(uri)); - if (!data || !data.document) { - return Promise.resolve(null); - } - + const document = this._documents.getDocument(URI.revive(uri)); const handlerData = this._documentProviders.get(handle); return asPromise(() => { - return handlerData.provider.provideDocumentComments(data.document, CancellationToken.None); + return handlerData.provider.provideDocumentComments(document, CancellationToken.None); }).then(commentInfo => commentInfo ? convertCommentInfo(handle, handlerData.extensionId, handlerData.provider, commentInfo, this._commandsConverter) : null); } diff --git a/src/vs/workbench/api/node/extHostDocuments.ts b/src/vs/workbench/api/node/extHostDocuments.ts index e7c6cff941a..5928a917384 100644 --- a/src/vs/workbench/api/node/extHostDocuments.ts +++ b/src/vs/workbench/api/node/extHostDocuments.ts @@ -67,6 +67,14 @@ export class ExtHostDocuments implements ExtHostDocumentsShape { return undefined; } + public getDocument(resource: vscode.Uri): vscode.TextDocument { + const data = this.getDocumentData(resource); + if (!data || !data.document) { + throw new Error('Unable to retrieve document from URI'); + } + return data.document; + } + public ensureDocumentData(uri: URI): Promise { let cached = this._documentsAndEditors.getDocument(uri.toString()); diff --git a/src/vs/workbench/api/node/extHostLanguageFeatures.ts b/src/vs/workbench/api/node/extHostLanguageFeatures.ts index ee1a0622d25..72cb8b1f545 100644 --- a/src/vs/workbench/api/node/extHostLanguageFeatures.ts +++ b/src/vs/workbench/api/node/extHostLanguageFeatures.ts @@ -40,7 +40,7 @@ class DocumentSymbolAdapter { } provideDocumentSymbols(resource: URI, token: CancellationToken): Promise { - let doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); return asPromise(() => this._provider.provideDocumentSymbols(doc, token)).then(value => { if (isFalsyOrEmpty(value)) { return undefined; @@ -105,7 +105,7 @@ class CodeLensAdapter { ) { } provideCodeLenses(resource: URI, token: CancellationToken): Promise { - const doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); return asPromise(() => this._provider.provideCodeLenses(doc, token)).then(lenses => { let result: CodeLensDto[] = []; @@ -161,7 +161,7 @@ class DefinitionAdapter { ) { } provideDefinition(resource: URI, position: IPosition, token: CancellationToken): Promise { - let doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); let pos = typeConvert.Position.to(position); return asPromise(() => this._provider.provideDefinition(doc, pos, token)).then(convertToLocationLinks); } @@ -175,7 +175,7 @@ class DeclarationAdapter { ) { } provideDeclaration(resource: URI, position: IPosition, token: CancellationToken): Promise { - let doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); let pos = typeConvert.Position.to(position); return asPromise(() => this._provider.provideDeclaration(doc, pos, token)).then(convertToLocationLinks); } @@ -189,7 +189,7 @@ class ImplementationAdapter { ) { } provideImplementation(resource: URI, position: IPosition, token: CancellationToken): Promise { - let doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); let pos = typeConvert.Position.to(position); return asPromise(() => this._provider.provideImplementation(doc, pos, token)).then(convertToLocationLinks); } @@ -203,7 +203,7 @@ class TypeDefinitionAdapter { ) { } provideTypeDefinition(resource: URI, position: IPosition, token: CancellationToken): Promise { - const doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); const pos = typeConvert.Position.to(position); return asPromise(() => this._provider.provideTypeDefinition(doc, pos, token)).then(convertToLocationLinks); } @@ -218,7 +218,7 @@ class HoverAdapter { public provideHover(resource: URI, position: IPosition, token: CancellationToken): Promise { - let doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); let pos = typeConvert.Position.to(position); return asPromise(() => this._provider.provideHover(doc, pos, token)).then(value => { @@ -246,7 +246,7 @@ class DocumentHighlightAdapter { provideDocumentHighlights(resource: URI, position: IPosition, token: CancellationToken): Promise { - let doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); let pos = typeConvert.Position.to(position); return asPromise(() => this._provider.provideDocumentHighlights(doc, pos, token)).then(value => { @@ -266,7 +266,7 @@ class ReferenceAdapter { ) { } provideReferences(resource: URI, position: IPosition, context: modes.ReferenceContext, token: CancellationToken): Promise { - let doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); let pos = typeConvert.Position.to(position); return asPromise(() => this._provider.provideReferences(doc, pos, context, token)).then(value => { @@ -296,7 +296,7 @@ class CodeActionAdapter { provideCodeActions(resource: URI, rangeOrSelection: IRange | ISelection, context: modes.CodeActionContext, token: CancellationToken): Promise { - const doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); const ran = Selection.isISelection(rangeOrSelection) ? typeConvert.Selection.to(rangeOrSelection) : typeConvert.Range.to(rangeOrSelection); @@ -371,7 +371,7 @@ class DocumentFormattingAdapter { provideDocumentFormattingEdits(resource: URI, options: modes.FormattingOptions, token: CancellationToken): Promise { - const { document } = this._documents.getDocumentData(resource); + const document = this._documents.getDocument(resource); return asPromise(() => this._provider.provideDocumentFormattingEdits(document, options, token)).then(value => { if (Array.isArray(value)) { @@ -391,7 +391,7 @@ class RangeFormattingAdapter { provideDocumentRangeFormattingEdits(resource: URI, range: IRange, options: modes.FormattingOptions, token: CancellationToken): Promise { - const { document } = this._documents.getDocumentData(resource); + const document = this._documents.getDocument(resource); const ran = typeConvert.Range.to(range); return asPromise(() => this._provider.provideDocumentRangeFormattingEdits(document, ran, options, token)).then(value => { @@ -414,7 +414,7 @@ class OnTypeFormattingAdapter { provideOnTypeFormattingEdits(resource: URI, position: IPosition, ch: string, options: modes.FormattingOptions, token: CancellationToken): Promise { - const { document } = this._documents.getDocumentData(resource); + const document = this._documents.getDocument(resource); const pos = typeConvert.Position.to(position); return asPromise(() => this._provider.provideOnTypeFormattingEdits(document, pos, ch, options, token)).then(value => { @@ -501,7 +501,7 @@ class RenameAdapter { provideRenameEdits(resource: URI, position: IPosition, newName: string, token: CancellationToken): Promise { - let doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); let pos = typeConvert.Position.to(position); return asPromise(() => this._provider.provideRenameEdits(doc, pos, newName, token)).then(value => { @@ -525,7 +525,7 @@ class RenameAdapter { return Promise.resolve(undefined); } - let doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); let pos = typeConvert.Position.to(position); return asPromise(() => this._provider.prepareRename(doc, pos, token)).then(rangeOrLocation => { @@ -591,7 +591,7 @@ class SuggestAdapter { provideCompletionItems(resource: URI, position: IPosition, context: modes.CompletionContext, token: CancellationToken): Promise { - const doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); const pos = typeConvert.Position.to(position); return asPromise( @@ -654,7 +654,7 @@ class SuggestAdapter { return suggestion; } - const doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); const pos = typeConvert.Position.to(position); const wordRangeBeforePos = (doc.getWordRangeAtPosition(pos) as Range || new Range(pos, pos)).with({ end: pos }); const newSuggestion = this._convertCompletionItem(resolvedItem, pos, wordRangeBeforePos, _id, _parentId); @@ -741,7 +741,7 @@ class SignatureHelpAdapter { ) { } provideSignatureHelp(resource: URI, position: IPosition, context: modes.SignatureHelpContext, token: CancellationToken): Promise { - const doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); const pos = typeConvert.Position.to(position); const vscodeContext = this.reviveContext(context); @@ -780,7 +780,7 @@ class LinkProviderAdapter { ) { } provideLinks(resource: URI, token: CancellationToken): Promise { - const doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); return asPromise(() => this._provider.provideDocumentLinks(doc, token)).then(links => { if (!Array.isArray(links)) { @@ -824,7 +824,7 @@ class ColorProviderAdapter { ) { } provideColors(resource: URI, token: CancellationToken): Promise { - const doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); return asPromise(() => this._provider.provideDocumentColors(doc, token)).then(colors => { if (!Array.isArray(colors)) { return []; @@ -842,7 +842,7 @@ class ColorProviderAdapter { } provideColorPresentations(resource: URI, raw: IRawColorInfo, token: CancellationToken): Promise { - const document = this._documents.getDocumentData(resource).document; + const document = this._documents.getDocument(resource); const range = typeConvert.Range.to(raw.range); const color = typeConvert.Color.to(raw.color); return asPromise(() => this._provider.provideColorPresentations(color, { document, range }, token)).then(value => { @@ -859,7 +859,7 @@ class FoldingProviderAdapter { ) { } provideFoldingRanges(resource: URI, context: modes.FoldingContext, token: CancellationToken): Promise { - const doc = this._documents.getDocumentData(resource).document; + const doc = this._documents.getDocument(resource); return asPromise(() => this._provider.provideFoldingRanges(doc, context, token)).then(ranges => { if (!Array.isArray(ranges)) { return undefined;