-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathcode-nautilus.py
More file actions
85 lines (67 loc) · 2.57 KB
/
code-nautilus.py
File metadata and controls
85 lines (67 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# VSCode Nautilus Extension
#
# Place me in ~/.local/share/nautilus-python/extensions/,
# ensure you have python-nautilus package, restart Nautilus, and enjoy :)
#
# This script is released to the public domain.
from gi.repository import Nautilus, GObject
from subprocess import call
import os
# path to vscode
FLATPAK_VSCODE_PATH = "/var/lib/flatpak/exports/bin/com.visualstudio.code"
VSCODE_PATH = "/usr/bin/code"
# path to vscodium
FLATPAK_VSCODIUM_PATH = "/var/lib/flatpak/exports/bin/com.vscodium.codium"
VSCODIUM_PATH = "/usr/bin/codium"
# VSCODENAME: what name do you want to see in the context menu?
if os.path.exists(FLATPAK_VSCODE_PATH):
VSCODE = "flatpak run com.visualstudio.code"
VSCODENAME = 'Code'
elif os.path.exists(FLATPAK_VSCODIUM_PATH):
VSCODE = "flatpak run com.vscodium.codium"
VSCODENAME = 'VSCodium'
elif os.path.exists(VSCODE_PATH):
VSCODE = 'code'
VSCODENAME = 'Code'
elif os.path.exists(VSCODIUM_PATH):
VSCODE = 'codium'
VSCODENAME = 'VSCodium'
# always create new window?
NEWWINDOW = False
# Force wayland? ( Flatpak packages may need to add permissions for wayland )
WAYLAND = True
class VSCodeExtension(GObject.GObject, Nautilus.MenuProvider):
def launch_vscode(self, menu, files):
safepaths = ''
args = ''
for file in files:
filepath = file.get_location().get_path()
safepaths += '"' + filepath + '" '
# If one of the files we are trying to open is a folder
# create a new instance of vscode
if os.path.isdir(filepath) and os.path.exists(filepath):
args = '--new-window '
if NEWWINDOW:
args = '--new-window'
if WAYLAND and os.environ.get('XDG_SESSION_TYPE') == "wayland":
print("true")
args += ' --ozone-platform-hint=auto --enable-features=WaylandWindowDecorations'
call(VSCODE + ' ' + args + ' ' + safepaths + '&', shell=True)
def get_file_items(self, *args):
files = args[-1]
item = Nautilus.MenuItem(
name='VSCodeOpen',
label='Open in ' + VSCODENAME,
tip='Opens the selected files with VSCode'
)
item.connect('activate', self.launch_vscode, files)
return [item]
def get_background_items(self, *args):
file_ = args[-1]
item = Nautilus.MenuItem(
name='VSCodeOpenBackground',
label='Open in ' + VSCODENAME,
tip='Opens the current directory in VSCode'
)
item.connect('activate', self.launch_vscode, [file_])
return [item]