-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update html-language-features to use doQuoteComplete
- Loading branch information
Showing
7 changed files
with
137 additions
and
0 deletions.
There are no files selected for viewing
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
86 changes: 86 additions & 0 deletions
86
extensions/html-language-features/client/src/quoteCreation.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,86 @@ | ||
/*--------------------------------------------------------------------------------------------- | ||
* Copyright (c) Microsoft Corporation. All rights reserved. | ||
* Licensed under the MIT License. See License.txt in the project root for license information. | ||
*--------------------------------------------------------------------------------------------*/ | ||
|
||
import { window, workspace, Disposable, TextDocument, Position, SnippetString, TextDocumentChangeEvent, TextDocumentChangeReason, Selection } from 'vscode'; | ||
import { Runtime } from './htmlClient'; | ||
|
||
export function activateQuoteCreation(quoteProvider: (document: TextDocument, position: Position) => Thenable<string>, supportedLanguages: { [id: string]: boolean }, configName: string, runtime: Runtime): Disposable { | ||
|
||
const disposables: Disposable[] = []; | ||
workspace.onDidChangeTextDocument(onDidChangeTextDocument, null, disposables); | ||
|
||
let isEnabled = false; | ||
updateEnabledState(); | ||
window.onDidChangeActiveTextEditor(updateEnabledState, null, disposables); | ||
|
||
let timeout: Disposable | undefined = undefined; | ||
|
||
disposables.push({ | ||
dispose: () => { | ||
timeout?.dispose(); | ||
} | ||
}); | ||
|
||
function updateEnabledState() { | ||
isEnabled = false; | ||
const editor = window.activeTextEditor; | ||
if (!editor) { | ||
return; | ||
} | ||
const document = editor.document; | ||
if (!supportedLanguages[document.languageId]) { | ||
return; | ||
} | ||
if (!workspace.getConfiguration(undefined, document.uri).get<boolean>(configName)) { | ||
return; | ||
} | ||
isEnabled = true; | ||
} | ||
|
||
function onDidChangeTextDocument({ document, contentChanges, reason }: TextDocumentChangeEvent) { | ||
if (!isEnabled || contentChanges.length === 0 || reason === TextDocumentChangeReason.Undo || reason === TextDocumentChangeReason.Redo) { | ||
return; | ||
} | ||
const activeDocument = window.activeTextEditor && window.activeTextEditor.document; | ||
if (document !== activeDocument) { | ||
return; | ||
} | ||
if (timeout) { | ||
timeout.dispose(); | ||
} | ||
|
||
const lastChange = contentChanges[contentChanges.length - 1]; | ||
const lastCharacter = lastChange.text[lastChange.text.length - 1]; | ||
if (lastChange.rangeLength > 0 || lastCharacter !== '=') { | ||
return; | ||
} | ||
const rangeStart = lastChange.range.start; | ||
const version = document.version; | ||
timeout = runtime.timer.setTimeout(() => { | ||
const position = new Position(rangeStart.line, rangeStart.character + lastChange.text.length); | ||
quoteProvider(document, position).then(text => { | ||
if (text && isEnabled) { | ||
const activeEditor = window.activeTextEditor; | ||
if (activeEditor) { | ||
const activeDocument = activeEditor.document; | ||
if (document === activeDocument && activeDocument.version === version) { | ||
const selections = activeEditor.selections; | ||
if (selections.length && selections.some(s => s.active.isEqual(position))) { | ||
activeEditor.insertSnippet(new SnippetString(text), selections.map(s => s.active)); | ||
// 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); | ||
// Selections gone or moved, don't move cursor | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
timeout = undefined; | ||
}, 100); | ||
} | ||
return Disposable.from(...disposables); | ||
} |
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
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
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