Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 116 additions & 0 deletions desktop/internal/core/references.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package core

import "context"

// ReferenceLocation is one location of a Find Usages answer as Core groups it.
type ReferenceLocation struct {
// FileKey is the file's identity under the platform's path equality: the path of a local
// file, or the URI of a location that is not one.
FileKey string `json:"fileKey"`
// DisplayPath is the path shown to the user, with / separators, relative to the project for
// a file in it. It decides the file order.
DisplayPath string `json:"displayPath"`
// Line and Column are the zero-based start, the column in UTF-16 units.
Line int `json:"line"`
Column int `json:"column"`
}

// ReferencesQuery asks Core to lay the References tool window out: the locations, the file
// nodes the user closed (by FileKey), whether the Usages node is closed, and the selected key.
type ReferencesQuery struct {
Locations []ReferenceLocation `json:"locations"`
RootCollapsed bool `json:"rootCollapsed,omitempty"`
Collapsed []string `json:"collapsed,omitempty"`
Selected string `json:"selected,omitempty"`
}

// ReferenceGroup is the usages in one file.
type ReferenceGroup struct {
// Key is the key of the file's row.
Key string `json:"key"`
FileKey string `json:"fileKey"`
DisplayPath string `json:"displayPath"`
// Name and Folder are DisplayPath's last segment and the rest; Folder is empty at the root.
Name string `json:"name"`
Folder string `json:"folder"`
// Indices point into the query's locations, in display order.
Indices []int `json:"indices"`
}

// The kinds of a References row.
const (
ReferenceRowRoot = "root"
ReferenceRowFile = "file"
ReferenceRowUsage = "usage"
)

// ReferenceRow is a row of the References tree that shows.
type ReferenceRow struct {
// Key stays the row's while the nodes above it open and close.
Key string `json:"key"`
Kind string `json:"kind"`
Depth int `json:"depth"`
// Expanded is whether a node's children show; nil on a usage.
Expanded *bool `json:"expanded,omitempty"`
// Group is the group of a file or usage row.
Group *int `json:"group,omitempty"`
// Shows is the location the row previews and opens: the usage itself or a node's first.
Shows int `json:"shows"`
// Previous and Next are the usages before and after the row in the tree's order, counting
// the usages under closed nodes; nil at either end.
Previous *int `json:"previous,omitempty"`
Next *int `json:"next,omitempty"`
}

// ReferencesResult is the References tree Core laid out.
type ReferencesResult struct {
Groups []ReferenceGroup `json:"groups"`
Rows []ReferenceRow `json:"rows"`
// Selected is the selected row's key, always one of Rows, and SelectedRow its place; both
// are absent when Rows is empty.
Selected string `json:"selected,omitempty"`
SelectedRow *int `json:"selectedRow,omitempty"`
}

// ReferenceUsageLine is a usage and the text of the line it starts on, by index.
type ReferenceUsageLine struct {
Text int `json:"text"`
Line int `json:"line"`
Column int `json:"column"`
EndLine int `json:"endLine"`
EndColumn int `json:"endColumn"`
}

// ReferenceLinesRequest is the distinct texts of the usages' lines and the usages on them.
type ReferenceLinesRequest struct {
Texts []string `json:"texts"`
Usages []ReferenceUsageLine `json:"usages"`
}

// ReferenceLine is a usage's line as its row shows it, the usage's UTF-16 range in it.
type ReferenceLine struct {
Text string `json:"text"`
Ranges []SearchNameRange `json:"ranges"`
}

// QueryReferences groups a Find Usages answer by file and lays its rows out.
func (c *Client) QueryReferences(ctx context.Context, request ReferencesQuery) (ReferencesResult, error) {
request.Locations = nonNil(request.Locations)
var result ReferencesResult
if err := c.Execute(ctx, "lsp.references.query", request, &result); err != nil {
return ReferencesResult{}, err
}
return result, nil
}

// ReferenceLines shows the usages' lines, one per usage in order.
func (c *Client) ReferenceLines(ctx context.Context, request ReferenceLinesRequest) ([]ReferenceLine, error) {
request.Texts, request.Usages = nonNil(request.Texts), nonNil(request.Usages)
var result struct {
Lines []ReferenceLine `json:"lines"`
}
if err := c.Execute(ctx, "lsp.references.lines", request, &result); err != nil {
return nil, err
}
return result.Lines, nil
}
72 changes: 72 additions & 0 deletions desktop/internal/core/references_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package core

import (
"encoding/json"
"os"
"reflect"
"testing"

"github.com/1lck/Lithe-IDEA/desktop/internal/contractfixture"
)

// The shared fixtures run through the real kernel, so a bridge type that drifted from the JSON
// Core emits fails here.
func TestQueryReferencesSharedFixture(t *testing.T) {
data, err := os.ReadFile(contractfixture.CoreFixture("lsp/references-query-v1.json"))
if err != nil {
t.Fatal(err)
}
var fixture struct {
Cases []struct {
Name string `json:"name"`
Request ReferencesQuery `json:"request"`
Response ReferencesResult `json:"response"`
} `json:"cases"`
}
if err := json.Unmarshal(data, &fixture); err != nil {
t.Fatal(err)
}
client := NewClient()
for _, c := range fixture.Cases {
t.Run(c.Name, func(t *testing.T) {
got, err := client.QueryReferences(testContext(t), c.Request)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, c.Response) {
t.Fatalf("result = %+v, want %+v", got, c.Response)
}
})
}
}

func TestReferenceLinesSharedFixture(t *testing.T) {
data, err := os.ReadFile(contractfixture.CoreFixture("lsp/references-lines-v1.json"))
if err != nil {
t.Fatal(err)
}
var fixture struct {
Cases []struct {
Name string `json:"name"`
Request ReferenceLinesRequest `json:"request"`
Response struct {
Lines []ReferenceLine `json:"lines"`
} `json:"response"`
} `json:"cases"`
}
if err := json.Unmarshal(data, &fixture); err != nil {
t.Fatal(err)
}
client := NewClient()
for _, c := range fixture.Cases {
t.Run(c.Name, func(t *testing.T) {
got, err := client.ReferenceLines(testContext(t), c.Request)
if err != nil {
t.Fatal(err)
}
if !reflect.DeepEqual(got, c.Response.Lines) {
t.Fatalf("lines = %+v, want %+v", got, c.Response.Lines)
}
})
}
}
3 changes: 3 additions & 0 deletions desktop/internal/core/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,9 @@ type WorkspaceSearchMatch struct {
// its matches in UTF-16 units, returned when the request sets IncludeContentRanges.
SourceLine *string `json:"sourceLine,omitempty"`
ContentMatchRanges []SearchNameRange `json:"contentMatchRanges,omitempty"`
// PreviewRanges, returned with SourceLine, are the matches in Preview, which is then the
// line as a result row shows it: without its indentation, cut in front of a far match.
PreviewRanges []SearchNameRange `json:"previewRanges,omitempty"`
}

// SearchNameRange locates one Core-selected highlight in UTF-16 code units.
Expand Down
24 changes: 22 additions & 2 deletions desktop/internal/i18n/shared.generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions desktop/internal/workbench/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,8 @@ type Kernel interface {
PlainPluginSnippet(ctx context.Context, value string) (string, error)
PlanPluginWorkspaceEdit(ctx context.Context, workspaceRoot string, changes map[string][]core.PluginTextEdit, open []core.PluginOpenDocument) (core.PluginWorkspaceEditPlan, error)
QueryProblems(ctx context.Context, request core.ProblemsQuery) (core.ProblemsResult, error)
QueryReferences(ctx context.Context, request core.ReferencesQuery) (core.ReferencesResult, error)
ReferenceLines(ctx context.Context, request core.ReferenceLinesRequest) ([]core.ReferenceLine, error)
ResolveMarketplaceSource(context.Context, string) (core.MarketplaceSource, error)
ValidateMarketplaceIndex(context.Context, json.RawMessage, core.MarketplaceSourceKind, string, map[string][]int) (core.MarketplaceIndex, error)
}
Expand Down
2 changes: 2 additions & 0 deletions desktop/internal/workbench/editor_menu.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ func (a *App) editorMenuItems(c ui.C, p *project, d *document) func(*ui.MenuItem
a.startPluginDefinition(p, d, line, column, true)
})
}
// A right click outside the selection has put the caret on the word clicked, so the
// usages are of that word.
if menu.Item(i18n.Shared(language, "desktop.editorMenu.findUsages")).Keys(gfx.ModifierShift, "f12").Clicked() {
c.After(func() {
line, column := d.view.CaretUTF16()
Expand Down
37 changes: 17 additions & 20 deletions desktop/internal/workbench/pluginsemantics.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,11 +172,8 @@ type completionState struct {

// definitionState is the multi-target definition popup.
type definitionState struct {
open bool
targets []core.PluginSemanticLocation
// references marks a find-references list, which is shown even for a
// single result instead of jumping.
references bool
open bool
targets []core.PluginSemanticLocation
selected int
offset int
documentID int
Expand Down Expand Up @@ -972,7 +969,16 @@ func (a *App) applySemanticDefinition(pending *semanticPending, event core.Backe
return
}
if pending.kind == pluginSemanticReferences {
a.presentDefinitionTargets(p, d, pending.kind, a.usableDefinitionTargets(locations, maxReferenceTargets), len(locations), pending.revision)
targets := a.usableDefinitionTargets(locations, maxReferenceTargets)
if len(targets) == 0 {
a.presentDefinitionTargets(p, d, pending.kind, targets, len(locations), pending.revision)
return
}
// Find Usages fills the References tool window, which keeps the answer while the
// user works through it, instead of a popup that closes on the first click. The
// window's tab names what was searched for, and Rerun asks at the same place again.
query := referenceQuery{documentID: d.id, line: pending.line, column: pending.column}
a.showReferences(p, targets, query, identifierAt(d.view.TextSnapshot(), d.view, pending.line, pending.column))
return
}
if a.plugins != nil && a.plugins.HasDefinitionProvider(p.root, d.path, d.languageID) {
Expand Down Expand Up @@ -1047,8 +1053,9 @@ func (a *App) mergeProviderDefinitions(p *project, d *document, pending *semanti
})
}

// presentDefinitionTargets shows the locations of a definition or references answer, from a language
// server or a plugin provider: one definition opens, several wait in the list, and nothing found says so.
// presentDefinitionTargets shows the locations of a definition answer, from a language server or a
// plugin provider: one definition opens, several wait in the list, and nothing found says so. A
// references answer comes here only when nothing was found; its usages fill the References tool window.
func (a *App) presentDefinitionTargets(p *project, d *document, kind semanticKind, targets []core.PluginSemanticLocation, answered int, revision int) {
references := kind == pluginSemanticReferences
if len(targets) == 0 {
Expand All @@ -1065,19 +1072,12 @@ func (a *App) presentDefinitionTargets(p *project, d *document, kind semanticKin
}
return
}
if len(targets) == 1 && !references {
if len(targets) == 1 {
a.openDefinitionTarget(targets[0])
return
}
if references {
// Find Usages fills the References tool window, which keeps the answer while the
// user works through it, instead of a popup that closes on the first click.
a.showReferences(p, targets, "")
return
}
a.semantics.definition = definitionState{
open: true,
references: references,
targets: targets,
documentID: d.id,
revision: revision,
Expand Down Expand Up @@ -1771,10 +1771,7 @@ func popupOrigin(x, y, width float32, bounds gfx.Rect) (float32, float32) {
}

// definitionPopupTitle is the accessibility title of the open location list.
func (a *App) definitionPopupTitle(references bool) string {
if references {
return i18n.Shared(a.language(), "desktop.semantics.references")
}
func (a *App) definitionPopupTitle() string {
return i18n.Shared(a.language(), "desktop.semantics.definitions")
}

Expand Down
Loading
Loading