diff --git a/README.md b/README.md index 1d079ce..2bf9edc 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,6 @@ Visual Studio Code plugin that autocompletes filenames. - ## Installation In the command palette (cmd-shift-p) select Install Extension and choose Path Intellisense. @@ -43,7 +42,7 @@ Add this to the keybinding: ### BaseUrl -Pathintellisense uses the ts.config.compilerOptions.baseUrl as a mapping. So no need to define it twice. There is no support for paths at the moment. +Pathintellisense uses the ts.config.compilerOptions.baseUrl as a mapping. So no need to define it twice. For example: @@ -80,6 +79,24 @@ You can disable this behaviour by setting it to true: } ``` +### Paths + +Pathintellisense uses the ts.config.compilerOptions.paths as a mapping. + +For example: + +```json +{ + "compilerOptions": { + "paths": { + "@/*": ["src/*"] + } + } +} +``` + +Note: Fallbacks are not supported. + ## Settings ### File extension in import statements diff --git a/src/configuration/tsconfig.service.ts b/src/configuration/tsconfig.service.ts index d951e09..765acc6 100644 --- a/src/configuration/tsconfig.service.ts +++ b/src/configuration/tsconfig.service.ts @@ -131,7 +131,7 @@ async function findTsConfigFiles(workfolder: vscode.WorkspaceFolder) { async function createMappingsFromWorkspaceConfig( tsconfig: { - compilerOptions: { baseUrl: string, paths?: Record }; + compilerOptions: { baseUrl: string; paths?: Record }; }, workfolder: vscode.WorkspaceFolder, showHiddenFiles: boolean, @@ -142,14 +142,15 @@ async function createMappingsFromWorkspaceConfig( const paths = tsconfig?.compilerOptions?.paths; if (paths && baseUrl && workfolder) { - for (const alias in paths) { - const destinations = paths[alias]; - - destinations.forEach((dest) => { - mappings.push({ - key: alias.replace(/\*$/, ''), - value: '${workspaceFolder}/' + join(baseUrl, dest.replace(/\*$/, '')), - }); + for (const [key, value] of Object.entries(paths)) { + if (value.length === 0) { + continue; + } + + mappings.push({ + key: key.replace(/\*$/, ""), + value: + "${workspaceFolder}/" + join(baseUrl, value[0].replace(/\*$/, "")), }); } } diff --git a/src/test/demo-workspace/project-with-paths/src/index.js b/src/test/demo-workspace/project-with-paths/src/index.js index fb751f8..bc4bffd 100644 --- a/src/test/demo-workspace/project-with-paths/src/index.js +++ b/src/test/demo-workspace/project-with-paths/src/index.js @@ -1 +1 @@ -import { } from '@/foo/foo'; \ No newline at end of file +import { } from '@/'; \ No newline at end of file diff --git a/src/test/suite/ts-paths.test.ts b/src/test/suite/ts-paths.test.ts new file mode 100644 index 0000000..29c49a9 --- /dev/null +++ b/src/test/suite/ts-paths.test.ts @@ -0,0 +1,39 @@ +import * as assert from "assert"; +import * as vscode from "vscode"; +import { beforeEach, afterEach } from "mocha"; +import { getFileUri } from "../utils/open-document"; +import { setConfig, setDefaults } from "../utils/set-config"; + +suite("tsconfig paths", () => { + beforeEach(async () => { + await setDefaults(); + }); + + afterEach(async () => { + await setDefaults(); + }); + + suite("with baseUrl ./", () => { + const project = "demo-workspace/project-with-paths"; + const fileInFolder = `${project}/src/index.js`; + + test("Get correct file when not using baseUrl", async () => { + await setConfig("ignoreTsConfigBaseUrl", false); + const result = await triggerCompletion(fileInFolder, 0, 19); + assert.ok(hasItem(result, "foo")); + assert.ok(hasItem(result, "bar")); + }); + }); +}); + +async function triggerCompletion(uri: string, line: number, column: number) { + return (await vscode.commands.executeCommand( + "vscode.executeCompletionItemProvider", + getFileUri(uri), + new vscode.Position(line, column) + )) as vscode.CompletionList; +} + +function hasItem(list: vscode.CompletionList, label: string) { + return list.items.some((item) => item.label === label); +}