Merge completion results so :isIncomplete is preserved (#1937)

- without this change lsp-completion-list? may return false for the stuff that
is returned by merge method
pull/1959/head
Ivan Yonchovski 2020-07-23 16:44:45 +03:00 committed by GitHub
parent a94f6f8618
commit 1692d9f31d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 108 additions and 53 deletions

View File

@ -107,21 +107,24 @@ Example usage with `dash`.
(plist-member object prop))
',required))))
`(cl-defun ,(intern (format "lsp-make-%s" (s-dashed-words (symbol-name interface))))
(&key ,@(-map (-lambda ((key))
(&rest plist &key ,@(-map (-lambda ((key))
(intern (substring (symbol-name key) 1))) params)
&allow-other-keys)
(ignore ,@(-map (-lambda ((key))
(intern (substring (symbol-name key) 1))) params))
,(format "Constructs %s from `plist.'
Allowed params: %s" interface (reverse (-map #'cl-first params)))
,(if lsp-use-plists
`(let ($$result)
,@(-map (-lambda ((name . key))
`(when ,(lsp-keyword->symbol name)
(setq $$result (plist-put $$result ,key ,(lsp-keyword->symbol name)))))
params)
$$result)
`(-mapcat (-lambda ((key value))
(list (or (cl-rest (assoc key ',params)) key) value))
(-partition 2 plist))
`(let (($$result (ht)))
,@(-map (-lambda ((name . key))
`(when ,(lsp-keyword->symbol name)
(puthash ,(lsp-keyword->string key) ,(lsp-keyword->symbol name) $$result)))
params)
(mapc (-lambda ((key value))
(puthash (lsp-keyword->string (or (cl-rest (assoc key ',params))
key))
value
$$result))
(-partition 2 plist))
$$result)))
(-mapcat (-lambda ((label . name))
(list
@ -136,9 +139,10 @@ Example usage with `dash`.
(s-dashed-words (symbol-name interface))
(substring (symbol-name label) 1)))
(object value)
,(if lsp-use-plists
`(plist-put object ,name value)
`(puthash ,(lsp-keyword->string name) value object)))))
,@(if lsp-use-plists
`((plist-put object ,name value))
`((puthash ,(lsp-keyword->string name) value object)
object)))))
params)))))
(apply #'append)
(cl-list* 'progn)))

View File

@ -122,8 +122,7 @@
'clangd)
"void function(int n);"))
(should-error (lsp-clients-extract-signature-on-hover
(lsp-make-markup-content :kind nil
:value "Wrong")
(lsp-make-markup-content :value "Wrong")
'clangd)))
(ert-deftest lsp-clients-join-region ()

View File

@ -158,38 +158,4 @@
(should (lsp-f-ancestor-of? "test\\tmp" "test/tmp/a"))
(should-not (lsp-f-ancestor-of? "test\\tmp" "test\\tmp-a"))))
(defun lsp--json-string-equal? (str1 str2)
"Roughly compare json string STR1 and STR2."
(equal (sort (split-string str1 "[:\f\t\n\r\v{},]+") #'string-lessp)
(sort (split-string str2 "[:\f\t\n\r\v{},]+") #'string-lessp)))
(ert-deftest lsp--merge-results ()
(should (lsp--json-string-equal?
(json-encode (lsp--merge-results
`(,(lsp-make-completion-item
:label "1"
:documentation? "bcd"
:detail? "b"
:additional-text-edits?
(lsp-make-text-edit :new-text "a"
:range (lsp-make-range :start 10 :end 11)))
,(lsp-make-completion-item
:label "1"
:documentation? (lsp-make-markup-content :kind lsp/markup-kind-markdown
:value "xyz")
:detail? "cd"
:additional-text-edits?
(lsp-make-text-edit :new-text "b"
:range (lsp-make-range :start 13 :end 15)))
,(lsp-make-completion-item
:label "1"))
"completionItem/resolve"))
"{\"additionalTextEdits\":[{\"newText\":\"a\",\"range\":{\"start\":10,\"end\":11}},{\"newText\":\"b\",\"range\":{\"start\":13,\"end\":15}}],\"detail\":\"b cd\",\"documentation\":{\"kind\":\"markdown\",\"value\":\"bcd\\nxyz\"},\"label\":\"1\"}"))
(should (lsp--json-string-equal?
(lsp--json-serialize (lsp--merge-results
`(,(lsp-make-completion-item :label "1")
,(lsp-make-completion-item :label "1"))
"completionItem/resolve"))
"{\"label\":\"1\"}")))
;;; lsp-common-test.el ends here

78
test/lsp-mode-test.el Normal file
View File

@ -0,0 +1,78 @@
;;; lsp-mode-test.el --- unit tests for lsp-mode.el -*- lexical-binding: t; -*-
;; Copyright (C) 2020 Ivan Yonchovski
;; Author: Ivan Yonchovski <yyoncho@gmail.com>
;; Keywords:
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;;
;;; Code:
(require 'lsp-mode)
(require 'ert)
(ert-deftest lsp-mode-test-merging-completion-results ()
(let ((merged-completions (lsp--merge-results
`([,(lsp-make-completion-item :label "$any"
:kind? 2
:detail? "method"
:sort-text? "$any")]
,(lsp:set-completion-list-is-incomplete (lsp-make-completion-list :items [])
nil))
"textDocument/completion")))
(should (lsp-completion-list? merged-completions))
(should (lsp--sort-completions (lsp:completion-list-items merged-completions)))))
(defun lsp--json-string-equal? (str1 str2)
"Roughly compare json string STR1 and STR2."
(equal (sort (split-string str1 "[:\f\t\n\r\v{},]+") #'string-lessp)
(sort (split-string str2 "[:\f\t\n\r\v{},]+") #'string-lessp)))
(ert-deftest lsp--merge-results ()
(should (lsp--json-string-equal?
(json-encode (lsp--merge-results
`(,(lsp-make-completion-item
:label "1"
:documentation? "bcd"
:detail? "b"
:additional-text-edits?
(lsp-make-text-edit :new-text "a"
:range (lsp-make-range :start 10 :end 11)))
,(lsp-make-completion-item
:label "1"
:documentation? (lsp-make-markup-content :kind lsp/markup-kind-markdown
:value "xyz")
:detail? "cd"
:additional-text-edits?
(lsp-make-text-edit :new-text "b"
:range (lsp-make-range :start 13 :end 15)))
,(lsp-make-completion-item
:label "1"))
"completionItem/resolve"))
"{\"additionalTextEdits\":[{\"newText\":\"a\",\"range\":{\"start\":10,\"end\":11}},{\"newText\":\"b\",\"range\":{\"start\":13,\"end\":15}}],\"detail\":\"b cd\",\"documentation\":{\"kind\":\"markdown\",\"value\":\"bcd\\nxyz\"},\"label\":\"1\"}"))
(should (lsp--json-string-equal?
(lsp--json-serialize (lsp--merge-results
`(,(lsp-make-completion-item :label "1")
,(lsp-make-completion-item :label "1"))
"completionItem/resolve"))
"{\"label\":\"1\"}")))
(provide 'lsp-mode-test)
;;; lsp-mode-test.el ends here

View File

@ -20,7 +20,7 @@
;;; Commentary:
;;
;; Tests for lsp-protocol.
;;; Code:
@ -55,7 +55,15 @@
(should (lsp-my-position? position))
(-let (((&MyPosition? :line) nil))
(should (null line)))))
(should (null line)))
(should (= (-> (lsp-make-my-position :character 1 :line 2)
(lsp:set-my-position-line 100)
(lsp:my-position-line))
100))
(should (lsp-my-position? (lsp-make-my-position :character nil :line 2 :camel-case nil)))
(should (lsp-my-position? (lsp-make-my-position :character nil :line 2 :camelCase nil)))))
(provide 'lsp-protocol-test)
;;; lsp-protocol-test.el ends here