Skip to content
This repository has been archived by the owner on Apr 14, 2023. It is now read-only.

Commit

Permalink
Fix workspace variable handling (#23)
Browse files Browse the repository at this point in the history
* Support `${workspaceFolder}`

* Update README

* Fix syntax highlighting
  • Loading branch information
jdkato authored Sep 13, 2020
1 parent a827541 commit cdf713c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 52 deletions.
26 changes: 19 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
```
24 changes: 18 additions & 6 deletions src/features/vsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> = [
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<string> = [
binaryLocation,
"--no-exit",
Expand All @@ -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);
}

Expand Down Expand Up @@ -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) => {
Expand Down
78 changes: 39 additions & 39 deletions src/features/vsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string>("vale.valeCLI.path");

let customBinaryPath = configuration.get<string>("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<string>("vale.valeCLI.config");

// Assume that the binary is installed globally
return customConfigPath;
let customConfigPath = configuration.get<string>("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 "";
};

/**
Expand Down Expand Up @@ -227,40 +245,22 @@ export const postString = async (content: string, ext: string): Promise<string>
return response;
};

export const getStylesPath = async (usingCLI: boolean): Promise<string> => {
export const getStylesPath = async (): Promise<string> => {
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<string> = [
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;
}
};

0 comments on commit cdf713c

Please sign in to comment.