Skip to content
This repository has been archived by the owner on Oct 12, 2022. It is now read-only.

Add goimports functionality to DocumentFormatting #298

Merged
merged 2 commits into from
Jul 25, 2018
Merged

Add goimports functionality to DocumentFormatting #298

merged 2 commits into from
Jul 25, 2018

Conversation

otto-md
Copy link
Contributor

@otto-md otto-md commented Jul 23, 2018

Original code by @JeanMertz, some tweaks by me.

Fixes #272

README.md Outdated
/**
* goimportsLocalPrefix sets the local prefix (comma-separated string) that goimports will use.
*
* Defaults to emptry string if not specified.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: empty


// GoimportsLocalPrefix sets the local prefix (comma-separated string) that goimports will use
//
// Defaults to emptry string if not specified.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: empty


if h.config.GoimportsEnabled {
imports.LocalPrefix = h.config.GoimportsLocalPrefix
formatted, err = imports.Process("", formatted, nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the file is actually formatted twice using cfg.Print (aka gofmt) and imports.Process (aka goimports). Seems unnecessary in my opinion...

In my opinion the code could look something like this:

if h.config.GoimportsEnabled {
    // set LocalPrefix
    // imports.Process(...)
} else {
    // ast.SortImports(...)
    // cfg.FPrint(...)
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I thought about this too. I have changed it now. It wasn't completely obvious to me how to do it though, but I ended up changing the imports.Process() call slightly, it seems to work.

@keegancsmith
Copy link
Member

Windows CI is not happy with this change. Note master branch is passing.

        --- FAIL: TestServer/go_basic/formatting-a.go (0.00s)
        	langserver_test.go:1707: jsonrpc2: code 0 message: open /src/test/pkg/a.go: The system cannot find the path specified.

if h.config.GoimportsEnabled {
imports.LocalPrefix = h.config.GoimportsLocalPrefix
var err error
formatted, err = imports.Process(filename, nil, nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The go-langserver maintains its own file cache, like all other LSP servers do.
So by passing nil as second parameter to Process forces the imports code to actually read the file from disk without trying to read from the internal file cache!
Just move the unformatted variable before this if and pass the unformatted as second argument to Process

@otto-md
Copy link
Contributor Author

otto-md commented Jul 23, 2018

Unfortunately I dont know what could cause the Windows CI issue and I dont have access to any windows computers.

if h.config.GoimportsEnabled {
imports.LocalPrefix = h.config.GoimportsLocalPrefix
var err error
formatted, err = imports.Process("", unformatted, nil)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pass filename instead of "" for the first argument. This should fix the builds.

Original code by @JeanMertz, some tweaks by me.

Fixes #272
@otto-md
Copy link
Contributor Author

otto-md commented Jul 23, 2018

I just squashed the commits now. Thanks for the help and review.

README.md Outdated
*
* Defaults to true if not specified.
*/
goimportsEnabled?: boolean;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is also goreturns. I think the more appropriate config would be an enum like what is done in vscode-go https://github.com/Microsoft/vscode-go/blob/0.6.84/package.json#L563-L574

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure: formatTool?: string; ?

that defaults to "goimports" but can also be "gofmt"?

And something like this in config.go?

type FormatTool string

const (
	FormatToolGoimports FormatTool = "goimports"
	FormatToolGofmt     FormatTool = "gofmt"
)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup that looks good to me. Thanks!

@@ -13,6 +13,7 @@ import (

"github.com/pmezard/go-difflib/difflib"
"golang.org/x/tools/go/buildutil"
"golang.org/x/tools/imports"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i don't think this is in our vendor tree. Can you run dep ensure?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

@keegancsmith keegancsmith left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't validating if the user wrote something invalid as the format tool, but that is fine.

README.md Outdated
*/
goimportsEnabled?: boolean;
formatTool?: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

given this is typescript, you could write this using a union of string literal types. formatTool?: "goimports" | "gofmt";. I'll update once merged.

@keegancsmith keegancsmith merged commit ee0174a into sourcegraph:master Jul 25, 2018
@keegancsmith
Copy link
Member

Thanks so much!

@maximbaz
Copy link

Thank you both for getting this in master!

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

Successfully merging this pull request may close these issues.

5 participants