Skip to content

using goimports for go_fmt_command moves the cursor on save #1342

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
clee opened this issue Jul 1, 2017 · 4 comments · Fixed by #1367
Closed

using goimports for go_fmt_command moves the cursor on save #1342

clee opened this issue Jul 1, 2017 · 4 comments · Fixed by #1367

Comments

@clee
Copy link
Contributor

clee commented Jul 1, 2017

Behavior

Anytime I add or delete a line that requires adding or deleting an import, my cursor gets moved when I save the file (when adding a line, my cursor gets moved up one line from where I was; when I delete a line, the cursor gets moved down a line, because goimports has added or removed lines). It would be nice if my cursor stayed locked to the same line of code I was on already, before saving.

Steps to reproduce:

  1. make sure to configure vim with let g:go_fmt_command = "goimports"
  2. open a new Go file (using the default template)
  3. add a line after fmt.Println("vim-go") with the contents:
    user := os.Getenv("user")
  4. position cursor on just-inserted line, at the u in user
  5. save the file with :w

Configuration

  • vimrc contains only let g:go_fmt_command = "goimports"
  • vim version: NVIM v0.2.1-15-g08b23d08
  • vim-go version: bcf3f23
  • go version: go1.8.2 darwin/amd64
@fatih
Copy link
Owner

fatih commented Jul 22, 2017

Thanks for the feedback. I know this is annoying. Actually the cursor still says on the same line number (vim does the correct thing) but because we added/removed lines, it seems like your cursor jumped to another position. Anyway, this is fixed now in master by getting the diff of the number of lines and then adjusting the cursor position after the formatting finishes. Please pull the latest master to use it.

@clee
Copy link
Contributor Author

clee commented Jul 23, 2017

That's awesome! Thanks for addressing this so quickly. I was going to take a go at it this weekend but you beat me to it :)

I'm grabbing the code to test now.

@clee
Copy link
Contributor Author

clee commented Jul 23, 2017

It looks like it retains the proper relative row now, but it seems like it's moving my cursor to the first column when goimports adjusts the file length (so the column position is getting lost).

@clee
Copy link
Contributor Author

clee commented Jul 23, 2017

Figured out how to preserve the column index, submitted #1369. Hope it makes sense. Thanks, @fatih!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants