Skip to content

Commit

Permalink
When user do delete' operation, length' is bigger than 0, and the s…
Browse files Browse the repository at this point in the history
…tring between `begin' and `end' must be empty. if string is not empty, something wrong, we should not send any request to completion backend.
  • Loading branch information
manateelazycat committed Jun 29, 2023
1 parent 0b01f18 commit e13ba84
Showing 1 changed file with 66 additions and 52 deletions.
118 changes: 66 additions & 52 deletions lsp-bridge.el
Original file line number Diff line number Diff line change
Expand Up @@ -1228,24 +1228,32 @@ 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)
;; 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)))))
(when (lsp-bridge--not-acm-doc-markdown-buffer)
(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 @@ -1271,42 +1279,48 @@ So we build this macro to restore postion after code format."

(defun lsp-bridge-monitor-after-change (begin end length)
;; 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 (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))
(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

0 comments on commit e13ba84

Please sign in to comment.