@@ -21,10 +21,51 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
21
21
private commands ( document : vscode . TextDocument , options : vscode . FormattingOptions ) : vscode . TextEdit [ ] {
22
22
const edits = [ ] ;
23
23
let indent = 1 ;
24
+ const isClass = document . fileName . toLowerCase ( ) . endsWith ( ".cls" ) ;
24
25
26
+ let inComment = false ;
27
+ let isCode = ! isClass ;
28
+ let jsScript = false ;
29
+ let sql = false ;
30
+ let sqlParens = 0 ;
25
31
for ( let i = 0 ; i < document . lineCount ; i ++ ) {
26
32
const line = document . lineAt ( i ) ;
33
+ const text = this . stripLineComments ( line . text ) ;
27
34
35
+ if ( text . match ( / < s c r i p t .* > / ) ) {
36
+ jsScript = true ;
37
+ }
38
+
39
+ if ( text . match ( "&sql" ) ) {
40
+ sql = true ;
41
+ sqlParens = 0 ;
42
+ }
43
+
44
+ if ( sql ) {
45
+ sqlParens = sqlParens + ( text . split ( "(" ) . length - 1 ) - ( text . split ( ")" ) . length - 1 ) ;
46
+ if ( sqlParens <= 0 ) {
47
+ sql = false ;
48
+ }
49
+ continue ;
50
+ }
51
+
52
+ if ( jsScript ) {
53
+ if ( text . match ( / < \/ s c r i p t > / ) ) {
54
+ jsScript = false ;
55
+ }
56
+ continue ;
57
+ }
58
+
59
+ if ( text . match ( / \/ \* / ) ) {
60
+ inComment = true ;
61
+ }
62
+
63
+ if ( inComment ) {
64
+ if ( text . match ( / \* \/ / ) ) {
65
+ inComment = false ;
66
+ }
67
+ continue ;
68
+ }
28
69
if ( line . text . length && ! line . text . trim ( ) . length ) {
29
70
edits . push ( {
30
71
newText : "" ,
@@ -33,6 +74,18 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
33
74
continue ;
34
75
}
35
76
77
+ if ( isClass ) {
78
+ if ( isCode ) {
79
+ isCode = text . match ( / ^ } $ / ) === null ;
80
+ } else {
81
+ isCode = text . match ( / ^ ( c l a s s ) ? m e t h o d | t r i g g e r / i) != null ;
82
+ continue ;
83
+ }
84
+ }
85
+ if ( ! isCode ) {
86
+ continue ;
87
+ }
88
+
36
89
const commentsMatch = line . text . match ( / ^ ( \s * ) ( \/ \/ + | # + ; \s * | ; ) ( .* ) / i) ;
37
90
if ( commentsMatch ) {
38
91
const indentSize = options . tabSize * indent ;
@@ -170,13 +223,13 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
170
223
for ( let i = 0 ; i < document . lineCount ; i ++ ) {
171
224
const line = document . lineAt ( i ) ;
172
225
173
- const pattern = / (?< ! \$ ) ( \$ \b [ a - z ] + ) \b / gi;
226
+ const pattern = / (?< ! \$ ) ( \$ \b [ a - z ] + ) \b ( \( ) ? / gi;
174
227
let functionsMatch = null ;
175
228
while ( ( functionsMatch = pattern . exec ( line . text ) ) !== null ) {
176
- const [ , found ] = functionsMatch ;
229
+ const [ , found , isFunc ] = functionsMatch ;
177
230
const pos = functionsMatch . index ;
178
231
const range = new vscode . Range ( new vscode . Position ( i , pos ) , new vscode . Position ( i , pos + found . length ) ) ;
179
- const systemFunction = [ ... systemFunctions , ... systemVariables ] . find ( el =>
232
+ const systemFunction = ( isFunc ? systemFunctions : systemVariables ) . find ( el =>
180
233
el . alias . includes ( found . toUpperCase ( ) )
181
234
) ;
182
235
if ( systemFunction ) {
@@ -193,4 +246,11 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
193
246
194
247
return edits ;
195
248
}
249
+
250
+ private stripLineComments ( text : string ) {
251
+ text = text . replace ( / \/ \/ .* $ / , "" ) ;
252
+ text = text . replace ( / # + ; .* $ / , "" ) ;
253
+ text = text . replace ( / ; .* $ / , "" ) ;
254
+ return text ;
255
+ }
196
256
}
0 commit comments