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 7 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
7 changes: 4 additions & 3 deletions acm/acm-backend-codeium.el
Original file line number Diff line number Diff line change
Expand Up @@ -160,11 +160,12 @@

(defun acm-backend-codeium-candidate-expand (candidate-info bound-start &optional preview)
;; We need replace whole area with codeium label.
(let ((end-position (line-end-position)))
(let ((start-position (line-beginning-position))
(middle-position (point)))
(forward-line (- (plist-get candidate-info :line) (count-lines (point-min) (line-beginning-position))))
(if preview
(acm-preview-create-overlay (point) end-position (plist-get candidate-info :label))
(delete-region (point) end-position)
(acm-preview-create-overlay start-position middle-position (plist-get candidate-info :label))
(delete-region start-position middle-position)
Copy link
Owner

Choose a reason for hiding this comment

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

Above code fix some bug?

(insert (plist-get candidate-info :label))
(when acm-backend-codeium-accept
(lsp-bridge-call-async
Expand Down
5 changes: 5 additions & 0 deletions acm/acm-backend-ctags.el
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
: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)

(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("lsp-bridge-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
49 changes: 48 additions & 1 deletion lsp-bridge.el
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
(require 'subr-x)
(require 'markdown-mode)
(require 'diff)
(require 'xref)

(defvar acm-library-path (expand-file-name "acm" (if load-file-name
(file-name-directory load-file-name)
Expand Down Expand Up @@ -165,6 +166,11 @@ Setting this to nil or 0 will turn off the heartbeat mechanism."
:type 'boolean
:group 'lsp-bridge)

(defcustom lsp-bridge-remote-enable-kill-process nil
"Whether kill remote lsp-bridge process when Emacs exit ."
:type 'boolean
:group 'lsp-bridge)

(defcustom lsp-bridge-completion-stop-commands
'("undo-tree-undo" "undo-tree-redo"
"kill-region" "delete-block-backward"
Expand Down Expand Up @@ -997,6 +1003,11 @@ So we build this macro to restore postion after code format."
(lsp-bridge-deferred-chain
(lsp-bridge-epc-call-deferred lsp-bridge-epc-process (read method) args)))

(defun lsp-bridge-call-sync (method &rest args)
"Call Python EPC function METHOD and ARGS synchronously."
(lsp-bridge-deferred-chain
(lsp-bridge-epc-call-sync lsp-bridge-epc-process (read method) args)))

Copy link
Owner

Choose a reason for hiding this comment

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

Don't use this function, it will block Emacs

(defvar-local lsp-bridge-buffer-file-deleted nil)

(defun lsp-bridge-process-live-p ()
Expand Down Expand Up @@ -1123,6 +1134,8 @@ So we build this macro to restore postion after code format."
(defun lsp-bridge--kill-python-process ()
"Kill LSP-Bridge background python process."
(when (lsp-bridge-process-live-p)
(when lsp-bridge-remote-enable-kill-process
(lsp-bridge-call-sync "close_client"))
Copy link
Owner

Choose a reason for hiding this comment

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

Shoud remove those code if code of close_client move to "cleanup" function.

And don't use lsp-bridge-call-sync, it will block Emacs.

;; Cleanup before exit LSP-Bridge server process.
(lsp-bridge-call-async "cleanup")
;; Delete LSP-Bridge server process.
Expand Down Expand Up @@ -1630,8 +1643,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 Expand Up @@ -2132,6 +2146,39 @@ Default is `bottom-right', you can choose other value: `top-left', `top-right',
(post-self-insert-hook lsp-bridge-monitor-post-self-insert 90 t)
))

(defface lsp-bridge-tag-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 lsp-bridge-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 'lsp-bridge-tag-annotation-face) str)
str)
(xref-make-file-location path line 0))))

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

(defun lsp-bridge-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))))
Copy link
Owner

Choose a reason for hiding this comment

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

Those code should move to file acm-backend-ctags.el , and rename with acm-backend-ctags-*


(defvar lsp-bridge-mode-map (make-sparse-keymap))

(defvar lsp-bridge-signature-help-timer nil)
Expand Down
14 changes: 12 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,6 +833,11 @@ 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):
"""Do some cleanup before exit python process."""
close_epc_client()
Expand Down
Loading