From 1b890237061be9419d7d160416d60f019ac16a0a Mon Sep 17 00:00:00 2001 From: Matt Alexander Date: Fri, 28 Jan 2022 00:49:08 -0500 Subject: [PATCH] Added support for workspaces, and also VSCodium (#6) Added support for workspaces, and also VSCodium --- README.md | 4 +- images/file.svg | 489 +------------------------------------------ images/folder.svg | 361 +------------------------------- images/workspace.svg | 8 + main.py | 51 +++-- 5 files changed, 56 insertions(+), 857 deletions(-) create mode 100644 images/workspace.svg diff --git a/README.md b/README.md index 4ee3e40..b2e0902 100644 --- a/README.md +++ b/README.md @@ -31,4 +31,6 @@ Default keyword to trigger this extension is **`code`**. This can be changed in ## License -[MIT](LICENSE) +This source code is released under the [MIT](LICENSE) license. + +The icons are from [github.com/vscode-icons/vscode-icons](https://github.com/vscode-icons/vscode-icons), which are available under the Creative Commons - ShareAlike (CC BY-SA) license. diff --git a/images/file.svg b/images/file.svg index 9c4e37b..cddf38b 100644 --- a/images/file.svg +++ b/images/file.svg @@ -1,484 +1,5 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - image/svg+xml - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + file_type_vscode2 + + \ No newline at end of file diff --git a/images/folder.svg b/images/folder.svg index 4cd236d..fe6177f 100644 --- a/images/folder.svg +++ b/images/folder.svg @@ -1,355 +1,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - image/svg+xml - - - - - - - - - - - - - - - - - - - - - - - - - - + + folder_type_vscode2 + + + \ No newline at end of file diff --git a/images/workspace.svg b/images/workspace.svg new file mode 100644 index 0000000..21e0f61 --- /dev/null +++ b/images/workspace.svg @@ -0,0 +1,8 @@ + + folder_type_vscode2_opened + + + + \ No newline at end of file diff --git a/main.py b/main.py index 62567df..3b617d0 100644 --- a/main.py +++ b/main.py @@ -28,12 +28,17 @@ def get_path(filename, from_home=False): class Code: - open_command_paths = ["/usr/bin/code", "/bin/code", "/snap/bin/code"] + path_dirs = ("/usr/bin", "/bin", "/snap/bin") + variants = ("Code", "VSCodium") - def get_installed_path(self): - for path in self.open_command_paths: - if os.path.exists(path): - return path + @staticmethod + def find_code(): + for path in (pathlib.Path(path_dir) for path_dir in Code.path_dirs): + for variant in Code.variants: + installed_path = path / variant.lower() + config_path = pathlib.Path.home() / ".config" / variant / "storage.json" + if installed_path.exists() and config_path.exists(): + return installed_path, config_path, return False def is_installed(self): @@ -41,28 +46,41 @@ def is_installed(self): def get_recents(self): recents = [] - storage = json.load( - open(Utils.get_path(".config/Code/storage.json", True), "r")) + + storage = json.load(self.config_path.open("r")) openedPaths = storage["openedPathsList"]["entries"] for path in openedPaths: - folder = "folderUri" in path - uri = path["folderUri"] if folder else path["fileUri"] + if "folderUri" in path: + uri = path["folderUri"] + icon = "folder" + option = "--folder-uri" + elif "fileUri" in path: + uri = path["fileUri"] + icon = "file" + option = "--file-uri" + elif "workspace" in path: + uri = path["workspace"]["configPath"] + icon = "workspace" + option = "--file-uri" + else: + continue + label = path["label"] if "label" in path else uri.split("/")[-1] recents.append({ - "folder": folder, - "uri": uri, - "label": label + "uri": uri, + "label": label, + "icon": icon, + "option": option, }) return recents def open_vscode(self, recent): if not self.is_installed(): return - option = "--folder-uri" if recent["folder"] else "--file-uri" - os.system(f"{self.installed_path} {option} {recent['uri']}") + os.system(f"{self.installed_path} {recent['option']} {recent['uri']}") def __init__(self): - self.installed_path = self.get_installed_path() + self.installed_path, self.config_path = self.find_code() class CodeExtension(Extension): @@ -96,8 +114,7 @@ def get_ext_result_items(self, query): for recent in data[:20]: items.append( ExtensionSmallResultItem( - icon=Utils.get_path( - f"images/{'folder' if recent['folder'] else 'file'}.svg"), + icon=Utils.get_path(f"images/{recent['icon']}.svg"), name=recent["label"], on_enter=ExtensionCustomAction(recent), )