Open phikal opened 4 years ago
The current limitations are the only the first 4k byte are used to find a package name, and that it fails if only one file has a different file name (for example in
golang.org/x/net/ipv4
there is a file calledgen.go
that's not part of the package, and thus preventsgo-mode-add-package-line
from doing anything). These could easily be fixed, but I'm not sure what the correct behavior is from the Go-side of things.
I wonder if the correct thing to do would be to pick the package name that occurs most frequently?
Also, it looks like this patch doesn’t seem to handle package foo_test
for _test.go
files, inside package foo
?
I wonder if the correct thing to do would be to pick the package name that occurs most frequently?
That could also be done, but I assume that there could be difficulties when there are an approximately equal number of packages in a directory. It would probably be best to provide a local-safe user option to decide how to do this.
Also, it looks like this patch doesn’t seem to handle package foo_test for _test.go files, inside package foo?
The last update should fix that.
I wonder if the correct thing to do would be to pick the package name that occurs most frequently?
That could also be done, but I assume that there could be difficulties when there are an approximately equal number of packages in a directory. It would probably be best to provide a local-safe user option to decide how to do this.
There are four cases that all seem to have a sensible default:
FYI gopls provides completion for the package
statement now in empty files.
This patch fixes the minor annoyance of having to write out the package name when creating a new file. It scans all other file names and adds a
package ...
header if all other files share the same package line.Even with packages with over 100 go files, this function takes less than 0.01 seconds to execute, and even less when compiled.
The current limitations are the only the first 4k byte are used to find a package name, and that it fails if only one file has a different file name (for example in
golang.org/x/net/ipv4
there is a file calledgen.go
that's not part of the package, and thus preventsgo-mode-add-package-line
from doing anything). These could easily be fixed, but I'm not sure what the correct behavior is from the Go-side of things.As this feature is intrusive, it's disabled by default. Toggling
go-mode-add-package-line
automatically adds it to thego-mode-hook
.