Skip to content

Commit

Permalink
feat(mapping): add support for workspaceFolder placeholders
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianKohler committed Sep 11, 2020
1 parent 66ff396 commit b07960e
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,14 @@ Define custom mappings which can be useful for using absolute paths or in combin
```javascript
{
"path-intellisense.mappings": {
"/": "${workspaceRoot}",
"lib": "${workspaceRoot}/lib",
"/": "${workspaceFolder}",
"lib": "${workspaceFolder}/lib",
"global": "/Users/dummy/globalLibs"
},
}
```

Use \${workspaceRoot} when the path should be relative to the current root of the current project.
Use \${workspaceFolder} when the path should be relative to the current root of the current project. V2.2.1 and lower used \${workspaceRoot}. Newer version support both placeholders.

## History

Expand Down
4 changes: 2 additions & 2 deletions src/configuration/configuration.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as vscode from "vscode";
import { Config, Mapping } from "./configuration.interface";
import { getWorkfolderTsConfigConfiguration } from "./tsconfig.service";
import { parseMappings, replaceWorkspaceRoot } from "./mapping.service";
import { parseMappings, replaceWorkspaceFolder } from "./mapping.service";

export async function getConfiguration(
resource: vscode.Uri
Expand Down Expand Up @@ -32,5 +32,5 @@ async function getMappings(
const mappings = parseMappings(configuration["mappings"]);
const tsConfigMappings = await getWorkfolderTsConfigConfiguration(workfolder);
const allMappings = [...mappings, ...tsConfigMappings];
return replaceWorkspaceRoot(allMappings, workfolder);
return replaceWorkspaceFolder(allMappings, workfolder);
}
30 changes: 25 additions & 5 deletions src/configuration/mapping.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,40 @@ export function parseMappings(mappings: { [key: string]: string }): Mapping[] {
* @param mappings
* @param workfolder
*/
export function replaceWorkspaceRoot(
export function replaceWorkspaceFolder(
mappings: Mapping[],
workfolder?: vscode.WorkspaceFolder
): Mapping[] {
const rootPath = workfolder?.uri.path;

if (rootPath) {
/** Replace placeholder with workspace folder */
return mappings.map(({ key, value }) => ({
key,
value: value.replace("${workspaceRoot}", rootPath),
value: replaceWorkspaceFolder(value, rootPath),
}));
} else {
return mappings.filter(
({ value }) => value.indexOf("${workspaceRoot}") === -1
);
/** Filter items out which contain a workspace root */
return mappings.filter(({ value }) => !valueContainsWorkspaceFolder(value));
}
}

/**
* Replaces both placeholders with the rootpath
* - ${workspaceRoot} // old way and only legacy support
* - ${workspaceFolder} // new way
* @param value
* @param rootPath
*/
function replaceWorkspaceFolder(value: string, rootPath: string) {
return value
.replace("${workspaceRoot}", rootPath)
.replace("${workspaceFolder}", rootPath);
}

function valueContainsWorkspaceFolder(value: string): boolean {
return (
value.indexOf("${workspaceRoot}") >= 0 ||
value.indexOf("${workspaceFolder}") >= 0
);
}
2 changes: 1 addition & 1 deletion src/configuration/tsconfig.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function createMappingsFromWorkspaceConfig(tsconfig: {
mappings.push({
key: baseUrl,
// value: `${workfolder.uri.path}/${baseUrl}`
value: "${workspaceRoot}/" + baseUrl,
value: "${workspaceFolder}/" + baseUrl,
});
}

Expand Down
2 changes: 1 addition & 1 deletion src/test/demo-workspace/demo.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"path-intellisense.absolutePathToWorkspace": true,
"path-intellisense.extensionOnImport": true,
"path-intellisense.mappings": {
"lib": "${workspaceRoot}/lib"
"lib": "${workspaceFolder}/lib"
}
}
}

0 comments on commit b07960e

Please sign in to comment.