Update html-language-features to use doQuoteComplete

This commit is contained in:
Robert Jin
2021-11-06 01:26:55 +00:00
parent c348f8164a
commit e6c9ee0796
7 changed files with 79 additions and 22 deletions

View File

@@ -3,15 +3,18 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import { window, workspace, Disposable, TextDocument, Position, SnippetString, TextDocumentChangeEvent, TextDocumentChangeReason } from 'vscode';
import { window, workspace, Disposable, TextDocument, Position, SnippetString, TextDocumentChangeEvent, TextDocumentChangeReason, Selection, TextDocumentContentChangeEvent } from 'vscode';
import { Runtime } from './htmlClient';
export function activateTagClosing(tagProvider: (document: TextDocument, position: Position) => Thenable<string>, supportedLanguages: { [id: string]: boolean }, configName: string, runtime: Runtime): Disposable {
export function activateAutoInsertion(provider: (kind: 'autoQuote' | 'autoClose', document: TextDocument, position: Position) => Thenable<string>, supportedLanguages: { [id: string]: boolean }, runtime: Runtime): Disposable {
const disposables: Disposable[] = [];
workspace.onDidChangeTextDocument(onDidChangeTextDocument, null, disposables);
let isEnabled = false;
let anyIsEnabled = false;
const isEnabled = {
'autoQuote': false,
'autoClose': false
};
updateEnabledState();
window.onDidChangeActiveTextEditor(updateEnabledState, null, disposables);
@@ -24,7 +27,7 @@ export function activateTagClosing(tagProvider: (document: TextDocument, positio
});
function updateEnabledState() {
isEnabled = false;
anyIsEnabled = false;
const editor = window.activeTextEditor;
if (!editor) {
return;
@@ -33,14 +36,13 @@ export function activateTagClosing(tagProvider: (document: TextDocument, positio
if (!supportedLanguages[document.languageId]) {
return;
}
if (!workspace.getConfiguration(undefined, document.uri).get<boolean>(configName)) {
return;
}
isEnabled = true;
isEnabled['autoQuote'] = workspace.getConfiguration(undefined, document.uri).get<boolean>('html.autoCreateQuotes') ?? false;
isEnabled['autoClose'] = workspace.getConfiguration(undefined, document.uri).get<boolean>('html.autoClosingTags') ?? false;
anyIsEnabled = isEnabled['autoQuote'] || isEnabled['autoClose'];
}
function onDidChangeTextDocument({ document, contentChanges, reason }: TextDocumentChangeEvent) {
if (!isEnabled || contentChanges.length === 0 || reason === TextDocumentChangeReason.Undo || reason === TextDocumentChangeReason.Redo) {
if (!anyIsEnabled || contentChanges.length === 0 || reason === TextDocumentChangeReason.Undo || reason === TextDocumentChangeReason.Redo) {
return;
}
const activeDocument = window.activeTextEditor && window.activeTextEditor.document;
@@ -53,15 +55,20 @@ export function activateTagClosing(tagProvider: (document: TextDocument, positio
const lastChange = contentChanges[contentChanges.length - 1];
const lastCharacter = lastChange.text[lastChange.text.length - 1];
if (lastChange.rangeLength > 0 || lastCharacter !== '>' && lastCharacter !== '/') {
return;
if (isEnabled['autoQuote'] && lastChange.rangeLength === 0 && lastCharacter === '=') {
doAutoInsert('autoQuote', document, lastChange);
} else if (isEnabled['autoClose'] && lastChange.rangeLength === 0 && (lastCharacter === '>' || lastCharacter === '/')) {
doAutoInsert('autoClose', document, lastChange);
}
}
function doAutoInsert(kind: 'autoQuote' | 'autoClose', document: TextDocument, lastChange: TextDocumentContentChangeEvent) {
const rangeStart = lastChange.range.start;
const version = document.version;
timeout = runtime.timer.setTimeout(() => {
const position = new Position(rangeStart.line, rangeStart.character + lastChange.text.length);
tagProvider(document, position).then(text => {
if (text && isEnabled) {
provider(kind, document, position).then(text => {
if (text && isEnabled[kind]) {
const activeEditor = window.activeTextEditor;
if (activeEditor) {
const activeDocument = activeEditor.document;
@@ -69,6 +76,10 @@ export function activateTagClosing(tagProvider: (document: TextDocument, positio
const selections = activeEditor.selections;
if (selections.length && selections.some(s => s.active.isEqual(position))) {
activeEditor.insertSnippet(new SnippetString(text), selections.map(s => s.active));
if (kind === 'autoQuote') {
// Move all cursors one forward
activeEditor.selections = selections.map(s => new Selection(s.active.translate(0, 1), s.active.translate(0, 1)));
}
} else {
activeEditor.insertSnippet(new SnippetString(text), position);
}

View File

@@ -15,9 +15,9 @@ import {
LanguageClientOptions, RequestType, TextDocumentPositionParams, DocumentRangeFormattingParams,
DocumentRangeFormattingRequest, ProvideCompletionItemsSignature, TextDocumentIdentifier, RequestType0, Range as LspRange, NotificationType, CommonLanguageClient
} from 'vscode-languageclient';
import { activateTagClosing } from './tagClosing';
import { FileSystemProvider, serveFileSystemRequests } from './requests';
import { getCustomDataSource } from './customData';
import { activateAutoInsertion } from './autoInsertion';
namespace CustomDataChangedNotification {
export const type: NotificationType<string[]> = new NotificationType('html/customDataChanged');
@@ -27,9 +27,6 @@ namespace CustomDataContent {
export const type: RequestType<string, string, any> = new RequestType('html/customDataContent');
}
namespace TagCloseRequest {
export const type: RequestType<TextDocumentPositionParams, string, any> = new RequestType('html/tag');
}
// experimental: semantic tokens
interface SemanticTokenParams {
textDocument: TextDocumentIdentifier;
@@ -133,11 +130,20 @@ export function startClient(context: ExtensionContext, newLanguageClient: Langua
client.onRequest(CustomDataContent.type, customDataSource.getContent);
let tagRequestor = (document: TextDocument, position: Position) => {
let insertRequestor = (kind: 'autoQuote' | 'autoClose', document: TextDocument, position: Position) => {
let param = client.code2ProtocolConverter.asTextDocumentPositionParams(document, position);
return client.sendRequest(TagCloseRequest.type, param);
let request: RequestType<TextDocumentPositionParams, string, any>;
switch (kind) {
case 'autoQuote':
request = new RequestType('html/quote');
break;
case 'autoClose':
request = new RequestType('html/tag');
break;
}
return client.sendRequest(request, param);
};
disposable = activateTagClosing(tagRequestor, { html: true, handlebars: true }, 'html.autoClosingTags', runtime);
let disposable = activateAutoInsertion(insertRequestor, { html: true, handlebars: true }, runtime);
toDispose.push(disposable);
disposable = client.onTelemetry(e => {