forked from golang/tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
gopls/internal/lsp: add selection range request
Fixes golang/go#36679
- Loading branch information
1 parent
19fb30d
commit e8e3f27
Showing
9 changed files
with
129 additions
and
2 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
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,55 @@ | ||
package lsp | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"golang.org/x/tools/go/ast/astutil" | ||
"golang.org/x/tools/gopls/internal/lsp/protocol" | ||
"golang.org/x/tools/gopls/internal/lsp/source" | ||
"golang.org/x/tools/internal/event" | ||
) | ||
|
||
func (s *Server) selectionRange(ctx context.Context, params *protocol.SelectionRangeParams) ([]protocol.SelectionRange, error) { | ||
ctx, done := event.Start(ctx, "lsp.Server.documentSymbol") | ||
defer done() | ||
|
||
snapshot, fh, ok, release, err := s.beginFileRequest(ctx, params.TextDocument.URI, source.UnknownKind) | ||
defer release() | ||
if !ok { | ||
return nil, err | ||
} | ||
|
||
pgf, err := snapshot.ParseGo(ctx, fh, source.ParseFull) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if len(params.Positions) != 2 { | ||
return nil, fmt.Errorf("expected 2 positions, received %d", len(params.Positions)) | ||
} | ||
|
||
start, err := pgf.Mapper.Pos(params.Positions[0]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
end, err := pgf.Mapper.Pos(params.Positions[1]) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
path, _ := astutil.PathEnclosingInterval(pgf.File, start, end) | ||
|
||
n := path[0] | ||
if len(path) >= 2 && n.Pos() == start && n.End() == end { | ||
n = path[1] | ||
} | ||
|
||
newSelection, err := pgf.Mapper.PosRange(n.Pos(), n.End()) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return []protocol.SelectionRange{{Range: newSelection}}, nil | ||
} |
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
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,13 @@ | ||
package foo | ||
|
||
import "time" | ||
|
||
func Bar(x, y int, t time.Time) int { | ||
zs := []int{1, 2, 3} //@selectionrange("1, 2", "[]int{1, 2, 3}") | ||
|
||
for _, z := range zs { | ||
x = x + z + y + zs[1] | ||
} | ||
|
||
return x + y //@selectionrange("x + ", "x + y") | ||
} |
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
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