auto-fix-return.nvim icon indicating copy to clipboard operation
auto-fix-return.nvim copied to clipboard

doing CW on the name of some methods confuses input with another return type

Open cachesdev opened this issue 1 year ago • 6 comments

doing DW, DIW, CW, and so on will make it so the parenthesis wraps around the input instead of letting you change the name, on some specific cases. refactoring via LSP works fine, so that's the workaround. this happens specifically on methods, functions work fine.

the only case i've found this to be an issue is when a a method only has 1 return, if it has more than 1 return then it will also work fine:

//                  CW here works
func (m MovieModel) Get(id int64) (*Movie, error) {
	return nil, nil
}
//                  This wont work
func (m MovieModel) Update(movie *Movie) error {
	return nil
}

doing CW on this function will result in this:

//                   Pipe is the cursor
func (m MovieModel) (|movie *Movie error) {
	return nil
}

cachesdev avatar Aug 22 '24 12:08 cachesdev

Hmm, this one is interesting. I dont think it is related to the commands themselves. It looks like it is because when the entire name is removed it parses differently on a method_declaration

func (m MovieModel) (movie *Movie) error {

parses as a func_literal

  (expression_statement ; [51, 0] - [52, 1]
    (func_literal ; [51, 0] - [52, 1]
      parameters: (parameter_list ; [51, 5] - [51, 19]
        (parameter_declaration ; [51, 6] - [51, 18]
          name: (identifier) ; [51, 6] - [51, 7]
          type: (type_identifier))) ; [51, 8] - [51, 18]
      result: (parameter_list ; [51, 20] - [51, 34]
        (parameter_declaration ; [51, 21] - [51, 33]
          name: (identifier) ; [51, 21] - [51, 26]
          type: (pointer_type ; [51, 27] - [51, 33]
            (type_identifier)))) ; [51, 28] - [51, 33]
      (ERROR ; [51, 35] - [51, 40]
        (identifier)) ; [51, 35] - [51, 40]
      body: (block))) ; [51, 41] - [52, 1]

and it ends up thinking (movie *Movie) is the actual desired return statement.

Bit of a weird one here. Might take some thought on how to handle it

Jay-Madden avatar Aug 22 '24 15:08 Jay-Madden

As for workaround the LSP rename options works well.

You can also use AutoFixReturnDisable and AutoFixReturnEnable to turn the autocmds on and off which will allow you to edit the line as you expect

Jay-Madden avatar Aug 22 '24 15:08 Jay-Madden

As for workaround the LSP rename options works well.

You can also use AutoFixReturnDisable and AutoFixReturnEnable to turn the autocmds on and off which will allow you to edit the line as you expect

That's how I do it when LSP rename is not suitable.

Hopefully you can figure out, I'm 2x out of my depth both with treesitter and lua so I can't really contribute much other than testing 🙂

cachesdev avatar Aug 22 '24 15:08 cachesdev

@cachesdev Ended up being a possible one character fix lol. Give the branch set-column-offset a try when you get the chance and let me know if it works for you

Jay-Madden avatar Aug 23 '24 01:08 Jay-Madden

@Jay-Madden Can confirm the fix is definitely working 👌

cachesdev avatar Aug 23 '24 01:08 cachesdev

Cool, fixed on master https://github.com/Jay-Madden/auto-fix-return.nvim/commit/0fd3c7b959bb1e30f272d0091b5521ee10b149da

Bit fragile tho, there are few other things i think can be done here long term. But for now this should solve the immediate case

Jay-Madden avatar Aug 23 '24 01:08 Jay-Madden

Finally got around to rewriting the parser to be more robust and also run a full parse validation before a fix is applied.

So hopefully this failure wont happen again.

Jay-Madden avatar Jun 23 '25 00:06 Jay-Madden