@@ -13,12 +13,16 @@ const outputChannelName = 'ast-grep'
1313const languageClientId = 'ast-grep-client'
1414const languageClientName = 'ast-grep language client'
1515
16- function getExecutable ( isDebug : boolean ) : Executable {
16+ function getExecutable (
17+ config : string | undefined ,
18+ isDebug : boolean ,
19+ ) : Executable {
1720 const uris = workspace . workspaceFolders ?. map ( i => i . uri ?. fsPath ) ?? [ ]
1821 const command = resolveBinary ( )
22+ const args = config ? [ 'lsp' , '-c' , config ] : [ 'lsp' ]
1923 return {
2024 command,
21- args : [ 'lsp' ] ,
25+ args,
2226 options : {
2327 env : {
2428 ...process . env ,
@@ -45,6 +49,26 @@ async function fileExists(pathFromRoot: string): Promise<boolean> {
4549 }
4650}
4751
52+ /** returns the path to the config file if found
53+ note: if the default sgconfig.yml is found, return ''
54+ returns undefined if no config file is found
55+ so you should not use Boolean to check the result
56+ */
57+ async function findConfigFile ( ) : Promise < string | undefined > {
58+ const userConfig = workspace . getConfiguration ( 'astGrep' ) . get ( 'configPath' , '' )
59+ if ( userConfig ) {
60+ if ( await fileExists ( userConfig ) ) {
61+ return userConfig
62+ }
63+ } else if (
64+ ( await fileExists ( 'sgconfig.yml' ) ) ||
65+ ( await fileExists ( 'sgconfig.yaml' ) )
66+ ) {
67+ return ''
68+ }
69+ return undefined
70+ }
71+
4872/**
4973 * Set up language server/client
5074 */
@@ -83,13 +107,10 @@ export async function activateLsp(context: ExtensionContext) {
83107 return
84108 }
85109
86- setupClient ( )
110+ const setupOkay = await setupClient ( )
87111
88112 // Automatically start the client only if we can find a config file
89- if (
90- ( await fileExists ( 'sgconfig.yml' ) ) ||
91- ( await fileExists ( 'sgconfig.yaml' ) )
92- ) {
113+ if ( setupOkay ) {
93114 // Start the client. This will also launch the server
94115 client . start ( )
95116 } else {
@@ -99,13 +120,14 @@ export async function activateLsp(context: ExtensionContext) {
99120 }
100121}
101122
102- function setupClient ( ) {
123+ async function setupClient ( ) {
124+ const configFile = await findConfigFile ( )
103125 // instantiate and set input which updates the view
104126 // If the extension is launched in debug mode then the debug server options are used
105127 // Otherwise the run options are used
106128 const serverOptions : ServerOptions = {
107- run : getExecutable ( false ) ,
108- debug : getExecutable ( true ) ,
129+ run : getExecutable ( configFile , false ) ,
130+ debug : getExecutable ( configFile , true ) ,
109131 }
110132
111133 // Options to control the language client
@@ -123,12 +145,13 @@ function setupClient() {
123145 serverOptions ,
124146 clientOptions ,
125147 )
148+ return configFile !== undefined
126149}
127150
128151async function restart ( ) : Promise < void > {
129152 await deactivate ( )
130153 if ( client ) {
131- setupClient ( )
154+ await setupClient ( )
132155 await client . start ( )
133156 }
134157}
0 commit comments