From 8af746141788aaf17bb29368fcec9c28a072e9bd Mon Sep 17 00:00:00 2001 From: jigsaw Date: Fri, 25 Mar 2022 20:04:29 +0800 Subject: [PATCH] :tada: Initial commit --- .gitignore | 3 + README.md | 10 +++ krunner_vscode/__main__.py | 89 +++++++++++++++++++++++ package/com.github.j1g5awi.vscode.service | 3 + package/plasma-runner-vscode.desktop | 15 ++++ poetry.lock | 17 +++++ pyproject.toml | 16 ++++ scripts/install.sh | 14 ++++ scripts/uninstall.sh | 10 +++ 9 files changed, 177 insertions(+) create mode 100644 .gitignore create mode 100644 README.md create mode 100755 krunner_vscode/__main__.py create mode 100644 package/com.github.j1g5awi.vscode.service create mode 100644 package/plasma-runner-vscode.desktop create mode 100644 poetry.lock create mode 100644 pyproject.toml create mode 100755 scripts/install.sh create mode 100755 scripts/uninstall.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e8023c5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.vscode +.venv +__pycache__ \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..fcd1f94 --- /dev/null +++ b/README.md @@ -0,0 +1,10 @@ +# Krunner VSCode + +## Requirements + +- python-gobject + +## Thanks + +[Shihira/krunner-bridge](https://github.com/Shihira/krunner-bridge) +[Merrit/vscode-runner](https://github.com/Merrit/vscode-runner) diff --git a/krunner_vscode/__main__.py b/krunner_vscode/__main__.py new file mode 100755 index 0000000..e530750 --- /dev/null +++ b/krunner_vscode/__main__.py @@ -0,0 +1,89 @@ +import json +import os +import pathlib +import sqlite3 +from typing import NamedTuple + +import dbus.service +from dbus.mainloop.glib import DBusGMainLoop +from gi.repository import GLib + +DBusGMainLoop(set_as_default=True) + +objpath = "/vscode" + +iface = "org.kde.krunner1" + + +class Match(NamedTuple): + data: str + display_text: str + icon: str + type: int + relevance: float + properties: dict + + +# TODO Check VSCode or Code - OSS or VSCodium + +# Read path_list from database +con = sqlite3.connect( + os.path.join( + os.environ["HOME"], ".config", "Code - OSS/User/globalStorage/state.vscdb" + ) +) +cur = con.cursor() +rows = cur.execute( + "SELECT value FROM ItemTable WHERE key = 'history.recentlyOpenedPathsList'" +) +data = json.loads(rows.fetchone()[0]) +con.close() +path_list = [ + "~" + path[len(os.environ["HOME"]) :] + for path in [i["folderUri"][7:] for i in data["entries"] if "folderUri" in i] + if os.environ["HOME"] in path +] + + +class Runner(dbus.service.Object): + def __init__(self): + dbus.service.Object.__init__( + self, + dbus.service.BusName( + "com.github.j1g5awi.vscode", dbus.SessionBus() # type:ignore + ), + objpath, + ) + + @dbus.service.method(iface, in_signature="s", out_signature="a(sssida{sv})") + def Match(self, query: str): + # data, display text, icon, type (Plasma::QueryType), relevance (0-1), properties (subtext, category and urls) + return [ + Match( + path, + pathlib.Path(path).name, + "com.visualstudio.code.oss", + 100, + 1.0, + {"subtext": path}, + ) + for path in path_list + if query in path + ] + + @dbus.service.method(iface, out_signature="a(sss)") + def Actions(self): + # id, text, icon + return [("id", "Open Folder", "document-open-folder")] + + @dbus.service.method(iface, in_signature="ss") + def Run(self, data: str, action_id: str): + if not action_id: + os.system("code " + data) + else: + os.system("xdg-open " + data) + + +runner = Runner() +loop = GLib.MainLoop() +loop.run() diff --git a/package/com.github.j1g5awi.vscode.service b/package/com.github.j1g5awi.vscode.service new file mode 100644 index 0000000..eb15408 --- /dev/null +++ b/package/com.github.j1g5awi.vscode.service @@ -0,0 +1,3 @@ +[D-BUS Service] +Name=com.github.j1g5awi.vscode +Exec=/usr/bin/python3 -m krunner_vscode" diff --git a/package/plasma-runner-vscode.desktop b/package/plasma-runner-vscode.desktop new file mode 100644 index 0000000..8c56bc7 --- /dev/null +++ b/package/plasma-runner-vscode.desktop @@ -0,0 +1,15 @@ +[Desktop Entry] +Name=VSCode +Comment=opening recent VSCode workspaces +X-KDE-ServiceTypes=Plasma/Runner +Type=Service +Icon=com.visualstudio.code.oss +X-KDE-PluginInfo-Author=Jigsaw +X-KDE-PluginInfo-Email=j1g5aw@foxmail.com +X-KDE-PluginInfo-Name=vscode +X-KDE-PluginInfo-Version=0.1.0 +X-KDE-PluginInfo-License=GPLv3 +X-KDE-PluginInfo-EnabledByDefault=true +X-Plasma-API=DBus +X-Plasma-DBusRunner-Service=com.github.j1g5awi.vscode +X-Plasma-DBusRunner-Path=/vscode diff --git a/poetry.lock b/poetry.lock new file mode 100644 index 0000000..297f490 --- /dev/null +++ b/poetry.lock @@ -0,0 +1,17 @@ +[[package]] +name = "dbus-python" +version = "1.2.18" +description = "Python bindings for libdbus" +category = "main" +optional = false +python-versions = "*" + +[metadata] +lock-version = "1.1" +python-versions = "^3.9" +content-hash = "f650f92ef846716ce9507b9b768c82722cfacb83005276bc71c5e8c1c831f6c6" + +[metadata.files] +dbus-python = [ + {file = "dbus-python-1.2.18.tar.gz", hash = "sha256:92bdd1e68b45596c833307a5ff4b217ee6929a1502f5341bae28fd120acf7260"}, +] diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..9c5655f --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,16 @@ +[tool.poetry] +name = "krunner-vscode" +version = "0.1.0" +description = "" +authors = ["jigsaw "] +readme = "README.md" + +[tool.poetry.dependencies] +python = "^3.9" +dbus-python = "^1.2.18" +PyGObject = "^3.42.0" + + +[build-system] +requires = ["poetry-core"] +build-backend = "poetry.core.masonry.api" diff --git a/scripts/install.sh b/scripts/install.sh new file mode 100755 index 0000000..659fa1d --- /dev/null +++ b/scripts/install.sh @@ -0,0 +1,14 @@ +#!/bin/bash + +# Exit if something fails +set -e + +pip install . + +mkdir -p ~/.local/share/kservices5/ +mkdir -p ~/.local/share/dbus-1/services/ + +cp ./package/plasma-runner-vscode.desktop ~/.local/share/kservices5/ +cp ./package/com.github.j1g5awi.vscode.service ~/.local/share/dbus-1/services/ + +kquitapp5 krunner diff --git a/scripts/uninstall.sh b/scripts/uninstall.sh new file mode 100755 index 0000000..e0219cc --- /dev/null +++ b/scripts/uninstall.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +# Exit if something fails +set -e + +pip uninstall krunner_vscode -y + +rm ~/.local/share/kservices5/plasma-runner-vscode.desktop +rm ~/.local/share/dbus-1/services/com.github.j1g5awi.vscode.service +kquitapp5 krunner