forked from sourcegraph/go-langserver
/
config.go
104 lines (92 loc) · 2.98 KB
/
config.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package langserver
import (
"os"
"runtime"
)
var (
// GOLSP_WARMUP_ON_INITIALIZE toggles if we typecheck the whole
// workspace in the background on initialize. This trades off initial
// CPU and memory to hide perceived latency of the first few
// requests. If the LSP server is long lived the tradeoff is usually
// worth it.
envWarmupOnInitialize = os.Getenv("GOLSP_WARMUP_ON_INITIALIZE")
)
// Config adjusts the behaviour of go-langserver. Please keep in sync with
// InitializationOptions in the README.
type Config struct {
// FuncSnippetEnabled enables the returning of argument snippets on `func`
// completions, eg. func(foo string, arg2 bar). Requires code completion
// to be enabled.
//
// Defaults to true if not specified.
FuncSnippetEnabled bool
// GocodeCompletionEnabled enables code completion feature (using gocode)
//
// Defaults to false if not specified.
GocodeCompletionEnabled bool
// FormatTool decides which tool is used to format documents. Supported: goimports and gofmt
//
// Defaults to goimports if not specified.
FormatTool string
// GoimportsLocalPrefix sets the local prefix (comma-separated string) that goimports will use
//
// Defaults to empty string if not specified.
GoimportsLocalPrefix string
// DiagnosticsEnabled enables handling of diagnostics
//
// Defaults to false if not specified.
DiagnosticsEnabled bool
// MaxParallelism controls the maximum number of goroutines that should be used
// to fulfill requests. This is useful in editor environments where users do
// not want results ASAP, but rather just semi quickly without eating all of
// their CPU.
//
// Defaults to half of your CPU cores if not specified.
MaxParallelism int
// UseBinaryPkgCache controls whether or not $GOPATH/pkg binary .a files should
// be used.
//
// Defaults to true if not specified.
UseBinaryPkgCache bool
}
// Apply sets the corresponding field in c for each non-nil field in o.
func (c Config) Apply(o *InitializationOptions) Config {
if o == nil {
return c
}
if o.FuncSnippetEnabled != nil {
c.FuncSnippetEnabled = *o.FuncSnippetEnabled
}
if o.GocodeCompletionEnabled != nil {
c.GocodeCompletionEnabled = *o.GocodeCompletionEnabled
}
if o.FormatTool != nil {
c.FormatTool = *o.FormatTool
}
if o.GoimportsLocalPrefix != nil {
c.GoimportsLocalPrefix = *o.GoimportsLocalPrefix
}
if o.MaxParallelism != nil {
c.MaxParallelism = *o.MaxParallelism
}
if o.UseBinaryPkgCache != nil {
c.UseBinaryPkgCache = *o.UseBinaryPkgCache
}
return c
}
// NewDefaultConfig returns the default config. See the field comments for the
// defaults.
func NewDefaultConfig() Config {
// Default max parallelism to half the CPU cores, but at least always one.
maxparallelism := runtime.NumCPU() / 2
if maxparallelism <= 0 {
maxparallelism = 1
}
return Config{
FuncSnippetEnabled: true,
GocodeCompletionEnabled: false,
FormatTool: formatToolGoimports,
MaxParallelism: maxparallelism,
UseBinaryPkgCache: true,
}
}