33 * Licensed under the MIT License. See License.txt in the project root for license information.
44 * ------------------------------------------------------------------------------------------ */
55
6+ import { getVSCodeDownloadUrl } from '@vscode/test-electron/out/util' ;
7+ import { time } from 'console' ;
68import * as path from 'path' ;
7- import { workspace , ExtensionContext } from 'vscode' ;
9+ import * as vscode from 'vscode' ;
810
911import {
1012 LanguageClient ,
@@ -15,7 +17,52 @@ import {
1517
1618let client : LanguageClient ;
1719
18- export function activate ( context : ExtensionContext ) {
20+ export function activate ( context : vscode . ExtensionContext ) {
21+
22+ // const command = "extension.formatDocument";
23+
24+ // context.subscriptions.push(vscode.commands.registerCommand(command, formatDocument));
25+
26+ context . subscriptions . push ( vscode . languages . registerDocumentFormattingEditProvider ( 'ini' , {
27+ provideDocumentFormattingEdits : ( document : vscode . TextDocument ) : vscode . TextEdit [ ] => {
28+ // return formatDocument(document);
29+ const edits : vscode . TextEdit [ ] = [ ] ;
30+ let indentlevel = 0 ;
31+ const indentSize = 2 ;
32+
33+ let singleBlockList = [ "^\\b([Oo]bject)\\s+[a-zA-Z0-9_]" , "^\\b([Oo]bject[Rr]eskin)\\s+[a-zA-Z0-9_]" , "^\\b([Aa]dd[Mm]odule)$" , "^\\b([Rr]eplace[Mm]odule)$" , "^\\b([Dd]efault[Cc]ondition[Ss]tate)$" , "^\\b([Uu]nit[Ss]pecific[Ss]ounds)$" , "^\\b([Pp]rerequisites)$" , "^\\b([Aa]rmor[Ss]et)$" , "^\\b([Ww]eapon[Ss]et)$" ] ;
34+ let multiblock = [ "\\b([Dd]raw)\\s*=" , "\\b([Cc]ondition[Ss]tate)\\s*=" , "\\b([Tt]ransition[Ss]tate)\\s*=" , "\\b([Bb]ody)\\s*=" , "\\b([Bb]ehavior)\\s*=" , "\\b([Cc]lient[Uu]pdate)\\s*=" , "^\\b(Turret)$" ] ;
35+
36+ for ( let i = 0 ; i < document . lineCount ; i ++ ) {
37+ const lineText = document . lineAt ( i ) . text ;
38+ const trimmedLine = lineText . trim ( ) ;
39+
40+ if ( trimmedLine . endsWith ( "End" ) || trimmedLine . endsWith ( "end" ) || trimmedLine . endsWith ( "END" ) ) {
41+ indentlevel = Math . max ( 0 , indentlevel - 1 ) ;
42+ }
43+
44+ const indentedLine = " " . repeat ( indentSize * indentlevel ) + trimmedLine ;
45+ const range = new vscode . Range ( i , 0 , i , lineText . length ) ;
46+ edits . push ( vscode . TextEdit . replace ( range , indentedLine ) ) ;
47+
48+ for ( let block of singleBlockList ) {
49+ if ( checkLineWithRegex ( trimmedLine , block ) ) {
50+ indentlevel ++ ;
51+ }
52+ }
53+
54+ for ( let block of multiblock ) {
55+ if ( checkLineWithRegex ( trimmedLine , block ) ) {
56+ indentlevel ++ ;
57+ }
58+ }
59+
60+ }
61+
62+ return edits ;
63+ }
64+ } ) ) ;
65+
1966 // The server is implemented in node
2067 const serverModule = context . asAbsolutePath (
2168 path . join ( 'server' , 'out' , 'server.js' )
@@ -39,7 +86,7 @@ export function activate(context: ExtensionContext) {
3986 documentSelector : [ { scheme : 'file' , language : 'ini' } ] ,
4087 synchronize : {
4188 // Notify the server about file changes to '.clientrc files contained in the workspace
42- fileEvents : workspace . createFileSystemWatcher ( '**/.clientrc' )
89+ fileEvents : vscode . workspace . createFileSystemWatcher ( '**/.clientrc' )
4390 }
4491 } ;
4592
@@ -55,6 +102,11 @@ export function activate(context: ExtensionContext) {
55102 client . start ( ) ;
56103}
57104
105+ function checkLineWithRegex ( line : string , regex : string ) {
106+ let Regex = new RegExp ( regex , "g" ) ;
107+ return Regex . test ( line ) ;
108+ }
109+
58110export function deactivate ( ) : Thenable < void > | undefined {
59111 if ( ! client ) {
60112 return undefined ;
0 commit comments