From 2182175b0657e4773fecf89c1ca277e9a99a5dc9 Mon Sep 17 00:00:00 2001 From: gnikit Date: Tue, 1 Jun 2021 18:13:21 +0100 Subject: [PATCH] Adds support for workspace built-in variables Now VSCode variables `${workspaceFolder} and `${workspaceRoot}` can be used in the `settings.json` file to set up include paths and output directories for the .mod linter files Fixes #176. --- CHANGELOG.md | 5 +++++ src/features/linter-provider.ts | 35 ++++++++++++++++++++++++++++----- 2 files changed, 35 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 403c291d..971c0509 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,11 @@ and this project adheres to [Semantic Versioning](http://semver.org/). ([#181](https://github.com/krvajal/vscode-fortran-support/issues/181)) via ([#218](https://github.com/krvajal/vscode-fortran-support/pull/218)) +### Added + +- Added capability to resolve `${workspaceFolder}` variable from settings + ([#176](https://github.com/krvajal/vscode-fortran-support/issues/176)) + ## [2.2.1] - 2020-04-11 ### Fixed diff --git a/src/features/linter-provider.ts b/src/features/linter-provider.ts index 479b5291..23de5017 100644 --- a/src/features/linter-provider.ts +++ b/src/features/linter-provider.ts @@ -1,5 +1,6 @@ "use strict"; +import * as os from "os"; import * as path from "path"; import * as cp from "child_process"; import ChildProcess = cp.ChildProcess; @@ -8,7 +9,7 @@ import { getIncludeParams, LANGUAGE_ID } from "../lib/helper"; import * as vscode from "vscode"; export default class FortranLintingProvider { - constructor() {} + constructor() { } private diagnosticCollection: vscode.DiagnosticCollection; @@ -87,8 +88,8 @@ export default class FortranLintingProvider { } private constructArgumentList(textDocument: vscode.TextDocument): string[] { - let options = vscode.workspace.rootPath - ? { cwd: vscode.workspace.rootPath } + let options = vscode.workspace.workspaceFolders + ? { cwd: vscode.workspace.workspaceFolders } : undefined; let args = [ "-fsyntax-only", @@ -164,7 +165,7 @@ export default class FortranLintingProvider { let config = vscode.workspace.getConfiguration("fortran"); let includePaths: string[] = config.get("includePaths", []); - return includePaths; + return includePaths.map(this.resolveVariables); } private getGfortranPath(): string { let config = vscode.workspace.getConfiguration("fortran"); @@ -172,6 +173,30 @@ export default class FortranLintingProvider { } private getLinterExtraArgs(): string[] { let config = vscode.workspace.getConfiguration("fortran"); - return config.get("linterExtraArgs", ["-Wall"]); + let args = config.get("linterExtraArgs", ["-Wall"]); + + return args.map(this.resolveVariables); + } + + /** + * Resolve the absolute paths of built-in vscode variables + * `${workspaceFolder}`, `${workspaceRoot}` and `~` + */ + private resolveVariables(input: string): string { + if (!input) { + return ""; + } + + // Get the top-most workspace folder + const folder: vscode.WorkspaceFolder | undefined = vscode.workspace.workspaceFolders[0]; + let ret: string = input; + // Replace workspaceFolder and workspaceRoot with absolute workspace path + ret = ret.replace(/(\${workspaceFolder}|\${workspaceRoot})/g, folder.uri.fsPath); + + // Resolve '~' at the start of the path. + let regexp = () => /^\~/g; + ret = ret.replace(regexp(), (match: string, name: string) => os.homedir()); + + return ret; } }