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

74 Commits

Author SHA1 Message Date
Muir Manders
1d8cfc4bd2 internal/lsp: omit "iota" completion outside const decls
Add a special check to skip builtin "iota" candidate outside of const
declarations.

Change-Id: I767c012585dfc51b4c07cf5847d3b4083a4a2a7b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/195044
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-09-15 20:16:06 +00:00
Muir Manders
3cd124fa3e internal/lsp: fix completion for nested *ast.BadStmt
Now when trying to fix *ast.BadStmt, we parse the manually extracted
expression using parser.ParseFile instead of parser.ParseExpr.
ParseFile will yield *ast.BadStmt nodes for any bad statements nested
in our first bad statement, allowing us to fix them recursively.

To turn our expression into a "valid" file we can pass to
parser.ParseFile, I wrapped it thusly:

package fake

func _() {
  <our expression>
}

Change-Id: I0d4fd4ebce6450021da8e03caa11d0ae5152ea8d
Reviewed-on: https://go-review.googlesource.com/c/tools/+/194342
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-09-10 16:55:22 +00:00
Muir Manders
5797d2e298 internal/lsp: add more flexible completion tests
Add a new @completePartial note that does not require you to specify
the full list of completions. This gets rid of a lot of noise when you
just want to test the relative order of some completion candidates but
don't care about all the other candidates in scope.

I changed one existing test to use @completePartial as an example.

Change-Id: I56005405477e562803f094c0cac05ef2b854ad1a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/192657
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-09-10 04:40:04 +00:00
Michael Matloob
75be6cdcda internal/lsp: enable suggested fixes by default
Remove the wantSuggestedFixes flag, and run the flagged code
by default.

Add test cases for suggested fixes.
Generate a suggested fix to the assign analysis that suggests removing redundant assignments.
Fix the propagation of suggested fixes (using rstambler's code).

Change-Id: I342c8e0b75790518f228b00ebd2979d24338be3b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/193265
Run-TryBot: Michael Matloob <matloob@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-09-09 19:40:07 +00:00
Muir Manders
dc339cc7c5 internal/lsp: improve completions in go and defer statements
Improve the existing fix-the-AST code to better identify the
expression following the "go" or "defer" keywords:

- Don't slurp the expression start outside the loop since the
  expression might only have a single token.
- Set expression end to the position after the final token, not the
  position of the final token.
- Track curly brace nesting to properly capture an entire "func() {}"
  expression.
- Fix parent node detection to work when BadStmt isn't first statement
  of block.
- Add special case to detect dangling period, e.g. "defer fmt.". We
  insert phantom "_" selectors like go/parser does to prevent the
  dangling "." from messing up the AST.
- Use reflect in offsetPositions so it updates positions in all node
  types. This code shouldn't be called often, so I don't think
  performance is a concern.

I also tweaked the function snippet code so it properly expands
"defer" and "go" expressions to function calls. It thought it didn't
have to expand since there was already a *ast.CallExpr, but the
CallExpr was faked by us and the source doesn't actually contain the
"()" calling parens.

Note that this does not work for nested go/defer statements. For
example, completions won't work properly in cases like this:

go func() {
  defer fmt.<>
}

I think we can fix this as well with some more work.

Change-Id: I8f9753fda76909b0e3a83489cdea69ad04ee237a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/193997
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-09-09 19:09:43 +00:00
Rebecca Stambler
8159a2d3d6 internal/lsp: use protocol.Position for textDocument/prepareRename
Change-Id: Ic896321dff17e788d46dae2efc12ecbf2b2f53dd
Reviewed-on: https://go-review.googlesource.com/c/tools/+/193723
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-09-06 18:55:03 +00:00
Rebecca Stambler
04840ef8f3 internal/lsp: switch to using protocol positions for document symbols
Change-Id: I8e550b753328b8e536bff3bb61b4ff4486fcd4f9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/193722
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-09-06 18:11:17 +00:00
Suzy Mueller
93dcc2f048 internal/lsp: fold contained lines when lineFoldingOnly
Folding ranges need to be computed to present folding ranges that make
sense when lineFoldingOnly is true. This change computes the folding
ranges to include the lines that are contained within the start and end
parenthesis/braces when applicable.

Folding ranges are not returned when the contained nodes begin or end on
the same lines as the parenthesis/brace. This is to avoid misleading
folding ranges like the following in unformatted code:

if true {
	fmt.Println("true") } else {
	fmt.Println("false")
}
---folding "if true {}"--->
if true {
	fmt.Println("false")
}

Change-Id: I2931d02837ad5f2dd96cc93da5ede59afd6bcdce
Reviewed-on: https://go-review.googlesource.com/c/tools/+/192678
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-09-05 23:56:50 +00:00
Rebecca Stambler
df305b82d2 internal/lsp: fix declarations in references
Fixes golang/go#34087

Change-Id: I854c03cd124fe783f838dc53ee76cec5fffa563b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/193379
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-09-05 03:51:44 +00:00
A. Jensen
2161848f5a internal/lsp/source: fixes completion for slice literals of pointers
The existing implementation did not suggest struct field names
when running completion from within a slice literal of
pointers. Now, struct field names are suggested in that case.

Fixes golang/go#33211

Change-Id: I6028420a9a789846b070fcc6e45ec89dc4d898d4
Reviewed-on: https://go-review.googlesource.com/c/tools/+/192277
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-08-30 17:14:47 +00:00
Muir Manders
6afc7fcab5 internal/lsp: enable deep completion and fuzzy matching by default
Invert "useDeepCompletions" config flag to "disableDeepCompletion" and
separate out "disableFuzzyMatching" which reverts to the previous
prefix matching behavior.

I separated fuzzy matching tests out to a separate file so they aren't
entangled with deep completion tests. In coming up with representative
test cases I found a couple issues which I fixed:

- We were treating a fuzzy matcher score of 0 as no match, but the
  matcher returns 0 for candidates that match but have no bonuses. I
  changed the matcher interface so that a score of 0 counts as a
  match. For example, this was preventing a pattern of "o" from
  matching "foo".
- When we lower a candidate's score based on its depth, we were
  subtracting a static multiplier which could result in the score
  going negative. A negative score messes up future score weighting
  because multiplying it by a value in the range [0, 1) makes it
  bigger instead of smaller. Fix by scaling a candidate's score based
  on its depth rather than subtracting a constant factor.

Updates golang/go#32754

Change-Id: Ie6f9111f1696b0d067d08f7eed7b0a338ad9cd67
Reviewed-on: https://go-review.googlesource.com/c/tools/+/192137
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-08-30 16:47:54 +00:00
Suzy Mueller
c9403068c1 internal/lsp: add prepare rename support
Prepare rename gets the range of the identifier to rename. Returns an
error when there is no identifier to rename.

Change-Id: I5e5865bc9ff97e6a95ac4f0c48edddcfd0f9ed67
Reviewed-on: https://go-review.googlesource.com/c/tools/+/191170
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-08-29 20:19:46 +00:00
Suzy Mueller
114c575556 internal/lsp: add foldingRange support
Support textDocument/foldingRange request. Provide folding ranges for
multiline comment blocks, declarations, block statements, field lists,
case clauses, and call expressions.

Fixes golang/go#32987

Change-Id: I9c76e850ffa0e5bb65bee273d8ee40577c342f92
Reviewed-on: https://go-review.googlesource.com/c/tools/+/192257
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-08-29 20:17:30 +00:00
Rebecca Stambler
42f498d34c internal/lsp: use protocol.Ranges for source.Identifier
Change-Id: I42cb957e3c1676e2ec7e3f50dd5e3613f3dd9555
Reviewed-on: https://go-review.googlesource.com/c/tools/+/191880
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-08-29 05:14:58 +00:00
Suzy Mueller
c4a336ef6a internal/lsp: support renaming of import spec
This change allows renamings of the name of an import spec.
Since there is not always explicit identifier available to select and
rename, allow renaming packages from positions within the import spec.

Change-Id: I0a8aaa92c26e1795ddb9c31a1165b2f2ee89aa34
Reviewed-on: https://go-review.googlesource.com/c/tools/+/191165
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-08-23 17:09:09 +00:00
Suzy Mueller
71f556f074 internal/lsp: use the explicit import spec name as ident
When there is an explicit name for an import spec, treat it as its own
identifier, separate from the import path.

Example:
	import h "hello"

The name h is defined in that import spec, not in the package hello
it contains its own references. If asked about a position within the
import path, continue treating that as referencing the imported package.
If the position is within the name, use the identifier there that is
local to that file.

This change allows for go to definition of the explicit name to point to
itself, find all references from the import spec, and rename the
explicit name from the import spec.

Change-Id: Ia1d927a26e73f2dc450d256d71909c006bdf4c37
Reviewed-on: https://go-review.googlesource.com/c/tools/+/191164
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-08-22 17:46:33 +00:00
Suzy Mueller
fc82fb2afd internal/lsp: return error when renaming within an import spec
Since renaming an identifier within an import spec is not yet supported,
return an error when this is encountered. These idents from the import
spec have a nil declaration object.

Import paths that contain '.' or '/' are caught by the valid identifier check
avoiding the crash, but import paths such as "fmt" are not as fmt is a
valid identifier. This change checks if i.decl.obj is nil and returns an error
if it is to avoid the crash.

Fixes golang/go#33768

Change-Id: I4e757b42bedffd648fc821590e4a383826200dc3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/191163
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-08-22 00:03:11 +00:00
Suzy Mueller
caa95bb40b internal/lsp: add completions of unimported std lib pkgs
Unimported packages may be suggested as completion items. Since these
are not yet imported, they should be ranked lower than other candidates.

They also require an additional import statement to be valid, which is
provided as an AdditionalTextEdit.

Adding this import does not use astutil.AddNamedImport, to avoid
editing the current ast and work even if there are errors. Additionally,
it can be hard to determine what changes need to be made to the source
document from the ast, as astutil.AddNamedImport includes a merging
pass. Instead, the completion item simply adds another import
declaration.

Change-Id: Icbde226d843bd49ee3713cafcbd5299d51530695
Reviewed-on: https://go-review.googlesource.com/c/tools/+/190338
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-08-16 18:32:40 +00:00
Rebecca Stambler
a857023c21 internal/lsp: use protocol.Range for diagnostics instead of span.Span
This is the first in a series of many changes that will change the API
of the source package to use different types for positions. Using
token.Pos is particularly fragile, since the pos has to refer to the
specific *ast.File from which it was derived.

Change-Id: I70c9b806f7dd45b2e229954ebdcdd86e2cf3bbbb
Reviewed-on: https://go-review.googlesource.com/c/tools/+/190340
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-08-16 17:06:24 +00:00
Suzy Mueller
9065c182e3 internal/lsp: fix bug renaming local vars with test files present
This fix adds all packages to the renamer packages map.

Renaming performs checks on each package to make sure there are no
conflicts. If there are multiple packages, each package needs to be
checked. The packages were being incorrectly added to the map and were
all being put under a single key.

Fixes golang/go#33664

Change-Id: I68466ce34ac49b700ce6d14ce0b53e2795926fa9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/190399
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-08-15 14:43:58 +00:00
Muir Manders
41f3357785 internal/lsp: don't deep complete struct field names
When it is certain we are completing a struct field name, we don't
want deep completions. The only possible completions are the remaining
field names.

I also silenced the log spam in tests by disabling the go/packages
logger and the lsp logger.

Fixes golang/go#33614

Change-Id: Icec8d92112b1674fa7a6a21145ab710d054919b4
Reviewed-on: https://go-review.googlesource.com/c/tools/+/190097
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-08-13 21:38:55 +00:00
Rebecca Stambler
f07d81a593 internal/lsp: fix documentation for completion items
This change fixes documentation for completion items by using cached
package and AST information to derive the documentation. We also add
testing for documentation in completion items.

Change-Id: I911fb80f5cef88640fc06a9fe474e5da403657e3
Reviewed-on: https://go-review.googlesource.com/c/tools/+/189237
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-08-12 17:13:29 +00:00
Rebecca Stambler
cae9aa5434 internal/lsp: show "do not edit" message when user opens generated file
This is probably a better approach than showing an extra diagnostic,
since a user cannot dismiss a diagnostic.

Fixes golang/go#33397

Change-Id: I92b9a00f51a463673993793abfd4cfb99ce69a91
Reviewed-on: https://go-review.googlesource.com/c/tools/+/188766
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-08-07 16:44:42 +00:00
Suzy Mueller
1bd56024c6 internal/lsp: format files in packages with errors
Packages with errors may still contain files that can be formatted.
Try to format the source of the files in packages that have errors.
This change will still not format files with parse errors.

Updates golang/go#31291

Change-Id: Ia5168d7908948d201eac7f2ee28534022a2d4eb0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/187757
Run-TryBot: Suzy Mueller <suzmue@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-07-26 23:07:22 +00:00
Rebecca Stambler
c81b74871b go/packages, internal/lsp: modify tests to expose overlays bug
https://golang.org/issue/33157 explains the issues with overlays. The
gopls tests caught this bug, but the go/packages tests didn't, so modify
the go/packages tests correspondingly.

Change-Id: I8ea8e06e145aa2420655cbe4884e60f36acfad7b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/186299
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-07-18 19:31:13 +00:00
litleleprikon
8308f91286 internal/lsp: add links search in comments and string literals
Add to "textDocument/documentLink" request handler ability to search
URLs in string literals and comments.

Fixes golang/go#32339

Change-Id: Ic67ad7bd94feba0bb67ab090a8903e30b2dff996
Reviewed-on: https://go-review.googlesource.com/c/tools/+/185219
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-07-12 18:53:41 +00:00
Ian Cottrell
5f9351755f internal/lsp: stop making background contexts everywhere
For all uses inside the lsp we use the detatch logic instead
For tests we build it in the test harness instead
This is in preparation for things on the context becomming important

Change-Id: I7e6910e0d3581b82abbeeb09f9c22a99efb73142
Reviewed-on: https://go-review.googlesource.com/c/tools/+/185677
Run-TryBot: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-07-11 16:38:52 +00:00
Suzy Mueller
7b25e351ac internal/lsp: rename identifiers in test packages
Support renaming of identifiers in test packages. The packages for
all of the references must be checked and the changes need to be
deduped, since both a package and its test package contain some of the
same files.

Fixes golang/go#32974

Change-Id: Ie51e19716faae77ce7e5254eeb3956faa42c2a09
Reviewed-on: https://go-review.googlesource.com/c/tools/+/185277
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-07-09 21:17:00 +00:00
Suzy Mueller
e491332ed8 internal/lsp: compare names when finding references
Objects for builtin types all have position token.NoPos. We do
not want all objects that have position token.NoPos to be matched
when we are looking for references for this object, so we need to
compare the names of the objects as well.

Fixes golang/go#32991

Change-Id: I67e7aba9909ebcbb246203ea5c572debf996c792
Reviewed-on: https://go-review.googlesource.com/c/tools/+/185247
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-07-09 18:45:27 +00:00
Suzy Mueller
07655f7ec7 internal/lsp: return an error when renaming a builtin
Return an error when attempting to rename a builtin identifier.

Fixes golang/go#32992

Change-Id: I7fb0f9cc9499e5afdfb14805c49c820e4da3b601
Reviewed-on: https://go-review.googlesource.com/c/tools/+/185246
Run-TryBot: Suzy Mueller <suzmue@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-07-09 18:23:51 +00:00
Suzy Mueller
c93f28baaa internal/lsp: check no errs for assignability rename
The satisfy package has a precondition for Finder.Find that requires
that the package has no type errors. If this is a check that we would
perform, give an error and do not rename.

Fixes golang/go#32882

Change-Id: Id44b451bf86ff883fd78a6306f2b2565ad3bdeb9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/184857
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-07-08 20:09:55 +00:00
Muir Manders
9d59f9e855 internal/lsp: improve completion support for untyped constants
Add some extra smarts when evaluating untyped constants as completion
candidates. Previously we called types.Default() on the expected type
and candidate type, but this loses the untypedness of an untyped
constant which prevents it from being assignable to any type or named
type other than the untyped constant's default type.

Note that the added logic does not take into account the untyped
constant's value, so you will still get some false positive
completions (e.g. suggesting an untyped negative integer constant when
only a uint would do). Unfortunately go/types doesn't provide a way of
answering the question "is this *types.Const assignable to this
types.Type" since types.AssignableTo only considers a constant's type,
not its value.

Change-Id: If7075642e928f712b127256ae7706a5190e2f42c
GitHub-Last-Rev: 124d2f05b0aec09c9d7004d9da0d900524185b92
GitHub-Pull-Request: golang/tools#128
Reviewed-on: https://go-review.googlesource.com/c/tools/+/184477
Reviewed-by: Suzy Mueller <suzmue@golang.org>
2019-07-03 16:30:32 +00:00
Rebecca Stambler
7e72c71c50 internal/lsp: hide signature help in function literals
Often anonymous functions can be passed as arguments to a function. In
these cases, it can be annoying for a user to see signature help for the
entire duration of their writing this function. This change detects if
the user is typing in a function literal and disables signature help in
that case.

Fixes golang/go#31633

Change-Id: I7166910739b6e1ec0da2ec852336136b81d13be0
Reviewed-on: https://go-review.googlesource.com/c/tools/+/184260
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Suzy Mueller <suzmue@golang.org>
2019-07-02 15:22:45 +00:00
Suzy Mueller
f80f67146e internal/lsp: support renaming import specs
Support the renaming of the imported name of a package within a file.
This case needs to be special cased because the ident may be added or
removed.

Change-Id: I333bc2b2ca5ce81c4a2afb8b10035f525dfad464
Reviewed-on: https://go-review.googlesource.com/c/tools/+/184199
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-07-02 14:51:11 +00:00
Rebecca Stambler
38ae2c8f64 internal/lsp, internal/imports: use the internal goimports library
This change modifies gopls to use the internal goimports library, which
allows us to manually configure the ProcessEnv. We also add a logger to
the ProcessEnv to allow this change not to conflict with gopls's logging
mechanism.

Fixes golang/go#32585

Change-Id: Ic9aae69c7cfbc9b1f2e66aa8d812175dbc0065ce
Reviewed-on: https://go-review.googlesource.com/c/tools/+/184198
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
Reviewed-by: Heschi Kreinick <heschi@google.com>
2019-07-01 19:45:22 +00:00
Suzy Mueller
58bb5bbe30 internal/lsp: find references in test packages
Find references to identifiers in both a package and its test package.

Change-Id: I9d9da4aa37c36c448336aed044df79cfd1c903f1
Reviewed-on: https://go-review.googlesource.com/c/tools/+/183990
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-07-01 19:17:42 +00:00
Suzy Mueller
6cfa55603c internal/lsp: check ident exists for comment rename
The identifier in a reference is used to check for a doc comment.
Implicits do not have an ident, so do not use that to look for a doc
comment.

Also set the context.Context for the renamer.

Change-Id: I085d9e6c11d919222592dcb6fb30982eeb0fc7cd
Reviewed-on: https://go-review.googlesource.com/c/tools/+/184042
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-06-28 17:52:03 +00:00
Rebecca Stambler
212fb13d59 internal/lsp: match completions case-insensitively
Change-Id: I31f2ea338ae3e2ec2837a444705f990e140ebc77
Reviewed-on: https://go-review.googlesource.com/c/tools/+/184159
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-06-28 03:43:36 +00:00
Muir Manders
4298585011 internal/lsp: provide deep completion candidates
Deep completion refers to searching through an object's fields and
methods for more completion candidates. For example:

func wantsInt(int) { }
var s struct { i int }
wantsInt(<>)

Will now give a candidate for "s.i" since its type matches the
expected type.

We limit to three deep completion results. In some cases there are
many useless deep completion matches. Showing too many options defeats
the purpose of "smart" completions. We also lower a completion item's
score according to its depth so that we favor shallower options. For
now we do not continue searching past function calls to limit our
search scope. In other words, we are not able to suggest results with
any chained fields/methods after the first method call.

Deep completions are behind the "useDeepCompletions" LSP config flag
for now.

Change-Id: I1b888c82e5c4b882f9718177ce07811e2bccbf22
GitHub-Last-Rev: 26522363730036e0b382a7bcd10aa1ed825f6866
GitHub-Pull-Request: golang/tools#100
Reviewed-on: https://go-review.googlesource.com/c/tools/+/177622
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-06-27 18:58:03 +00:00
Muir Manders
5d636af2a9 internal/lsp: improve completion support for type assertions
In type assertion expressions and type switch clauses we now infer the
type from which candidates must be assertable. For example in:

var foo io.Writer
bar := foo.(<>)

When suggesting concrete types we will prefer types that actually
implement io.Writer.

I also added support for the "*" type name modifier. Using the above
example:

bar := foo.(*<>)

we will prefer type T such that *T implements io.Writer.

Change-Id: Ib483bf5e7b339338adc1bfb17b34bc4050d05ad1
GitHub-Last-Rev: 965b028cc00b036019bfdc97561d9e09b7b912ec
GitHub-Pull-Request: golang/tools#123
Reviewed-on: https://go-review.googlesource.com/c/tools/+/183137
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-06-26 23:33:08 +00:00
Rebecca Stambler
3cbd95df51 internal/lsp: support a file belonging to multiple packages
This change adds supports for a package belonging to multiple files.
It requires additional packages.Loads for all of the packages to which a
file belongs (for example, if a non-test file also belongs to a package's
test variant).

For now, we re-run go/packages.Load for each file we open, regardless of
whether or not we already know about it.

This solves the issue of packages randomly belonging to a test or not.
Follow-up work needs to be done to support multiple packages in
references, rename, and diagnostics.

Fixes golang/go#32791
Fixes golang/go#30100

Change-Id: I0a5870a05825fc16cc46d405ef50c775094b0fbb
Reviewed-on: https://go-review.googlesource.com/c/tools/+/183628
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-06-26 17:56:19 +00:00
Muir Manders
431033348d internal/lsp: fix function value completions
Previously we would always expand *types.Func completion candidates to
function calls, even if the expected type matched the function itself,
not its return value. Now we check the function itself before we check
its return value. This fixes cases like this:

func foo() int { return 0 }
var f func() int
f = <foo> // now completes to "foo" instead of "foo()"

Also, *types.Var function values were never getting expanded to calls.
I fixed the completion formatting to know that both *types.Func
and *types.Var objects might need to be invoked in the completion
item. This fixes cases like this:

foo := func() int { return 0 }
var i int
i = <foo()> // now completes to "foo()" instead of "foo"

Change-Id: I8d0e9e2774f92866a3dd881092c13019fb3f3fd5
GitHub-Last-Rev: 7442bc84b5bbb86296289bbc745ec56a5f89d901
GitHub-Pull-Request: golang/tools#122
Reviewed-on: https://go-review.googlesource.com/c/tools/+/182879
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-06-20 15:43:39 +00:00
Suzy Mueller
4adf7a708c internal/lsp: add identifier renaming
This change provides support to rename identifiers within a single
package.

The renaming is performed by finding all references to an identifier,
and then creating text edits to replace the existing text with the
new identifier.

Editing an import spec is not supported.

Fixes #27571

Change-Id: I0881b65a1b3c72d7c53d7d6ab1ea386160dc00fb
Reviewed-on: https://go-review.googlesource.com/c/tools/+/182585
Run-TryBot: Suzy Mueller <suzmue@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-06-19 21:54:42 +00:00
Muir Manders
22e91af008 internal/lsp: apply type modifiers to completion candidate
In situations like:

var buf bytes.Buffer
var w io.Writer = &b<>

if we want to complete to "buf" properly we need to apply the "&" type
modifier to buf's type of bytes.Buffer to see that it is assignable
to type io.Writer. Previously we applied type modifiers in reverse to
the "expected" type (io.Writer in this case), but that is obviously
incorrect in this situation since it is nonsensical to
dereference (the reverse of "&") io.Writer.

Change-Id: Ib7ab5761f625217e023286384c23b8c60e677aac
GitHub-Last-Rev: 4be528f2572c9c987334552e3f8a31d4eddce81a
GitHub-Pull-Request: golang/tools#121
Reviewed-on: https://go-review.googlesource.com/c/tools/+/182598
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-06-19 20:27:14 +00:00
Suzy Mueller
b76e30ffa0 internal/lsp: fix references for type switch vars
Implicit local variables for type switches do not appear in the Uses
map and do not have objects associated with them.  This change
associates all of the different types objects for the same local type
switch declaration with one another in the declaration.

The identifier for the implicit local variable does not have a type but
does have declaration objects.

Find references for type switch vars will return references to all the
identifiers in all of the case clauses and the declaration.

Fixes golang/go#32584

Change-Id: I5563a2a48d31ca615c1e4e73b46eabca0f5dd72a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/182462
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
2019-06-19 18:18:01 +00:00
Muir Manders
da514acc47 internal/lsp: suggest completions that satisfy interfaces
When checking if a completion candidate matches the expected type at
the cursor position, we now use types.AssignableTo instead of
types.Identical. This properly handles cases like using a concrete
type to satisfy an interface type.

Calling AssignableTo triggered some crashes related to the fake
"resolved" types we create. Their underlying type was nil, which is
not allowed. We now set their underlying type to the invalid type.

I've also rearranged things so expected type information lives in a
dedicated typeInference struct. For now there is no new information added,
but in subsequent commits there will be more metadata about the
expected type.

Change-Id: I14e537c548960c30e444cf512a4413d75bb3ee45
GitHub-Last-Rev: 7e64ebe32938562648938d7a480195d954b018f2
GitHub-Pull-Request: golang/tools#116
Reviewed-on: https://go-review.googlesource.com/c/tools/+/182358
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-06-17 19:08:20 +00:00
Koichi Shiraishi
028e009f40 internal/lsp: fix typo on license year
Change-Id: I3cd4a55c633fd7a154d35fd3669e13562295242b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/181897
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
2019-06-12 17:47:02 +00:00
Ian Cottrell
346706fc3d internal/lsp: memoize all the parsing
Change-Id: Ic7864a564f88b3fe0a421bb825b01d750610dee9
Reviewed-on: https://go-review.googlesource.com/c/tools/+/181119
Run-TryBot: Ian Cottrell <iancottrell@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-06-11 20:12:58 +00:00
Rebecca Stambler
5b939d657d internal/lsp: fix type checking for unsafe package
Change-Id: I229a9329f38b8fc7f38e964652c582858c4edb5b
Reviewed-on: https://go-review.googlesource.com/c/tools/+/181678
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
2019-06-11 19:41:53 +00:00
Suzy Mueller
bca362e842 internal/lsp: add find all references
This change implements the find all references feature by finding all of
the uses and definitions of the identifier within the current package.

Testing for references is done using "refs" in the testdata files and
marking the references in the package.

Change-Id: Ieb44b68608e940df5f65c3052eb9ec974f6fae6c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/181122
Run-TryBot: Suzy Mueller <suzmue@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2019-06-10 19:06:22 +00:00