Skip to content

Commit

Permalink
Check lsp-bridge--not-acm-doc-markdown-buffer with lsp-bridge-default…
Browse files Browse the repository at this point in the history
…-mode-hooks
  • Loading branch information
manateelazycat committed Jun 30, 2023
1 parent 8532cef commit 42ac9d5
Showing 1 changed file with 75 additions and 74 deletions.
149 changes: 75 additions & 74 deletions lsp-bridge.el
Original file line number Diff line number Diff line change
Expand Up @@ -1228,32 +1228,25 @@ So we build this macro to restore postion after code format."
(defvar-local lsp-bridge--before-change-begin-pos nil)
(defvar-local lsp-bridge--before-change-end-pos nil)

(defun lsp-bridge--not-acm-doc-markdown-buffer ()
"`markdown-code-fontification:*' buffer will create when we render markdown code in acm doc buffer.
We need exclude `markdown-code-fontification:*' buffer in `lsp-bridge-monitor-before-change' and `lsp-bridge-monitor-after-change'."
(not (string-prefix-p " markdown-code-fontification:" (buffer-name (current-buffer)))))

(defun lsp-bridge-monitor-before-change (begin end)
(when (lsp-bridge--not-acm-doc-markdown-buffer)
;; Use `save-match-data' protect match data, avoid conflict with command call `search-regexp'.
(save-match-data
(when (lsp-bridge-has-lsp-server-p)
;; send whole org src block to lsp server
(when (and lsp-bridge-enable-org-babel (eq major-mode 'org-mode)
lsp-bridge-org-babel--block-bop
lsp-bridge-org-babel--update-file-before-change)
(setq-local lsp-bridge-org-babel--update-file-before-change nil)
(lsp-bridge-call-file-api "update_file" (buffer-name)
(1- (line-number-at-pos lsp-bridge-org-babel--block-bop t)))))

;; Set `lsp-bridge--before-change-begin-pos' and `lsp-bridge--before-change-end-pos'
;; if `lsp-bridge-has-lsp-server-p' or `lsp-bridge-is-remote-file'
(when (or (lsp-bridge-has-lsp-server-p)
(lsp-bridge-is-remote-file))
(setq-local lsp-bridge--before-change-begin-pos (lsp-bridge--point-position begin))
(setq-local lsp-bridge--before-change-end-pos (lsp-bridge--point-position end))
))))
;; Use `save-match-data' protect match data, avoid conflict with command call `search-regexp'.
(save-match-data
(when (lsp-bridge-has-lsp-server-p)
;; send whole org src block to lsp server
(when (and lsp-bridge-enable-org-babel (eq major-mode 'org-mode)
lsp-bridge-org-babel--block-bop
lsp-bridge-org-babel--update-file-before-change)
(setq-local lsp-bridge-org-babel--update-file-before-change nil)
(lsp-bridge-call-file-api "update_file" (buffer-name)
(1- (line-number-at-pos lsp-bridge-org-babel--block-bop t)))))

;; Set `lsp-bridge--before-change-begin-pos' and `lsp-bridge--before-change-end-pos'
;; if `lsp-bridge-has-lsp-server-p' or `lsp-bridge-is-remote-file'
(when (or (lsp-bridge-has-lsp-server-p)
(lsp-bridge-is-remote-file))
(setq-local lsp-bridge--before-change-begin-pos (lsp-bridge--point-position begin))
(setq-local lsp-bridge--before-change-end-pos (lsp-bridge--point-position end))
)))

(defun lsp-bridge-monitor-post-self-insert ()
;; Make sure this function be called after `electric-pair-mode'
Expand All @@ -1278,49 +1271,48 @@ We need exclude `markdown-code-fontification:*' buffer in `lsp-bridge-monitor-be
(list (current-buffer) (buffer-chars-modified-tick) (point))))

(defun lsp-bridge-monitor-after-change (begin end length)
(when (lsp-bridge--not-acm-doc-markdown-buffer)
;; When user do `delete' operation, `length' is bigger than 0,
;; and the string between `begin' and `end' must be empty.
;; if string is not empty, something wrong, we should not send any request to completion backend.
(unless (and (> length 0)
(> (length (buffer-substring-no-properties begin end)) 0))
;; Use `save-match-data' protect match data, avoid conflict with command call `search-regexp'.
(save-match-data
(unless lsp-bridge-revert-buffer-flag
;; Record last command to `lsp-bridge-last-change-command'.
(setq lsp-bridge-last-change-command (format "%s" this-command))

;; Record last change position to avoid popup outdate completions.
(setq lsp-bridge-last-change-position (list (current-buffer) (buffer-chars-modified-tick) (point)))

;; Set `lsp-bridge-last-change-is-delete-command-p'
(setq lsp-bridge-last-change-is-delete-command-p (> length 0))

;; Sync change for org babel if we enable it
(lsp-bridge-org-babel-monitor-after-change begin end length)

;; Send change_file request to trigger LSP completion.
(when (or (lsp-bridge-call-file-api-p)
(lsp-bridge-is-remote-file))
(lsp-bridge-call-file-api "change_file"
lsp-bridge--before-change-begin-pos
lsp-bridge--before-change-end-pos
length
(buffer-substring-no-properties begin end)
(lsp-bridge--position)
(acm-char-before)
(buffer-name)
(acm-get-input-prefix)))


;; Complete other non-LSP backends.
(lsp-bridge-complete-other-backends)

;; Update search words backend.
(lsp-bridge-search-words-update
lsp-bridge--before-change-begin-pos
lsp-bridge--before-change-end-pos
(buffer-substring-no-properties begin end)))))))
;; When user do `delete' operation, `length' is bigger than 0,
;; and the string between `begin' and `end' must be empty.
;; if string is not empty, something wrong, we should not send any request to completion backend.
(unless (and (> length 0)
(> (length (buffer-substring-no-properties begin end)) 0))
;; Use `save-match-data' protect match data, avoid conflict with command call `search-regexp'.
(save-match-data
(unless lsp-bridge-revert-buffer-flag
;; Record last command to `lsp-bridge-last-change-command'.
(setq lsp-bridge-last-change-command (format "%s" this-command))

;; Record last change position to avoid popup outdate completions.
(setq lsp-bridge-last-change-position (list (current-buffer) (buffer-chars-modified-tick) (point)))

;; Set `lsp-bridge-last-change-is-delete-command-p'
(setq lsp-bridge-last-change-is-delete-command-p (> length 0))

;; Sync change for org babel if we enable it
(lsp-bridge-org-babel-monitor-after-change begin end length)

;; Send change_file request to trigger LSP completion.
(when (or (lsp-bridge-call-file-api-p)
(lsp-bridge-is-remote-file))
(lsp-bridge-call-file-api "change_file"
lsp-bridge--before-change-begin-pos
lsp-bridge--before-change-end-pos
length
(buffer-substring-no-properties begin end)
(lsp-bridge--position)
(acm-char-before)
(buffer-name)
(acm-get-input-prefix)))


;; Complete other non-LSP backends.
(lsp-bridge-complete-other-backends)

;; Update search words backend.
(lsp-bridge-search-words-update
lsp-bridge--before-change-begin-pos
lsp-bridge--before-change-end-pos
(buffer-substring-no-properties begin end))))))

(defun lsp-bridge-complete-other-backends ()
(let ((this-command-string (format "%s" this-command)))
Expand Down Expand Up @@ -2066,18 +2058,27 @@ SymbolKind (defined in the LSP)."
(message "Turn on SDCV helper."))
(setq-local acm-enable-search-sdcv-words (not acm-enable-search-sdcv-words)))

(defun lsp-bridge--not-acm-doc-markdown-buffer ()
"`markdown-code-fontification:*' buffer will create when we render markdown code in acm doc buffer.
We need exclude `markdown-code-fontification:*' buffer in `lsp-bridge-monitor-before-change' and `lsp-bridge-monitor-after-change'."
(not (string-prefix-p " markdown-code-fontification:" (buffer-name (current-buffer)))))

(defun lsp-bridge--not-mind-wave-chat-buffer ()
"Not mind-wave's `*.chat' buffer."
(or
(not (buffer-file-name))
(not (string-equal (file-name-extension (buffer-file-name)) "chat"))))

;;;###autoload
(defun global-lsp-bridge-mode ()
(interactive)

(dolist (hook lsp-bridge-default-mode-hooks)
(add-hook hook (lambda ()
;; Except `mind-wave-chat-mode'.
(when (or
(not (buffer-file-name))
(not (string-equal (file-name-extension (buffer-file-name)) "chat")))
(lsp-bridge-mode 1))
))))
(when (and (lsp-bridge--not-mind-wave-chat-buffer)
(lsp-bridge--not-acm-doc-markdown-buffer))
(lsp-bridge-mode 1))))))

(with-eval-after-load 'evil
(evil-add-command-properties #'lsp-bridge-find-def :jump t)
Expand Down

0 comments on commit 42ac9d5

Please sign in to comment.