-
Notifications
You must be signed in to change notification settings - Fork 30.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Part of #34307. Add manual completions for closing jsx tags. Requires TS 3.0
- Loading branch information
Showing
4 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
66 changes: 66 additions & 0 deletions
66
extensions/typescript-language-features/src/features/tagCompletion.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import * as vscode from 'vscode'; | ||
import * as Proto from '../protocol'; | ||
import { ITypeScriptServiceClient } from '../typescriptService'; | ||
import API from '../utils/api'; | ||
import { VersionDependentRegistration } from '../utils/dependentRegistration'; | ||
import * as typeConverters from '../utils/typeConverters'; | ||
|
||
class TypeScriptTagCompletion implements vscode.CompletionItemProvider { | ||
constructor( | ||
private readonly client: ITypeScriptServiceClient | ||
) { } | ||
|
||
async provideCompletionItems( | ||
document: vscode.TextDocument, | ||
position: vscode.Position, | ||
token: vscode.CancellationToken, | ||
_context: vscode.CompletionContext | ||
): Promise<vscode.CompletionItem[] | undefined> { | ||
const filepath = this.client.toPath(document.uri); | ||
if (!filepath) { | ||
return undefined; | ||
} | ||
|
||
const args: Proto.JsxClosingTagRequestArgs = typeConverters.Position.toFileLocationRequestArgs(filepath, position); | ||
let body: Proto.TextInsertion | undefined = undefined; | ||
try { | ||
const response = await this.client.execute('jsxClosingTag', args, token); | ||
body = response && response.body; | ||
if (!body) { | ||
return undefined; | ||
} | ||
} catch { | ||
return undefined; | ||
} | ||
|
||
return [this.getCompletion(body)]; | ||
} | ||
|
||
private getCompletion(body: Proto.TextInsertion) { | ||
const completion = new vscode.CompletionItem(body.newText); | ||
completion.insertText = this.getTagSnippet(body); | ||
return completion; | ||
} | ||
|
||
private getTagSnippet(closingTag: Proto.TextInsertion): vscode.SnippetString { | ||
const snippet = new vscode.SnippetString(); | ||
snippet.appendPlaceholder('', 0); | ||
snippet.appendText(closingTag.newText); | ||
return snippet; | ||
} | ||
} | ||
|
||
export function register( | ||
selector: vscode.DocumentSelector, | ||
client: ITypeScriptServiceClient, | ||
) { | ||
return new VersionDependentRegistration(client, API.v300, () => | ||
vscode.languages.registerCompletionItemProvider(selector, | ||
new TypeScriptTagCompletion(client), | ||
'>')); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
import * as Proto from 'typescript/lib/protocol'; | ||
export = Proto; | ||
export = Proto; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters