From 2aa37ecb8b36917268ceb4cb7cd13f194aa4d245 Mon Sep 17 00:00:00 2001 From: Eval EXEC Date: Sat, 24 May 2025 17:12:41 +0800 Subject: [PATCH] Improve lsp--buffer-string-visible performance, avoide headvy generate-new-buffer Signed-off-by: Eval EXEC --- CHANGELOG.org | 1 + lsp-mode.el | 32 +++++++++++++++----------------- 2 files changed, 16 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.org b/CHANGELOG.org index f2901974bf..3fca764e07 100644 --- a/CHANGELOG.org +++ b/CHANGELOG.org @@ -1,5 +1,6 @@ * Changelog ** Unreleased 9.0.1 + * Improve lsp--buffer-string-visible performance, avoide headvy generate-new-buffer * Add format on save support * Fix beancount journal file init option * Add support for [[https://github.com/glehmann/earthlyls][earthlyls]] diff --git a/lsp-mode.el b/lsp-mode.el index e5ccdd0a9f..452e58d5c2 100644 --- a/lsp-mode.el +++ b/lsp-mode.el @@ -5554,23 +5554,21 @@ MODE is the mode used in the parent frame." (goto-char end)))))) (defun lsp--buffer-string-visible () - "Return visible buffer string. -Stolen from `org-copy-visible'." - (let ((temp (generate-new-buffer " *temp*")) - (beg (point-min)) - (end (point-max))) - (while (/= beg end) - (when (get-char-property beg 'invisible) - (setq beg (next-single-char-property-change beg 'invisible nil end))) - (let* ((next (next-single-char-property-change beg 'invisible nil end)) - (substring (buffer-substring beg next))) - (with-current-buffer temp (insert substring)) - ;; (setq result (concat result substring)) - (setq beg next))) - (setq deactivate-mark t) - (prog1 (with-current-buffer temp - (s-chop-suffix "\n" (buffer-string))) - (kill-buffer temp)))) + "Return a string of the buffer's visible text." + (let ((beg (point-min)) + (end (point-max)) + (parts '())) + (while (< beg end) + (when (get-char-property beg 'invisible) + (setq beg (next-single-char-property-change beg 'invisible nil end))) + (let ((next (next-single-char-property-change beg 'invisible nil end))) + (push (buffer-substring-no-properties beg next) parts) + (setq beg next))) + (setq parts (nreverse parts)) + (let ((result (apply #'concat parts))) + (if (string-suffix-p "\n" result) + (substring result 0 -1) + result)))) (defvar lsp-buffer-major-mode nil "Holds the major mode when fontification function is running.