Skip to content

Commit 3372767

Browse files
feat: improve error message in output
1 parent 9164cc6 commit 3372767

File tree

1 file changed

+26
-20
lines changed

1 file changed

+26
-20
lines changed

src/extension/lsp.ts

Lines changed: 26 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,24 +49,26 @@ async function fileExists(pathFromRoot: string): Promise<boolean> {
4949
}
5050
}
5151

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> {
52+
interface Found {
53+
found: boolean
54+
// empty path means default config file
55+
path: string
56+
}
57+
58+
/** returns the path to the config file if found */
59+
async function findConfigFile(): Promise<Found> {
5860
const userConfig = workspace.getConfiguration('astGrep').get('configPath', '')
61+
let found = false
5962
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 ''
63+
found = await fileExists(userConfig)
64+
} else {
65+
found =
66+
(await fileExists('sgconfig.yml')) || (await fileExists('sgconfig.yaml'))
67+
}
68+
return {
69+
found,
70+
path: userConfig || '',
6871
}
69-
return undefined
7072
}
7173

7274
/**
@@ -110,12 +112,16 @@ export async function activateLsp(context: ExtensionContext) {
110112
const setupOkay = await setupClient()
111113

112114
// Automatically start the client only if we can find a config file
113-
if (setupOkay) {
115+
if (setupOkay.found) {
114116
// Start the client. This will also launch the server
115117
client.start()
116118
} else {
119+
const path = setupOkay.path || 'sgconfig.yml'
120+
client.outputChannel.appendLine(
121+
`no project file "${path}" found in root. Skip starting LSP.`,
122+
)
117123
client.outputChannel.appendLine(
118-
'no project file sgconfig.yml found in root. Skip starting LSP.',
124+
'See LSP setup guide https://ast-grep.github.io/guide/tools/editors.html#vscode.',
119125
)
120126
}
121127
}
@@ -126,8 +132,8 @@ async function setupClient() {
126132
// If the extension is launched in debug mode then the debug server options are used
127133
// Otherwise the run options are used
128134
const serverOptions: ServerOptions = {
129-
run: getExecutable(configFile, false),
130-
debug: getExecutable(configFile, true),
135+
run: getExecutable(configFile.path, false),
136+
debug: getExecutable(configFile.path, true),
131137
}
132138

133139
// Options to control the language client
@@ -145,7 +151,7 @@ async function setupClient() {
145151
serverOptions,
146152
clientOptions,
147153
)
148-
return configFile !== undefined
154+
return configFile
149155
}
150156

151157
async function restart(): Promise<void> {

0 commit comments

Comments
 (0)