@@ -21,10 +21,51 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
2121 private commands ( document : vscode . TextDocument , options : vscode . FormattingOptions ) : vscode . TextEdit [ ] {
2222 const edits = [ ] ;
2323 let indent = 1 ;
24+ const isClass = document . fileName . toLowerCase ( ) . endsWith ( ".cls" ) ;
2425
26+ let inComment = false ;
27+ let isCode = ! isClass ;
28+ let jsScript = false ;
29+ let sql = false ;
30+ let sqlParens = 0 ;
2531 for ( let i = 0 ; i < document . lineCount ; i ++ ) {
2632 const line = document . lineAt ( i ) ;
33+ const text = this . stripLineComments ( line . text ) ;
2734
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+ }
2869 if ( line . text . length && ! line . text . trim ( ) . length ) {
2970 edits . push ( {
3071 newText : "" ,
@@ -33,6 +74,18 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
3374 continue ;
3475 }
3576
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+
3689 const commentsMatch = line . text . match ( / ^ ( \s * ) ( \/ \/ + | # + ; \s * | ; ) ( .* ) / i) ;
3790 if ( commentsMatch ) {
3891 const indentSize = options . tabSize * indent ;
@@ -170,13 +223,13 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
170223 for ( let i = 0 ; i < document . lineCount ; i ++ ) {
171224 const line = document . lineAt ( i ) ;
172225
173- const pattern = / (?< ! \$ ) ( \$ \b [ a - z ] + ) \b / gi;
226+ const pattern = / (?< ! \$ ) ( \$ \b [ a - z ] + ) \b ( \( ) ? / gi;
174227 let functionsMatch = null ;
175228 while ( ( functionsMatch = pattern . exec ( line . text ) ) !== null ) {
176- const [ , found ] = functionsMatch ;
229+ const [ , found , isFunc ] = functionsMatch ;
177230 const pos = functionsMatch . index ;
178231 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 =>
180233 el . alias . includes ( found . toUpperCase ( ) )
181234 ) ;
182235 if ( systemFunction ) {
@@ -193,4 +246,11 @@ export class DocumentFormattingEditProvider implements vscode.DocumentFormatting
193246
194247 return edits ;
195248 }
249+
250+ private stripLineComments ( text : string ) {
251+ text = text . replace ( / \/ \/ .* $ / , "" ) ;
252+ text = text . replace ( / # + ; .* $ / , "" ) ;
253+ text = text . replace ( / ; .* $ / , "" ) ;
254+ return text ;
255+ }
196256}
0 commit comments