go-mode.el
go-mode.el copied to clipboard
Add function to automatically add package header
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 called gen.go
that's not part of the package, and thus prevents go-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 the go-mode-hook
.
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:
- The buffer represents the first .go file in the directory.
- In this case, it seems sensible to guess that the package name is the directory name.
- There is only one package name that shows up in the directory.
- Use that name. This is what your code does now.
- There are multiple packages in the directory, but one of them is most frequent.
- Use the most frequent name.
- There are two package names that have the most .go files in the directory.
- If one of the package names matches the directory name, that is probably the best default.
- Otherwise, pick one of the most frequent names at random.
FYI gopls provides completion for the package
statement now in empty files.