I want a helper command for refactoring code in Go language. I have not found ready for use function for this task in go-mode and lsp. It should declare variable from the word under the point and place it as a type definition for example in the previous row. In Go syntax it looks like: var name type where name is a variable name and type is a variable type.

I have got the code:

(defun my-go-declare-var ()
  "Insert var declaration."
  (let ((name (current-word)))
    (insert "var " name " <type>")))

It looks for current word and inserts it on previous line.

The problem is how to get the type of my variable? I tried to find appropriate lsp functions but not found any that returns the type in plain text. Also lsp-ui shows the hint with variable type in echo area:

lsp-ui shows variable type

But I can't understand how to catch it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.