Skip to content

Commit

Permalink
Merge pull request #722 from liuyinz/feat/add-diagnostic-var
Browse files Browse the repository at this point in the history
provide diagnostic interface for flymake/flycheck frontend
  • Loading branch information
manateelazycat authored Oct 5, 2023
2 parents c97bd84 + e68086d commit 78c2e9a
Showing 1 changed file with 49 additions and 31 deletions.
80 changes: 49 additions & 31 deletions lsp-bridge-diagnostic.el
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,17 @@
:type 'float
:group 'lsp-bridge)

(defcustom lsp-bridge-diagnostic-enable-overlays t
"If non-nil, rendering overlays in lsp-bridge-diagnostic byself.
Set to nil if flymake/flycheck frontend is used instead."
:type 'boolean
:group 'lsp-bridge)

(defcustom lsp-bridge-diagnostic-update-hook '()
"The hook for diagnostics updates."
:type 'hook
:group 'lsp-bridge)

(defface lsp-bridge-diagnostics-error-face
'((t (:underline (:style wave :color "Red1"))))
"Face error diagnostic."
Expand Down Expand Up @@ -149,6 +160,8 @@ You can set this value with `(2 3 4) if you just need render error diagnostic."
:type 'list
:group 'lsp-bridge)

(defvar-local lsp-bridge-diagnostic-records nil)

(defvar-local lsp-bridge-diagnostic-overlays '())

(defvar lsp-bridge-diagnostic-frame nil)
Expand All @@ -169,37 +182,42 @@ You can set this value with `(2 3 4) if you just need render error diagnostic."

(defun lsp-bridge-diagnostic--render (filepath filehost diagnostics)
(lsp-bridge--with-file-buffer filepath filehost
(lsp-bridge-diagnostic-hide-overlays)

(let ((diagnostic-index 0)
(diagnostic-number (length diagnostics)))
(dolist (diagnostic diagnostics)
(let ((severity (plist-get diagnostic :severity)))
(unless (member severity lsp-bridge-diagnostic-hide-severities)
(let* ((diagnostic-start (acm-backend-lsp-position-to-point (plist-get (plist-get diagnostic :range) :start)))
(diagnostic-end (acm-backend-lsp-position-to-point (plist-get (plist-get diagnostic :range) :end)))
(overlay (if (eq diagnostic-start diagnostic-end)
;; Adjust diagnostic end position if start and end is same position.
(make-overlay diagnostic-start (1+ diagnostic-start))
(make-overlay diagnostic-start diagnostic-end)))
(message (plist-get diagnostic :message))
(overlay-face (cl-case severity
(1 'lsp-bridge-diagnostics-error-face)
(2 'lsp-bridge-diagnostics-warning-face)
(3 'lsp-bridge-diagnostics-info-face)
(4 'lsp-bridge-diagnostics-hint-face))))
(overlay-put overlay 'color (plist-get (face-attribute overlay-face :underline) :color))
(overlay-put overlay 'face overlay-face)
(overlay-put overlay 'message message)
(overlay-put overlay
'display-message
(if (> diagnostic-number 1)
(format "[%s:%s] %s" (1+ diagnostic-index) diagnostic-number message)
message))
(push overlay lsp-bridge-diagnostic-overlays))))

(setq diagnostic-index (1+ diagnostic-index))))
(setq-local lsp-bridge-diagnostic-overlays (reverse lsp-bridge-diagnostic-overlays))))
(unless lsp-bridge-diagnostic-enable-overlays
(setq-local lsp-bridge-diagnostic-records diagnostics))

(run-hooks 'lsp-bridge-diagnostic-update-hook)

(when lsp-bridge-diagnostic-enable-overlays
(lsp-bridge-diagnostic-hide-overlays)
(let ((diagnostic-index 0)
(diagnostic-number (length diagnostics)))
(dolist (diagnostic diagnostics)
(let ((severity (plist-get diagnostic :severity)))
(unless (member severity lsp-bridge-diagnostic-hide-severities)
(let* ((diagnostic-start (acm-backend-lsp-position-to-point (plist-get (plist-get diagnostic :range) :start)))
(diagnostic-end (acm-backend-lsp-position-to-point (plist-get (plist-get diagnostic :range) :end)))
(overlay (if (eq diagnostic-start diagnostic-end)
;; Adjust diagnostic end position if start and end is same position.
(make-overlay diagnostic-start (1+ diagnostic-start))
(make-overlay diagnostic-start diagnostic-end)))
(message (plist-get diagnostic :message))
(overlay-face (cl-case severity
(1 'lsp-bridge-diagnostics-error-face)
(2 'lsp-bridge-diagnostics-warning-face)
(3 'lsp-bridge-diagnostics-info-face)
(4 'lsp-bridge-diagnostics-hint-face))))
(overlay-put overlay 'color (plist-get (face-attribute overlay-face :underline) :color))
(overlay-put overlay 'face overlay-face)
(overlay-put overlay 'message message)
(overlay-put overlay
'display-message
(if (> diagnostic-number 1)
(format "[%s:%s] %s" (1+ diagnostic-index) diagnostic-number message)
message))
(push overlay lsp-bridge-diagnostic-overlays))))

(setq diagnostic-index (1+ diagnostic-index))))
(setq-local lsp-bridge-diagnostic-overlays (reverse lsp-bridge-diagnostic-overlays)))))

(defvar lsp-bridge-diagnostic-frame nil)

Expand Down

0 comments on commit 78c2e9a

Please sign in to comment.