Skip to content

Commit

Permalink
Merge pull request #13 from Moyf/support-goto-line
Browse files Browse the repository at this point in the history
feat: Add supporting for "go to line"
  • Loading branch information
NomarCub authored Jul 29, 2024
2 parents 22fb8df + 5d52a39 commit 95c03b3
Show file tree
Hide file tree
Showing 7 changed files with 29 additions and 17 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ You can template the command opening VSCode however you like with its [provided

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

You can use the following variables: `{{vaultpath}}` (absolute), `{{filepath}}` (relative), `{{folderpath}}` (relative).
You can use the following variables: `{{vaultpath}}` (absolute), `{{filepath}}` (relative), `{{folderpath}}` (relative), `{{line}}` and `{{ch}}`.
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.

If you want to jump to the line (and character), you can use `code -g "{{vaultpath}}" "{{vaultpath}}/{{filepath}}:{{line}}:{{ch}}`.
See also: [VSCode CLI - Opening Files and Folders](https://code.visualstudio.com/docs/editor/command-line#_opening-files-and-folders).

### Settings for `open-vscode-via-url`

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

[Toggle ribbon setting](https://github.com/NomarCub/obsidian-open-vscode/pull/1) by [ozntel](https://github.com/ozntel).
[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).
[Go to line support](https://github.com/NomarCub/obsidian-open-vscode/pull/13) by [Moyf](https://github.com/Moyf).

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.
The icon is from [Simple Icons](https://simpleicons.org/?q=visual-studio-code) ([SVG](https://simpleicons.org/icons/visualstudiocode.svg)).
6 changes: 3 additions & 3 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"id": "open-vscode",
"name": "Open vault in VSCode",
"version": "1.2.2",
"minAppVersion": "1.4.11",
"description": "Ribbon button and command to open vault as a Visual Studio Code workspace",
"version": "1.2.3",
"minAppVersion": "1.6.6",
"description": "Ribbon button and command to open the vault as a Visual Studio Code (VSCode) workspace",
"author": "NomarCub",
"authorUrl": "https://github.com/NomarCub",
"fundingUrl": "https://ko-fi.com/nomarcub",
Expand Down
12 changes: 6 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "open-vscode",
"version": "1.2.2",
"version": "1.2.3",
"description": "Open vault in Visual Studio Code ribbon button and command for Obsidian (https://obsidian.md)",
"main": "main.js",
"scripts": {
Expand All @@ -19,7 +19,7 @@
"builtin-modules": "4.0.0",
"esbuild": "0.21.5",
"eslint": "^8.57.0",
"obsidian": "~1.5.7-1",
"obsidian": "~1.6.6",
"obsidian-typings": "^1.1.6",
"tslib": "2.6.3",
"typescript": "5.5.2"
Expand Down
13 changes: 10 additions & 3 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { FileSystemAdapter, Plugin, addIcon } from "obsidian";
import { FileSystemAdapter, Plugin, addIcon, MarkdownView } from "obsidian";
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import * as internal from "obsidian-typings";
import * as obsidianInternal from "obsidian-typings";
import { DEFAULT_SETTINGS, OpenVSCodeSettings, OpenVSCodeSettingsTab } from "./settings";
import { exec } from "child_process";

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

const cursor = this.app.workspace.getActiveViewOfType(MarkdownView)?.editor.getCursor();
// VSCode line and column are 1-based
const line = (cursor?.line ?? 0) + 1;
const ch = (cursor?.ch ?? 0) + 1;

let command = executeTemplate.trim() === "" ? DEFAULT_SETTINGS.executeTemplate : executeTemplate;
command = command
.replaceAll("{{vaultpath}}", path)
.replaceAll("{{filepath}}", filePath)
.replaceAll("{{folderpath}}", folderPath);
.replaceAll("{{folderpath}}", folderPath)
.replaceAll("{{line}}", line.toString())
.replaceAll("{{ch}}", ch.toString());
if (this.DEV) console.log("[openVSCode]", { command });
exec(command, error => {
if (error) {
Expand Down
2 changes: 1 addition & 1 deletion src/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export class OpenVSCodeSettingsTab extends PluginSettingTab {

new Setting(containerEl)
.setName("Template for executing the `code` command")
.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}}"`')
.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}}"`')
.addText(text => text
.setPlaceholder(DEFAULT_SETTINGS.executeTemplate)
.setValue(this.plugin.settings.executeTemplate || DEFAULT_SETTINGS.executeTemplate)
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"1.0.0": "0.11.13",
"1.2.0": "0.14.8",
"1.2.1": "0.16.3",
"1.2.2": "1.4.11"
"1.2.2": "1.4.11",
"1.2.3": "1.6.6"
}

0 comments on commit 95c03b3

Please sign in to comment.