ocaml-lsp
ocaml-lsp copied to clipboard
Optional arguments supplied with ~ are completed with ? instead
When requesting completion for a function's optional labeled argument, which the user intends to pass with ~, OCaml-LSP's completion suggestion uses ? instead and replaces the original intended meaning, e.g.:
let f ?abc ~def () = assert false
let () = f ~
If, at the end of that snippet, the user presses TAB (or whatever completion key), OCaml-LSP suggests:
?abc : 'a option
~def : 'b
as some of the completion results. Selecting ?abc causes the code to now look like:
let () = f ?abc
whereas the expected behavior is more likely:
let () = f ~abc
This issue also occurs if the user has typed some prefix of the optional argument label first.
Here's the event log item I got from Emacs:
(:jsonrpc "2.0" :id 176 :method "textDocument/completion" :params
(:textDocument
(:uri "file:///home/azeng/z.ml")
:position
(:line 8 :character 12)
:context
(:triggerKind 1)))
[server-reply] (id:176) Tue Sep 12 16:49:13 2023:
(:id 176 :jsonrpc "2.0" :result
(:isIncomplete :json-false :items
[(:data
(:position
(:character 12 :line 8)
:textDocument
(:uri "file:///home/azeng/z.ml"))
:deprecated :json-false :detail "'a option" :kind 5 :label "?abc" :sortText "0000" :textEdit
(:newText "?abc" :range
(:end
(:character 12 :line 8)
:start
(:character 10 :line 8))))
(:data
(:position
(:character 12 :line 8)
:textDocument
(:uri "file:///home/azeng/z.ml"))
:deprecated :json-false :detail "'b" :kind 5 :label "~def" :sortText "0001" :textEdit
(:newText "~def" :range
(:end
(:character 12 :line 8)
:start
(:character 10 :line 8))))
(:data
(:position
(:character 12 :line 8)
:textDocument
(:uri "file:///home/azeng/z.ml"))
:deprecated :json-false :detail "'_weak42" :kind 5 :label "~a" :sortText "0002" :textEdit
(:newText "~a" :range
(:end
(:character 12 :line 8)
:start
(:character 10 :line 8))))]))
I think maybe the label should remain ?abc but the newText should be ~abc instead?
Or we could offer completions for ?abc, ~abc, and ~def.
In Neovim ?abc doesn't even appear as a completion option. I agree with @bcc32 though; newText should be ~abc. @jfeser I'm not sure adding ?abc as an additional completion candidate adds more value than noise, as the user has already signaled how they want to pass the argument.