Path: blob/main/extensions/html-language-features/client/src/autoInsertion.ts
3320 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { window, workspace, Disposable, TextDocument, Position, SnippetString, TextDocumentChangeEvent, TextDocumentChangeReason, TextDocumentContentChangeEvent } from 'vscode';6import { Runtime } from './htmlClient';7import { LanguageParticipants } from './languageParticipants';89export function activateAutoInsertion(provider: (kind: 'autoQuote' | 'autoClose', document: TextDocument, position: Position) => Thenable<string>, languageParticipants: LanguageParticipants, runtime: Runtime): Disposable {10const disposables: Disposable[] = [];11workspace.onDidChangeTextDocument(onDidChangeTextDocument, null, disposables);1213let anyIsEnabled = false;14const isEnabled = {15'autoQuote': false,16'autoClose': false17};18updateEnabledState();19window.onDidChangeActiveTextEditor(updateEnabledState, null, disposables);2021let timeout: Disposable | undefined = undefined;2223disposables.push({24dispose: () => {25timeout?.dispose();26}27});2829function updateEnabledState() {30anyIsEnabled = false;31const editor = window.activeTextEditor;32if (!editor) {33return;34}35const document = editor.document;36if (!languageParticipants.useAutoInsert(document.languageId)) {37return;38}39const configurations = workspace.getConfiguration(undefined, document.uri);40isEnabled['autoQuote'] = configurations.get<boolean>('html.autoCreateQuotes') ?? false;41isEnabled['autoClose'] = configurations.get<boolean>('html.autoClosingTags') ?? false;42anyIsEnabled = isEnabled['autoQuote'] || isEnabled['autoClose'];43}4445function onDidChangeTextDocument({ document, contentChanges, reason }: TextDocumentChangeEvent) {46if (!anyIsEnabled || contentChanges.length === 0 || reason === TextDocumentChangeReason.Undo || reason === TextDocumentChangeReason.Redo) {47return;48}49const activeDocument = window.activeTextEditor && window.activeTextEditor.document;50if (document !== activeDocument) {51return;52}53if (timeout) {54timeout.dispose();55}5657const lastChange = contentChanges[contentChanges.length - 1];58if (lastChange.rangeLength === 0 && isSingleLine(lastChange.text)) {59const lastCharacter = lastChange.text[lastChange.text.length - 1];60if (isEnabled['autoQuote'] && lastCharacter === '=') {61doAutoInsert('autoQuote', document, lastChange);62} else if (isEnabled['autoClose'] && (lastCharacter === '>' || lastCharacter === '/')) {63doAutoInsert('autoClose', document, lastChange);64}65}66}6768function isSingleLine(text: string): boolean {69return !/\n/.test(text);70}7172function doAutoInsert(kind: 'autoQuote' | 'autoClose', document: TextDocument, lastChange: TextDocumentContentChangeEvent) {73const rangeStart = lastChange.range.start;74const version = document.version;75timeout = runtime.timer.setTimeout(() => {76const position = new Position(rangeStart.line, rangeStart.character + lastChange.text.length);77provider(kind, document, position).then(text => {78if (text && isEnabled[kind]) {79const activeEditor = window.activeTextEditor;80if (activeEditor) {81const activeDocument = activeEditor.document;82if (document === activeDocument && activeDocument.version === version) {83const selections = activeEditor.selections;84if (selections.length && selections.some(s => s.active.isEqual(position))) {85activeEditor.insertSnippet(new SnippetString(text), selections.map(s => s.active));86} else {87activeEditor.insertSnippet(new SnippetString(text), position);88}89}90}91}92});93timeout = undefined;94}, 100);95}96return Disposable.from(...disposables);97}9899100