Skip to content

Commit 95c03b3

Browse files
authored
Merge pull request #13 from Moyf/support-goto-line
feat: Add supporting for "go to line"
2 parents 22fb8df + 5d52a39 commit 95c03b3

File tree

7 files changed

+29
-17
lines changed

7 files changed

+29
-17
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,12 @@ You can template the command opening VSCode however you like with its [provided
2929

3030
Note that on MacOS, a full path to the VSCode executable is required (generally "/usr/local/bin/code").
3131

32-
You can use the following variables: `{{vaultpath}}` (absolute), `{{filepath}}` (relative), `{{folderpath}}` (relative).
32+
You can use the following variables: `{{vaultpath}}` (absolute), `{{filepath}}` (relative), `{{folderpath}}` (relative), `{{line}}` and `{{ch}}`.
3333
The default template is `code "{{vaultpath}}" "{{vaultpath}}/{{filepath}}"`, which opens the current file (if there is one) in the workspace that is the vault's root folder. This gets expanded to be executed in your shell as `code "C:\Users\YourUser\Documents\vault" "C:\Users\YourUser\Documents\vault/Note.md"`, for example.
3434

35+
If you want to jump to the line (and character), you can use `code -g "{{vaultpath}}" "{{vaultpath}}/{{filepath}}:{{line}}:{{ch}}`.
36+
See also: [VSCode CLI - Opening Files and Folders](https://code.visualstudio.com/docs/editor/command-line#_opening-files-and-folders).
37+
3538
### Settings for `open-vscode-via-url`
3639

3740
On some systems, this may be faster than using the `child_process` approach.
@@ -85,6 +88,7 @@ If you like this plugin you can sponsor me here on GitHub: [![Sponsor NomarCub](
8588

8689
[Toggle ribbon setting](https://github.com/NomarCub/obsidian-open-vscode/pull/1) by [ozntel](https://github.com/ozntel).
8790
[UseURL: open file in workspace](https://github.com/NomarCub/obsidian-open-vscode/pull/5) [feature](https://github.com/NomarCub/obsidian-open-vscode/pull/7) and restructure by [ptim](https://github.com/ptim).
91+
[Go to line support](https://github.com/NomarCub/obsidian-open-vscode/pull/13) by [Moyf](https://github.com/Moyf).
8892

8993
Thank you to the makers of the [DEVONlink](https://github.com/ryanjamurphy/DEVONlink-obsidian) plugin, as it was a great starting point for working with ribbon icons in Obsidian.
9094
The icon is from [Simple Icons](https://simpleicons.org/?q=visual-studio-code) ([SVG](https://simpleicons.org/icons/visualstudiocode.svg)).

manifest.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"id": "open-vscode",
33
"name": "Open vault in VSCode",
4-
"version": "1.2.2",
5-
"minAppVersion": "1.4.11",
6-
"description": "Ribbon button and command to open vault as a Visual Studio Code workspace",
4+
"version": "1.2.3",
5+
"minAppVersion": "1.6.6",
6+
"description": "Ribbon button and command to open the vault as a Visual Studio Code (VSCode) workspace",
77
"author": "NomarCub",
88
"authorUrl": "https://github.com/NomarCub",
99
"fundingUrl": "https://ko-fi.com/nomarcub",

package-lock.json

Lines changed: 6 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "open-vscode",
3-
"version": "1.2.2",
3+
"version": "1.2.3",
44
"description": "Open vault in Visual Studio Code ribbon button and command for Obsidian (https://obsidian.md)",
55
"main": "main.js",
66
"scripts": {
@@ -19,7 +19,7 @@
1919
"builtin-modules": "4.0.0",
2020
"esbuild": "0.21.5",
2121
"eslint": "^8.57.0",
22-
"obsidian": "~1.5.7-1",
22+
"obsidian": "~1.6.6",
2323
"obsidian-typings": "^1.1.6",
2424
"tslib": "2.6.3",
2525
"typescript": "5.5.2"

src/main.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { FileSystemAdapter, Plugin, addIcon } from "obsidian";
1+
import { FileSystemAdapter, Plugin, addIcon, MarkdownView } from "obsidian";
22
// eslint-disable-next-line @typescript-eslint/no-unused-vars
3-
import * as internal from "obsidian-typings";
3+
import * as obsidianInternal from "obsidian-typings";
44
import { DEFAULT_SETTINGS, OpenVSCodeSettings, OpenVSCodeSettingsTab } from "./settings";
55
import { exec } from "child_process";
66

@@ -77,11 +77,18 @@ export default class OpenVSCode extends Plugin {
7777
const filePath = file?.path ?? "";
7878
const folderPath = file?.parent?.path ?? "";
7979

80+
const cursor = this.app.workspace.getActiveViewOfType(MarkdownView)?.editor.getCursor();
81+
// VSCode line and column are 1-based
82+
const line = (cursor?.line ?? 0) + 1;
83+
const ch = (cursor?.ch ?? 0) + 1;
84+
8085
let command = executeTemplate.trim() === "" ? DEFAULT_SETTINGS.executeTemplate : executeTemplate;
8186
command = command
8287
.replaceAll("{{vaultpath}}", path)
8388
.replaceAll("{{filepath}}", filePath)
84-
.replaceAll("{{folderpath}}", folderPath);
89+
.replaceAll("{{folderpath}}", folderPath)
90+
.replaceAll("{{line}}", line.toString())
91+
.replaceAll("{{ch}}", ch.toString());
8592
if (this.DEV) console.log("[openVSCode]", { command });
8693
exec(command, error => {
8794
if (error) {

src/settings.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export class OpenVSCodeSettingsTab extends PluginSettingTab {
5959

6060
new Setting(containerEl)
6161
.setName("Template for executing the `code` command")
62-
.setDesc('You can use the following variables: `{{vaultpath}}` (absolute), `{{filepath}}` (relative), `{{folderpath}}` (relative). Note that on MacOS, a full path to the VSCode executable is required (generally "/usr/local/bin/code"). Example: `/usr/local/bin/code "{{vaultpath}}" "{{vaultpath}}/{{filepath}}"`')
62+
.setDesc('You can use the following variables: `{{vaultpath}}` (absolute), `{{filepath}}` (relative), `{{folderpath}}` (relative), `{{line}}` and `{{ch}}`. Note that on MacOS, a full path to the VSCode executable is required (generally "/usr/local/bin/code"). Example: `/usr/local/bin/code "{{vaultpath}}" "{{vaultpath}}/{{filepath}}"`')
6363
.addText(text => text
6464
.setPlaceholder(DEFAULT_SETTINGS.executeTemplate)
6565
.setValue(this.plugin.settings.executeTemplate || DEFAULT_SETTINGS.executeTemplate)

versions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@
22
"1.0.0": "0.11.13",
33
"1.2.0": "0.14.8",
44
"1.2.1": "0.16.3",
5-
"1.2.2": "1.4.11"
5+
"1.2.2": "1.4.11",
6+
"1.2.3": "1.6.6"
67
}

0 commit comments

Comments
 (0)