Skip to content
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ The following are tested so far:

* Snippets

* Terminal links to store paths open (if file exists) in workspace.

## Todos

**PRs welcome** for them
Expand Down
47 changes: 47 additions & 0 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@ import { startLinting } from "./linter";
import { config } from "./configuration";
import * as client from "./client";

const storePath = process.env["NIX_STORE_DIR"] || "/nix/store";
const storeRegex = new RegExp(`${storePath}/.{32}-[^/]*/([^:]*)`, "g");

interface StoreTerminalLink extends vscode.TerminalLink {
relPath: string;
absPath: string;
}

/**
* Activate this extension.
*
Expand All @@ -28,6 +36,45 @@ export async function activate(context: ExtensionContext): Promise<void> {
].map((func) => func("nix", formattingProviders));
context.subscriptions.concat(subs);
}

vscode.window.registerTerminalLinkProvider({
provideTerminalLinks: (
context: vscode.TerminalLinkContext,
token: vscode.CancellationToken
) => {
const matches = [...context.line.matchAll(storeRegex)];
if (matches.length === 0) {
return [];
}

return matches.map((match) => {
return {
startIndex: context.line.indexOf(match[0]),
length: match[0].length,
tooltip: `Open file in workspace ${match[1]}`,
absPath: match[0],
relPath: match[1],
};
});
},
handleTerminalLink: (link: StoreTerminalLink) => {
// get absolute path to workspace, if it exists
const root = vscode.workspace.workspaceFolders
? vscode.workspace.workspaceFolders[0].uri
: vscode.Uri.file("");
let uri = vscode.Uri.joinPath(root, link.relPath);

try {
// check if file exists
void vscode.workspace.fs.stat(uri);
} catch {
// if not, use store path
uri = vscode.Uri.file(link.absPath);
} finally {
void vscode.commands.executeCommand("vscode.open", uri);
}
},
});
}

export async function deactivate(): Promise<void> {
Expand Down