Add function to automatically add package header#358
Add function to automatically add package header#358phikal wants to merge 1 commit intodominikh:masterfrom
Conversation
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 |
7d179ae to
c1d1f42
Compare
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.
The last update should fix that. |
There are four cases that all seem to have a sensible default:
|
|
FYI gopls provides completion for the |
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/ipv4there is a file calledgen.gothat's not part of the package, and thus preventsgo-mode-add-package-linefrom 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-lineautomatically adds it to thego-mode-hook.