1
0
mirror of https://github.com/golang/go synced 2024-10-01 16:38:34 -06:00
Commit Graph

2786 Commits

Author SHA1 Message Date
Ian Cottrell
2a3f5192be internal/lsp: handle $GOROOT in file paths
This happens whenever we load standard library information from export
data, and prevents the editor from understanding the file names

Change-Id: If5c04176a3e669ba396f322275993616e51ec097
Reviewed-on: https://go-review.googlesource.com/c/149612
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-11-15 01:11:54 +00:00
Ian Cottrell
6778892796 internal/lsp: use the new marker support
We use the custom marker support to allow us to simplify the
annotations, making it much easier to understand the tests.

Change-Id: Id818a286e4e85f48cfe505f14ec82a80498e494c
Reviewed-on: https://go-review.googlesource.com/c/149611
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-11-15 01:11:27 +00:00
Ian Cottrell
cd212e53e1 make the packagestest marker system more flexible
This exposes the ability to add markers to the public interface, and
changes the way markers are collected to make it so a standard call to
Expect can replicate the internal behaviour.
This allows custom rules to also add marks.

Also add a special EOF identifier that acts like a mark at the end of
the file in which it occurs.

Change-Id: Ic5e41cbc5b7ae3c4d1c5b8baba980147c1d22ef1
Reviewed-on: https://go-review.googlesource.com/c/149610
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-11-15 01:11:15 +00:00
Ian Cottrell
94339b8328 internal/jsonrpc2: change to a more synchronous dispatch model
Delivering each message in a go routine turned out to be problematic, there are some messages
that must be fully processed before later messages are started, and there was no way to guarantee that.
We now push concurrence handling up to the higher level, this has the disadvantage of not being able to guarantee
we respond to call messages correctly, but its a small price to pay.
The LSP currently processes each message fully in order blocking the handler, while we still work on basic
functionality.

Change-Id: If0648c77713ddbe4fed69da97a57696f433b8002
Reviewed-on: https://go-review.googlesource.com/c/149497
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2018-11-14 19:09:51 +00:00
Alan Donovan
7d6b83ca4d go/analysis: exit nonzero upon diagnostics
This change causes singlechecker and multichecker to exit with the
correct error code: 0 for success, 1 for load/analysis and other
errors, 3 for diagnostics.  (We avoid 2 because the flag package uses
it.)

In JSON mode, errors in package loading, parsing, typechecking and
analysis are successfully in JSON format, with exit code 0.

+ Test.

Change-Id: Iaf130ed3d4cb3e747a628af6da8dc97d065aa869
Reviewed-on: https://go-review.googlesource.com/c/149603
Run-TryBot: Alan Donovan <adonovan@google.com>
Reviewed-by: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-11-14 17:55:09 +00:00
Alan Donovan
2f5a1a7a23 go/analysis/passes/stdmethods: show p.T not dir/p.T in diagnostic
Type.String prints named types using the complete package path: "dir/pkg.T"

The notation used by canonicalMethod, and the cmd/vet/all whitelist,
and the one users want to see, uses only the package name: "pkg.T".

Change-Id: If2334a8cca1fb80e947cb105530b946a5a8dec7b
Reviewed-on: https://go-review.googlesource.com/c/149597
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-11-14 16:30:30 +00:00
Alan Donovan
4f7cb802ba go/analysis/passes/asmdecl: turn two diagnostics into log messages
Vet issues a warning (non-error diagnostic) when, for example, it
cannot check an assembly file because the Go and asm symbols are in
different packages. The new analysis API has no concept of warnings:
any diagnostic always causes a non-zero exit.

This change turns the asmdecl diagnostics back into warnings using
log.Print, which is not ideal, but is necessary to pacify cmd/vet/all
and its whitelist during the transition. Better solutions would be for
the new analysis API to have a concept of warning, or for asmdecl to
be silent and cmd/vet/all's whitelist not to expect these messages.

Also, fix a bug in the "cross-check" predicate: cmd/vet confuses the
name of a package and its path. The a∕b∕c names (using Unicode
division slash) that appear in assembly correspond directly to the
path.

The only effective test of this change will be cmd/vet/all itself.

Change-Id: I2e402d48717df723e2efdc2379636ec9b204031d
Reviewed-on: https://go-review.googlesource.com/c/149598
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-11-14 16:30:01 +00:00
Alan Donovan
99072bc9d7 go/analysis/passes/stdmethods: rewrite check to use go/types
Now that vet can rely on go/types, there's no reason to do extra work to
avoid using it. The rewrite lets us get rid of the field list flattening
code, as well as the slight verbosity that comes with go/printer.

While at it, make the testdata/method.go expected errors be more
specific, to make sure that we're not breaking the warnings that are
printed.

This change was originally made to cmd/vet in
https://go-review.googlesource.com/c/148919

Change-Id: I123e64d369e521199712c9807583c53d428534ac
Reviewed-on: https://go-review.googlesource.com/c/149418
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-11-14 14:52:09 +00:00
Rebecca Stambler
7e59e591a2 internal/lsp: implement signature help
Add SignatureHelp functionality to source package. Tests will be added
in a subsequent change.

Change-Id: Ia43280946d96a984c5741273a00c12255d637b2a
Reviewed-on: https://go-review.googlesource.com/c/149177
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
2018-11-13 20:09:34 +00:00
Alan Donovan
0a8e63141f go/analysis/passes/printf: fix false negative with nested pointers
Pointers to compound objects (structs, slices, arrays, maps) are only
followed by fmt if the pointer is at the top level of an argument. This
is to minimise the chances of fmt running into loops.

However, vet did not follow this rule. It likely doesn't help that fmt
does not document that restriction well, which is being tracked in
 #28625.

This change was originally made to cmd/vet as
https://go-review.googlesource.com/c/147997.

Updates #27672.

Change-Id: I65944cf355baedb4578af57046e2bbfd3fe6a9dc
Reviewed-on: https://go-review.googlesource.com/c/149319
Reviewed-by: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
2018-11-13 19:37:33 +00:00
Alan Donovan
c921186869 go/analysis/internal/analysisflags: add flag aliases for renames
Some of the Analyzers' names were changed during the refactoring.
These legacy flags ensure the old names continue to work.

Change-Id: I466aa38ec55071c944fb73571915aa7afb42dbc2
Reviewed-on: https://go-review.googlesource.com/c/149417
Reviewed-by: Michael Matloob <matloob@golang.org>
Run-TryBot: Michael Matloob <matloob@golang.org>
2018-11-13 19:21:26 +00:00
Alan Donovan
ea84011da2 go/analysis/passes/printf: fix some pointer false positives
fmt's godoc reads:

	For compound objects, the elements are printed using these
	rules, recursively, laid out like this:

		struct:             {field0 field1 ...}
		array, slice:       [elem0 elem1 ...]
		maps:               map[key1:value1 key2:value2 ...]
		pointer to above:   &{}, &[], &map[]

That is, a pointer to a struct, array, slice, or map, can be correctly
printed by fmt if the type pointed to can be printed without issues.

vet was only following this rule for pointers to structs, omitting
arrays, slices, and maps. Fix that, and add tests for all the
combinations.

This change was originally made to cmd/vet in
https://go-review.googlesource.com/c/147758

Updates #27672.

Change-Id: I7e25ecaeed619ae8b6ada79bccacba6b67171733
Reviewed-on: https://go-review.googlesource.com/c/149318
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-11-13 18:24:24 +00:00
Alan Donovan
150d8ac285 go/analysis/cmd/vet-lite: remove deprecation warnings
Per discussion with Russ,
the -all/-source/-v flags now silently do nothing, and
the -printffuncs (et al) shims now silently delegate to -printf.funcs, and
the -NAME.enable (et al) flags are now called just -NAME.

Various minor tweaks to command-line help messages.

Change-Id: If6587937f58446e605eca4d3a5be0aaf6287065d
Reviewed-on: https://go-review.googlesource.com/c/148879
Reviewed-by: Russ Cox <rsc@golang.org>
2018-11-13 15:29:50 +00:00
Rebecca Stambler
a8570e12b6 internal/lsp: use bytes.Buffer instead of strings.Builder
x/tools only supports Go 1.10 and Go 1.11, but it's such a simple fix to
support 1.9, so change from strings.Builder to bytes.Buffer.

Change-Id: Ie37d6c7da7ce7dbbd4e9ec933e1eff3304142a59
Reviewed-on: https://go-review.googlesource.com/c/149178
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-11-13 03:38:37 +00:00
Alan Donovan
5a00de994c go/packages: remove .s files from go list's CompiledGoFiles
This is a workaround for a go list regression that broke
go/packages but went unnoticed by because of a missing
call to packages.PrintErrors, added here.

Updates golang/go#28749

Change-Id: I1819a6143134a422791106ac037d3458ef864322
Reviewed-on: https://go-review.googlesource.com/c/149237
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2018-11-13 00:58:03 +00:00
Agniva De Sarker
c340431777 cmd/godoc: start RunIndexer synchronously when index is present
Until the index is read completely, search requests will serve
an "indexing in progress" message. We make this synchronous
to allow the index to be read completely before starting to serve
requests.

Fixes golang/go#24965

Change-Id: I6b094374a9c5cc923f0582107dde2b652e64fd96
Reviewed-on: https://go-review.googlesource.com/c/148998
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-11-12 23:23:51 +00:00
Mikołaj Baranowski
5f9a541373 internal/fastwalk: avoid slice bounds out of range for long file names
Fixes golang/go#28715

Change-Id: Ibed78d2376f4ec010ee9e5e772069afd14fd5552
Reviewed-on: https://go-review.googlesource.com/c/148884
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-11-12 23:16:31 +00:00
Rebecca Stambler
7f27c5d70a internal/lsp: move diagnostics logic to source directory
Change-Id: I6bea7a76501e852bbf381eb5dbc79217e1ad10ac
Reviewed-on: https://go-review.googlesource.com/c/148889
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2018-11-12 22:54:56 +00:00
Ian Cottrell
4b1f3b6b16 internal/lsp: make format work on the ast not the source
This makes the format code use the AST that is already cached on the file to do
the formatting. It also moves the core format code into the source directory.

Change-Id: Iaa79169708e92525cce326ea094ab98144fe1011
Reviewed-on: https://go-review.googlesource.com/c/148198
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2018-11-12 21:02:38 +00:00
Rebecca Stambler
806e1cfd89 internal/lsp: add a preliminary test for completion
Use the packagestest framework to test completion. Add support for a
slice of token.Position to packagestest to support this.

Change-Id: Ie5ddece4446a3c74419727461a77faa3788cb040
Reviewed-on: https://go-review.googlesource.com/c/148197
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
2018-11-12 19:56:38 +00:00
Michael Matloob
680468b755 go/packages: fix flaky TestJSON and reenable it on Go 1.10
The fallback driver wasn't returning the roots in a
deterministic order because it was using sticking them in
a map and then iterating over that map to get each element.
Put them into a slice instead (and make a few small
associated changes) to preserve behavior.

Fixes golang/go#28040
Fixes golang/go#28609

Change-Id: Ib8f8c88d65b7a48b2b04ca91e2d3c316d5bb5803
Reviewed-on: https://go-review.googlesource.com/c/148880
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-11-12 16:24:42 +00:00
Diogo Pinela
6d71ab8aad go/internal/gcimporter: ensure tests pass even if GOROOT is read-only
This mainly entails writing compiler output files to a temporary
directory, as well as the corrupted files in TestVersionHandling.

This is a backport of CL 146119.

Fixes golang/go#28644

Change-Id: I5701fe3fda7d6364411eef8265c575c279dbf9a1
Reviewed-on: https://go-review.googlesource.com/c/149017
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Griesemer <gri@golang.org>
2018-11-11 00:37:25 +00:00
Alan Donovan
92d8274bd7 go/analysis/passes/printf: preload with facts for std lib
This change adds to the list of standard library functions known to be
print or printf wrappers.

Although the printf Analyzer is capable of identifying wrapper
functions in the standard library, some drivers (e.g. Bazel) do not
apply analyzers to the standard packages. Really this is a  bug
in those drivers but it is not likely to be fixed for a while.

Change-Id: I2032d0cb5fcb50e7b9933a75809becdd680380ec
Reviewed-on: https://go-review.googlesource.com/c/148572
Run-TryBot: Alan Donovan <adonovan@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-11-09 20:29:20 +00:00
Alan Donovan
a00bb74625 go/analysis/cmd/vet-lite: make CLI closer to cmd/vet
Details:
- Add -source, -v, and -all flags to vet-lite.
  These have no effect and issue a warning.
- Add usage message to vet-lite that lists all
  analyzers and explains -foo.enable and other flags.
- Factor this help message (common to vet-lite and
  multichecker) into analysisflags.
- Add legacy aliases of new flags.
  e.g. -printfuncs is now -printf.funcs
  The old names work but issue a warning when used.

Also: update comments to say -vettool not$GOVETTOOL

I think we should probably do away with singlechecker
in a follow-up: a singleton multichecker is good enough,
and will allow us to remove cases in the flag-processing
logic.

Change-Id: Ib62f16b5e2f4c382a29e6300a6246b2db9e08049
Reviewed-on: https://go-review.googlesource.com/c/148559
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-11-09 19:31:51 +00:00
Alan Donovan
4e34152f16 cmd/vet: lostcancel: suppress the check in the main.main function
When main.main returns, the process exits, so there's no need to cancel contexts.

This CL was originally reviewed as
  https://go-review.googlesource.com/c/go/+/106915
and then retried in
  https://go-review.googlesource.com/c/go/+/148758
but then reverted due to an embarrassing sequence
of careless moves.

Change-Id: Icdee0650996a442023e030697f10d2c31fd5fdff
Reviewed-on: https://go-review.googlesource.com/c/148877
Run-TryBot: Alan Donovan <adonovan@google.com>
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-11-09 18:25:37 +00:00
Daniel Martí
d3a25d70bd go/packages: fix minor godoc typo
Spotted while reading some docs.

Change-Id: I856c73b55f459fd980591ba5525d5170d035d79c
Reviewed-on: https://go-review.googlesource.com/c/148797
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-11-09 17:23:37 +00:00
Michael Stapelberg
138c20b932 go/callgraph/rta: fix comment
Change-Id: I52d37297118f9e99f3a71d3a9cf47a01da4aa72c
Reviewed-on: https://go-review.googlesource.com/c/38260
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-11-09 15:26:31 +00:00
Nicholas Ng
8542fc29bd go/ssa: updated inconsistent docs after Program.Method rename
Fixes golang/go#20225

Change-Id: Ifd069546698981f3c6a3673dd73a1bd770560f8b
Reviewed-on: https://go-review.googlesource.com/c/42570
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-11-09 15:25:51 +00:00
matematik7
84d77ece31 cmd/bundle: permit empty -prefix
This enables bundling of programs with `main` method to single file.
I used this to upload code for the new codejam.

Change-Id: I9e65ba8717c1d81d6fb9bef8f234c21fc5b91421
GitHub-Last-Rev: 5d0d623c46ff1629e7371693ddf0916df328e319
GitHub-Pull-Request: golang/tools#32
Reviewed-on: https://go-review.googlesource.com/c/105515
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-11-09 15:23:16 +00:00
Alan Donovan
879803d4ad go/analysis/passes/unmarshal: port vet's unmarshal checker
The checker has been modified to avoid making two memory allocations
for every CallExpr in the program.

Originally: https://go-review.googlesource.com/c/139997
Updates golang/go#27564

Change-Id: I168869272a1d78d47d84c049aba619bb223cad70
Reviewed-on: https://go-review.googlesource.com/c/148562
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-11-09 14:28:11 +00:00
Alan Donovan
77439c5518 go/analysis/passes/shadow: add shadow command
In vet, the shadow check is experimental, meaning not on by default.
The new analysis API has no concept of experimental, but you can
easily supply a different checker. By providing a shadow command, we
make it easy for users that want it to run it:

  $ go install golang.org/x/tools/go/analysis/passes/shadow/cmd/shadow
  $ go vet -vettool $(which shadow) my/project

Change-Id: I25dc7f3c830296121c7217e4615e8ff90e1b7c79
Reviewed-on: https://go-review.googlesource.com/c/148565
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-11-08 22:19:41 +00:00
Fazlul Shahriar
5b8b0ce6cc go/packages: pass TestConfigDefaultEnv on Plan 9
Change-Id: Ice0d44c97dc76bf0ebfe433577d55eb6763cb6d3
Reviewed-on: https://go-review.googlesource.com/c/132601
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-11-08 22:19:39 +00:00
Alan Donovan
fc0741f0ff go/analysis/printf: delete
The printf checker is in the passes/ subdirectory.

Change-Id: I0a912231280bc954fee3088050541ba5ecb17dde
Reviewed-on: https://go-review.googlesource.com/c/148571
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
2018-11-08 21:04:34 +00:00
Alan Donovan
353f99afd4 go/analysis/passes/cgocall: fix typo in comment
Change-Id: Ie71f93c1fa0d5e7de498ad2af611c4db8a27171d
Reviewed-on: https://go-review.googlesource.com/c/148561
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-11-08 19:46:55 +00:00
Alan Donovan
e5d4b58ff5 go/analysis/passes/cgocall: analyze raw cgo files (again)
The cgocall analyzer was originally written in vet to analyze "raw" cgo files,
which are type-checked on a best-effort basis.  However, the go/analysis
API presents analyzers with "cooked" cgo files, which are well-typed
legal Go but obscure the relationship between a call C.f(...) and its arguments.
Prior to this CL, cgocall attempted to "uncook" the file, which was
as predictably fragile as it sounds, and it rapidly broke as the cgo
recipe evolved.

This change causes cgocall to parse, modify, type-check and analyze "raw"
cgo files. The approach (based on dot-importing the "cooked" package)
is rather too clever but should be more robust than the one it replaces.

Fixes golang/go#28566

Change-Id: I3092a313c64d27153eaaa115fe8635abfed17023
Reviewed-on: https://go-review.googlesource.com/c/147317
Reviewed-by: Michael Matloob <matloob@golang.org>
2018-11-08 19:39:40 +00:00
Brad Fitzpatrick
dfbbb7b6d4 go/packages, go/analysis/internal/unitchecker: skip broken tests for now
Updates golang/go#28609
Updates golang/go#28676

Change-Id: Id0fbc6cb0ce14aed9b20afcd0488708df33d5a62
Reviewed-on: https://go-review.googlesource.com/c/148637
Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-11-08 18:34:11 +00:00
Rebecca Stambler
c26e340d2f internal/lsp: refactor completion and move into source directory
The completion function belongs in internal/lsp/source, so move it
there. Some small refactoring of completion, by moving each type of
completion into helper functions that append to the list of results.

Change-Id: I8599092906609591d499183657fe2d21d1f74df1
Reviewed-on: https://go-review.googlesource.com/c/148397
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2018-11-08 16:15:53 +00:00
Heschi Kreinick
a28dfb48e0 revert "imports: create named imports for name/path mismatches"
This reverts CL 145699 (commit 864069cfd1)

Reason for revert: If the mismatch is bad enough that goimports can't find
it again, then the import is just removed, even if the user adds it back again.

Fixes #28645.

Change-Id: I6c8fc5434c2b56c73b260bcec2c12d8746fac4ad
Reviewed-on: https://go-review.googlesource.com/c/148237
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-11-07 22:50:58 +00:00
Dmitri Shuralyov
4ca49954c3 go/ast/astutil: allow AddNamedImport to add imports with different names
This change makes AddNamedImport more symmetrical to DeleteNamedImport
in behavior, signature, and documentation.

In Go, it is valid to import a package (identified by an import path)
with different names in a single .go file¹. For example, this is ok,
and the program will build as long as there are no unused imports²:

	import (
		"path"
		. "path"
		_ "path"
		pathpkg "path"
	)

The four imports (represented by ast.ImportSpec type) have the same
import path, but different names.

It's currently possible to use DeleteNamedImport to delete the exact
{name, import path} ast.ImportSpec from an AST.

Previously, it wasn't possible to use AddNamedImport to add an exact
{name, import path} ast.ImportSpec if the AST already has another
ast.ImportSpec with the same import path (but different name).
This change fixes that, making it possible to use AddNamedImport as
expected.

Rename the ipath parameter to path in AddNamedImport for consistency
with DeleteNamedImport. Also change the language used in its
documentation to be more precise and consistent with DeleteNamedImport.

Add test cases for this behavior. The DeleteNamedImport test cases
were passing before any changes to the astutil code.

Add test coverage for the return bool value of {Add,Delete}NamedImport.
Document the behavior of DeleteNamedImport when there are duplicate
matching imports.

Simplify string(buf.Bytes()) to buf.String() in test helper code.

¹ https://golang.org/ref/spec#Import_declarations
² https://play.golang.org/p/bi6L34rQbcD

Fixes golang/go#28605
Fixes golang/go#16411

Change-Id: I70e887f5174764ab1cf32a761c8c734e216eea67
Reviewed-on: https://go-review.googlesource.com/c/147448
Run-TryBot: Dmitri Shuralyov <dmitshur@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-11-07 22:33:19 +00:00
Michael Matloob
34b416bd17 go/packages: change the driverResponse.Sizes to have type StdSizes
This will allow Sizes to be marshalled and unmarshalled. All the Sizes
we care about ane StdSizes anyways.

Change-Id: I79d1dcaebba32f7730de4375945e372eeefa78fe
Reviewed-on: https://go-review.googlesource.com/c/147978
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-11-07 21:56:32 +00:00
Heschi Kreinick
42c3d2e9aa internal/gopathwalk: ignore $GOROOT/.../vendor/ in module mode
go list in module mode doesn't like looking at vendor directories in
GOROOT. Skip them.

Change-Id: Iec501fbab70914ea6dd76dcbed97ecda461358d0
Reviewed-on: https://go-review.googlesource.com/c/148159
Run-TryBot: Heschi Kreinick <heschi@google.com>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2018-11-07 20:51:50 +00:00
Heschi Kreinick
864069cfd1 imports: create named imports for name/path mismatches
For clarity and performance reasons, we want the fast path of goimports
to be purely syntactic. Packages whose import paths don't match their
package names make this harder. Before this CL, we parsed each imported
package to get its real package name. Now, we make named imports for
such packages, and on subsequent runs we don't have to do the extra
work.

A package name matches its import path if the name is the last segment
of the path, or the next-to-last before a version suffix vNN. gopkg.in
style .vNN suffixes are considered mismatching.

goimports already had almost exactly the desired logic, but only when
adding a new import. So the bulk of this change is simply removing the
logic that allowed it to recognize that a mismatched import satisfied
some uses. With that gone, it will remove those imports as unused, then
add a new renamed import. Some comments may be destroyed.

Fixes golang/go#28428

Change-Id: I53846e6046affb420f41719f84c71086c5b9e5e6
Reviewed-on: https://go-review.googlesource.com/c/145699
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
2018-11-07 19:53:05 +00:00
Ian Cottrell
15ad1aa0cb internal/lsp: implement go to definition
This enables go to definition for the lsp.
It has one known non working case (where the filenames have $GOROOT in them)

Change-Id: I142c6e04b8691c5076dfcd55592ea710b4b361a4
Reviewed-on: https://go-review.googlesource.com/c/148158
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2018-11-07 18:43:41 +00:00
Ian Cottrell
d0600fd9f1 internal/lsp: make source independent of protocol
I realized this was a mistake, we should try to keep the source
directory independent of the LSP protocol itself, and adapt in
the outer layer.
This will keep us honest about capabilities, let us add the
caching and conversion layers easily, and also allow for a future
where we expose the source directory as a supported API for other
tools.
The outer lsp package then becomes the adapter from the core
features to the specifics of the LSP protocol.

Change-Id: I68fd089f1b9f2fd38decc1cbc13c6f0f86157b94
Reviewed-on: https://go-review.googlesource.com/c/148157
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2018-11-07 18:42:35 +00:00
Michael Matloob
aa0cdd1ef5 go/packages: remove contains: query. it's been superceeded by file=
Change-Id: I472787d50e799ef0ee663168201933dd70a1a487
Reviewed-on: https://go-review.googlesource.com/c/147977
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-11-07 16:12:02 +00:00
Michael Matloob
e21233ffa6 go/packages: remove unnecessary TODO
See adonovan's comment here:
https://go-review.googlesource.com/c/tools/+/146757/6/go/packages/golist.go#330

Change-Id: I7885fc1e7787c6b265be345f8622476b69d14325
Reviewed-on: https://go-review.googlesource.com/c/147447
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2018-11-06 21:36:28 +00:00
Rebecca Stambler
6f4a86804e internal/lsp: add implementation of completion to LSP
Add an autocompletion implementation to the LSP. Also, add a function
that type-checks a package at a certain position, returning an *ast.File
and token.Pos for querying completion.

Change-Id: I288d4e6ed168e6014e79a4436f31f5b4a99b7139
Reviewed-on: https://go-review.googlesource.com/c/147657
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2018-11-06 21:04:51 +00:00
Michael Matloob
78dc5bac0c go/packages: determine sizes by calling go list
This is more reliable than looking in the environment.

Change-Id: I96c093b89faaece6b6256eefb4a4aac4d66b9cc9
Reviewed-on: https://go-review.googlesource.com/c/146757
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Alan Donovan <adonovan@google.com>
2018-11-05 23:00:42 +00:00
Ian Cottrell
e504f914a8 internal/lsp: make file a first class concept
A file is strongly associated with a view, and knows how to manage it's own
contents.
We can also now track files that are not "active"

Change-Id: Ib9474cd40e5caa3db6596548612a9f90168b8a19
Reviewed-on: https://go-review.googlesource.com/c/147204
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2018-11-05 21:38:40 +00:00
Ian Cottrell
9b5bafe36f internal/lsp: extract view to its own package
This allows us to write the lsp verbs in terms of a stable underlying source
management layer.
This should make it easier to refactor the underlying layer to add more powerful
caching and incremental modes as we go.

Change-Id: Iab97b061d80394a6fa6748a93a4c68f2deb46129
Reviewed-on: https://go-review.googlesource.com/c/147201
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2018-11-05 21:38:13 +00:00