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

Commit

Permalink
Merge pull request #44 from jvilk-stripe/jvilk/fix-out-of-workspace-f…
Browse files Browse the repository at this point in the history
…iles

Do not run Vale on out-of-workspace files when ${workspaceFolder} is used in settings.
  • Loading branch information
ChrisChinchilla authored Jan 5, 2021
2 parents 78cd80f + e77be87 commit f568f57
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
9 changes: 7 additions & 2 deletions src/features/vsProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,13 @@ 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 binaryLocation = utils.readBinaryLocation(this.logger, file);
const configLocation = utils.readFileLocation(this.logger, file);
if (binaryLocation == null || configLocation == null) {
// `file` is not part of the workspace, so we could not resolve a workspace-relative path. Ignore this file.
return;
}

// There are two cases we need to handle here:
//
// (1) If we're given an explicit value for `--config`, then we should
Expand Down
39 changes: 19 additions & 20 deletions src/features/vsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,37 @@ import { execFile } from "child_process";

import * as vscode from "vscode";

export const readBinaryLocation = (file: vscode.TextDocument) => {
// If `customPath` contains `${workspaceFolder}`, replaces it with the workspace that `file` comes from.
// Return `null` if `customPath` contains `${workspaceFolder}` and `file` is _not_ part of the workspace.
function replaceWorkspaceFolder(logger: vscode.OutputChannel, customPath: string, file: vscode.TextDocument): string | null {
customPath = path.normalize(customPath);
const workspaceFolder = vscode.workspace.getWorkspaceFolder(file.uri);
if (workspaceFolder) {
return customPath.replace(
"${workspaceFolder}",
workspaceFolder.uri.fsPath
);
}
logger.appendLine(`Not running Vale on file '${file.uri}' as it is not contained within the workspace`);
return null;
}

export const readBinaryLocation = (logger: vscode.OutputChannel, file: vscode.TextDocument): string | null => {
const configuration = vscode.workspace.getConfiguration();

let customBinaryPath = configuration.get<string>("vale.valeCLI.path");
if (customBinaryPath) {
customBinaryPath = path.normalize(customBinaryPath);
const workspaceFolder = vscode.workspace.getWorkspaceFolder(file.uri);
if (workspaceFolder) {
customBinaryPath = customBinaryPath.replace(
"${workspaceFolder}",
workspaceFolder.uri.fsPath
);
}
return customBinaryPath;
return replaceWorkspaceFolder(logger, customBinaryPath, file);
}
return which.sync("vale");
};

export const readFileLocation = (file: vscode.TextDocument) => {
export const readFileLocation = (logger: vscode.OutputChannel, file: vscode.TextDocument): string | null => {
const configuration = vscode.workspace.getConfiguration();

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 replaceWorkspaceFolder(logger, customConfigPath, file);
}
return "";
};
Expand Down

0 comments on commit f568f57

Please sign in to comment.