Skip to content

Commit c3d9988

Browse files
authored
Merge pull request godotengine#158 from katomuso/refactor-gdscript-eglot-contact
Refactor gdscript-eglot-contact
2 parents bee7f99 + f320e02 commit c3d9988

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

gdscript-eglot.el

+27-24
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,27 @@
4242
"The version of godot in use."
4343
:type 'string)
4444

45+
(defun gdscript-eglot--get-config-dir ()
46+
"Get system-specific directory with Godot configuration files."
47+
(pcase system-type
48+
('darwin "~/Library/Application Support/Godot/")
49+
('windows-nt (substitute-in-file-name "$APPDATA/Godot/"))
50+
('gnu/linux (file-name-concat
51+
(or (getenv "XDG_CONFIG_HOME") "~/.config/")
52+
"godot"))))
53+
54+
(defun gdscript-eglot--extract-port (editor-settings-file)
55+
"Extract LSP port from Godot editor settings file."
56+
(when (file-exists-p editor-settings-file)
57+
(with-temp-buffer
58+
(insert-file-contents editor-settings-file)
59+
(when (re-search-forward
60+
(rx "network/language_server/remote_port"
61+
(* space) ?= (* space)
62+
(group (+ digit)))
63+
nil t)
64+
(string-to-number (match-string 1))))))
65+
4566
;;;###autoload
4667
(defun gdscript-eglot-contact (_interactive)
4768
"Attempt to help `eglot' contact the running gdscript LSP.
@@ -52,30 +73,12 @@ definitions of HOST, PORT, and INTERACTIVE.
5273
For more context, see
5374
https://lists.gnu.org/archive/html/bug-gnu-emacs/2023-04/msg01070.html."
5475
(save-excursion
55-
(let* ((cfg-dir (pcase system-type
56-
('darwin "~/Library/Application Support/Godot/")
57-
('windows-nt (substitute-in-file-name "$APPDATA/Godot/"))
58-
('gnu/linux (file-name-concat
59-
(or (getenv "XDG_CONFIG_HOME") "~/.config/")
60-
"godot"))))
61-
(cfg-buffer (find-file-noselect
62-
(file-name-concat
63-
cfg-dir
64-
(format "editor_settings-%s.tres"
65-
gdscript-eglot-version))))
66-
(port
67-
(with-current-buffer cfg-buffer
68-
(goto-char 0)
69-
(and
70-
(re-search-forward
71-
(rx "network/language_server/remote_port"
72-
(* space) ?= (* space)
73-
(group (+ digit)))
74-
nil t)
75-
(string-to-number (match-string 1))))))
76-
(kill-buffer cfg-buffer)
77-
;; then return the host-port list when found
78-
(and port (list "localhost" port)))))
76+
(let* ((config-dir (gdscript-eglot--get-config-dir))
77+
(settings-file (file-name-concat
78+
config-dir
79+
(format "editor_settings-%s.tres" gdscript-eglot-version))))
80+
(when-let ((port (gdscript-eglot--extract-port settings-file)))
81+
(list "localhost" port)))))
7982

8083
(provide 'gdscript-eglot)
8184
;;; gdscript-eglot.el ends here.

0 commit comments

Comments
 (0)