Skip to content

Commit

Permalink
feat: open terminal links for store paths in workspace
Browse files Browse the repository at this point in the history
  • Loading branch information
hall committed Sep 10, 2022
1 parent c19ceba commit de06d12
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
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

0 comments on commit de06d12

Please sign in to comment.