Skip to content

Commit

Permalink
Merge pull request #8 from yuhldr/main
Browse files Browse the repository at this point in the history
fix: nautilus decode URI and support vscode-remote://ssh-remote
  • Loading branch information
ZanzyTHEbar authored Feb 18, 2025
2 parents 9188e7d + 929b278 commit 6c28a4c
Showing 1 changed file with 41 additions and 4 deletions.
45 changes: 41 additions & 4 deletions vscode_nautilus_workspaces.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from gi.repository import Nautilus, GObject
from gi.repository import Nautilus, GObject, GLib
import os
import logging
from pathlib import Path
from urllib.parse import unquote
from subprocess import call
import json

Expand Down Expand Up @@ -37,6 +37,8 @@ def launch_vscode(self, menu, files):
args = ""

for file in files:
if file.startswith("file://"):
file = file.replace("file://", "")
safepaths += '"' + file + '" '

logging.info(f"File path: {safepaths}")
Expand All @@ -48,6 +50,10 @@ def launch_vscode(self, menu, files):
args = "--new-window "

args = "--new-window " if NEWWINDOW else ""

if len(files) == 1 and files[0].startswith("vscode-remote://"):
args = "--folder-uri"

command = f"{VSCODE} {args} {safepaths} &"
logging.info(f"Command to execute: {command}")

Expand All @@ -69,14 +75,43 @@ def _get_recent_workspaces(self):

# Extract workspaces from profileAssociations
workspaces = storage_data.get("profileAssociations", {}).get("workspaces", {})
workspace_paths = [ws.replace("file://", "") for ws in workspaces.keys()]
workspace_paths = []
for ws in workspaces.keys():
ws = unquote(ws)
if ws.startswith("file://"):
if os.path.exists(ws.replace("file://", "")):
workspace_paths.append(ws)
else:
workspace_paths.append(ws)
workspace_paths = workspace_paths[::-1]
logging.info(f"Workspace paths: {workspace_paths}")
return workspace_paths

def _open_workspace(self, menu, workspace_path):
logging.debug(f"Opening workspace: {workspace_path}")
self.launch_vscode(menu, [workspace_path])

def _get_name(self, workspace):
if workspace.startswith("file://"):
return workspace.replace("file://", "").replace(GLib.get_home_dir(), "~")

if workspace.startswith("vscode-remote://"):
workspace_name = workspace.replace("vscode-remote://", "")
if workspace_name.startswith("ssh-remote+"):
workspace_name = workspace_name.replace("ssh-remote+", "")
if "/" not in workspace_name:
return None
wns = workspace_name.split("/")
if len(wns) < 2:
return None
ssh_host = wns[0]
workspace_name = workspace_name.replace(ssh_host, "")
if len(wns) >= 4:
workspace_name = "~/" + "/".join(wns[3:])
return f"[SHH: {ssh_host}] {workspace_name}"

return workspace

def get_background_items(self, window):
recent_workspaces = self._get_recent_workspaces()
logging.debug(f"Recent workspaces: {recent_workspaces}")
Expand All @@ -94,7 +129,9 @@ def get_background_items(self, window):
menu_item.set_submenu(submenu)

for workspace in recent_workspaces:
workspace_name = os.path.basename(workspace)
workspace_name = self._get_name(workspace)
if workspace_name is None:
continue
item = Nautilus.MenuItem(
name=f"VSCodeWorkspacesExtension::Open_{workspace_name}",
label=workspace_name,
Expand Down

0 comments on commit 6c28a4c

Please sign in to comment.