提交 3fc69f20 编写于 作者: M Matt Bierner

Add toOpenedFilePath method

Make sure we don't try making requests against TS for files that are not currently open
上级 7140c912
......@@ -48,7 +48,7 @@ export abstract class TypeScriptBaseCodeLensProvider implements vscode.CodeLensP
}
async provideCodeLenses(document: vscode.TextDocument, token: vscode.CancellationToken): Promise<vscode.CodeLens[]> {
const filepath = this.client.toPath(document.uri);
const filepath = this.client.toOpenedFilePath(document);
if (!filepath) {
return [];
}
......
......@@ -348,7 +348,7 @@ class TypeScriptCompletionItemProvider implements vscode.CompletionItemProvider
});
}
const file = this.client.toPath(document.uri);
const file = this.client.toOpenedFilePath(document);
if (!file) {
return null;
}
......@@ -435,7 +435,7 @@ class TypeScriptCompletionItemProvider implements vscode.CompletionItemProvider
return undefined;
}
const filepath = this.client.toPath(item.document.uri);
const filepath = this.client.toOpenedFilePath(item.document);
if (!filepath) {
return undefined;
}
......
......@@ -20,7 +20,7 @@ export default class TypeScriptDefinitionProviderBase {
position: vscode.Position,
token: vscode.CancellationToken
): Promise<vscode.Location[] | undefined> {
const filepath = this.client.toPath(document.uri);
const filepath = this.client.toOpenedFilePath(document);
if (!filepath) {
return undefined;
}
......
......@@ -22,7 +22,7 @@ export default class TypeScriptDefinitionProvider extends DefinitionProviderBase
token: vscode.CancellationToken
): Promise<vscode.DefinitionLink[] | vscode.Definition | undefined> {
if (this.client.apiVersion.gte(API.v270)) {
const filepath = this.client.toPath(document.uri);
const filepath = this.client.toOpenedFilePath(document);
if (!filepath) {
return undefined;
}
......
......@@ -45,7 +45,7 @@ class DirectiveCommentCompletionProvider implements vscode.CompletionItemProvide
position: vscode.Position,
_token: vscode.CancellationToken
): vscode.CompletionItem[] {
const file = this.client.toPath(document.uri);
const file = this.client.toOpenedFilePath(document);
if (!file) {
return [];
}
......
......@@ -14,11 +14,11 @@ class TypeScriptDocumentHighlightProvider implements vscode.DocumentHighlightPro
) { }
public async provideDocumentHighlights(
resource: vscode.TextDocument,
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken
): Promise<vscode.DocumentHighlight[]> {
const file = this.client.toPath(resource.uri);
const file = this.client.toOpenedFilePath(document);
if (!file) {
return [];
}
......
......@@ -35,14 +35,14 @@ class TypeScriptDocumentSymbolProvider implements vscode.DocumentSymbolProvider
private cachedResponse: CachedResponse<Proto.NavTreeResponse>,
) { }
public async provideDocumentSymbols(resource: vscode.TextDocument, token: vscode.CancellationToken): Promise<vscode.DocumentSymbol[] | undefined> {
const file = this.client.toPath(resource.uri);
public async provideDocumentSymbols(document: vscode.TextDocument, token: vscode.CancellationToken): Promise<vscode.DocumentSymbol[] | undefined> {
const file = this.client.toOpenedFilePath(document);
if (!file) {
return undefined;
}
const args: Proto.FileRequestArgs = { file };
const response = await this.cachedResponse.execute(resource, () => this.client.execute('navtree', args, token));
const response = await this.cachedResponse.execute(document, () => this.client.execute('navtree', args, token));
if (response.type !== 'response' || !response.body) {
return undefined;
}
......@@ -51,7 +51,7 @@ class TypeScriptDocumentSymbolProvider implements vscode.DocumentSymbolProvider
if (tree && tree.childItems) {
// The root represents the file. Ignore this when showing in the UI
const result: vscode.DocumentSymbol[] = [];
tree.childItems.forEach(item => TypeScriptDocumentSymbolProvider.convertNavTree(resource.uri, result, item));
tree.childItems.forEach(item => TypeScriptDocumentSymbolProvider.convertNavTree(document.uri, result, item));
return result;
}
......
......@@ -84,7 +84,7 @@ export default class FileConfigurationManager {
options: vscode.FormattingOptions,
token: vscode.CancellationToken
): Promise<void> {
const file = this.client.toPath(document.uri);
const file = this.client.toOpenedFilePath(document);
if (!file) {
return;
}
......
......@@ -20,7 +20,7 @@ class TypeScriptFoldingProvider implements vscode.FoldingRangeProvider {
_context: vscode.FoldingContext,
token: vscode.CancellationToken
): Promise<vscode.FoldingRange[] | undefined> {
const file = this.client.toPath(document.uri);
const file = this.client.toOpenedFilePath(document);
if (!file) {
return;
}
......
......@@ -22,7 +22,7 @@ class TypeScriptFormattingProvider implements vscode.DocumentRangeFormattingEdit
options: vscode.FormattingOptions,
token: vscode.CancellationToken
): Promise<vscode.TextEdit[] | undefined> {
const file = this.client.toPath(document.uri);
const file = this.client.toOpenedFilePath(document);
if (!file) {
return undefined;
}
......@@ -45,7 +45,7 @@ class TypeScriptFormattingProvider implements vscode.DocumentRangeFormattingEdit
options: vscode.FormattingOptions,
token: vscode.CancellationToken
): Promise<vscode.TextEdit[]> {
const file = this.client.toPath(document.uri);
const file = this.client.toOpenedFilePath(document);
if (!file) {
return [];
}
......
......@@ -21,7 +21,7 @@ class TypeScriptHoverProvider implements vscode.HoverProvider {
position: vscode.Position,
token: vscode.CancellationToken
): Promise<vscode.Hover | undefined> {
const filepath = this.client.toPath(document.uri);
const filepath = this.client.toOpenedFilePath(document);
if (!filepath) {
return undefined;
}
......
......@@ -44,7 +44,7 @@ class JsDocCompletionProvider implements vscode.CompletionItemProvider {
position: vscode.Position,
token: vscode.CancellationToken
): Promise<vscode.CompletionItem[] | undefined> {
const file = this.client.toPath(document.uri);
const file = this.client.toOpenedFilePath(document);
if (!file) {
return undefined;
}
......
......@@ -77,7 +77,7 @@ export class OrganizeImportsCodeActionProvider implements vscode.CodeActionProvi
context: vscode.CodeActionContext,
token: vscode.CancellationToken
): vscode.CodeAction[] {
const file = this.client.toPath(document.uri);
const file = this.client.toOpenedFilePath(document);
if (!file) {
return [];
}
......
......@@ -203,7 +203,7 @@ class TypeScriptQuickFixProvider implements vscode.CodeActionProvider {
context: vscode.CodeActionContext,
token: vscode.CancellationToken
): Promise<vscode.CodeAction[]> {
const file = this.client.toPath(document.uri);
const file = this.client.toOpenedFilePath(document);
if (!file) {
return [];
}
......
......@@ -132,7 +132,7 @@ class TypeScriptRefactorProvider implements vscode.CodeActionProvider {
return undefined;
}
const file = this.client.toPath(document.uri);
const file = this.client.toOpenedFilePath(document);
if (!file) {
return undefined;
}
......
......@@ -19,7 +19,7 @@ class TypeScriptReferenceSupport implements vscode.ReferenceProvider {
options: vscode.ReferenceContext,
token: vscode.CancellationToken
): Promise<vscode.Location[]> {
const filepath = this.client.toPath(document.uri);
const filepath = this.client.toOpenedFilePath(document);
if (!filepath) {
return [];
}
......
......@@ -79,7 +79,7 @@ class TypeScriptRenameProvider implements vscode.RenameProvider {
position: vscode.Position,
token: vscode.CancellationToken
): Promise<ServerResponse<Proto.RenameResponse> | undefined> {
const file = this.client.toPath(document.uri);
const file = this.client.toOpenedFilePath(document);
if (!file) {
return undefined;
}
......
......@@ -24,7 +24,7 @@ class TypeScriptSignatureHelpProvider implements vscode.SignatureHelpProvider {
token: vscode.CancellationToken,
context: vscode.SignatureHelpContext,
): Promise<vscode.SignatureHelp | undefined> {
const filepath = this.client.toPath(document.uri);
const filepath = this.client.toOpenedFilePath(document);
if (!filepath) {
return undefined;
}
......
......@@ -52,7 +52,7 @@ class TagClosing extends Disposable {
return;
}
const filepath = this.client.toPath(document.uri);
const filepath = this.client.toOpenedFilePath(document);
if (!filepath) {
return;
}
......
......@@ -30,12 +30,12 @@ class TypeScriptWorkspaceSymbolProvider implements vscode.WorkspaceSymbolProvide
search: string,
token: vscode.CancellationToken
): Promise<vscode.SymbolInformation[]> {
const uri = this.getUri();
if (!uri) {
const document = this.getDocument();
if (!document) {
return [];
}
const filepath = this.client.toPath(uri);
const filepath = this.client.toOpenedFilePath(document);
if (!filepath) {
return [];
}
......@@ -70,7 +70,7 @@ class TypeScriptWorkspaceSymbolProvider implements vscode.WorkspaceSymbolProvide
return label;
}
private getUri(): vscode.Uri | undefined {
private getDocument(): vscode.TextDocument | undefined {
// typescript wants to have a resource even when asking
// general questions so we check the active editor. If this
// doesn't match we take the first TS document.
......@@ -79,14 +79,14 @@ class TypeScriptWorkspaceSymbolProvider implements vscode.WorkspaceSymbolProvide
if (editor) {
const document = editor.document;
if (document && this.modeIds.indexOf(document.languageId) >= 0) {
return document.uri;
return document;
}
}
const documents = vscode.workspace.textDocuments;
for (const document of documents) {
if (this.modeIds.indexOf(document.languageId) >= 0) {
return document.uri;
return document;
}
}
return undefined;
......
......@@ -77,6 +77,13 @@ export interface ITypeScriptServiceClient {
*/
toResource(filepath: string): vscode.Uri;
/**
* Tries to ensure that a vscode document is open on the TS server.
*
* Returns the normalized path.
*/
toOpenedFilePath(document: vscode.TextDocument): string | undefined;
getWorkspaceRootForResource(resource: vscode.Uri): string | undefined;
readonly onTsServerStarted: vscode.Event<API>;
......
......@@ -514,6 +514,14 @@ export default class TypeScriptServiceClient extends Disposable implements IType
return this.normalizedPath(resource);
}
public toOpenedFilePath(document: vscode.TextDocument): string | undefined {
if (!this.bufferSyncSupport.handles(document.uri)) {
console.error(`Unexpected resource ${document.uri}`);
return undefined;
}
return this.toPath(document.uri) || undefined;
}
private get inMemoryResourcePrefix(): string {
return this._apiVersion.gte(API.v270) ? '^' : '';
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册