1
0
mirror of https://github.com/golang/go synced 2024-10-01 01:48:32 -06:00

internal/lsp: refactor and document options

This change cleans up the structure of the Options struct in order to
clearly delineate which options should be configurable for the user.
Follow-up work is needed to refactor the completion options to fit into
this structure, as well as to make sure that the name of the field is
the name of the setting. This will make it easier to generate markdown
documentation from the code.

Also, remove options that are no longer in-use and mark them as
deprecated.

Change-Id: Ib34ae25789e21b76077a564601e487fbebfc5f48
Reviewed-on: https://go-review.googlesource.com/c/tools/+/212519
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Heschi Kreinick <heschi@google.com>
This commit is contained in:
Rebecca Stambler 2019-12-24 16:16:06 -05:00
parent 8647d4c879
commit 99d11d0e63
4 changed files with 77 additions and 47 deletions

View File

@ -92,7 +92,7 @@ func (s *Server) initialize(ctx context.Context, params *protocol.ParamInitializ
TriggerCharacters: []string{"(", ","}, TriggerCharacters: []string{"(", ","},
}, },
TextDocumentSync: &protocol.TextDocumentSyncOptions{ TextDocumentSync: &protocol.TextDocumentSyncOptions{
Change: options.TextDocumentSyncKind, Change: protocol.Incremental,
OpenClose: true, OpenClose: true,
Save: protocol.SaveOptions{ Save: protocol.SaveOptions{
IncludeText: false, IncludeText: false,
@ -130,7 +130,7 @@ func (s *Server) initialized(ctx context.Context, params *protocol.InitializedPa
) )
} }
if options.WatchFileChanges && options.DynamicWatchedFilesSupported { if options.DynamicWatchedFilesSupported {
registrations = append(registrations, protocol.Registration{ registrations = append(registrations, protocol.Registration{
ID: "workspace/didChangeWatchedFiles", ID: "workspace/didChangeWatchedFiles",
Method: "workspace/didChangeWatchedFiles", Method: "workspace/didChangeWatchedFiles",

View File

@ -43,11 +43,22 @@ import (
var ( var (
DefaultOptions = Options{ DefaultOptions = Options{
Env: os.Environ(), ClientOptions: DefaultClientOptions,
TextDocumentSyncKind: protocol.Incremental, ServerOptions: DefaultServerOptions,
HoverKind: SynopsisDocumentation, UserOptions: DefaultUserOptions,
InsertTextFormat: protocol.PlainTextTextFormat, DebuggingOptions: DefaultDebuggingOptions,
PreferredContentFormat: protocol.Markdown, ExperimentalOptions: DefaultExperimentalOptions,
Hooks: DefaultHooks,
}
DefaultClientOptions = ClientOptions{
InsertTextFormat: protocol.SnippetTextFormat,
PreferredContentFormat: protocol.Markdown,
ConfigurationSupported: true,
DynamicConfigurationSupported: true,
DynamicWatchedFilesSupported: true,
LineFoldingOnly: false,
}
DefaultServerOptions = ServerOptions{
SupportedCodeActions: map[FileKind]map[protocol.CodeActionKind]bool{ SupportedCodeActions: map[FileKind]map[protocol.CodeActionKind]bool{
Go: { Go: {
protocol.SourceOrganizeImports: true, protocol.SourceOrganizeImports: true,
@ -61,6 +72,10 @@ var (
SupportedCommands: []string{ SupportedCommands: []string{
"tidy", // for go.mod files "tidy", // for go.mod files
}, },
}
DefaultUserOptions = UserOptions{
Env: os.Environ(),
HoverKind: SynopsisDocumentation,
Completion: CompletionOptions{ Completion: CompletionOptions{
Documentation: true, Documentation: true,
Deep: true, Deep: true,
@ -68,61 +83,84 @@ var (
Literal: true, Literal: true,
Budget: 100 * time.Millisecond, Budget: 100 * time.Millisecond,
}, },
LinkTarget: "pkg.go.dev",
}
DefaultHooks = Hooks{
ComputeEdits: myers.ComputeEdits, ComputeEdits: myers.ComputeEdits,
URLRegexp: regexp.MustCompile(`(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?`), URLRegexp: regexp.MustCompile(`(http|ftp|https)://([\w_-]+(?:(?:\.[\w_-]+)+))([\w.,@?^=%&:/~+#-]*[\w@?^=%&/~+#-])?`),
Analyzers: defaultAnalyzers, Analyzers: defaultAnalyzers,
GoDiff: true, GoDiff: true,
LinkTarget: "pkg.go.dev",
TempModfile: false,
} }
DefaultExperimentalOptions = ExperimentalOptions{
TempModfile: false,
}
DefaultDebuggingOptions = DebuggingOptions{}
) )
type Options struct { type Options struct {
// Env is the current set of environment overrides on this view. ClientOptions
Env []string ServerOptions
UserOptions
DebuggingOptions
ExperimentalOptions
Hooks
}
// BuildFlags is used to adjust the build flags applied to the view. type ClientOptions struct {
BuildFlags []string
HoverKind HoverKind
DisabledAnalyses map[string]struct{}
StaticCheck bool
GoDiff bool
WatchFileChanges bool
InsertTextFormat protocol.InsertTextFormat InsertTextFormat protocol.InsertTextFormat
ConfigurationSupported bool ConfigurationSupported bool
DynamicConfigurationSupported bool DynamicConfigurationSupported bool
DynamicWatchedFilesSupported bool DynamicWatchedFilesSupported bool
PreferredContentFormat protocol.MarkupKind PreferredContentFormat protocol.MarkupKind
LineFoldingOnly bool LineFoldingOnly bool
}
type ServerOptions struct {
SupportedCodeActions map[FileKind]map[protocol.CodeActionKind]bool SupportedCodeActions map[FileKind]map[protocol.CodeActionKind]bool
SupportedCommands []string
}
SupportedCommands []string type UserOptions struct {
// Env is the current set of environment overrides on this view.
Env []string
// TODO: Remove the option once we are certain there are no issues here. // BuildFlags is used to adjust the build flags applied to the view.
TextDocumentSyncKind protocol.TextDocumentSyncKind BuildFlags []string
Completion CompletionOptions // HoverKind specifies the format of the content for hover requests.
HoverKind HoverKind
ComputeEdits diff.ComputeEdits // DisabledAnalyses specify analyses that the user would like to disable.
URLRegexp *regexp.Regexp DisabledAnalyses map[string]struct{}
Analyzers map[string]*analysis.Analyzer // StaticCheck enables additional analyses from staticcheck.io.
StaticCheck bool
// LinkTarget is the website used for documentation.
LinkTarget string
// LocalPrefix is used to specify goimports's -local behavior. // LocalPrefix is used to specify goimports's -local behavior.
LocalPrefix string LocalPrefix string
VerboseOutput bool Completion CompletionOptions
}
type Hooks struct {
GoDiff bool
ComputeEdits diff.ComputeEdits
URLRegexp *regexp.Regexp
Analyzers map[string]*analysis.Analyzer
}
type ExperimentalOptions struct {
// WARNING: This configuration will be changed in the future. // WARNING: This configuration will be changed in the future.
// It only exists while this feature is under development. // It only exists while this feature is under development.
// Disable use of the -modfile flag in Go 1.14. // Disable use of the -modfile flag in Go 1.14.
TempModfile bool TempModfile bool
}
LinkTarget string type DebuggingOptions struct {
VerboseOutput bool
} }
type CompletionOptions struct { type CompletionOptions struct {
@ -241,12 +279,6 @@ func (o *Options) set(name string, value interface{}) OptionResult {
} }
o.BuildFlags = flags o.BuildFlags = flags
case "noIncrementalSync":
if v, ok := result.asBool(); ok && v {
o.TextDocumentSyncKind = protocol.Full
}
case "watchFileChanges":
result.setBool(&o.WatchFileChanges)
case "completionDocumentation": case "completionDocumentation":
result.setBool(&o.Completion.Documentation) result.setBool(&o.Completion.Documentation)
case "usePlaceholders": case "usePlaceholders":
@ -312,9 +344,6 @@ func (o *Options) set(name string, value interface{}) OptionResult {
case "staticcheck": case "staticcheck":
result.setBool(&o.StaticCheck) result.setBool(&o.StaticCheck)
case "go-diff":
result.setBool(&o.GoDiff)
case "local": case "local":
localPrefix, ok := value.(string) localPrefix, ok := value.(string)
if !ok { if !ok {
@ -349,6 +378,15 @@ func (o *Options) set(name string, value interface{}) OptionResult {
result.State = OptionDeprecated result.State = OptionDeprecated
result.Replacement = "completeUnimported" result.Replacement = "completeUnimported"
case "noIncrementalSync":
result.State = OptionDeprecated
case "watchFileChanges":
result.State = OptionDeprecated
case "go-diff":
result.State = OptionDeprecated
default: default:
result.State = OptionUnexpected result.State = OptionUnexpected
} }

View File

@ -132,11 +132,6 @@ func (s *Server) changedText(ctx context.Context, uri span.URI, changes []protoc
return []byte(changes[0].Text), nil return []byte(changes[0].Text), nil
} }
// We only accept an incremental change if that's what the server expects.
if s.session.Options().TextDocumentSyncKind == protocol.Full {
return nil, errors.Errorf("expected a full content change, received incremental changes for %s", uri)
}
return s.applyIncrementalChanges(ctx, uri, changes) return s.applyIncrementalChanges(ctx, uri, changes)
} }

View File

@ -20,9 +20,6 @@ func (s *Server) didChangeWatchedFiles(ctx context.Context, params *protocol.Did
ctx := telemetry.File.With(ctx, uri) ctx := telemetry.File.With(ctx, uri)
for _, view := range s.session.Views() { for _, view := range s.session.Views() {
if !view.Options().WatchFileChanges {
continue
}
action := toFileAction(change.Type) action := toFileAction(change.Type)
switch action { switch action {
case source.Change, source.Create: case source.Change, source.Create: