Skip to content

Commit 4f5479e

Browse files
committed
Improve resource leftovers
There are two things here that are both aimed at reducing the amount of resource clutter: 1. I've seen many tmp files left. Looking at the code, it seems to me like a reasonable thing to get rid of them when a file is saved. I did this via adding `tide-remove-tmp-file` on `after-save-hook`. 2. I also saw that there is a general tendency to accumulate many servers since they're never removed (actually, more than just the servers -- the whole project resources are kept, but the server is the main problem wrt resources). This is also mentioned in #256. So I implemented a function that scans all buffers and cleanup all projects that have no live buffers. It looks to me like a good idea to do this, since you can just kill old buffers to reduce resource usage. (And killing old buffers is more obvious than explicitly openning the server list to kill old ones, especially since there's no way to tell if a server is used by some buffer or not.) (I added this function onto `kill-buffer-hook`, but if that's too extreme, then a more mild option is to not do that and just let people add it themselves.)
1 parent e4c76a4 commit 4f5479e

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

tide.el

+14
Original file line numberDiff line numberDiff line change
@@ -729,6 +729,16 @@ If TIDE-TSSERVER-EXECUTABLE is set by the user use it. Otherwise check in the n
729729
(remhash project-name tide-tsserver-unsupported-commands)
730730
(remhash project-name tide-project-configs))
731731

732+
(defun tide-cleanup-dead-projects ()
733+
(let ((live-projects '()))
734+
(dolist (b (buffer-list))
735+
(-when-let (proj (with-current-buffer b
736+
(and (bound-and-true-p tide-mode)
737+
(tide-project-name))))
738+
(cl-pushnew proj live-projects)))
739+
(dolist (proj (-difference (hash-table-keys tide-servers) live-projects))
740+
(tide-cleanup-project proj))))
741+
732742
(defun tide-start-server-if-required ()
733743
(unless (tide-current-server)
734744
(tide-start-server)))
@@ -1953,15 +1963,19 @@ code-analysis."
19531963
(progn
19541964
(add-hook 'after-save-hook 'tide-sync-buffer-contents nil t)
19551965
(add-hook 'after-save-hook 'tide-auto-compile-file nil t)
1966+
(add-hook 'after-save-hook 'tide-remove-tmp-file nil t)
19561967
(add-hook 'after-change-functions 'tide-handle-change nil t)
19571968
(add-hook 'kill-buffer-hook 'tide-cleanup-buffer nil t)
1969+
(add-hook 'kill-buffer-hook 'tide-cleanup-dead-projects nil t)
19581970
(add-hook 'hack-local-variables-hook 'tide-configure-buffer nil t)
19591971
(when (commandp 'typescript-insert-and-indent)
19601972
(eldoc-add-command 'typescript-insert-and-indent)))
19611973
(remove-hook 'after-save-hook 'tide-sync-buffer-contents t)
19621974
(remove-hook 'after-save-hook 'tide-auto-compile-file t)
1975+
(remove-hook 'after-save-hook 'tide-remove-tmp-file t)
19631976
(remove-hook 'after-change-functions 'tide-handle-change t)
19641977
(remove-hook 'kill-buffer-hook 'tide-cleanup-buffer t)
1978+
(remove-hook 'kill-buffer-hook 'tide-cleanup-dead-projects t)
19651979
(remove-hook 'hack-local-variables-hook 'tide-configure-buffer t)
19661980
(tide-cleanup-buffer)))
19671981

0 commit comments

Comments
 (0)