-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathellsp-tdsync.el
107 lines (87 loc) · 3.63 KB
/
ellsp-tdsync.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
;;; ellsp-tdsync.el --- Text document sync -*- lexical-binding: t; -*-
;; Copyright (C) 2023-2025 Shen, Jen-Chieh
;; 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:
;;
;; Text document sync.
;;
;;; Code:
(require 'ellsp-util)
(defclass ellsp-file ()
((name :type string :initarg :name)
(buffer :type buffer :initarg :buffer))
"Tracked buffer file.")
(defclass ellsp-state ()
((files
:type hash-table :initform (make-hash-table :test #'equal)
:documentation "Workspace files.")
(dependencies
:type list :initform nil
:documentation "Loaded dependencies.
We use this list to remove dependencies which don't need to
be re-analysed during textDocument/didOpen handler.")))
(defvar ellsp-workspace (ellsp-state)
"Workspace state.")
(cl-defmethod ellsp-get-file ((state ellsp-state) (file string))
"Return the file if found."
(gethash file (oref state files)))
(cl-defmethod ellsp-get-buffer ((state ellsp-state) (file string))
"Get the buffer from workspace state."
(when-let* ((file (ellsp-get-file state file)))
(oref file buffer)))
(cl-defmethod ellsp-update-file-buffer ((state ellsp-state) (file string) &optional content)
"Sync the text document buffer."
(message "Trying to update file %s" file)
(when-let* ((buffer (ellsp-get-buffer state file)))
(with-current-buffer buffer
(erase-buffer)
(if (and content (stringp content))
(insert content)
(insert-file-contents file)))
(message "Updated file %s" file)))
(cl-defmethod ellsp-add-file ((state ellsp-state) (file string))
"Add a file to workspace."
(message "Added file %s to state" file)
(puthash file
(ellsp-file
:name file
:buffer (with-current-buffer
(get-buffer-create (format "*ellsp:%s*" file))
(emacs-lisp-mode)
(current-buffer)))
(oref state files))
(ellsp-update-file-buffer state file))
(defun ellsp--handle-textDocument/didOpen (params)
"On method `textDocument/didOpen'."
(-let* (((&DidOpenTextDocumentParams :text-document (&TextDocumentItem :uri :version)) params)
(file (lsp--uri-to-path uri)))
(ellsp-add-file ellsp-workspace file)
nil))
(defun ellsp--handle-textDocument/didSave (params)
"On method `textDocument/didSave'."
(-let* (((&DidSaveTextDocumentParams :text-document (&TextDocumentItem :uri :version)) params)
(file (lsp--uri-to-path uri)))
(ellsp-update-file-buffer ellsp-workspace file)
nil))
(defun ellsp--handle-textDocument/didChange (params)
"On method `textDocument/didChange'."
(-let* (((&DidChangeTextDocumentParams
:text-document (&VersionedTextDocumentIdentifier :uri :version?)
:content-changes [(&TextDocumentContentChangeEvent :text)])
params)
(file (lsp--uri-to-path uri))
(buffer (ellsp-get-buffer ellsp-workspace file)))
(ellsp-update-file-buffer ellsp-workspace file text)
nil))
(provide 'ellsp-tdsync)
;;; ellsp-tdsync.el ends here