Skip to content

Commit

Permalink
finish path support
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianKohler committed Nov 29, 2024
1 parent 06c4593 commit 6a7f1b2
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
21 changes: 19 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:

Expand Down Expand Up @@ -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
Expand Down
19 changes: 10 additions & 9 deletions src/configuration/tsconfig.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ async function findTsConfigFiles(workfolder: vscode.WorkspaceFolder) {

async function createMappingsFromWorkspaceConfig(
tsconfig: {
compilerOptions: { baseUrl: string, paths?: Record<string, string[]> };
compilerOptions: { baseUrl: string; paths?: Record<string, string[]> };
},
workfolder: vscode.WorkspaceFolder,
showHiddenFiles: boolean,
Expand All @@ -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(/\*$/, "")),
});
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/test/demo-workspace/project-with-paths/src/index.js
Original file line number Diff line number Diff line change
@@ -1 +1 @@
import { } from '@/foo/foo';
import { } from '@/';
39 changes: 39 additions & 0 deletions src/test/suite/ts-paths.test.ts
Original file line number Diff line number Diff line change
@@ -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);
}

0 comments on commit 6a7f1b2

Please sign in to comment.