-
-
Notifications
You must be signed in to change notification settings - Fork 34
/
gdscript-eglot.el
84 lines (75 loc) · 2.95 KB
/
gdscript-eglot.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
;;; gdscript-eglot.el --- Integration with eglot -*- lexical-binding: t; -*-
;;
;; Copyright (C) 2023 GDQuest and contributors
;;
;; Author: Ruijie Yu <[email protected]>
;; URL: https://github.com/godotengine/emacs-gdscript-mode/
;; Version: 1.0.0
;; Package-Requires: ((emacs "26.3"))
;; Maintainer: [email protected]
;; Created: June 2020
;; Keywords: languages
;;
;; This file is not part of GNU Emacs.
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;
;;; Commentary:
;;
;; Handles the configuraiton of eglot.
;; This supports `gdscript-mode' using `eglot'.
;;
;;; Code:
;;;###autoload
(defgroup gdscript-eglot nil
"Configurations in gdscript related to `eglot'."
:group 'gdscript)
;;;###autoload
(defcustom gdscript-eglot-version "4.3"
"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))))))
;;;###autoload
(defun gdscript-eglot-contact (_interactive)
"Attempt to help `eglot' contact the running gdscript LSP.
Returns a list (HOST PORT) if successful, nil otherwise. See the
last definition of CONTACT in `eglot-server-programs' for
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* ((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.