@@ -3,20 +3,37 @@ import { commands, ExtensionContext, window, workspace } from "vscode";
33import { OxcCommands } from "./commands" ;
44import { ConfigService } from "./ConfigService" ;
55import StatusBarItemHandler from "./StatusBarItemHandler" ;
6+ import Formatter from "./tools/formatter" ;
67import Linter from "./tools/linter" ;
8+ import ToolInterface from "./tools/ToolInterface" ;
79
810const outputChannelName = "Oxc" ;
9- const linter = new Linter ( ) ;
11+ const tools : ToolInterface [ ] = [ ] ;
12+
13+ if ( process . env . SKIP_LINTER_TEST !== "true" ) {
14+ tools . push ( new Linter ( ) ) ;
15+ }
16+ if ( process . env . SKIP_FORMATTER_TEST !== "true" ) {
17+ tools . push ( new Formatter ( ) ) ;
18+ }
1019
1120export async function activate ( context : ExtensionContext ) {
1221 const configService = new ConfigService ( ) ;
1322
14- const outputChannel = window . createOutputChannel ( outputChannelName , {
23+ const outputChannelLint = window . createOutputChannel ( outputChannelName + " (Lint)" , {
24+ log : true ,
25+ } ) ;
26+
27+ const outputChannelFormat = window . createOutputChannel ( outputChannelName + " (Fmt)" , {
1528 log : true ,
1629 } ) ;
1730
18- const showOutputCommand = commands . registerCommand ( OxcCommands . ShowOutputChannel , ( ) => {
19- outputChannel . show ( ) ;
31+ const showOutputLintCommand = commands . registerCommand ( OxcCommands . ShowOutputChannelLint , ( ) => {
32+ outputChannelLint . show ( ) ;
33+ } ) ;
34+
35+ const showOutputFmtCommand = commands . registerCommand ( OxcCommands . ShowOutputChannelFmt , ( ) => {
36+ outputChannelFormat . show ( ) ;
2037 } ) ;
2138
2239 const onDidChangeWorkspaceFoldersDispose = workspace . onDidChangeWorkspaceFolders (
@@ -33,35 +50,92 @@ export async function activate(context: ExtensionContext) {
3350 const statusBarItemHandler = new StatusBarItemHandler ( context . extension . packageJSON ?. version ) ;
3451
3552 context . subscriptions . push (
36- showOutputCommand ,
53+ showOutputLintCommand ,
54+ showOutputFmtCommand ,
3755 configService ,
38- outputChannel ,
56+ outputChannelLint ,
57+ outputChannelFormat ,
3958 onDidChangeWorkspaceFoldersDispose ,
4059 statusBarItemHandler ,
4160 ) ;
4261
4362 configService . onConfigChange = async function onConfigChange ( event ) {
44- await linter . onConfigChange ( event , configService , statusBarItemHandler ) ;
45- } ;
46- const binaryPath = await linter . getBinary ( context , outputChannel , configService ) ;
47-
48- // For the linter this should never happen, but just in case.
49- if ( ! binaryPath ) {
50- statusBarItemHandler . setColorAndIcon ( "statusBarItem.errorBackground" , "error" ) ;
51- statusBarItemHandler . updateToolTooltip (
52- "linter" ,
53- "Error: No valid oxc language server binary found." ,
63+ await Promise . all (
64+ tools . map ( ( tool ) => tool . onConfigChange ( event , configService , statusBarItemHandler ) ) ,
5465 ) ;
55- statusBarItemHandler . show ( ) ;
56- outputChannel . error ( "No valid oxc language server binary found." ) ;
57- return ;
66+ } ;
67+
68+ const binaryPaths = await Promise . all (
69+ tools . map ( ( tool ) =>
70+ tool . getBinary (
71+ context ,
72+ tool instanceof Linter ? outputChannelLint : outputChannelFormat ,
73+ configService ,
74+ ) ,
75+ ) ,
76+ ) ;
77+
78+ // remove this block, when `oxfmt` binary is always required. This will be a breaking change.
79+ if (
80+ binaryPaths . some ( ( path ) => path ?. includes ( "oxc_language_server" ) ) &&
81+ ! configService . vsCodeConfig . binPathOxfmt
82+ ) {
83+ configService . useOxcLanguageServerForFormatting = true ;
5884 }
5985
60- await linter . activate ( context , binaryPath , outputChannel , configService , statusBarItemHandler ) ;
61- // Show status bar item after activation
86+ await Promise . all (
87+ tools . map ( ( tool ) : Promise < void > => {
88+ const binaryPath = binaryPaths [ tools . indexOf ( tool ) ] ;
89+
90+ // For the linter this should never happen, but just in case.
91+ if ( ! binaryPath && tool instanceof Linter ) {
92+ statusBarItemHandler . setColorAndIcon ( "statusBarItem.errorBackground" , "error" ) ;
93+ statusBarItemHandler . updateToolTooltip (
94+ "linter" ,
95+ "**oxlint disabled**\n\nError: No valid oxc language server binary found." ,
96+ ) ;
97+ return Promise . resolve ( ) ;
98+ }
99+
100+ if ( tool instanceof Formatter ) {
101+ if ( configService . useOxcLanguageServerForFormatting ) {
102+ // The formatter is already handled by the linter tool in this case.
103+ statusBarItemHandler . updateToolTooltip (
104+ "formatter" ,
105+ "**oxfmt disabled**\n\noxc_language_server is used for formatting." ,
106+ ) ;
107+ outputChannelFormat . appendLine ( "oxc_language_server is used for formatting." ) ;
108+ return Promise . resolve ( ) ;
109+ } else if ( ! binaryPath ) {
110+ // No valid binary found for the formatter.
111+ statusBarItemHandler . updateToolTooltip (
112+ "formatter" ,
113+ "**oxfmt disabled**\n\nNo valid oxfmt binary found." ,
114+ ) ;
115+ outputChannelFormat . appendLine (
116+ "No valid oxfmt binary found. Formatter will not be activated." ,
117+ ) ;
118+ return Promise . resolve ( ) ;
119+ }
120+ }
121+
122+ // binaryPath is guaranteed to be defined here.
123+ const binaryPathResolved = binaryPath ! ;
124+
125+ return tool . activate (
126+ context ,
127+ binaryPathResolved ,
128+ tool instanceof Linter ? outputChannelLint : outputChannelFormat ,
129+ configService ,
130+ statusBarItemHandler ,
131+ ) ;
132+ } ) ,
133+ ) ;
134+
135+ // Finally show the status bar item.
62136 statusBarItemHandler . show ( ) ;
63137}
64138
65139export async function deactivate ( ) : Promise < void > {
66- await linter . deactivate ( ) ;
140+ await Promise . all ( tools . map ( ( tool ) => tool . deactivate ( ) ) ) ;
67141}
0 commit comments