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

Refactor gdscript-eglot-contact #158

Merged
Merged
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
51 changes: 27 additions & 24 deletions gdscript-eglot.el
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,27 @@
"The version of godot in use."
:type 'string)

(defun gdscript-eglot--get-config-dir ()
"Get system-specific directory with Godot configuration files."
(pcase system-type
('darwin "~/Library/Application Support/Godot/")
('windows-nt (substitute-in-file-name "$APPDATA/Godot/"))
('gnu/linux (file-name-concat
(or (getenv "XDG_CONFIG_HOME") "~/.config/")
"godot"))))

(defun gdscript-eglot--extract-port (editor-settings-file)
"Extract LSP port from Godot editor settings file."
(when (file-exists-p editor-settings-file)
(with-temp-buffer
(insert-file-contents editor-settings-file)
(when (re-search-forward
(rx "network/language_server/remote_port"
(* space) ?= (* space)
(group (+ digit)))
nil t)
(string-to-number (match-string 1))))))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you add documentation to these functions? Thanks!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've added documentation for gdscript-eglot--get-config-dir and gdscript-eglot--extract-port functions.


;;;###autoload
(defun gdscript-eglot-contact (_interactive)
"Attempt to help `eglot' contact the running gdscript LSP.
Expand All @@ -52,30 +73,12 @@ definitions of HOST, PORT, and INTERACTIVE.
For more context, see
https://lists.gnu.org/archive/html/bug-gnu-emacs/2023-04/msg01070.html."
(save-excursion
(let* ((cfg-dir (pcase system-type
('darwin "~/Library/Application Support/Godot/")
('windows-nt (substitute-in-file-name "$APPDATA/Godot/"))
('gnu/linux (file-name-concat
(or (getenv "XDG_CONFIG_HOME") "~/.config/")
"godot"))))
(cfg-buffer (find-file-noselect
(file-name-concat
cfg-dir
(format "editor_settings-%s.tres"
gdscript-eglot-version))))
(port
(with-current-buffer cfg-buffer
(goto-char 0)
(and
(re-search-forward
(rx "network/language_server/remote_port"
(* space) ?= (* space)
(group (+ digit)))
nil t)
(string-to-number (match-string 1))))))
(kill-buffer cfg-buffer)
;; then return the host-port list when found
(and port (list "localhost" port)))))
(let* ((config-dir (gdscript-eglot--get-config-dir))
(settings-file (file-name-concat
config-dir
(format "editor_settings-%s.tres" gdscript-eglot-version))))
(when-let ((port (gdscript-eglot--extract-port settings-file)))
(list "localhost" port)))))

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