Skip to content
Open
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
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ The extension is very useful for giving rspec (or a different unit test runner)

<img width="1624" alt="Screenshot 2022-11-25 at 1 56 59 AM" src="https://user-images.githubusercontent.com/1789832/203859452-a7bdf842-93e0-438d-95fc-89da1b199608.png">

## Usage

- Press `Alt + l` (`Ctrl + l` on MacOS) to copy the current file's **full** path and current line number
- Press `Alt + Shift + l` (`Ctrl + Shift + l` on MacOS) to copy the current file's **relative** path and current line number


## Install

Visit https://marketplace.visualstudio.com/items?itemName=nisanthchunduru.copy-filepath-with-line-number and click the "Install" button

Press Alt + l (Ctrl + l on MacOS) to copy the current file's path and current line number

### Install from source

Expand Down
29 changes: 27 additions & 2 deletions main.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class NoTextEditorOpen extends Error {
class DocumentIsUntitled extends Error {
}

function copyCurrentFilePathWithCurrentLineNumber() {
function copyCurrentFilePathWithCurrentLineNumber(relative=false) {
if (!vscode.workspace.rootPath) {
throw new NoWorkspaceOpen;
}
Expand All @@ -26,7 +26,10 @@ function copyCurrentFilePathWithCurrentLineNumber() {
throw new DocumentIsUntitled;
}

const path = document.uri.path;
let path = document.uri.path;
if (relative) {
path = vscode.workspace.asRelativePath(path)
}
const lineNumber = editor.selection.active.line + 1;

return `${path}:${lineNumber}`;
Expand Down Expand Up @@ -69,7 +72,29 @@ function activate(context) {
}
);

let copyRelativeFilePathWithLineNumberCommand = vscode.commands.registerCommand(
"copy-relative-filepath-with-line-number",
() => {
let filePathWithLineNumber;
try {
filePathWithLineNumber = copyCurrentFilePathWithCurrentLineNumber(relative=true);
} catch (e) {
if (e instanceof NoWorkspaceOpen) {
} else if (e instanceof NoTextEditorOpen) {
} else if (e instanceof DocumentIsUntitled) {
} else {
throw e;
}
}

vscode.env.clipboard.writeText(filePathWithLineNumber).then(() => {
toast(`'${filePathWithLineNumber}' copied to clipboard`);
});
}
);

context.subscriptions.push(copyFilePathWithLineNumberCommand);
context.subscriptions.push(copyRelativeFilePathWithLineNumberCommand);
}
exports.activate = activate;

Expand Down
13 changes: 12 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"Other"
],
"activationEvents": [
"onCommand:copy-filepath-with-line-number"
"onCommand:copy-filepath-with-line-number",
"onCommand:copy-relative-filepath-with-line-number"
],
"main": "./main.js",
"contributes": {
Expand All @@ -32,12 +33,22 @@
"key": "alt+l",
"mac": "ctrl+l",
"when": "editorTextFocus"
},
{
"command": "copy-relative-filepath-with-line-number",
"key": "alt+shift+l",
"mac": "ctrl+shift+l",
"when": "editorTextFocus"
}
],
"commands": [
{
"command": "copy-filepath-with-line-number",
"title": "Copy current file full path with current line number"
},
{
"command": "copy-relative-filepath-with-line-number",
"title": "Copy current relative file path with current line number"
}
]
},
Expand Down