Skip to content

Commit 9164cc6

Browse files
feat: support config in extension
fix #251
1 parent 52fac30 commit 9164cc6

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,14 +83,16 @@ ast-grep new
8383

8484
This extension contributes the following settings:
8585

86-
- `astGrep.serverPath`: Specify the language server binary path.
86+
- `astGrep.serverPath`: Specify the language server binary path. It can be a relative path to workspace root or an absolute path.
87+
88+
- `astGrep.serverPath`: Customize ast-grep config file path relative. Default is `sgconfig.yml`
8789

8890
## Video Introduction
8991

9092
See the introduction on YouTube! Please give it a like~
9193

9294
<div align="center">
9395
<a href="https://www.youtube.com/watch?v=1ZM4RfIvWKc" target="_blank">
94-
<img src="https://github.com/ast-grep/ast-grep-vscode/assets/2883231/62face2b-3ee4-4f70-b1e0-4a922471794d" alt="ast-grep VSCode introduction"/>
96+
<img src="https://github.com/ast-grep/ast-grep-vscode/assets/2883231/62face2b-3ee4-4f70-b1e0-4a922471794d" alt="ast-grep VSCode introduction"/>
9597
</a>
96-
</div>
98+
</div>

package.json

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@
3232
},
3333
"icon": "media/favicon.png",
3434
"categories": ["Linters", "Programming Languages", "Other"],
35-
"activationEvents": [
36-
"onView:ast-grep-sidebar-view",
37-
"workspaceContains:**/sgconfig.{yml,yaml}"
38-
],
35+
"activationEvents": ["onStartupFinished"],
3936
"contributes": {
4037
"commands": [
4138
{
@@ -64,7 +61,7 @@
6461
"astGrep.serverPath": {
6562
"scope": "window",
6663
"type": "string",
67-
"description": "Specify the language server binary path."
64+
"description": "Specify the language server binary path. It can be a relative path to workspace root or an absolute path."
6865
},
6966
"astGrep.configPath": {
7067
"scope": "resource",

src/extension/lsp.ts

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,16 @@ const outputChannelName = 'ast-grep'
1313
const languageClientId = 'ast-grep-client'
1414
const 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

128151
async function restart(): Promise<void> {
129152
await deactivate()
130153
if (client) {
131-
setupClient()
154+
await setupClient()
132155
await client.start()
133156
}
134157
}

0 commit comments

Comments
 (0)