1
Fork 0
mirror of https://github.com/j1g5awi/krunner-vscode.git synced 2024-12-22 02:46:00 +00:00

🎉 Initial commit

This commit is contained in:
jigsaw 2022-03-25 20:04:29 +08:00
commit 8af7461417
9 changed files with 177 additions and 0 deletions

3
.gitignore vendored Normal file
View file

@ -0,0 +1,3 @@
.vscode
.venv
__pycache__

10
README.md Normal file
View file

@ -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)

89
krunner_vscode/__main__.py Executable file
View file

@ -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()

View file

@ -0,0 +1,3 @@
[D-BUS Service]
Name=com.github.j1g5awi.vscode
Exec=/usr/bin/python3 -m krunner_vscode"

View file

@ -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

17
poetry.lock generated Normal file
View file

@ -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"},
]

16
pyproject.toml Normal file
View file

@ -0,0 +1,16 @@
[tool.poetry]
name = "krunner-vscode"
version = "0.1.0"
description = ""
authors = ["jigsaw <j1g5aw@foxmail.com>"]
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"

14
scripts/install.sh Executable file
View file

@ -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

10
scripts/uninstall.sh Executable file
View file

@ -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