From cdf713cf66d775d4d3740cba2a6144a9bc40a26c Mon Sep 17 00:00:00 2001 From: Joseph Kato Date: Sun, 13 Sep 2020 13:13:56 -0700 Subject: [PATCH] Fix workspace variable handling (#23) * Support `${workspaceFolder}` * Update README * Fix syntax highlighting --- README.md | 26 +++++++++---- src/features/vsProvider.ts | 24 +++++++++--- src/features/vsUtils.ts | 78 +++++++++++++++++++------------------- 3 files changed, 76 insertions(+), 52 deletions(-) diff --git a/README.md b/README.md index 50e6d99..0b13092 100644 --- a/README.md +++ b/README.md @@ -80,18 +80,30 @@ The extension offers a number of settings and configuration options (_Preference - `vale.server.lintContext` (default: `0`): Only lint the *active* portion of a document (as determined by the cursor position), allowing for efficient on-the-fly linting of large documents. There are three supported values: `-1` (applies to all files), `0` (disabled), `n` (applies to any file with `lines >= n`). -- `vale.valeCLI.config` (default: `null`): Absolute path to a Vale configuration file. If not specified, the extension uses the default search process (relative to the current file). +- `vale.valeCLI.config` (default: `null`): Absolute path to a Vale configuration file. Use the predefined [`${workspaceFolder}`](https://code.visualstudio.com/docs/editor/variables-reference#_predefined-variables) variable to reference configuration file from a custom location. (**NOTE**: On Windows you can use '/' and can omit `.cmd` in the path value.) If not specified, the extension uses the default search process (relative to the current file). -- `vale.valeCLI.path` (default: `null`): Absolute path to the Vale binary. Use the predefined [`${workspaceFolder}`](https://code.visualstudio.com/docs/editor/variables-reference#_predefined-variables) variable to reference a non-global binary. (**NOTE**: On Windows you can use '/' and can omit `.cmd` in the path value.) + **Example** + + ```jsonc + { + // You can use ${workspaceFolder} it will be replaced by workspace folder path + "vale.valeCLI.config": "${workspaceFolder}/node_modules/some-package/.vale.ini" + + // or use some absolute path + "vale.valeCLI.config": "/some/path/to/.vale.ini" + } + ``` + +- `vale.valeCLI.path` (default: `null`): Absolute path to the Vale binary. Use the predefined [`${workspaceFolder}`](https://code.visualstudio.com/docs/editor/variables-reference#_predefined-variables) variable to reference a non-global binary. (**NOTE**: On Windows you can use '/' and can omit `.cmd` in the path value.) **Example** - ```js + ```jsonc { - // You can use ${workspaceFolder} it will be replaced by workspace folder path - "vscode-vale.path": "${workspaceFolder}/node_modules/.bin/vale" + // You can use ${workspaceFolder} it will be replaced by workspace folder path + "vale.valeCLI.path": "${workspaceFolder}/node_modules/.bin/vale" - // or use some absolute path - "vscode-vale.path": "/some/path/to/vale" + // or use some absolute path + "vale.valeCLI.path": "/some/path/to/vale" } ``` diff --git a/src/features/vsProvider.ts b/src/features/vsProvider.ts index e975315..6ac1aa1 100644 --- a/src/features/vsProvider.ts +++ b/src/features/vsProvider.ts @@ -56,10 +56,22 @@ export default class ValeServerProvider implements vscode.CodeActionProvider { } private async runVale(file: vscode.TextDocument) { - const binaryLocation = utils.readBinaryLocation(); - const configLocation = utils.readFileLocation()!; + const binaryLocation = utils.readBinaryLocation(file); + const configLocation = utils.readFileLocation(file); + const folder = path.dirname(file.fileName); + + const stylesPath: ReadonlyArray = [ + binaryLocation, + "--no-exit", + "--config", + configLocation, + "ls-config" + ]; + + const configOut = await utils.runInWorkspace(folder, stylesPath); + const configCLI = JSON.parse(configOut); - this.stylesPath = await utils.getStylesPath(true); + this.stylesPath = configCLI.StylesPath; const command: ReadonlyArray = [ binaryLocation, "--no-exit", @@ -70,9 +82,7 @@ export default class ValeServerProvider implements vscode.CodeActionProvider { file.fileName, ]; - const folder = path.dirname(file.fileName); const stdout = await utils.runInWorkspace(folder, command); - this.handleJSON(stdout.toString(), file, 0); } @@ -216,7 +226,9 @@ export default class ValeServerProvider implements vscode.CodeActionProvider { this.diagnosticCollection = vscode.languages.createDiagnosticCollection(); this.useCLI = configuration.get('vale.core.useCLI', false); - this.stylesPath = await utils.getStylesPath(this.useCLI); + if (!this.useCLI) { + this.stylesPath = await utils.getStylesPath(); + } vscode.workspace.onDidOpenTextDocument(this.doVale, this, subscriptions); vscode.workspace.onDidCloseTextDocument((textDocument) => { diff --git a/src/features/vsUtils.ts b/src/features/vsUtils.ts index c1de22a..a0a8f11 100644 --- a/src/features/vsUtils.ts +++ b/src/features/vsUtils.ts @@ -7,22 +7,40 @@ import { execFile } from "child_process"; import * as vscode from 'vscode'; import { off } from 'process'; -export const readBinaryLocation = () => { +export const readBinaryLocation = (file: vscode.TextDocument) => { const configuration = vscode.workspace.getConfiguration(); - const customBinaryPath = configuration.get("vale.valeCLI.path"); + + let customBinaryPath = configuration.get("vale.valeCLI.path"); if (customBinaryPath) { - return path.normalize(customBinaryPath); + customBinaryPath = path.normalize(customBinaryPath); + const workspaceFolder = vscode.workspace.getWorkspaceFolder(file.uri); + if (workspaceFolder) { + customBinaryPath = customBinaryPath.replace( + "${workspaceFolder}", + workspaceFolder.uri.fsPath, + ); + } + return customBinaryPath; } - // Assume that the binary is installed globally return which.sync("vale"); }; -export const readFileLocation = () => { +export const readFileLocation = (file: vscode.TextDocument) => { const configuration = vscode.workspace.getConfiguration(); - const customConfigPath = configuration.get("vale.valeCLI.config"); - // Assume that the binary is installed globally - return customConfigPath; + let customConfigPath = configuration.get("vale.valeCLI.config"); + if (customConfigPath) { + customConfigPath = path.normalize(customConfigPath); + const workspaceFolder = vscode.workspace.getWorkspaceFolder(file.uri); + if (workspaceFolder) { + customConfigPath = customConfigPath.replace( + "${workspaceFolder}", + workspaceFolder.uri.fsPath, + ); + } + return customConfigPath; + } + return ""; }; /** @@ -227,40 +245,22 @@ export const postString = async (content: string, ext: string): Promise return response; }; -export const getStylesPath = async (usingCLI: boolean): Promise => { +export const getStylesPath = async (): Promise => { const configuration = vscode.workspace.getConfiguration(); let path: string = ""; - if (!usingCLI) { - let server: string = configuration.get( - 'vale.server.serverURL', - 'http://localhost:7777' - ); + let server: string = configuration.get( + 'vale.server.serverURL', + 'http://localhost:7777' + ); - await request.get({ uri: server + '/path', json: true }) - .catch((error) => { - throw new Error(`Vale Server could not connect: ${error}.`); - }) - .then((body) => { - path = body.path; - }); - } else { - const binaryLocation = readBinaryLocation(); - const configLocation = readFileLocation()!; - - const stylesPath: ReadonlyArray = [ - binaryLocation, - "--no-exit", - "--config", - configLocation, - "ls-config" - ]; - - var configOut = await runInWorkspace(undefined, stylesPath); - const configCLI = JSON.parse(configOut); - - path = configCLI.StylesPath; - } + await request.get({ uri: server + '/path', json: true }) + .catch((error) => { + throw new Error(`Vale Server could not connect: ${error}.`); + }) + .then((body) => { + path = body.path; + }); return path; -} +};