Skip to content

Adds support for workspace built-in variables #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
35 changes: 30 additions & 5 deletions src/features/linter-provider.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand Down Expand Up @@ -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 }
Comment on lines +91 to +92
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like this option is not used at all. When running the linter the cwd passed is the one of the active file. This works well for single files but I can imagine that it does not when working on a project context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, I hadn't noticed I simply updated the rootPath option which was deprecated. I will remove options completely.

: undefined;
let args = [
"-fsyntax-only",
Expand Down Expand Up @@ -164,14 +165,38 @@ 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");
return config.get("gfortranExecutable", "gfortran");
}
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);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

External args passed by the user can be anything, we should not be overriding them. It can break people's workflow.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think we would be overriding something the user can define outside of VS Code, since settings.json does not inherit shell or build-system (cmake/autoconf) variables.
So when the extension calls getLinterExtraArgs() and the variable ${workspaceFolder} is encountered it can be 2 things:

  1. An internal VS Code variable or
  2. A gfortran flag.

However, gfortran does not declare arguments in this format, therefore, to the best of my knowledge ${} would be an illegal argument to pass to gfortran, and hence ${} can be uniquely interpreted as a VS Code internal variable.

Moreover, this is very similar to how the C/C++ extension works which allows for compilerArgs to make use of the predefined VS Code variables.

}

/**
* 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;
}
}