Skip to content

Commit

Permalink
feat: support config in extension
Browse files Browse the repository at this point in the history
fix #251
  • Loading branch information
HerringtonDarkholme committed Mar 24, 2024
1 parent 52fac30 commit 9164cc6
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 19 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,16 @@ ast-grep new

This extension contributes the following settings:

- `astGrep.serverPath`: Specify the language server binary path.
- `astGrep.serverPath`: Specify the language server binary path. It can be a relative path to workspace root or an absolute path.

- `astGrep.serverPath`: Customize ast-grep config file path relative. Default is `sgconfig.yml`

## Video Introduction

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

<div align="center">
<a href="https://www.youtube.com/watch?v=1ZM4RfIvWKc" target="_blank">
<img src="https://github.com/ast-grep/ast-grep-vscode/assets/2883231/62face2b-3ee4-4f70-b1e0-4a922471794d" alt="ast-grep VSCode introduction"/>
<img src="https://github.com/ast-grep/ast-grep-vscode/assets/2883231/62face2b-3ee4-4f70-b1e0-4a922471794d" alt="ast-grep VSCode introduction"/>
</a>
</div>
</div>
7 changes: 2 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,7 @@
},
"icon": "media/favicon.png",
"categories": ["Linters", "Programming Languages", "Other"],
"activationEvents": [
"onView:ast-grep-sidebar-view",
"workspaceContains:**/sgconfig.{yml,yaml}"
],
"activationEvents": ["onStartupFinished"],
"contributes": {
"commands": [
{
Expand Down Expand Up @@ -64,7 +61,7 @@
"astGrep.serverPath": {
"scope": "window",
"type": "string",
"description": "Specify the language server binary path."
"description": "Specify the language server binary path. It can be a relative path to workspace root or an absolute path."
},
"astGrep.configPath": {
"scope": "resource",
Expand Down
45 changes: 34 additions & 11 deletions src/extension/lsp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ const outputChannelName = 'ast-grep'
const languageClientId = 'ast-grep-client'
const languageClientName = 'ast-grep language client'

function getExecutable(isDebug: boolean): Executable {
function getExecutable(
config: string | undefined,
isDebug: boolean,
): Executable {
const uris = workspace.workspaceFolders?.map(i => i.uri?.fsPath) ?? []
const command = resolveBinary()
const args = config ? ['lsp', '-c', config] : ['lsp']
return {
command,
args: ['lsp'],
args,
options: {
env: {
...process.env,
Expand All @@ -45,6 +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> {
const userConfig = workspace.getConfiguration('astGrep').get('configPath', '')
if (userConfig) {
if (await fileExists(userConfig)) {
return userConfig
}
} else if (
(await fileExists('sgconfig.yml')) ||
(await fileExists('sgconfig.yaml'))
) {
return ''
}
return undefined
}

/**
* Set up language server/client
*/
Expand Down Expand Up @@ -83,13 +107,10 @@ export async function activateLsp(context: ExtensionContext) {
return
}

setupClient()
const setupOkay = await setupClient()

// Automatically start the client only if we can find a config file
if (
(await fileExists('sgconfig.yml')) ||
(await fileExists('sgconfig.yaml'))
) {
if (setupOkay) {
// Start the client. This will also launch the server
client.start()
} else {
Expand All @@ -99,13 +120,14 @@ export async function activateLsp(context: ExtensionContext) {
}
}

function setupClient() {
async function setupClient() {
const configFile = await findConfigFile()
// instantiate and set input which updates the view
// 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(false),
debug: getExecutable(true),
run: getExecutable(configFile, false),
debug: getExecutable(configFile, true),
}

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

async function restart(): Promise<void> {
await deactivate()
if (client) {
setupClient()
await setupClient()
await client.start()
}
}
Expand Down

0 comments on commit 9164cc6

Please sign in to comment.