|
6 | 6 | *--------------------------------------------------------------------------------------------*/ |
7 | 7 | 'use strict'; |
8 | 8 |
|
9 | | -import { window, workspace, Disposable, TextDocumentContentChangeEvent, TextDocument, Position, SnippetString } from 'vscode'; |
| 9 | +import { window, workspace, Disposable, TextDocumentContentChangeEvent, TextDocument, Position, SnippetString, Range } from 'vscode'; |
10 | 10 |
|
11 | | -export function activateTagClosing(tagProvider: (document: TextDocument, position: Position) => Thenable<string>, supportedLanguages: { [id: string]: boolean }, configName: string): Disposable { |
| 11 | +export interface AutoCloseResult { |
| 12 | + snippet: string, |
| 13 | + range?: Range |
| 14 | +} |
12 | 15 |
|
| 16 | +export function activateTagClosing(tagProvider: (document: TextDocument, position: Position) => Thenable<AutoCloseResult>, supportedLanguages: { [id: string]: boolean }, configName: string): Disposable { |
| 17 | + const TRIGGER_CHARACTERS = ['>', '/']; |
13 | 18 | let disposables: Disposable[] = []; |
14 | 19 | workspace.onDidChangeTextDocument(event => onDidChangeTextDocument(event.document, event.contentChanges), null, disposables); |
15 | 20 |
|
@@ -50,25 +55,37 @@ export function activateTagClosing(tagProvider: (document: TextDocument, positio |
50 | 55 | } |
51 | 56 | let lastChange = changes[changes.length - 1]; |
52 | 57 | let lastCharacter = lastChange.text[lastChange.text.length - 1]; |
53 | | - if (lastChange.rangeLength > 0 || lastCharacter !== '>' && lastCharacter !== '/') { |
| 58 | + if (lastChange.rangeLength > 0 || lastChange.text.length > 1 || lastCharacter in TRIGGER_CHARACTERS) { |
54 | 59 | return; |
55 | 60 | } |
56 | 61 | let rangeStart = lastChange.range.start; |
57 | 62 | let version = document.version; |
58 | 63 | timeout = setTimeout(() => { |
59 | 64 | let position = new Position(rangeStart.line, rangeStart.character + lastChange.text.length); |
60 | | - tagProvider(document, position).then(text => { |
| 65 | + tagProvider(document, position).then(result => { |
| 66 | + let text = result.snippet; |
| 67 | + let replaceLocation : Position | Range; |
| 68 | + let range : Range = result.range; |
| 69 | + if(range != null) { |
| 70 | + // re-create Range |
| 71 | + let line = range.start.line; |
| 72 | + let character = range.start.character; |
| 73 | + let startPosition = new Position(line, character); |
| 74 | + line = range.end.line; |
| 75 | + character = range.end.character; |
| 76 | + let endPosition = new Position(line, character); |
| 77 | + replaceLocation = new Range(startPosition, endPosition); |
| 78 | + } |
| 79 | + else { |
| 80 | + replaceLocation = position; |
| 81 | + } |
61 | 82 | if (text && isEnabled) { |
62 | 83 | let activeEditor = window.activeTextEditor; |
63 | 84 | if (activeEditor) { |
64 | 85 | let activeDocument = activeEditor.document; |
65 | 86 | if (document === activeDocument && activeDocument.version === version) { |
66 | | - let selections = activeEditor.selections; |
67 | | - if (selections.length && selections.some(s => s.active.isEqual(position))) { |
68 | | - activeEditor.insertSnippet(new SnippetString(text), selections.map(s => s.active)); |
69 | | - } else { |
70 | | - activeEditor.insertSnippet(new SnippetString(text), position); |
71 | | - } |
| 87 | + activeEditor.insertSnippet(new SnippetString(text), replaceLocation); |
| 88 | + |
72 | 89 | } |
73 | 90 | } |
74 | 91 | } |
|
0 commit comments