This repository has been archived by the owner on Sep 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Goto definition of package and imports (#188)
See docs/ folder for more information
- Loading branch information
Showing
44 changed files
with
2,625 additions
and
332 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dump.lsif |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
set -x | ||
|
||
lsif-go-imports | ||
|
||
lsif-visualize dump.lsif \ | ||
--exclude=sourcegraph:documentationResult \ | ||
--exclude=hoverResult \ | ||
| dot -Tsvg > dump.svg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module imports | ||
|
||
go 1.16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
. "net/http" | ||
s "sort" | ||
) | ||
|
||
func Main() { | ||
sortedStrings := []string{"hello", "world", "!"} | ||
|
||
// s -> sort | ||
s.Strings(sortedStrings) | ||
|
||
// http.CanonicalHeaderKey -> CanonicalHeaderKey | ||
fmt.Println(CanonicalHeaderKey(sortedStrings[0])) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dump.lsif |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#!/bin/bash | ||
|
||
set -e | ||
set -x | ||
|
||
lsif-go | ||
|
||
lsif-visualize dump.lsif \ | ||
--exclude=sourcegraph:documentationResult \ | ||
--exclude=hoverResult \ | ||
| dot -Tsvg > dump.svg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
module smollest | ||
|
||
go 1.16 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
// Hello world, this is a docstring. So we pick this file. | ||
package smollest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
package smollest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Imports | ||
|
||
There are two types of imports available in Go. In both cases, we generate the same reference | ||
to the package itself. This is done by creating an importMoniker. This import moniker | ||
|
||
```go | ||
import "fmt" | ||
// ^^^------ reference github.com/golang/go/std/fmt | ||
|
||
import f "fmt" | ||
// ^--------- local definition | ||
// ^^^---- reference github.com/golang/go/std/fmt | ||
|
||
|
||
// Special Case, "." generates no local def | ||
import . "fmt" | ||
// no local def | ||
// ^^^---- reference github.com/golang/go/std/fmt | ||
``` | ||
|
||
## Example | ||
|
||
So given this kind of import, you will see the following. | ||
|
||
```go | ||
import ( | ||
"fmt" | ||
. "net/http" | ||
s "sort" | ||
) | ||
``` | ||
|
||
- Regular `"fmt"` import. Creates only a reference to the moniker | ||
|
||
 | ||
|
||
- Named `s "sort"` import. Creates both a reference and a definition. Any local | ||
references to `s` in this case will link back to the definition of this import. | ||
`"sort"` will still link to the external package. | ||
|
||
 | ||
|
||
 | ||
|
||
- `.` import. This will also only create a reference, because `.` does not | ||
create a new definition. It just pulls it into scope. | ||
|
||
 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Package Declarations | ||
|
||
|
||
In general, we have used `types.*` structs that match the `types.Object` | ||
interface. However there was no struct that represented the statement: | ||
|
||
```go | ||
package mypkg | ||
``` | ||
|
||
That's the because the majority of the information is held in `types.Package` | ||
and the corresponding definition in `packages.Package.Syntax`. | ||
|
||
Since there was no types.PkgDeclaration or similar available, we created our own. | ||
See [types.go](/internal/indexer/types.go) | ||
|
||
## Definition vs. Reference | ||
|
||
We only emit one definition for a package declaration. The way we pick this is detailed | ||
in `findBestPackageDefinitionPath(...)`. For the `package mypkg`, only the "best" is | ||
picked as the defintion, the other are all emitted as references. This makes sure that we | ||
always jump to the best package declaration when jumping between packages. | ||
|
||
For example, if we have a project that contains two files: | ||
- [lib.go](/docs/examples/smollest/lib.go) | ||
- [sub.go](/docs/examples/smollest/sub.go) | ||
|
||
In this case the project is literally just two | ||
package declarations. The lsif graph will look like this (some nodes removed): | ||
|
||
 | ||
|
||
NOTE: the two ranges point to the same resultSet but only one of the ranges | ||
(the range from the `lib.go` file) is chosen as the result for the definition | ||
request. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Structs | ||
|
||
Structs are generally implemented in a relatively straightforward way. | ||
|
||
For example: | ||
|
||
```go | ||
type MyStruct struct { | ||
Cli http.Client | ||
^^^----------------- definition MyStruct.Cli | ||
^^^^------------ reference github.com/golang/go/std/http | ||
^^^^^^----- reference github.com/golang/go/std/http.Client | ||
} | ||
|
||
``` | ||
|
||
But, for anonymous fields, it is a little more complicated, and ends up looking something like this. | ||
|
||
```go | ||
type NestedHandler struct { | ||
LocalItem | ||
^^^^^^^^^-------- definition MyStruct.LocalItem | ||
^^^^^^^^^-------- reference LocalItem | ||
} | ||
``` | ||
|
||
In this case it is possible to have the same ranges overlapping, so `lsif-go` | ||
will re-use the same range. | ||
|
||
However, in the following case, we have three separate ranges that, while they overlap | ||
are not identical, so they cannot be shared and a new range must be created. | ||
|
||
```go | ||
type Nested struct { | ||
http.Handler | ||
^^^^^^^^^^^^-------- definition Nested.Handler | ||
^^^^---------------- reference github.com/golang/go/std/http | ||
^^^^^^^-------- reference github.com/golang/go/std/http.Handler | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.