Add Suggestion Provider For TS Directives (#25806)
* Add Suggestion Provider For TS Directives Adds a new completion provider to suggest typescript comment directives, such as `@ts-check` or `@ts-ignore` Fixes #25413 * Add descriptions to snippets
This commit is contained in:
parent
de0303353f
commit
c70f922201
2 changed files with 76 additions and 2 deletions
|
@ -0,0 +1,71 @@
|
|||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
|
||||
'use strict';
|
||||
|
||||
import { Position, CompletionItemProvider, CompletionItemKind, TextDocument, CancellationToken, CompletionItem, ProviderResult, Range } from 'vscode';
|
||||
|
||||
import { ITypescriptServiceClient } from '../typescriptService';
|
||||
|
||||
import * as nls from 'vscode-nls';
|
||||
const localize = nls.loadMessageBundle();
|
||||
|
||||
interface Directive {
|
||||
value: string;
|
||||
description: string;
|
||||
}
|
||||
|
||||
const directives: Directive[] = [
|
||||
{
|
||||
value: '@ts-check',
|
||||
description: localize(
|
||||
'ts-check',
|
||||
'Enables semantic checking in a JavaScript file. Must be at the top of a file.')
|
||||
}, {
|
||||
value: '@ts-nocheck',
|
||||
description: localize(
|
||||
'ts-nocheck',
|
||||
'Disables semantic checking in a JavaScript file. Must be at the top of a file.')
|
||||
}, {
|
||||
value: '@ts-ignore',
|
||||
description: localize(
|
||||
'ts-ignore',
|
||||
'Suppresses @ts-check errors on the next line of a file.')
|
||||
}
|
||||
];
|
||||
|
||||
export class DirectiveCommentCompletionProvider implements CompletionItemProvider {
|
||||
constructor(
|
||||
private client: ITypescriptServiceClient,
|
||||
) { }
|
||||
|
||||
public provideCompletionItems(document: TextDocument, position: Position, _token: CancellationToken): ProviderResult<CompletionItem[]> {
|
||||
if (!this.client.apiVersion.has230Features()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const file = this.client.normalizePath(document.uri);
|
||||
if (!file) {
|
||||
return [];
|
||||
}
|
||||
|
||||
const line = document.lineAt(position.line).text;
|
||||
const prefix = line.slice(0, position.character);
|
||||
const match = prefix.match(/^\s*\/\/+\s?(@[a-zA-Z\-]*)?$/);
|
||||
if (match) {
|
||||
return directives.map(directive => {
|
||||
const item = new CompletionItem(directive.value, CompletionItemKind.Snippet);
|
||||
item.detail = directive.description;
|
||||
item.range = new Range(position.line, Math.max(0, position.character - match[1].length), position.line, position.character);
|
||||
return item;
|
||||
});
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
public resolveCompletionItem(item: CompletionItem, _token: CancellationToken) {
|
||||
return item;
|
||||
}
|
||||
}
|
|
@ -41,6 +41,8 @@ import WorkspaceSymbolProvider from './features/workspaceSymbolProvider';
|
|||
import CodeActionProvider from './features/codeActionProvider';
|
||||
import ReferenceCodeLensProvider from './features/referencesCodeLensProvider';
|
||||
import { JsDocCompletionProvider, TryCompleteJsDocCommand } from './features/jsDocCompletionProvider';
|
||||
import { DirectiveCommentCompletionProvider } from './features/directiveCommentCompletionProvider';
|
||||
|
||||
import ImplementationCodeLensProvider from './features/implementationsCodeLensProvider';
|
||||
|
||||
import * as BuildStatus from './utils/buildStatus';
|
||||
|
@ -215,6 +217,8 @@ class LanguageProvider {
|
|||
this.completionItemProvider.updateConfiguration();
|
||||
this.disposables.push(languages.registerCompletionItemProvider(selector, this.completionItemProvider, '.'));
|
||||
|
||||
this.disposables.push(languages.registerCompletionItemProvider(selector, new DirectiveCommentCompletionProvider(client), '@'));
|
||||
|
||||
this.formattingProvider = new FormattingProvider(client);
|
||||
this.formattingProvider.updateConfiguration(config);
|
||||
this.disposables.push(languages.registerOnTypeFormattingEditProvider(selector, this.formattingProvider, ';', '}', '\n'));
|
||||
|
@ -416,7 +420,7 @@ class LanguageProvider {
|
|||
|
||||
class TypeScriptServiceClientHost implements ITypescriptServiceClientHost {
|
||||
private client: TypeScriptServiceClient;
|
||||
private languages: LanguageProvider[];
|
||||
private languages: LanguageProvider[] = [];
|
||||
private languagePerId: ObjectMap<LanguageProvider>;
|
||||
private readonly disposables: Disposable[] = [];
|
||||
|
||||
|
@ -442,7 +446,6 @@ class TypeScriptServiceClientHost implements ITypescriptServiceClientHost {
|
|||
configFileWatcher.onDidChange(handleProjectChange, this, this.disposables);
|
||||
|
||||
this.client = new TypeScriptServiceClient(this, storagePath, globalState, workspaceState, this.disposables);
|
||||
this.languages = [];
|
||||
this.languagePerId = Object.create(null);
|
||||
for (const description of descriptions) {
|
||||
const manager = new LanguageProvider(this.client, description);
|
||||
|
|
Loading…
Reference in a new issue