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

Kill remote process #953

Merged
merged 9 commits into from
Jun 9, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
40 changes: 40 additions & 0 deletions acm/acm-backend-ctags.el
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
;;; Commentary:

;;; Code:
(require 'xref)

(defcustom acm-backend-ctags-candidate-min-length 3
"Minimal length of candidate."
Expand All @@ -14,6 +15,45 @@
:type 'boolean
:group 'acm-backend-ctags)

(defcustom acm-backend-ctags-max-candidates 10
"Maximal number of candidate of menu."
:type 'integer
:group 'acm-backend-ctags)
(require 'xref)
Copy link
Owner

Choose a reason for hiding this comment

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

Duplicate code?


(defface acm-backend-ctags-annotation-face
'((((background light))
:foreground "#666666" :slant italic)
(t
:foreground "#c0c0c0" :slant italic))
"Face used for annotations when presenting a tag.
Annotations include kind, type, etc.")

(defun acm-backend-ctags-xref--make-object (tag)
"Make xref object of TAG."
(let* ((path (plist-get tag :ext-abspath))
(line (plist-get tag :line))
(str (plist-get tag :str))
(annotation (plist-get tag :annotation)))
(xref-make
(if annotation
(concat (propertize (concat "(" annotation ") ") 'face 'acm-backend-ctags-annotation-face) str)
str)
(xref-make-file-location path line 0))))

(defun acm-backend-ctags-xref-callback (tags)
(let ((fetcher (lambda () (mapcar #'lsp-bridge-xref--make-object tags))))
(xref-show-xrefs fetcher nil)))

(defun acm-backend-ctags-find-def-at-point ()
(interactive)
(if (lsp-bridge-is-remote-file)
(lsp-bridge-remote-send-func-request "ctags_find_def"
(list
(symbol-at-point)
(file-local-name (buffer-file-name))))
(lsp-bridge-call-async "ctags_find_def" (symbol-at-point) (buffer-file-name))))

(defvar-local acm-backend-ctags-items nil)

(defun acm-backend-ctags-candidates (keyword)
Expand Down
39 changes: 38 additions & 1 deletion core/ctags.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ def locate_dominating_file(self, file_or_dir, filename):

return None

def make_complete(self, symbol, filename, cursor_offset):
def make_complete(self, symbol, filename, max_lines, cursor_offset):
self.lock.acquire()
try:
self.current_cursor_offset = cursor_offset
Expand All @@ -147,12 +147,49 @@ def make_complete(self, symbol, filename, cursor_offset):
cmd = self.readtags_get_cmd(tagsfile, symbol, "prefix", False, DEFAULT_FILTER_CMD, DEFAULT_SORTER_CMD, "")

lines = self.run_cmd_in_path(cmd, filename)
lines = lines[0:max_lines]

tags = map(self.parse_tag_line, lines)
candidates = map(self.make_ctags_acm_candidate, tags)

self.dispatch(list(candidates), cursor_offset)

def make_xref(self, tag: dict, rootdir):
candidate = {}
tagname = tag["tagname"]

path = tag.get("tagfile", "")
line = int(tag.get("line", "1"))
pattern = tag.get("tagaddress", "")
annotation = self.make_tag_annotation(tag)

ext_abspath = os.path.join(rootdir, path)
tramp_method = get_remote_connection_info()

candidate["ext-abspath"] = tramp_method + ext_abspath
candidate["line"] = line
candidate["str"] = pattern[2:-2]
candidate["annotation"] = annotation

return candidate

def find_definition(self, symbol, filename):
tagsfile = self.locate_dominating_file(filename, "tags")
if not tagsfile:
return

cmd = self.readtags_get_cmd(tagsfile, symbol, "exact", False, "", "", "")
lines = self.run_cmd_in_path(cmd, filename)
tags = map(self.parse_tag_line, lines)

candidates = map(lambda tag: self.make_xref(tag, os.path.dirname(tagsfile)), tags)
candidates = list(candidates)

if candidates == []:
message_emacs(f"symbol {symbol} not found by ctags")
else:
eval_in_emacs("acm-backend-ctags-xref-callback", candidates)

def dispatch(self, candidates, cursor_offset):
self.lock.acquire()
try:
Expand Down
30 changes: 27 additions & 3 deletions core/remote_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ def __init__(self, ssh_conf, server_port, callback):
self.ssh_port = ssh_conf.get('port', 22)
self.server_port = server_port
self.callback = callback
[self.remote_python_command, self.remote_python_file, self.remote_log] = get_emacs_vars(["lsp-bridge-remote-python-command", "lsp-bridge-remote-python-file", "lsp-bridge-remote-log"])

[self.user_ssh_private_key] = get_emacs_vars(["lsp-bridge-user-ssh-private-key"])

Expand Down Expand Up @@ -169,9 +170,9 @@ def run(self):
self.chan.close()

def start_lsp_bridge_process(self):
[remote_python_command] = get_emacs_vars(["lsp-bridge-remote-python-command"])
[remote_python_file] = get_emacs_vars(["lsp-bridge-remote-python-file"])
[remote_log] = get_emacs_vars(["lsp-bridge-remote-log"])
remote_python_command = self.remote_python_command
remote_python_file = self.remote_python_file
remote_log = self.remote_log

# use -l option to bash as a login shell, ensuring that login scripts (like ~/.bash_profile) are read and executed.
# This is useful for lsp-bridge to use environment settings to correctly find out language server command
Expand All @@ -194,6 +195,29 @@ def start_lsp_bridge_process(self):
print("stdout:" + stdout.read().decode())
print("stderr:" + stderr.read().decode())

def kill_lsp_bridge_process(self):
remote_log = self.remote_log

try:
self.ssh.exec_command(
f"""
nohup /bin/bash -l -c '
pid=$(ps aux | grep -v grep | grep lsp_bridge.py | cut -d " " -f2)
echo "try kill" | tee >> {remote_log}
if ! [ "$pid" == "" ]; then
echo -e "kill lsp-bridge process as user $(whoami)" | tee >>{remote_log}
kill $pid
if [ "$?" = "0" ]; then
echo -e "Kill lsp-bridge successfully" | tee >>{remote_log}
else
echo -e "Kill lsp-bridge failed" | tee >>{remote_log}
fi
fi'
"""
)
except:
pass


class RemoteFileServer:
def __init__(self, host, port):
Expand Down
3 changes: 2 additions & 1 deletion lsp-bridge.el
Original file line number Diff line number Diff line change
Expand Up @@ -1630,8 +1630,9 @@ So we build this macro to restore postion after code format."
(list
current-word
(tramp-file-local-name lsp-bridge-remote-file-path)
acm-backend-ctags-max-candidates
(1- (point)))))
(lsp-bridge-call-async "ctags_complete" current-word (buffer-file-name) (1- (point))))))
(lsp-bridge-call-async "ctags_complete" current-word (buffer-file-name) acm-backend-ctags-max-candidates (1- (point))))))

;; Search sdcv dictionary.
(when acm-enable-search-sdcv-words
Expand Down
15 changes: 13 additions & 2 deletions lsp_bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -793,12 +793,17 @@ def tabnine_complete(self, before, after, filename, region_includes_beginning, r
self.tabnine.complete(before, after, filename, region_includes_beginning, region_includes_end, max_num_results)

@threaded
def ctags_complete(self, symbol, filename, cursor_offset):
self.ctags.make_complete(symbol, filename, cursor_offset)
def ctags_complete(self, symbol, filename, max_lines, cursor_offset):
self.ctags.make_complete(symbol, filename, max_lines, cursor_offset)

@threaded
def ctags_find_def(self, symbol, filename):
self.ctags.find_definition(symbol, filename)

def copilot_complete(self, position, editor_mode, file_path, relative_path, tab_size, text, insert_spaces):
self.copilot.complete(position, editor_mode, file_path, relative_path, tab_size, text, insert_spaces)

@threaded
def codeium_complete(self, cursor_offset, editor_language, tab_size, text, insert_spaces, prefix, language):
self.codeium.complete(cursor_offset, editor_language, tab_size, text, insert_spaces, prefix, language)

Expand Down Expand Up @@ -828,7 +833,13 @@ def handle_server_process_exit(self, server_name):
log_time("Exit server {}".format(server_name))
del LSP_SERVER_DICT[server_name]

def close_client(self):
for client in self.client_dict.values():
if hasattr(client, "kill_lsp_bridge_process"):
client.kill_lsp_bridge_process()

Comment on lines +836 to +840
Copy link
Owner

Choose a reason for hiding this comment

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

Why not add close_client code to function "cleanup" ?

def cleanup(self):
self.close_client()
"""Do some cleanup before exit python process."""
Comment on lines +842 to 843
Copy link
Owner

Choose a reason for hiding this comment

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

close_client should move to below of function comment

close_epc_client()

Expand Down
Loading