Skip to content
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

adjust imports on save #2797

Closed
wants to merge 1 commit into from
Closed

adjust imports on save #2797

wants to merge 1 commit into from

Conversation

bhcleek
Copy link
Collaborator

@bhcleek bhcleek commented Apr 1, 2020

Introduce a new option, g:go_imports_autosave, to allow imports to be
adjusted even when g:go_fmt_command is not set to goimports.

Closes #2795

@orlangure
Copy link
Contributor

I tested this option, and got some errors:

line    5:
E714: List required
Error detected while processing function go#auto#fmt_autosave[10]..go#fmt#Format[9]..go#lsp#Imports[18]..<SNR>114_await[3]..18:
line   13:
E171: Missing :endif
Error detected while processing function go#auto#fmt_autosave[10]..go#fmt#Format[9]..go#lsp#Imports[18]..<SNR>114_await:
line    3:
E170: Missing :endwhile
Error detected while processing function go#auto#fmt_autosave[10]..go#fmt#Format:
line    9:
E171: Missing :endif
Error detected while processing function go#auto#fmt_autosave:
line   10:
E171: Missing :endif
Press ENTER or type command to continue

My new config is:

  let g:go_fmt_autosave=0 " or 1 as well
  let g:go_imports_autosave=1
  let g:go_fmt_command='gopls'
  let g:go_imports_mode='gopls'

It happens if I try to save a file that does not exist yet.

With existing files, these options work, and imports are organized.

There is a weird issue when imports look like this:

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strings"
	"time"
	"github.com/orlangure/gnomock"
)

In this case, a new line is added between stdlib imports and 3rd party imports, but it is added with trailing whitespace:

import (
	"encoding/json"
	"fmt"
	"net/http"
	"strings"
	"time"
	
	"github.com/orlangure/gnomock"
)

There is a tab on the blank line.

@bhcleek
Copy link
Collaborator Author

bhcleek commented Apr 1, 2020

Thanks @orlangure

I don't intend to merge this until I've used it for a while; I fully expect to make changes before merging this.

Introduce a new option, g:go_imports_autosave, to allow imports to be
adjusted even when g:go_fmt_command is not set to goimports.

Closes fatih#2795
@bhcleek bhcleek closed this Apr 1, 2020
@bhcleek bhcleek deleted the fmt/goimports branch April 1, 2020 17:29
@bhcleek bhcleek mentioned this pull request Apr 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Organizing imports on save with gopls
2 participants