@@ -7,7 +7,7 @@ import {updateStatusBar} from './status-bar';
7
7
import { xoRootCache } from './cache' ;
8
8
import { registerCommands } from './register-commands' ;
9
9
10
- let client : LanguageClient ;
10
+ let languageClient : LanguageClient ;
11
11
12
12
const queue = new Queue ( { autostart : true , concurrency : 1 } ) ;
13
13
@@ -16,20 +16,19 @@ export async function activate(context: ExtensionContext) {
16
16
xoRootCache . logger = logger ;
17
17
18
18
logger . info ( `[client] Activating XO extension v${ pkg . version } ` ) ;
19
- logger . clear ( ) ;
20
19
21
- const shouldStartServer = await xoRootCache . get ( window . activeTextEditor ?. document . uri . fsPath ) ;
22
20
const xoConfig = workspace . getConfiguration ( 'xo' ) ;
23
21
const runtime = xoConfig . get < string > ( 'runtime' ) ;
24
- let languages = xoConfig . get < string [ ] > ( 'validate' ) ! ;
22
+ const languages = xoConfig . get < string [ ] > ( 'validate' ) ;
23
+ const hasValidXoRoot = await xoRootCache . get ( window . activeTextEditor ?. document . uri . fsPath ) ;
25
24
26
- client = await createLanguageClient ( { context, outputChannel : logger , runtime, languages} ) ;
25
+ languageClient = await createLanguageClient ( { context, outputChannel : logger , runtime, languages} ) ;
27
26
/**
28
27
* Update status bar on activation, and dispose of the status bar when the extension is deactivated
29
28
*/
30
29
const statusBar = await updateStatusBar ( window . activeTextEditor ?. document ) ;
31
30
32
- registerCommands ( { context, client, logger} ) ;
31
+ registerCommands ( { context, client : languageClient , logger} ) ;
33
32
34
33
context . subscriptions . push (
35
34
/**
@@ -38,6 +37,8 @@ export async function activate(context: ExtensionContext) {
38
37
workspace . onDidChangeConfiguration ( ( configChange : ConfigurationChangeEvent ) => {
39
38
queue . push ( async ( ) => {
40
39
try {
40
+ logger . debug ( '[client] Configuration change detected' ) ;
41
+
41
42
const isValidateChanged = configChange . affectsConfiguration ( 'xo.validate' ) ;
42
43
43
44
if ( isValidateChanged ) {
@@ -48,40 +49,29 @@ export async function activate(context: ExtensionContext) {
48
49
statusBar . text = '$(gear~spin)' ;
49
50
statusBar . show ( ) ;
50
51
51
- languages = workspace . getConfiguration ( 'xo' ) . get < string [ ] > ( 'validate' , languages ) ;
52
+ const languages = workspace . getConfiguration ( 'xo' ) . get < string [ ] > ( 'validate' ) ;
52
53
53
- client . clientOptions . documentSelector = [ ] ;
54
+ languageClient . clientOptions . documentSelector = [ ] ;
54
55
if ( languages && languages . length > 0 )
55
56
for ( const language of languages ) {
56
- ( client . clientOptions . documentSelector as DocumentSelector ) . push (
57
+ ( languageClient . clientOptions . documentSelector as DocumentSelector ) . push (
57
58
{ language, scheme : 'file' } ,
58
59
{ language, scheme : 'untitled' }
59
60
) ;
60
61
}
61
62
62
- await client . restart ( ) ;
63
+ await languageClient . restart ( ) ;
63
64
64
65
statusBar . text = '$(xo-logo)' ;
65
66
66
67
statusBar . hide ( ) ;
67
68
logger . info ( '[client] Restarted client with new xo.validate options.' ) ;
68
69
}
69
-
70
- const isEnabledChanged = configChange . affectsConfiguration ( 'xo.enable' ) ;
71
-
72
- if ( isEnabledChanged ) {
73
- const enabled = workspace . getConfiguration ( 'xo' ) . get < boolean > ( 'enable' , true ) ;
74
-
75
- if ( client . needsStart ( ) && enabled ) {
76
- await client . start ( ) ;
77
- }
78
-
79
- if ( client . needsStop ( ) && ! enabled ) {
80
- await client . dispose ( ) ;
81
- }
82
- }
83
70
} catch ( error ) {
84
- logger . error ( `[client] Restarting client failed` , error ) ;
71
+ if ( error instanceof Error ) {
72
+ logger . error ( `[client] There was a problem handling the configuration change.` ) ;
73
+ logger . error ( error ) ;
74
+ }
85
75
}
86
76
} ) ;
87
77
} ) ,
@@ -94,20 +84,33 @@ export async function activate(context: ExtensionContext) {
94
84
queue . push ( async ( ) => {
95
85
try {
96
86
const { document : textDocument } = textEditor ?? { } ;
97
- logger . debug ( '[client] Active text editor changed' , textDocument ?. uri . fsPath ) ;
87
+
88
+ logger . debug ( '[client] onDidChangeActiveTextEditor' , textDocument ?. uri . fsPath ) ;
89
+
90
+ const isEnabled = workspace . getConfiguration ( 'xo' ) . get < boolean > ( 'enable' , true ) ;
91
+
92
+ if ( ! isEnabled ) {
93
+ logger . debug ( '[client] onDidChangeActiveTextEditor > XO is not enabled' ) ;
94
+ return ;
95
+ }
96
+
98
97
await updateStatusBar ( textDocument ) ;
99
- // if the client was not started
98
+
100
99
if (
100
+ isEnabled &&
101
101
textDocument &&
102
- client . needsStart ( ) &&
102
+ languageClient . needsStart ( ) &&
103
103
( await xoRootCache . get ( textDocument . uri . fsPath ) )
104
104
) {
105
- logger . debug ( '[client] Starting LSP client ' ) ;
106
- await client . start ( ) ;
105
+ logger . debug ( '[client] Starting Language Client ' ) ;
106
+ await languageClient . start ( ) ;
107
107
}
108
108
} catch ( error ) {
109
- statusBar . text = '$(xo-logo)' ;
110
- logger . error ( `[client] There was a problem updating the statusbar` , error ) ;
109
+ if ( error instanceof Error ) {
110
+ statusBar . text = '$(xo-logo)' ;
111
+ logger . error ( `[client] There was a problem handling the active text editor change.` ) ;
112
+ logger . error ( error ) ;
113
+ }
111
114
}
112
115
} ) ;
113
116
} ) ,
@@ -120,22 +123,28 @@ export async function activate(context: ExtensionContext) {
120
123
xoRootCache . delete ( textDocument . uri . fsPath ) ;
121
124
} ) ;
122
125
} ) ,
126
+ /**
127
+ * Dispose of the status bar when the extension is deactivated
128
+ */
123
129
statusBar
124
130
) ;
125
131
126
- if ( shouldStartServer ) {
127
- logger . info ( '[client] XO was present in the workspace, server is now starting.' ) ;
128
- await client . start ( ) ;
129
- context . subscriptions . push ( client ) ;
130
- } else {
131
- logger . info ( '[client] XO was not present in the workspace, server will not be started.' ) ;
132
+ if ( hasValidXoRoot ) {
133
+ logger . info ( '[client] XO is enabled and is needed for linting file, server is now starting.' ) ;
134
+ await languageClient . start ( ) ;
135
+ context . subscriptions . push ( languageClient ) ;
136
+ return ;
137
+ }
138
+
139
+ if ( ! hasValidXoRoot ) {
140
+ logger . info ( '[client] XO is enabled and server will start when a relevant file is opened.' ) ;
132
141
}
133
142
}
134
143
135
144
export async function deactivate ( ) {
136
- if ( ! client ) {
145
+ if ( ! languageClient ) {
137
146
return undefined ;
138
147
}
139
148
140
- return client . stop ( ) ;
149
+ return languageClient . stop ( ) ;
141
150
}
0 commit comments