-
Notifications
You must be signed in to change notification settings - Fork 783
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
AndreasArvidsson
merged 3 commits into
talonhub:main
from
auscompgeek:clean-up-jetbrains-logging
Nov 9, 2024
Merged
Changes from 2 commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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... | ||
|
@@ -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() | ||
|
@@ -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) | ||
except Exception as e: | ||
app.notify(e) | ||
raise e | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was going to recommend just
phillco marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def idea_grab(times: int): | ||
"""Copies specified number of words to the left""" | ||
|
@@ -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""" | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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)