Skip to content

Commit 9c0760d

Browse files
skewb1kadonovan
authored andcommitted
gopls/internal/settings: add ui.newGoFileHeader option
Toggles auto-completion of copyright comment and package clause for newly created Go files. Fixes golang/go#74825 Change-Id: Ia08697ab885568a44ffeab5e379c61a57f9ff266 Reviewed-on: https://go-review.googlesource.com/c/tools/+/694275 Reviewed-by: Alan Donovan <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]> Reviewed-by: Robert Findley <[email protected]> Auto-Submit: Alan Donovan <[email protected]> Commit-Queue: Alan Donovan <[email protected]>
1 parent c212c4a commit 9c0760d

File tree

7 files changed

+41
-2
lines changed

7 files changed

+41
-2
lines changed

gopls/doc/release/v0.21.0.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@ title: "Gopls release v0.21.0 (forthcoming)"
33
---
44

55
## Configuration changes
6+
7+
- The new `newGoFileHeader` option allows toggling automatic insertion of the copyright comment
8+
and package declaration in a newly created Go file.
9+
610
## Web-based features
711
## Editing features
812
## Analysis features

gopls/doc/settings.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,14 @@ By default, all modifiers are enabled.
261261

262262
Default: `{}`.
263263

264+
<a id='newGoFileHeader'></a>
265+
### `newGoFileHeader bool`
266+
267+
newGoFileHeader enables automatic insertion of the copyright comment
268+
and package declaration in a newly created Go file.
269+
270+
Default: `true`.
271+
264272
<a id='completion'></a>
265273
## Completion
266274

gopls/internal/doc/api.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2037,6 +2037,20 @@
20372037
"Hierarchy": "ui",
20382038
"DeprecationMessage": ""
20392039
},
2040+
{
2041+
"Name": "newGoFileHeader",
2042+
"Type": "bool",
2043+
"Doc": "newGoFileHeader enables automatic insertion of the copyright comment\nand package declaration in a newly created Go file.\n",
2044+
"EnumKeys": {
2045+
"ValueType": "",
2046+
"Keys": null
2047+
},
2048+
"EnumValues": null,
2049+
"Default": "true",
2050+
"Status": "",
2051+
"Hierarchy": "ui",
2052+
"DeprecationMessage": ""
2053+
},
20402054
{
20412055
"Name": "local",
20422056
"Type": "string",

gopls/internal/golang/completion/newfile.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,11 @@ import (
1616
"golang.org/x/tools/gopls/internal/protocol"
1717
)
1818

19-
// NewFile returns a document change to complete an empty Go source file.
19+
// NewFile returns a document change to complete an empty Go source file. Document change may be nil.
2020
func NewFile(ctx context.Context, snapshot *cache.Snapshot, fh file.Handle) (*protocol.DocumentChange, error) {
21+
if !snapshot.Options().NewGoFileHeader {
22+
return nil, nil
23+
}
2124
content, err := fh.Content()
2225
if err != nil {
2326
return nil, err

gopls/internal/server/workspace.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,9 @@ func (s *server) DidCreateFiles(ctx context.Context, params *protocol.CreateFile
163163
// any error, including "it's not a new file"
164164
continue
165165
}
166-
allChanges = append(allChanges, *change)
166+
if change != nil {
167+
allChanges = append(allChanges, *change)
168+
}
167169
default:
168170
}
169171
}

gopls/internal/settings/default.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@ func DefaultOptions(overrides ...func(*Options)) *Options {
132132
CodeLensVendor: true,
133133
CodeLensRunGovulncheck: false, // TODO(hyangah): enable
134134
},
135+
NewGoFileHeader: true,
135136
},
136137
},
137138
InternalOptions: InternalOptions{

gopls/internal/settings/settings.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,10 @@ type UIOptions struct {
246246
// disabling modifiers by setting each value to false.
247247
// By default, all modifiers are enabled.
248248
SemanticTokenModifiers map[string]bool `status:"experimental"`
249+
250+
// NewGoFileHeader enables automatic insertion of the copyright comment
251+
// and package declaration in a newly created Go file.
252+
NewGoFileHeader bool
249253
}
250254

251255
// A CodeLensSource identifies an (algorithmic) source of code lenses.
@@ -1287,6 +1291,9 @@ func (o *Options) setOne(name string, value any) (applied []CounterPath, _ error
12871291
case "semanticTokenModifiers":
12881292
return setBoolMap(&o.SemanticTokenModifiers, value)
12891293

1294+
case "newGoFileHeader":
1295+
return setBool(&o.NewGoFileHeader, value)
1296+
12901297
case "expandWorkspaceToModule":
12911298
// See golang/go#63536: we can consider deprecating
12921299
// expandWorkspaceToModule, but probably need to change the default

0 commit comments

Comments
 (0)