Skip to content

Commit 853cad8

Browse files
authored
Allow to not cut the signature received on hover (#3637)
* Allow to not cut the signature received on hover Some servers (i.e. the ocamllsp one) return only the signature on hover (without the documentation) but tend to put newlines between the different items. With the current implementation of `lsp-clients-extract-signature-on-hover`, the displayed signature will only contain the first item ``` config1:string list -> config2:string list -> config3:string list -> int -> int -> int ``` will then be rendered as ``` config1:string list -> ``` With the new implementation, a custom variable allows to take control over this behaviour and the function has been reimplemented to either return the previous result or return the signature with newlines replaced by spaces * Three modes, default, space and "newline" The third mode is only reachable if you use the function on your own (I use it to kill the returned type since this is a feature that exists in the previous OCaml mode I used) Thanks to @yyoncho I moved my code to the lsp-ocaml.el file to make it OCaml related. The ocaml-lsp server returns a markdown file with the following content: ```ocaml <type> --- <doc> ``` so it's easy to split it to only keep the signature. If the second mode is chosen the type will be edited to be displayed on one line and end with `...` if it is too wide * Small updates and better function documentation * Update lsp-mode.el
1 parent b114f4a commit 853cad8

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

clients/lsp-ocaml.el

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,57 @@
7777
:priority 0
7878
:server-id 'ocaml-lsp-server))
7979

80+
(defcustom lsp-cut-signature 'space
81+
"If non-nil, signatures returned on hover will not be split on newline."
82+
:group 'lsp-ocaml
83+
:type '(choice (symbol :tag "Default behaviour" 'cut)
84+
(symbol :tag "Display all the lines with spaces" 'space)))
85+
86+
(cl-defmethod lsp-clients-extract-signature-on-hover (contents (_server-id (eql ocamllsp)) &optional storable)
87+
"Extract a representative line from OCaml's CONTENTS, to show in the echo area.
88+
This function splits the content between the signature
89+
and the documentation to display the signature
90+
and truncate it if it's too wide.
91+
The STORABLE argument is used if you want to use this
92+
function to get the type and, for example, kill and yank it.
93+
94+
An example of function using STORABLE is:
95+
96+
(defun mdrp/lsp-get-type-and-kill ()
97+
(interactive)
98+
(let ((contents (-some->> (lsp--text-document-position-params)
99+
(lsp--make-request \"textDocument/hover\")
100+
(lsp--send-request)
101+
(lsp:hover-contents))))
102+
(let ((contents (and contents
103+
(lsp--render-on-hover-content
104+
contents
105+
t))))
106+
(let ((contents
107+
(pcase (lsp-workspaces)
108+
(`(,workspace)
109+
(lsp-clients-extract-signature-on-hover
110+
contents
111+
(lsp--workspace-server-id workspace)
112+
t))
113+
(lsp-clients-extract-signature-on-hover
114+
contents
115+
nil)
116+
)))
117+
(message \"Copied %s to kill-ring\" contents)
118+
(kill-new contents)))))"
119+
(let ((type (s-trim (lsp--render-element (lsp-make-marked-string
120+
:language "ocaml"
121+
:value (car (s-split "---" (lsp--render-element contents))))))))
122+
(if (equal nil storable)
123+
(if (eq lsp-cut-signature 'cut)
124+
(car (s-lines type))
125+
;; else lsp-cut-signature is 'space
126+
(let ((ntype (s-replace "\n" " " type)))
127+
(if (>= (length ntype) (frame-width))
128+
(concat (substring ntype 0 (- (frame-width) 4)) "...")
129+
ntype)))
130+
type)))
80131

81132
(lsp-consistency-check lsp-ocaml)
82133

0 commit comments

Comments
 (0)