From 2318e21682d1cf0c4c649d5df2715623bd17c1b3 Mon Sep 17 00:00:00 2001 From: corvofeng Date: Fri, 2 Apr 2021 15:51:22 +0800 Subject: [PATCH 1/4] [vscode]: Add new plugin for vscode projects. Signed-off-by: corvofeng --- vscode_projects/__init__.py | 93 +++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 vscode_projects/__init__.py diff --git a/vscode_projects/__init__.py b/vscode_projects/__init__.py new file mode 100644 index 00000000..dbf19855 --- /dev/null +++ b/vscode_projects/__init__.py @@ -0,0 +1,93 @@ +# -*- coding: utf-8 -*- + +""" + +""" + + +import os +import re +import json +from pathlib import Path + +try: + from albert import * +except Exception as e: + def info(v): + return print(v) + + def iconLookup(v): + pass + +__title__ = "VSCode Projects" +__version__ = "0.1.0" +__triggers__ = "&c" +__authors__ = "corvofeng" +__exec_deps__ = ['code'] + + +storage_file = str(Path.home()) + "/.config/Code/storage.json" +iconPath = iconLookup("visual-studio-code") +mtime = 0 +projects = [] + + +def get_vscode_project(storage_json): + storage_dict = json.loads(storage_json) + entries = storage_dict['openedPathsList']['entries'] + result = [] + for entry in entries: + if 'folderUri' in entry: + uri = entry['folderUri'] + title = os.path.basename(uri) + if uri.startswith("vscode-remote://"): + title = "remote: {}".format(title) + result.append({ + 'title': title, + 'paths': [uri], + }) + return result + + +def updateProjects(): + global mtime + try: + new_mtime = os.path.getmtime(storage_file) + except Exception as e: + warning("Could not get mtime of file: " + storage_file + str(e)) + if mtime != new_mtime: + mtime = new_mtime + with open(storage_file) as f: + global projects + projects = get_vscode_project(f.read()) + + +def handleQuery(query): + if not query.isTriggered: + return + + updateProjects() + + stripped = query.string.strip() + + items = [] + for project in projects: + if re.search(stripped, project['title'], re.IGNORECASE): + items.append(Item(id=__title__ + project['title'], + icon=iconPath, + text=project['title'], + subtext="Path: %s" % (project['paths']), + actions=[ + ProcAction(text="Open project in VSCode", + commandline=["code"] + ["--folder-uri"] + project['paths']) + ])) + return items + + +def main(): + with open(storage_file) as f: + print(get_vscode_project(f.read())) + + +if __name__ == "__main__": + main() \ No newline at end of file From f1d53714306d6e204763cbd8b479687edc54e089 Mon Sep 17 00:00:00 2001 From: corvofeng Date: Tue, 10 Aug 2021 09:23:57 +0800 Subject: [PATCH 2/4] [vscode] Ignore the noexists directory. Signed-off-by: corvofeng --- vscode_projects/__init__.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/vscode_projects/__init__.py b/vscode_projects/__init__.py index dbf19855..9f5d7fd5 100644 --- a/vscode_projects/__init__.py +++ b/vscode_projects/__init__.py @@ -42,6 +42,9 @@ def get_vscode_project(storage_json): title = os.path.basename(uri) if uri.startswith("vscode-remote://"): title = "remote: {}".format(title) + if uri.startswith("file://"): + if not os.path.isdir(uri[7:]): + continue result.append({ 'title': title, 'paths': [uri], @@ -90,4 +93,4 @@ def main(): if __name__ == "__main__": - main() \ No newline at end of file + main() From 41768e0e9d6cfeeaad114f4b5ce3449bf926942f Mon Sep 17 00:00:00 2001 From: corvofeng Date: Mon, 14 Feb 2022 11:36:22 +0800 Subject: [PATCH 3/4] Update vscode support. Signed-off-by: corvofeng --- vscode_projects/__init__.py | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/vscode_projects/__init__.py b/vscode_projects/__init__.py index 9f5d7fd5..fd89b6b3 100644 --- a/vscode_projects/__init__.py +++ b/vscode_projects/__init__.py @@ -8,6 +8,7 @@ import os import re import json +import sqlite3 from pathlib import Path try: @@ -27,6 +28,8 @@ def iconLookup(v): storage_file = str(Path.home()) + "/.config/Code/storage.json" +storage_db = str(Path.home()) + "/.config/Code/User/globalStorage/state.vscdb" + iconPath = iconLookup("visual-studio-code") mtime = 0 projects = [] @@ -34,7 +37,25 @@ def iconLookup(v): def get_vscode_project(storage_json): storage_dict = json.loads(storage_json) - entries = storage_dict['openedPathsList']['entries'] + entries = [] + + try: + entries = storage_dict['openedPathsList']['entries'] + except KeyError: + pass + + conn = sqlite3.connect(storage_db) + cur = conn.cursor() + try: + df = cur.execute("select value from ItemTable where key='history.recentlyOpenedPathsList'") + row = cur.fetchone() + entries = json.loads(row[0])['entries'] + except KeyError: + pass + finally: + cur.close() + conn.close() + result = [] for entry in entries: if 'folderUri' in entry: From 1e18916202fd31c4bff660a4c1b5d179ec925f8f Mon Sep 17 00:00:00 2001 From: corvofeng Date: Tue, 10 May 2022 12:06:17 +0800 Subject: [PATCH 4/4] Fix storage.json. Signed-off-by: corvofeng --- vscode_projects/__init__.py | 28 +++++++++------------------- 1 file changed, 9 insertions(+), 19 deletions(-) diff --git a/vscode_projects/__init__.py b/vscode_projects/__init__.py index fd89b6b3..5fe7a611 100644 --- a/vscode_projects/__init__.py +++ b/vscode_projects/__init__.py @@ -35,19 +35,11 @@ def iconLookup(v): projects = [] -def get_vscode_project(storage_json): - storage_dict = json.loads(storage_json) - entries = [] - - try: - entries = storage_dict['openedPathsList']['entries'] - except KeyError: - pass - +def get_vscode_project(): conn = sqlite3.connect(storage_db) cur = conn.cursor() try: - df = cur.execute("select value from ItemTable where key='history.recentlyOpenedPathsList'") + df = cur.execute("select value from ItemTable where key='history.recentlyOpenedPathsList'") # noqa row = cur.fetchone() entries = json.loads(row[0])['entries'] except KeyError: @@ -61,6 +53,7 @@ def get_vscode_project(storage_json): if 'folderUri' in entry: uri = entry['folderUri'] title = os.path.basename(uri) + print(entry) if uri.startswith("vscode-remote://"): title = "remote: {}".format(title) if uri.startswith("file://"): @@ -76,26 +69,24 @@ def get_vscode_project(storage_json): def updateProjects(): global mtime try: - new_mtime = os.path.getmtime(storage_file) + new_mtime = os.path.getmtime(storage_db) except Exception as e: warning("Could not get mtime of file: " + storage_file + str(e)) if mtime != new_mtime: mtime = new_mtime - with open(storage_file) as f: - global projects - projects = get_vscode_project(f.read()) + projects = get_vscode_project() def handleQuery(query): if not query.isTriggered: return - updateProjects() + # updateProjects() stripped = query.string.strip() items = [] - for project in projects: + for project in get_vscode_project(): if re.search(stripped, project['title'], re.IGNORECASE): items.append(Item(id=__title__ + project['title'], icon=iconPath, @@ -103,14 +94,13 @@ def handleQuery(query): subtext="Path: %s" % (project['paths']), actions=[ ProcAction(text="Open project in VSCode", - commandline=["code"] + ["--folder-uri"] + project['paths']) + commandline=["code"] + ["--folder-uri"] + project['paths']) # noqa ])) return items def main(): - with open(storage_file) as f: - print(get_vscode_project(f.read())) + get_vscode_project() if __name__ == "__main__":