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

Commit

Permalink
Add support for --minAlertLevel (#25)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Chinchilla <[email protected]>
  • Loading branch information
jdkato and ChrisChinchilla authored Oct 20, 2020
1 parent 37e62a9 commit 3142231
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 40 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -107,3 +107,5 @@ The extension offers a number of settings and configuration options (_Preference
"vale.valeCLI.path": "/some/path/to/vale"
}
```

- `vale.valeCLI.minAlertLevel` (default: `inherited`): Defines from which level of errors and above to display in the problems output.
23 changes: 18 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@
"category": "Vale",
"when": "!config.vale.core.useCLI"
},

{
"command": "vale.addToAccept",
"category": "Vale",
Expand All @@ -97,7 +96,6 @@
"default": false,
"markdownDescription": "Use Vale's CLI instead of Vale Server. (**NOTE**: Some features, such as [Quick Fixes](https://github.com/errata-ai/vale-vscode/pull/4) and [Vocab Management](https://github.com/errata-ai/vale-vscode/pull/4), are only available when using Vale Server.)"
},

"vale.server.serverURL": {
"type": "string",
"default": "http://127.0.0.1:7777",
Expand All @@ -113,7 +111,6 @@
"default": 0,
"markdownDescription": "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": {
"scope": "resource",
"type": "string",
Expand All @@ -126,8 +123,24 @@
"default": null,
"markdownDescription": "Absolute path to the Vale binary. The predefined [`${workspaceFolder}`](https://code.visualstudio.com/docs/editor/variables-reference#_predefined-variables) variable can be used to reference a non-global binary. (**NOTE**: On Windows you can use '/' and can omit `.cmd` in the path value.)"
},


"vale.valeCLI.minAlertLevel": {
"scope": "resource",
"type": "string",
"default": "inherited",
"enum": [
"inherited",
"suggestion",
"warning",
"error"
],
"markdownEnumDescriptions": [
"Inherits the `minAlertLevel` from the active configuration file.",
"Sets `minAlertLevel` to `suggestion`, overriding any configuration files.",
"Sets `minAlertLevel` to `warning`, overriding any configuration files.",
"Sets `minAlertLevel` to `error`, overriding any configuration files."
],
"markdownDescription": "Defines from which level of errors and above to display in the problems output."
},
"vale-server.serverURL": {
"type": "string",
"default": "http://127.0.0.1:7777",
Expand Down
78 changes: 52 additions & 26 deletions src/features/vsProvider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

import * as path from 'path';
import * as fs from 'fs';
import * as request from 'request-promise-native';
import * as vscode from 'vscode';

Expand Down Expand Up @@ -56,35 +57,60 @@ export default class ValeServerProvider implements vscode.CodeActionProvider {
}

private async runVale(file: vscode.TextDocument) {
const folder = path.dirname(file.fileName);

const binaryLocation = utils.readBinaryLocation(file);
const configLocation = utils.readFileLocation(file);
const folder = path.dirname(file.fileName);
// There are two cases we need to handle here:
//
// (1) If we're given an explicit value for `--config`, then we should
// error if it doesn't exist.
//
// (2) If we're not given a value (the default is ""), then we need to look
// for a `.vale.ini`. However, we can't send an error if we don't find one
// because the user may simply be editing a non-Vale project/file.
let stylesPath: Array<string> = [];
if (configLocation !== "" && !fs.existsSync(configLocation)) {
vscode.window.showErrorMessage(
`There was an error running Vale: '${configLocation}' does not exist.`
);
} else if (configLocation !== "") {
stylesPath = [
binaryLocation,
"--no-exit",
"--config",
configLocation,
"ls-config"
];
} else {
stylesPath = [
binaryLocation,
"--no-exit",
"ls-config"
];
}

const configOut = await utils.runInWorkspace(folder, stylesPath);
try {
const configCLI = JSON.parse(configOut);

this.stylesPath = configCLI.StylesPath;
const command = utils.buildCommand(
binaryLocation,
configLocation,
file.fileName);

const stdout = await utils.runInWorkspace(folder, command);
this.handleJSON(stdout.toString(), file, 0);
} catch (error) {
// We can't find a configuration, but this might not be an error:
//
// TODO: in case (2), how do we unintrusively communicate that we
// couldn't find a config file?
console.log(`[Vale] runtime error: ${error}`);
}
}

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 = configCLI.StylesPath;
const command: ReadonlyArray<string> = [
binaryLocation,
"--no-exit",
"--config",
configLocation,
"--output",
"JSON",
file.fileName,
];

const stdout = await utils.runInWorkspace(folder, command);
this.handleJSON(stdout.toString(), file, 0);
}

private handleJSON(contents: string, doc: vscode.TextDocument, offset: number) {
const diagnostics: vscode.Diagnostic[] = [];
Expand Down
34 changes: 25 additions & 9 deletions src/features/vsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import * as request from 'request-promise-native';
import { execFile } from "child_process";

import * as vscode from 'vscode';
import { off } from 'process';

export const readBinaryLocation = (file: vscode.TextDocument) => {
const configuration = vscode.workspace.getConfiguration();
Expand Down Expand Up @@ -145,14 +144,7 @@ export const runInWorkspace = (
command.slice(1),
{ cwd, maxBuffer },
(error, stdout) => {
if (error) {
// Throw system errors, but do not fail if the command
// fails with a non-zero exit code.
console.error("Command error", command, error);
reject(error);
} else {
resolve(stdout);
}
resolve(stdout);
},
);
});
Expand Down Expand Up @@ -264,3 +256,27 @@ export const getStylesPath = async (): Promise<string> => {

return path;
};

export const buildCommand = (
exe: string,
config: string,
path: string
): Array<string> => {
const configuration = vscode.workspace.getConfiguration();

let command: Array<string> = [exe, "--no-exit"];
if (config !== "") {
command = command.concat(["--config", config]);
}

let minAlertLevel: string = configuration.get<string>(
"vale.valeCLI.minAlertLevel", "inherited");

if (minAlertLevel !== "inherited") {
command = command.concat(["--minAlertLevel", minAlertLevel]);
}

command = command.concat(["--output", "JSON", path]);
return command;
};

0 comments on commit 3142231

Please sign in to comment.