Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

jetbrains: Clean up and fix logging and error handling #1445

Merged
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 30 additions & 37 deletions apps/jetbrains/jetbrains.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import os
import os.path
import tempfile
import time
from pathlib import Path
from typing import Optional

import requests
from talon import Context, Module, actions, clip, ui
from talon import Context, Module, actions, app, clip, ui

# Courtesy of https://github.com/anonfunc/talon-user/blob/master/apps/jetbrains.py

extendCommands = []

# Each IDE gets its own port, as otherwise you wouldn't be able
# to run two at the same time and switch between them.
# Note that MPS and IntelliJ ultimate will conflict...
Expand Down Expand Up @@ -59,55 +57,44 @@
}


def _get_nonce(port, file_prefix):
def _get_nonce(port: int, file_prefix: str) -> Optional[str]:
file_name = file_prefix + str(port)
try:
with open(os.path.join(tempfile.gettempdir(), file_name)) as fh:
return fh.read()
except FileNotFoundError as e:
except FileNotFoundError:
try:
home = str(Path.home())
with open(os.path.join(home, file_name)) as fh:
with open(Path.home() / file_name) as fh:
return fh.read()
except FileNotFoundError as eb:
except FileNotFoundError:
print(f"Could not find {file_name} in tmp or home")
return None
except OSError as e:
print(e)
return None


def send_idea_command(cmd):
print(f"Sending {cmd}")
def send_idea_command(cmd: str) -> str:
active_app = ui.active_app()
bundle = active_app.bundle or active_app.name
port = port_mapping.get(bundle, None)
if not port:
raise Exception(f"unknown application {bundle}")
nonce = _get_nonce(port, ".vcidea_") or _get_nonce(port, "vcidea_")
proxies = {"http": None, "https": None}
print(f"sending {bundle} {port} {nonce}")
if port and nonce:
response = requests.get(
f"http://localhost:{port}/{nonce}/{cmd}",
proxies=proxies,
timeout=(0.05, 3.05),
)
response.raise_for_status()
return response.text


def get_idea_location():
return send_idea_command("location").split()
if not nonce:
raise FileNotFoundError(f"Couldn't find IDEA nonce file for port {port}")

response = requests.get(
f"http://localhost:{port}/{nonce}/{cmd}",
proxies={"http": None, "https": None},
timeout=(0.05, 3.05),
)
response.raise_for_status()
return response.text

def idea_commands(commands):
command_list = commands.split(",")
print("executing jetbrains", commands)
global extendCommands
extendCommands = command_list
for cmd in command_list:
if cmd:
send_idea_command(cmd.strip())
time.sleep(0.1)

def get_idea_location() -> list[str]:
return send_idea_command("location").split()


ctx = Context()
Expand Down Expand Up @@ -148,7 +135,15 @@ def idea_commands(commands):
class Actions:
def idea(commands: str):
"""Send a command to Jetbrains product"""
idea_commands(commands)
command_list = commands.split(",")
try:
for cmd in command_list:
if cmd:
send_idea_command(cmd.strip())
actions.sleep(0.1)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't have to be done in this PR but we should really see if this can be removed. It makes the running of multiple actions like idea_grab() unnecessarily slower/jankier.

(I ran with it removed for many months, but I've since diverged from community enough that it'd be good for someone else to test too)

except Exception as e:
app.notify(e)
raise e
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was going to recommend just raise, but it looks like in Python 3 this doesn't actually remove any of the context, waited under Python 2? https://stackoverflow.com/a/45986945/303833

phillco marked this conversation as resolved.
Show resolved Hide resolved

def idea_grab(times: int):
"""Copies specified number of words to the left"""
Expand All @@ -162,8 +157,6 @@ def idea_grab(times: int):
send_idea_command("action EditorPaste")
finally:
clip.set(old_clip)
global extendCommands
extendCommands = []


ctx.matches = r"""
Expand Down