@@ -44,6 +44,63 @@ function findNushellExecutable(): string | null {
4444 }
4545}
4646
47+ function startLanguageServer (
48+ context : vscode . ExtensionContext ,
49+ found_nushell_path : string ,
50+ ) : void {
51+ // Use Nushell's native LSP server
52+ const serverOptions : ServerOptions = {
53+ run : {
54+ command : found_nushell_path ,
55+ args : [ '--lsp' ] ,
56+ } ,
57+ debug : {
58+ command : found_nushell_path ,
59+ args : [ '--lsp' ] ,
60+ } ,
61+ } ;
62+
63+ // Options to control the language client
64+ const clientOptions : LanguageClientOptions = {
65+ initializationOptions : {
66+ timeout : 10000 , // 10 seconds
67+ } ,
68+ // Register the server for nushell files
69+ documentSelector : [ { scheme : 'file' , language : 'nushell' } ] ,
70+ synchronize : {
71+ // Notify the server about file changes to nushell files
72+ fileEvents : vscode . workspace . createFileSystemWatcher ( '**/*.nu' ) ,
73+ } ,
74+ } ;
75+
76+ // Create the language client and start the client.
77+ client = new LanguageClient (
78+ 'nushellLanguageServer' ,
79+ 'Nushell Language Server' ,
80+ serverOptions ,
81+ clientOptions ,
82+ ) ;
83+
84+ // Start the language client and register a disposable that stops it when disposed
85+ client . start ( ) . catch ( ( error ) => {
86+ vscode . window . showErrorMessage (
87+ `Failed to start Nushell language server: ${ error . message } ` ,
88+ ) ;
89+ } ) ;
90+
91+ const disposable = new vscode . Disposable ( ( ) => {
92+ if ( client ) {
93+ client . stop ( ) . catch ( ( error ) => {
94+ console . error (
95+ 'Failed to stop Nushell Language Server on dispose:' ,
96+ error ,
97+ ) ;
98+ } ) ;
99+ }
100+ } ) ;
101+ context . subscriptions . push ( disposable ) ;
102+ }
103+
47104export function activate ( context : vscode . ExtensionContext ) {
48105 console . log ( 'Terminals: ' + ( < any > vscode . window ) . terminals . length ) ;
49106
@@ -111,45 +168,47 @@ export function activate(context: vscode.ExtensionContext) {
111168 return ;
112169 }
113170
114- // Use Nushell's native LSP server
115- const serverOptions : ServerOptions = {
116- run : {
117- command : found_nushell_path ,
118- args : [ '--lsp' ] ,
119- } ,
120- debug : {
121- command : found_nushell_path ,
122- args : [ '--lsp' ] ,
123- } ,
124- } ;
125-
126- // Options to control the language client
127- const clientOptions : LanguageClientOptions = {
128- initializationOptions : {
129- timeout : 10000 , // 10 seconds
171+ // Start the language server when the extension is activated
172+ startLanguageServer ( context , found_nushell_path ) ;
173+
174+ // Register a command to stop the language server
175+ const stopCommand = vscode . commands . registerCommand (
176+ 'nushell.stopLanguageServer' ,
177+ async ( ) => {
178+ if ( client ) {
179+ try {
180+ await client . stop ( ) ;
181+ client = undefined ;
182+ vscode . window . showInformationMessage (
183+ 'Nushell Language Server stopped.' ,
184+ ) ;
185+ } catch ( error ) {
186+ vscode . window . showErrorMessage (
187+ `Failed to stop Nushell Language Server: ${ error } ` ,
188+ ) ;
189+ }
190+ } else {
191+ vscode . window . showInformationMessage (
192+ 'Nushell Language Server is not running.' ,
193+ ) ;
194+ }
130195 } ,
131- // Register the server for nushell files
132- documentSelector : [ { scheme : 'file' , language : 'nushell' } ] ,
133- synchronize : {
134- // Notify the server about file changes to nushell files
135- fileEvents : vscode . workspace . createFileSystemWatcher ( '**/*.nu' ) ,
196+ ) ;
197+ context . subscriptions . push ( stopCommand ) ;
198+
199+ // Register a command to start the language server
200+ const startCommand = vscode . commands . registerCommand (
201+ 'nushell.startLanguageServer' ,
202+ ( ) => {
203+ startLanguageServer ( context , found_nushell_path ) ;
204+ if ( client ) {
205+ vscode . window . showInformationMessage (
206+ 'Nushell Language Server started.' ,
207+ ) ;
208+ }
136209 } ,
137- } ;
138-
139- // Create the language client and start the client.
140- client = new LanguageClient (
141- 'nushellLanguageServer' ,
142- 'Nushell Language Server' ,
143- serverOptions ,
144- clientOptions ,
145210 ) ;
146-
147- // Start the client. This will also launch the server
148- client . start ( ) . catch ( ( error ) => {
149- vscode . window . showErrorMessage (
150- `Failed to start Nushell language server: ${ error . message } ` ,
151- ) ;
152- } ) ;
211+ context . subscriptions . push ( startCommand ) ;
153212}
154213
155214export function deactivate ( ) : Thenable < void > | undefined {
0 commit comments