diff --git a/src/config.ts b/src/config.ts new file mode 100644 index 0000000..e0c5cd0 --- /dev/null +++ b/src/config.ts @@ -0,0 +1,39 @@ +import * as path from 'path'; +import * as vscode from 'vscode'; +import * as cp from 'child_process'; + +const DEFAULT_PROTO_LINT_PATH = 'protolint'; + +let config: { protoLintPath?: string } = { + protoLintPath: undefined +}; + +export function reloadConfig(): boolean { + config.protoLintPath = resolveProtoLintPath(); + + // Verify that protolint can be successfully executed on the host machine by running the version command. + // In the event the binary cannot be executed, tell the user where to download protolint from. + const result = cp.spawnSync(config.protoLintPath, ['version']); + if (result.status !== 0) { + vscode.window.showErrorMessage("protolint was not detected using path `" + config.protoLintPath + "`. Download from: https://github.com/yoheimuta/protolint"); + return false; + } + + return true; +} + +export function getConfig() { + return config; +} + +function resolveProtoLintPath(): string { + let p = vscode.workspace.getConfiguration('protolint').get('path'); + + if (!p || p === DEFAULT_PROTO_LINT_PATH) { + return DEFAULT_PROTO_LINT_PATH; + } else if (!path.isAbsolute(p) && vscode.workspace.workspaceFolders) { + return path.join(vscode.workspace.workspaceFolders[0].uri.fsPath, p); + } else { + return p; + } +} diff --git a/src/extension.ts b/src/extension.ts index b90abf0..2d28d76 100644 --- a/src/extension.ts +++ b/src/extension.ts @@ -1,6 +1,6 @@ import * as vscode from 'vscode'; -import * as cp from 'child_process'; import Linter, { LinterError } from './linter'; +import { reloadConfig } from './config'; const diagnosticCollection = vscode.languages.createDiagnosticCollection("protolint"); @@ -17,16 +17,13 @@ export function activate(context: vscode.ExtensionContext) { vscode.commands.executeCommand('protolint.lint'); }); - // Verify that protolint can be successfully executed on the host machine by running the version command. - // In the event the binary cannot be executed, tell the user where to download protolint from. - let protoLintPath = vscode.workspace.getConfiguration('protolint').get('path'); - if (!protoLintPath) { - protoLintPath = "protolint" - } + vscode.workspace.onDidChangeConfiguration((e: vscode.ConfigurationChangeEvent) => { + if (e.affectsConfiguration('protolint')) { + reloadConfig(); + } + }); - const result = cp.spawnSync(protoLintPath, ['version']); - if (result.status !== 0) { - vscode.window.showErrorMessage("protolint was not detected using path `" + protoLintPath + "`. Download from: https://github.com/yoheimuta/protolint"); + if (!reloadConfig()) { return; } } @@ -34,7 +31,7 @@ export function activate(context: vscode.ExtensionContext) { function runLint() { let editor = vscode.window.activeTextEditor; if (!editor) { - return; + return; } // We only want to run protolint on documents that are known to be diff --git a/src/linter.ts b/src/linter.ts index 35a2c51..1b15abb 100644 --- a/src/linter.ts +++ b/src/linter.ts @@ -3,6 +3,7 @@ import * as vscode from 'vscode'; import * as util from 'util'; import * as path from 'path'; +import { getConfig } from './config'; import { ProtoError, parseProtoError } from './protoError'; export interface LinterError { @@ -44,11 +45,7 @@ export default class Linter { let currentFile = this.codeDocument.uri.fsPath; let currentDirectory = path.dirname(currentFile); - let protoLintPath = vscode.workspace.getConfiguration('protolint').get('path'); - if (!protoLintPath) { - protoLintPath = "protolint" - } - + let { protoLintPath } = getConfig(); const cmd = `${protoLintPath} lint "${currentFile}"`; // Execute the protolint binary and store the output from standard error.