Skip to content

Commit

Permalink
new module for lsp related stuff. test fixes.
Browse files Browse the repository at this point in the history
eventually more of the lsp-model include functions have to be moved to lsp-util.
  • Loading branch information
mdbergmann committed Nov 6, 2022
1 parent 3e391ad commit aa9e7ec
Show file tree
Hide file tree
Showing 7 changed files with 103 additions and 97 deletions.
2 changes: 1 addition & 1 deletion apps/lfe-ls/src/code-util.lfe
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(defmodule code-util-tests
(defmodule code-util
(export
(add-code-paths 1)))

Expand Down
42 changes: 1 addition & 41 deletions apps/lfe-ls/src/completion-util.lfe
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
(defmodule completion-util
(export (find-completions-at 3)
(to-json 1)))
(export (find-completions-at 3)))

(include-lib "apps/lfe-ls/include/utils.lfe")
(include-lib "apps/lfe-ls/include/lsp-model.lfe")
Expand Down Expand Up @@ -145,42 +144,3 @@ Two entries if there was a ':' in the text that was parsed. The second element i
(if (is_binary thing)
thing
(list_to_binary thing)))
(defun to-json (citem)
"Converts `completion-item` to LSP json representation.
!!!The conversion to LSP json can be considered business logic which doesn't belong to this module.
But for right now there is no better place."
(let ((module (completion-item-module citem))
(func (completion-item-func citem))
(arity (completion-item-arity citem))
(detail (completion-item-detail citem))
(kind (completion-item-kind citem)))
(let* ((base-label (case func
('null (lfe_io:format1 "~s" `(,module)))
(#"" (lfe_io:format1 "~s" `(,module)))
(fn (lfe_io:format1 "~s:~s" `(,module ,fn)))))
(label (list_to_binary
(case arity
('null base-label)
(ar (lfe_io:format1 "~s~s"
`(,base-label
,(lfe_io:format1 "/~p" `(,ar))))))))
(insertText (list_to_binary
(case func
('null module)
(#"" module)
(fn (case arity
('null fn)
(0 fn)
(n (lists:foldl (lambda (i acc)
(lfe_io:format1 "~s ${~p:arg~p}" `(,acc ,i ,i)))
fn
(lists:seq 1 n)))
)))))
(result `(#(#"label" ,label)
#(#"kind" ,kind)
#(#"detail" ,detail)
#(#"insertTextFormat" 2)
#(#"insertText" ,insertText))))
;;(logger:notice "result: ~p" `(,result))
result)))
2 changes: 1 addition & 1 deletion apps/lfe-ls/src/lsp-proc.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ Handler functions (like `%on-initialize-req`) are expected to return:
(defun %make-completions-result (completions)
"`completions' is a list of `completion-item' records."
(lists:map #'completion-util:to-json/1 completions))
(lists:map #'lsp-util:completion-item-to-json/1 completions))
(defun %make-notification (method params)
`(#(#"jsonrpc" #"2.0")
Expand Down
41 changes: 41 additions & 0 deletions apps/lfe-ls/src/lsp-util.lfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
(defmodule lsp-util
(export (completion-item-to-json 1)))

(include-lib "apps/lfe-ls/include/lsp-model.lfe")

(defun completion-item-to-json (citem)
"Converts `completion-item` to LSP json representation."
(let ((module (completion-item-module citem))
(func (completion-item-func citem))
(arity (completion-item-arity citem))
(detail (completion-item-detail citem))
(kind (completion-item-kind citem)))
(let* ((base-label (case func
('null (lfe_io:format1 "~s" `(,module)))
(#"" (lfe_io:format1 "~s" `(,module)))
(fn (lfe_io:format1 "~s:~s" `(,module ,fn)))))
(label (list_to_binary
(case arity
('null base-label)
(ar (lfe_io:format1 "~s~s"
`(,base-label
,(lfe_io:format1 "/~p" `(,ar))))))))
(insertText (case func
('null module)
(#"" module)
(fn (case arity
('null fn)
(0 fn)
(n (list_to_binary
(lists:foldl (lambda (i acc)
(lfe_io:format1 "~s ${~p:arg~p}" `(,acc ,i ,i)))
fn
(lists:seq 1 n))))))))
(result `(#(#"label" ,label)
#(#"kind" ,kind)
#(#"detail" ,detail)
#(#"insertTextFormat" 2)
#(#"insertText" ,insertText))))
;;(logger:notice "result: ~p" `(,result))
result)))

53 changes: 0 additions & 53 deletions apps/lfe-ls/test/completion-util-tests.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -149,56 +149,3 @@
(car funs))
(is-equal `#(completion-item #"zlib" #"" null #"" 9)
(car (lists:reverse funs)))))
(deftest generate-json--arity-null
(is-equal '(#(#"label" #"foo:bar")
#(#"kind" 3)
#(#"detail" #"")
#(#"insertTextFormat" 2)
#(#"insertText" #"bar"))
(completion-util:to-json
(make-completion-item
module "foo"
func "bar"
arity 'null
kind (completion-item-kind-function)))))
(defmacro make-citem (arity)
`(completion-util:to-json
(make-completion-item
module "foo"
func "bar"
arity ,arity
kind (completion-item-kind-function))))
(deftest generate-json--arity-0
(is-equal '(#(#"label" #"foo:bar/0")
#(#"kind" 3)
#(#"detail" #"")
#(#"insertTextFormat" 2)
#(#"insertText" #"bar"))
(make-citem 0)))
(deftest generate-json--arity-1
(is-equal '(#(#"label" #"foo:bar/1")
#(#"kind" 3)
#(#"detail" #"")
#(#"insertTextFormat" 2)
#(#"insertText" #"bar ${1:arg1}"))
(make-citem 1)))
(deftest generate-json--arity-2
(is-equal '(#(#"label" #"foo:bar/2")
#(#"kind" 3)
#(#"detail" #"")
#(#"insertTextFormat" 2)
#(#"insertText" #"bar ${1:arg1} ${2:arg2}"))
(make-citem 2)))
(deftest generate-json--arity-3
(is-equal '(#(#"label" #"foo:bar/3")
#(#"kind" 3)
#(#"detail" #"")
#(#"insertTextFormat" 2)
#(#"insertText" #"bar ${1:arg1} ${2:arg2} ${3:arg3}"))
(make-citem 3)))
2 changes: 1 addition & 1 deletion apps/lfe-ls/test/lsp-proc-tests.lfe
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ This one will just push the computed result to our fake-lsp-resp-sender actor"
(meck:unload 'completion-util)
(is (expected-result-p
#(reply
#"{\"id\":99,\"result\":[{\"label\":\"foo:defun/3\",\"kind\":2,\"detail\":\"foo\",\"insertTextFormat\":2,\"insertText\":\"defun\"}]}"))))
#"{\"id\":99,\"result\":[{\"label\":\"foo:defun/3\",\"kind\":2,\"detail\":\"foo\",\"insertTextFormat\":2,\"insertText\":\"defun ${1:arg1} ${2:arg2} ${3:arg3}\"}]}"))))
(is (meck:validate 'compile-util))
(meck:unload 'compile-util)))

Expand Down
58 changes: 58 additions & 0 deletions apps/lfe-ls/test/lsp-util-tests.lfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
(defmodule lsp-util-tests
(behaviour ltest-unit))

(include-lib "ltest/include/ltest-macros.lfe")
(include-lib "apps/lfe-ls/include/lsp-model.lfe")

(deftest generate-json--arity-null
(is-equal '(#(#"label" #"foo:bar")
#(#"kind" 3)
#(#"detail" #"")
#(#"insertTextFormat" 2)
#(#"insertText" #"bar"))
(lsp-util:completion-item-to-json
(make-completion-item
module #"foo"
func #"bar"
arity 'null
kind (completion-item-kind-function)))))

(defmacro make-citem (arity)
`(lsp-util:completion-item-to-json
(make-completion-item
module #"foo"
func #"bar"
arity ,arity
kind (completion-item-kind-function))))

(deftest generate-json--arity-0
(is-equal '(#(#"label" #"foo:bar/0")
#(#"kind" 3)
#(#"detail" #"")
#(#"insertTextFormat" 2)
#(#"insertText" #"bar"))
(make-citem 0)))

(deftest generate-json--arity-1
(is-equal '(#(#"label" #"foo:bar/1")
#(#"kind" 3)
#(#"detail" #"")
#(#"insertTextFormat" 2)
#(#"insertText" #"bar ${1:arg1}"))
(make-citem 1)))
(deftest generate-json--arity-2
(is-equal '(#(#"label" #"foo:bar/2")
#(#"kind" 3)
#(#"detail" #"")
#(#"insertTextFormat" 2)
#(#"insertText" #"bar ${1:arg1} ${2:arg2}"))
(make-citem 2)))
(deftest generate-json--arity-3
(is-equal '(#(#"label" #"foo:bar/3")
#(#"kind" 3)
#(#"detail" #"")
#(#"insertTextFormat" 2)
#(#"insertText" #"bar ${1:arg1} ${2:arg2} ${3:arg3}"))
(make-citem 3)))

0 comments on commit aa9e7ec

Please sign in to comment.