Skip to content

Commit 6a9e490

Browse files
committed
Adjust inline messages offests properly
1 parent f1e0fd8 commit 6a9e490

File tree

1 file changed

+55
-26
lines changed

1 file changed

+55
-26
lines changed

lsp-sonarlint.el

Lines changed: 55 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -398,14 +398,6 @@ BEGIN-END-POSITIONS is a plist with :begin and :end positions."
398398
(goto-char pos)
399399
(current-column)))
400400

401-
(defun lsp-sonarlint--secondary-msg-lens-offset (position)
402-
"Compute and return number of characters to align message with POSITION."
403-
(let* ((msg-height (face-attribute 'lsp-sonarlint-embedded-msg-face :height nil 'default))
404-
(default-height (face-attribute 'default :height)))
405-
(1+ (/ (* (lsp-sonarlint--get-column position)
406-
default-height)
407-
msg-height))))
408-
409401
(defun lsp-sonarlint--procure-overlays-for-secondary-locations (flows)
410402
"Create overlays for secondary locations in FLOWS.
411403
@@ -532,21 +524,19 @@ pointing to the `:overlay' from LOC-MESSAGE."
532524
(let ((line-to-msg (make-hash-table :test #'equal)))
533525
(mapc (lambda (location)
534526
(let* ((precise-overlay (plist-get location :overlay))
535-
(message-offset (lsp-sonarlint--secondary-msg-lens-offset
536-
(overlay-start precise-overlay)))
537-
(message (plist-get location :message))
527+
(message-offset (lsp-sonarlint--get-column (overlay-start precise-overlay)))
538528
(line (line-number-at-pos (overlay-start precise-overlay))))
539-
(push `(:message ,message :offset ,message-offset)
529+
(push `(:offset ,message-offset ,@location)
540530
(gethash line line-to-msg))))
541531
locations)
542532
line-to-msg))
543533

544-
(defun lsp-sonarlint--deduplicate (sorted-list)
545-
"Remove duplicate elements from sorted SORTED-LIST."
534+
(defun lsp-sonarlint--deduplicate (sorted-list test)
535+
"Remove duplicate elements (according to TEST) from sorted SORTED-LIST."
546536
(let ((result '())
547537
(last-element nil))
548538
(dolist (element sorted-list (nreverse result))
549-
(unless (equal element last-element)
539+
(unless (funcall test element last-element)
550540
(push element result)
551541
(setq last-element element)))))
552542

@@ -572,24 +562,63 @@ MESSAGES-WITH-OFFSETS must be sorted by offset."
572562
(pop reversed)
573563
(push msg-off result)))))))
574564

565+
(defun lsp-sonarlint--count-digits (num)
566+
"Count digits in decimal representation of the NUM integer."
567+
(length (number-to-string num)))
568+
569+
(defun lsp-sonarlint--adjust-offsets (messages-with-offsets)
570+
"Shift offsets in MESSAGES-WITH-OFFSETS to account for number labels.
571+
572+
MESSAGES-WITH-OFFSETS must be sorted."
573+
(let ((accumulated-adjustment 0))
574+
(mapcar (lambda (msg-with-offset)
575+
(let ((increment
576+
(if-let ((step-num (plist-get
577+
msg-with-offset
578+
:step-num)))
579+
(lsp-sonarlint--count-digits step-num)
580+
0)))
581+
(setq accumulated-adjustment (+ accumulated-adjustment increment))
582+
(setf (plist-get msg-with-offset :offset)
583+
(+ accumulated-adjustment
584+
(plist-get msg-with-offset :offset)))
585+
msg-with-offset))
586+
messages-with-offsets)))
587+
588+
589+
(defun lsp-sonarlint--scale-msg-lens-offset (msg-with-offset)
590+
"Adjust offset in MSG-WITH-OFFSET preserving column with smaller font."
591+
(let* ((original-offset (plist-get msg-with-offset :offset))
592+
(msg-height (face-attribute 'lsp-sonarlint-embedded-msg-face :height nil 'default))
593+
(default-height (face-attribute 'default :height)))
594+
(setf (plist-get msg-with-offset :offset)
595+
(/ (* original-offset
596+
default-height)
597+
msg-height))
598+
msg-with-offset))
599+
575600
(defun lsp-sonarlint--process-offsets (messages-with-offsets)
576601
"Sort, deduplicate, adjust, and combine MESSAGES-WITH-OFFSETS.
577602
578603
Sort them in increasing order, remove duplicate messages with identical offsets,
579-
adjust offsets to account for the number labels prepended to each location."
604+
adjust offsets to account for the number labels prepended to each location,
605+
and combine non-overlapping messages toreduce the number of lines."
580606
(let* ((sorted (sort messages-with-offsets (lambda (msg-off1 msg-off2)
581607
(< (plist-get msg-off1 :offset)
582608
(plist-get msg-off2 :offset)))))
583-
(deduplicated (lsp-sonarlint--deduplicate sorted))
584-
(accumulated-adjustment 0)
585-
(adjusted (mapcar (lambda (msg-with-offset)
586-
(setf (plist-get msg-with-offset :offset)
587-
(+ accumulated-adjustment
588-
(plist-get msg-with-offset :offset)))
589-
(setq accumulated-adjustment 1)
590-
msg-with-offset)
591-
deduplicated)))
592-
(lsp-sonarlint--combine adjusted)))
609+
(deduplicated (lsp-sonarlint--deduplicate
610+
sorted
611+
(lambda (msg-off1 msg-off2) (and (equal (plist-get msg-off1 :offset)
612+
(plist-get msg-off2 :offset))
613+
(equal (plist-get msg-off1 :message)
614+
(plist-get msg-off2 :message))))))
615+
(adjusted (lsp-sonarlint--adjust-offsets deduplicated))
616+
;; Should scale after adjusting, because adjustment is done
617+
;; in terms of the default font
618+
(scaled (mapcar #'lsp-sonarlint--scale-msg-lens-offset adjusted)))
619+
;; Should combine after scaling to also scale the potential gaps between
620+
;; combined messages properly
621+
(lsp-sonarlint--combine scaled)))
593622

594623
(defun lsp-sonarlint--concat-msg-lines (msg-offsets)
595624
"Combine the list of MSG-OFFSETS into a single string."

0 commit comments

Comments
 (0)