@@ -22,6 +22,7 @@ import { Executable, LanguageClient, LanguageClientOptions, ServerOptions } from
2222
2323import { join } from 'node:path' ;
2424import { ConfigService } from './ConfigService' ;
25+ import { VSCodeConfig } from './VSCodeConfig' ;
2526
2627const languageClientName = 'oxc' ;
2728const outputChannelName = 'Oxc' ;
@@ -42,8 +43,16 @@ let client: LanguageClient | undefined;
4243
4344let myStatusBarItem : StatusBarItem ;
4445
46+ // Global flag to check if the user allows us to start the server.
47+ // When `oxc.requireConfig` is `true`, make sure one `.oxlintrc.json` file is present.
48+ let allowedToStartServer : boolean ;
49+
4550export async function activate ( context : ExtensionContext ) {
4651 const configService = new ConfigService ( ) ;
52+ allowedToStartServer = configService . vsCodeConfig . requireConfig
53+ ? ( await workspace . findFiles ( `**/.oxlintrc.json` , '**/node_modules/**' , 1 ) ) . length > 0
54+ : true ;
55+
4756 const restartCommand = commands . registerCommand (
4857 OxcCommands . RestartServer ,
4958 async ( ) => {
@@ -77,7 +86,7 @@ export async function activate(context: ExtensionContext) {
7786 async ( ) => {
7887 await configService . vsCodeConfig . updateEnable ( ! configService . vsCodeConfig . enable ) ;
7988
80- if ( client === undefined ) {
89+ if ( client === undefined || ! allowedToStartServer ) {
8190 return ;
8291 }
8392
@@ -245,7 +254,7 @@ export async function activate(context: ExtensionContext) {
245254 context . subscriptions . push ( onDidChangeWorkspaceFoldersDispose ) ;
246255
247256 configService . onConfigChange = async function onConfigChange ( event ) {
248- updateStatsBar ( this . vsCodeConfig . enable ) ;
257+ updateStatsBar ( context , this . vsCodeConfig . enable ) ;
249258
250259 if ( client === undefined ) {
251260 return ;
@@ -264,29 +273,13 @@ export async function activate(context: ExtensionContext) {
264273 }
265274 } ;
266275
267- function updateStatsBar ( enable : boolean ) {
268- if ( ! myStatusBarItem ) {
269- myStatusBarItem = window . createStatusBarItem (
270- StatusBarAlignment . Right ,
271- 100 ,
272- ) ;
273- myStatusBarItem . command = OxcCommands . ToggleEnable ;
274- context . subscriptions . push ( myStatusBarItem ) ;
275- myStatusBarItem . show ( ) ;
276+ updateStatsBar ( context , configService . vsCodeConfig . enable ) ;
277+ if ( allowedToStartServer ) {
278+ if ( configService . vsCodeConfig . enable ) {
279+ await client . start ( ) ;
276280 }
277- let bgColor = new ThemeColor (
278- enable
279- ? 'statusBarItem.activeBackground'
280- : 'statusBarItem.warningBackground' ,
281- ) ;
282- myStatusBarItem . text = `oxc: ${ enable ? '$(check-all)' : '$(check)' } ` ;
283-
284- myStatusBarItem . backgroundColor = bgColor ;
285- }
286-
287- updateStatsBar ( configService . vsCodeConfig . enable ) ;
288- if ( configService . vsCodeConfig . enable ) {
289- await client . start ( ) ;
281+ } else {
282+ generateActivatorByConfig ( configService . vsCodeConfig , context ) ;
290283 }
291284}
292285
@@ -297,3 +290,47 @@ export async function deactivate(): Promise<void> {
297290 await client . stop ( ) ;
298291 client = undefined ;
299292}
293+
294+ function updateStatsBar (
295+ context : ExtensionContext ,
296+ enable : boolean ,
297+ ) {
298+ if ( ! myStatusBarItem ) {
299+ myStatusBarItem = window . createStatusBarItem (
300+ StatusBarAlignment . Right ,
301+ 100 ,
302+ ) ;
303+ myStatusBarItem . command = OxcCommands . ToggleEnable ;
304+ context . subscriptions . push ( myStatusBarItem ) ;
305+ myStatusBarItem . show ( ) ;
306+ }
307+ let bgColor : string ;
308+ let icon : string ;
309+ if ( ! allowedToStartServer ) {
310+ bgColor = 'statusBarItem.offlineBackground' ;
311+ icon = '$(circle-slash)' ;
312+ } else if ( ! enable ) {
313+ bgColor = 'statusBarItem.warningBackground' ;
314+ icon = '$(check)' ;
315+ } else {
316+ bgColor = 'statusBarItem.activeBackground' ;
317+ icon = '$(check-all)' ;
318+ }
319+
320+ myStatusBarItem . text = `oxc: ${ icon } ` ;
321+ myStatusBarItem . backgroundColor = new ThemeColor ( bgColor ) ;
322+ }
323+
324+ function generateActivatorByConfig ( config : VSCodeConfig , context : ExtensionContext ) : void {
325+ const watcher = workspace . createFileSystemWatcher ( '**/.oxlintrc.json' , false , true , true ) ;
326+ watcher . onDidCreate ( async ( ) => {
327+ watcher . dispose ( ) ;
328+ allowedToStartServer = true ;
329+ updateStatsBar ( context , config . enable ) ;
330+ if ( client && ! client . isRunning ( ) && config . enable ) {
331+ await client . start ( ) ;
332+ }
333+ } ) ;
334+
335+ context . subscriptions . push ( watcher ) ;
336+ }
0 commit comments