Skip to content

Commit 792b9df

Browse files
committed
Make offset scaling optional to fix test in batch mode
1 parent 93c7974 commit 792b9df

File tree

2 files changed

+51
-23
lines changed

2 files changed

+51
-23
lines changed

lsp-sonarlint.el

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -585,17 +585,39 @@ MESSAGES-WITH-OFFSETS must be sorted."
585585
msg-with-offset))
586586
messages-with-offsets)))
587587

588+
(defcustom lsp-sonarlint--scale-inline-msg-offset t
589+
"Whether to scale the offset for inline messages (code-lens style).
590+
591+
Usually these messages (including their whitespace offset) are
592+
printed with smaller font, so they need adjustment to account for
593+
smaller size of the space character.
594+
595+
- Set to nil if it causes problems.
596+
- Set to a floating-point number if you want to adjust this factor.
597+
- If t it will deduce the scaling factor
598+
from the `lsp-sonarlint-embedded-msg-face' height."
599+
:type '(choice
600+
(const :tag "Disable" nil)
601+
(integer :tag "Factor")
602+
(const :tag "Auto" t)))
603+
604+
(defun lsp-sonarlint--scale-offset (offset)
605+
"Adjust OFFSET preserving column position with smaller font."
606+
(if lsp-sonarlint--scale-inline-msg-offset
607+
(if (numberp lsp-sonarlint--scale-inline-msg-offset)
608+
(floor (* lsp-sonarlint--scale-inline-msg-offset offset))
609+
(let ((msg-height (face-attribute 'lsp-sonarlint-embedded-msg-face :height nil 'default))
610+
(default-height (face-attribute 'default :height)))
611+
(/ (* offset
612+
default-height)
613+
msg-height)))
614+
offset))
588615

589616
(defun lsp-sonarlint--scale-msg-lens-offset (msg-with-offset)
590617
"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))
618+
(setf (plist-get msg-with-offset :offset)
619+
(lsp-sonarlint--scale-offset (plist-get msg-with-offset :offset)))
620+
msg-with-offset)
599621

600622
(defun lsp-sonarlint--process-offsets (messages-with-offsets)
601623
"Sort, deduplicate, adjust, and combine MESSAGES-WITH-OFFSETS.

test/lsp-sonarlint-secondary-locations-test.el

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ SonarLint LSP server."
168168
(+ to (line-beginning-position))
169169
(current-buffer)))))
170170

171-
(ert-deftest lsp-sonarlint-test--overlays-string ()
171+
(ert-deftest lsp-sonarlint-test--overlay-strings ()
172172
"Test `lsp-sonarlint-test--buf-string-with-overlay-strings' on corner cases"
173173
(with-temp-buffer
174174
(unwind-protect
@@ -212,7 +212,8 @@ next str
212212

213213
(ert-deftest lsp-sonarlint-test--display-secondary-messages ()
214214
"Test that secondary locations are displayed correctly."
215-
(let ((target-file-buf (find-file-noselect lsp-sonarlint-test--file-path)))
215+
(let ((target-file-buf (find-file-noselect lsp-sonarlint-test--file-path))
216+
(lsp-sonarlint--scale-inline-msg-offset nil))
216217
(with-current-buffer target-file-buf
217218
(let* ((primary-range (lsp-sonarlint-test-range-make
218219
(buffer-string)
@@ -243,10 +244,10 @@ next str
243244
int divide_seventeen(int param) {
244245
Redundant branching
245246
if (param == 0) {
246-
Identical code
247+
Identical code
247248
1int a = 0;
248249
} else {
249-
Identical code
250+
Identical code
250251
2int b = 0;
251252
}
252253
return 10 / param;
@@ -265,7 +266,8 @@ int divide_seventeen(int param) {
265266

266267
(ert-deftest lsp-sonarlint-test--navigate-to-sec-location ()
267268
"Test that point moves to locations of selected messages."
268-
(let ((target-file-buf (find-file-noselect lsp-sonarlint-test--file-path)))
269+
(let ((target-file-buf (find-file-noselect lsp-sonarlint-test--file-path))
270+
(lsp-sonarlint--scale-inline-msg-offset nil))
269271
(with-current-buffer target-file-buf
270272
(let* ((primary-range (lsp-sonarlint-test-range-make
271273
(buffer-string)
@@ -300,7 +302,8 @@ int divide_seventeen(int param) {
300302

301303
(ert-deftest lsp-sonarlint-test--display-execution-flow ()
302304
"Test that flow steps are displayed correctly and in order."
303-
(let ((target-file-buf (find-file-noselect lsp-sonarlint-test--file-path)))
305+
(let ((target-file-buf (find-file-noselect lsp-sonarlint-test--file-path))
306+
(lsp-sonarlint--scale-inline-msg-offset nil))
304307
(with-current-buffer target-file-buf
305308
(let* ((primary-range (lsp-sonarlint-test-range-make
306309
(buffer-string)
@@ -352,15 +355,15 @@ int divide_seventeen(int param) {
352355
"
353356
int divide_seventeen(int param) {
354357
Taking true branch
355-
Evaluating condition
358+
Evaluating condition
356359
3if (1param 2== 0) {
357-
Assuming param is 0
358-
Assigning a 0
360+
Assuming param is 0
361+
Assigning a 0
359362
int 4a = 0;
360363
} else {
361364
int b = 0;
362365
}
363-
Division by 0
366+
Division by 0
364367
return 10 5/ param;
365368
}
366369
")))))
@@ -380,7 +383,8 @@ Some long line with words clearly separated
380383
`(:message "first"
381384
:overlay ,(lsp-sonarlint-test--place-overlay
382385
"Some long line with words clearly separated"
383-
"^^^^ ")))))
386+
"^^^^ "))))
387+
(lsp-sonarlint--scale-inline-msg-offset nil))
384388
(unwind-protect
385389
(progn
386390
(lsp-sonarlint--add-inline-messages locations)
@@ -406,7 +410,8 @@ Some long line with words clearly separated
406410
`(:message "second"
407411
:overlay ,(lsp-sonarlint-test--place-overlay
408412
"Some long line with words clearly separated"
409-
"^^^^ ")))))
413+
"^^^^ "))))
414+
(lsp-sonarlint--scale-inline-msg-offset nil))
410415
(unwind-protect
411416
(progn
412417
(lsp-sonarlint--add-inline-messages locations)
@@ -441,15 +446,16 @@ Some long line with words clearly separated
441446
`(:message "fourth"
442447
:overlay ,(lsp-sonarlint-test--place-overlay
443448
"Some long line with words clearly separated"
444-
" ^ ")))))
449+
" ^ "))))
450+
(lsp-sonarlint--scale-inline-msg-offset nil))
445451
(unwind-protect
446452
(progn
447453
(lsp-sonarlint--add-inline-messages locations)
448454
(should (equal (lsp-sonarlint-test--buf-string-with-overlay-strings)
449455
"
450-
first fourth
456+
first fourth
451457
Some long line with words clearly separated
452-
third second
458+
third second
453459
")))
454460
(remove-overlays)))))
455461

0 commit comments

Comments
 (0)