6
6
*--------------------------------------------------------------------------------------------*/
7
7
'use strict' ;
8
8
9
- import {
10
- window ,
11
- workspace ,
12
- Disposable ,
13
- TextDocument ,
14
- Position ,
15
- SnippetString ,
16
- } from 'vscode' ;
9
+ import { window , workspace , Disposable , TextDocument , Position , SnippetString } from 'vscode' ;
17
10
18
- import { TextDocumentContentChangeEvent } from " vscode-languageserver-protocol" ;
11
+ import { TextDocumentContentChangeEvent } from ' vscode-languageserver-protocol' ;
19
12
20
13
export function activateTagClosing (
21
14
tagProvider : ( document : TextDocument , position : Position ) => Thenable < string > ,
@@ -24,7 +17,7 @@ export function activateTagClosing(
24
17
) : Disposable {
25
18
const disposables : Disposable [ ] = [ ] ;
26
19
workspace . onDidChangeTextDocument (
27
- event => onDidChangeTextDocument ( event . document , event . contentChanges ) ,
20
+ ( event ) => onDidChangeTextDocument ( event . document , event . contentChanges ) ,
28
21
null ,
29
22
disposables ,
30
23
) ;
@@ -67,17 +60,35 @@ export function activateTagClosing(
67
60
}
68
61
const lastChange = changes [ changes . length - 1 ] ;
69
62
const lastCharacter = lastChange . text [ lastChange . text . length - 1 ] ;
70
- if ( "range" in lastChange && ( lastChange . rangeLength ?? 0 ) > 0 || ( lastCharacter !== '>' && lastCharacter !== '/' ) ) {
63
+ if (
64
+ ( 'range' in lastChange && ( lastChange . rangeLength ?? 0 ) > 0 ) ||
65
+ ( lastCharacter !== '>' && lastCharacter !== '/' )
66
+ ) {
71
67
return ;
72
68
}
73
- const rangeStart = "range" in lastChange ? lastChange . range . start : new Position ( 0 , document . getText ( ) . length ) ;
69
+
70
+ const docContent = document . getText ( ) ;
71
+
72
+ // VSCode has this property, but it's not in the standard typings
73
+ const offsetOfPrevCharacter = ( < any > lastChange ) . rangeOffset - 1 ;
74
+ const previousCharacter =
75
+ lastChange . text [ lastChange . text . length - 2 ] || docContent [ offsetOfPrevCharacter ] ;
76
+ // handle false positive of arrow function inside moustache tag so that this
77
+ // <div {() =>
78
+ // does not trigger auto close
79
+ if ( previousCharacter === '=' ) {
80
+ return ;
81
+ }
82
+
83
+ const rangeStart =
84
+ 'range' in lastChange ? lastChange . range . start : new Position ( 0 , docContent . length ) ;
74
85
const version = document . version ;
75
86
timeout = setTimeout ( ( ) => {
76
87
const position = new Position (
77
88
rangeStart . line ,
78
89
rangeStart . character + lastChange . text . length ,
79
90
) ;
80
- tagProvider ( document , position ) . then ( text => {
91
+ tagProvider ( document , position ) . then ( ( text ) => {
81
92
if ( text && isEnabled ) {
82
93
const activeEditor = window . activeTextEditor ;
83
94
if ( activeEditor ) {
@@ -86,11 +97,11 @@ export function activateTagClosing(
86
97
const selections = activeEditor . selections ;
87
98
if (
88
99
selections . length &&
89
- selections . some ( s => s . active . isEqual ( position ) )
100
+ selections . some ( ( s ) => s . active . isEqual ( position ) )
90
101
) {
91
102
activeEditor . insertSnippet (
92
103
new SnippetString ( text ) ,
93
- selections . map ( s => s . active ) ,
104
+ selections . map ( ( s ) => s . active ) ,
94
105
) ;
95
106
} else {
96
107
activeEditor . insertSnippet ( new SnippetString ( text ) , position ) ;
0 commit comments