Skip to content

Commit

Permalink
feat: improve error message in output
Browse files Browse the repository at this point in the history
  • Loading branch information
HerringtonDarkholme committed Mar 24, 2024
1 parent 9164cc6 commit 3372767
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions src/extension/lsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,24 +49,26 @@ async function fileExists(pathFromRoot: string): Promise<boolean> {
}
}

/** returns the path to the config file if found
note: if the default sgconfig.yml is found, return ''
returns undefined if no config file is found
so you should not use Boolean to check the result
*/
async function findConfigFile(): Promise<string | undefined> {
interface Found {
found: boolean
// empty path means default config file
path: string
}

/** returns the path to the config file if found */
async function findConfigFile(): Promise<Found> {
const userConfig = workspace.getConfiguration('astGrep').get('configPath', '')
let found = false
if (userConfig) {
if (await fileExists(userConfig)) {
return userConfig
}
} else if (
(await fileExists('sgconfig.yml')) ||
(await fileExists('sgconfig.yaml'))
) {
return ''
found = await fileExists(userConfig)
} else {
found =
(await fileExists('sgconfig.yml')) || (await fileExists('sgconfig.yaml'))
}
return {
found,
path: userConfig || '',
}
return undefined
}

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

// Automatically start the client only if we can find a config file
if (setupOkay) {
if (setupOkay.found) {
// Start the client. This will also launch the server
client.start()
} else {
const path = setupOkay.path || 'sgconfig.yml'
client.outputChannel.appendLine(
`no project file "${path}" found in root. Skip starting LSP.`,
)
client.outputChannel.appendLine(
'no project file sgconfig.yml found in root. Skip starting LSP.',
'See LSP setup guide https://ast-grep.github.io/guide/tools/editors.html#vscode.',
)
}
}
Expand All @@ -126,8 +132,8 @@ async function setupClient() {
// If the extension is launched in debug mode then the debug server options are used
// Otherwise the run options are used
const serverOptions: ServerOptions = {
run: getExecutable(configFile, false),
debug: getExecutable(configFile, true),
run: getExecutable(configFile.path, false),
debug: getExecutable(configFile.path, true),
}

// Options to control the language client
Expand All @@ -145,7 +151,7 @@ async function setupClient() {
serverOptions,
clientOptions,
)
return configFile !== undefined
return configFile
}

async function restart(): Promise<void> {
Expand Down

0 comments on commit 3372767

Please sign in to comment.