From 71c3ad9cb704149e1ae1c2c6143be90c7d844af7 Mon Sep 17 00:00:00 2001 From: Ian Cottrell Date: Wed, 12 Jun 2019 22:04:42 -0400 Subject: [PATCH] gopls: adding static check to the new gopls module Change-Id: Ic07741211632edb2d808f0e5fd213da3dfef5676 Reviewed-on: https://go-review.googlesource.com/c/tools/+/182179 Run-TryBot: Ian Cottrell TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- gopls/doc/settings.md | 4 ++++ gopls/go.mod | 5 ++++- gopls/go.sum | 16 ++++++++++++++++ gopls/internal/hooks/analysis.go | 28 ++++++++++++++++++++++++++++ gopls/internal/hooks/hooks.go | 3 +++ internal/lsp/source/options.go | 5 +++++ 6 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 gopls/internal/hooks/analysis.go diff --git a/gopls/doc/settings.md b/gopls/doc/settings.md index ebdb8b1c5c..33d01acdad 100644 --- a/gopls/doc/settings.md +++ b/gopls/doc/settings.md @@ -54,6 +54,10 @@ The below settings are considered experimental. They may be deprecated or change A list of the names of analysis passes that should be disabled. You can use this to turn off analyses that you feel are not useful in the editor. +### **staticcheck** *boolean* + +If true, it enables the use of the staticcheck.io analyzers. + ### **completionDocumentation** *boolean* If false, indicates that the user does not want documentation with completion results. diff --git a/gopls/go.mod b/gopls/go.mod index 399c801ce5..d9ec987126 100644 --- a/gopls/go.mod +++ b/gopls/go.mod @@ -2,6 +2,9 @@ module golang.org/x/tools/gopls go 1.11 -require golang.org/x/tools v0.0.0-20190918214516-5a1a30219888 +require ( + golang.org/x/tools v0.0.0-20190918214516-5a1a30219888 + honnef.co/go/tools v0.0.1-2019.2.3 +) replace golang.org/x/tools => ../ diff --git a/gopls/go.sum b/gopls/go.sum index d6e776a530..c17f492e1b 100644 --- a/gopls/go.sum +++ b/gopls/go.sum @@ -1,8 +1,24 @@ +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/google/renameio v0.1.0/go.mod h1:KWCgfxg9yswjAJkECMjeO8J8rahYeXnNhOm40UhjYkI= +github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= +github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= +github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ= +github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI= +github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/crypto v0.0.0-20190510104115-cbcb75029529/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= +golang.org/x/mod v0.0.0-20190513183733-4bf6d317e70e/go.mod h1:mXi4GBBbnImb6dmsKGUJ2LatrhH/nqhxcFungHvyanc= +golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/sync v0.0.0-20190423024810-112230192c58 h1:8gQV6CLnAEikrhgkHFbMAEhagSSnXWGV915qUMm9mrU= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7 h1:9zdDQZ7Thm29KFXgAX/+yaf3eVbP7djjWp/dXAppNCc= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI= +honnef.co/go/tools v0.0.1-2019.2.3 h1:3JgtbtFHMiCmsznwGVTUWbgGov+pVqnlf1dEJTNAXeM= +honnef.co/go/tools v0.0.1-2019.2.3/go.mod h1:a3bituU0lyd329TUQxRnasdCoJDkEUEAqEt0JzvZhAg= diff --git a/gopls/internal/hooks/analysis.go b/gopls/internal/hooks/analysis.go new file mode 100644 index 0000000000..ef385a36ad --- /dev/null +++ b/gopls/internal/hooks/analysis.go @@ -0,0 +1,28 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package hooks + +import ( + "golang.org/x/tools/go/analysis" + "golang.org/x/tools/internal/lsp/source" + "honnef.co/go/tools/simple" + "honnef.co/go/tools/staticcheck" + "honnef.co/go/tools/stylecheck" +) + +func updateAnalyzers(v source.View, analyzers []*analysis.Analyzer) []*analysis.Analyzer { + if v.Options().StaticCheck { + for _, a := range simple.Analyzers { + analyzers = append(analyzers, a) + } + for _, a := range staticcheck.Analyzers { + analyzers = append(analyzers, a) + } + for _, a := range stylecheck.Analyzers { + analyzers = append(analyzers, a) + } + } + return analyzers +} diff --git a/gopls/internal/hooks/hooks.go b/gopls/internal/hooks/hooks.go index 1c133e837b..f9127dc167 100644 --- a/gopls/internal/hooks/hooks.go +++ b/gopls/internal/hooks/hooks.go @@ -9,8 +9,11 @@ package hooks // import "golang.org/x/tools/gopls/internal/hooks" import ( "context" + + "golang.org/x/tools/internal/lsp/cache" ) func Install(ctx context.Context) context.Context { + cache.UpdateAnalyzers = updateAnalyzers return ctx } diff --git a/internal/lsp/source/options.go b/internal/lsp/source/options.go index a5819a5f77..07d7c45a5c 100644 --- a/internal/lsp/source/options.go +++ b/internal/lsp/source/options.go @@ -53,6 +53,8 @@ type Options struct { HoverKind HoverKind DisabledAnalyses map[string]struct{} + StaticCheck bool + WatchFileChanges bool InsertTextFormat protocol.InsertTextFormat ConfigurationSupported bool @@ -232,6 +234,9 @@ func (o *Options) set(name string, value interface{}) OptionResult { o.DisabledAnalyses[fmt.Sprint(a)] = struct{}{} } + case "staticcheck": + result.setBool(&o.StaticCheck) + // Deprecated settings. case "wantSuggestedFixes": result.State = OptionDeprecated