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
2 changes: 2 additions & 0 deletions desktop/internal/workbench/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,8 @@ type App struct {
// editorFocus is the document whose editor had the keyboard last frame; the file is
// saved when the keyboard leaves it (document_autosave.go).
editorFocus int
// statusPath is the navigation path at the leading end of the status bar (status_path.go).
statusPath statusPath
// github is the GitHub account and its sign-in (github_account.go).
github githubAccount
// pullRequests is the pull-request panel of the Git log (github_prs.go).
Expand Down
10 changes: 4 additions & 6 deletions desktop/internal/workbench/chrome_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -476,16 +476,14 @@ func (a *App) hiddenToolWindowRow(c ui.C, i int) (ui.Box, bool) {
return row, row.Clicked()
}

// statusBar is the strip across the bottom of the workbench: the project on the left, and at
// the trailing end the readouts the user can arrange.
// statusBar is the strip across the bottom of the workbench: the navigation path of the item the
// user is on at the leading end (status_path_view.go), and at the trailing end the readouts the
// user can arrange.
func (a *App) statusBar(c ui.C) {
clicked := -1
ui.Row(c).FillWidth().Height(statusBarHeight).Shrink(0).Background(a.chromeFill()).
Items(ui.Center).PadX(chromeEdge).Children(func() {
k := c.Part("path")
ui.Text(k, a.project.name).Grow(1).Ellipsis().FontSize(a.theme.Metrics.FontSizeS).LineHeight(chromeLineHeight).
Color(a.colors.Subtle).
HitWith(ui.ID(k), a.text.Status.Path, gfx.HitOptions{Role: gfx.RoleStatus, Value: a.project.name, SkipFocus: true})
a.statusPathView(c)
zone := ui.Row(c).Items(ui.Center).Gap(chromeStatusGap).Children(func() {
for _, i := range a.chrome.items[zoneStatus] {
if !a.vimStatusShown(i) {
Expand Down
7 changes: 2 additions & 5 deletions desktop/internal/workbench/document_autosave.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,8 @@ func (a *App) noteEditorFocus(focused gfx.ID) {
return
}
current := 0
for _, d := range p.docs.list {
if d.view.Owns(focused) {
current = d.id
break
}
if d := p.documentOwning(focused); d != nil {
current = d.id
}
left := a.editorFocus
a.editorFocus = current
Expand Down
199 changes: 199 additions & 0 deletions desktop/internal/workbench/status_path.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package workbench

import (
"path/filepath"

"github.com/1lck/Lithe-IDEA/desktop/internal/core"
"github.com/vivaui/viva/gfx"
)

// This file decides what the navigation path at the leading end of the status bar names. As in
// IntelliJ IDEA, the path follows the area the user last put the keyboard in, by a click or by the
// keyboard. The editor names its file, the Project tool window the row selected in it, and the
// Problems tool window the file of the problem selected in it. When the focus moves somewhere
// with no item of its own, such as a terminal, a button or a toolbar, the path keeps naming the
// area it named. The item is read from the area's current selection on every paint, so the path
// follows the arrow keys inside the area. status_path_view.go draws the path.

// statusPathSource is the area whose current item the navigation path names.
type statusPathSource uint8

const (
// statusPathEditor names the file of the selected editor tab. It is the source until the
// user focuses one of the others.
statusPathEditor statusPathSource = iota
// statusPathProject names the row selected in the Project tool window.
statusPathProject
// statusPathProblems names the file of the row selected in the Problems tool window.
statusPathProblems
)

// statusPath is the state of the navigation path.
type statusPath struct {
source statusPathSource
// focus is the keyboard focus the source was last decided for. The source is decided again
// only when the focus moves.
focus gfx.ID
// root and file are the last absolute path made relative to the project. rel is what it
// gave, and inside says whether the file lies in the project. A paint that names the same
// file reuses them instead of allocating the relative path again.
root, file, rel string
inside bool
// valueOf is the project name and crumbs of the last path the bar drew, and value is the
// accessible value made from them.
valueOf statusValueKey
value string
}

// statusCrumbs is what the path names after the project. rel is a project-relative path with
// forward slashes, and dir says its last segment is a folder. A document that is not in the
// project has no rel: name holds its name, and the path shows that name alone, without the
// project. When both rel and name are empty, the path names the project itself.
type statusCrumbs struct {
rel, name string
dir bool
}

// statusValueKey is what the accessible value of the path is made from.
type statusValueKey struct {
project string
crumbs statusCrumbs
}

// noteStatusPathFocus makes the area the focus moved into the source of the path. The status bar
// is declared after the editors and the tool windows, so the rows they declared in this paint are
// known. Focus on anything else leaves the source as it was.
func (a *App) noteStatusPathFocus(p *project, focused gfx.ID) {
s := &a.statusPath
if focused == s.focus {
return
}
s.focus = focused
switch {
case focused.IsZero():
case p.documentOwning(focused) != nil:
s.source = statusPathEditor
case p.tree.rowFocused(focused):
s.source = statusPathProject
case a.problems.rowFocused(focused):
s.source = statusPathProblems
}
}

// statusPathCrumbs is what the path names now: the current item of its source. A source with no
// current item, such as the Problems tool window after it closed, names the editor's file
// instead. With no file open, the path names the project alone.
func (a *App) statusPathCrumbs(p *project) statusCrumbs {
switch a.statusPath.source {
case statusPathProject:
if n := p.tree.selected; n != nil {
if n.path == "" {
return statusCrumbs{}
}
return statusCrumbs{rel: n.path, dir: n.dir}
}
case statusPathProblems:
if file, selected := a.problems.selectedFile(); selected {
if file == "" {
return statusCrumbs{}
}
return a.statusFileCrumbs(p, file, "")
}
case statusPathEditor:
}
d := a.selectedEditorDocument()
if d == nil {
return statusCrumbs{}
}
return a.statusFileCrumbs(p, d.path, d.name)
}

// statusFileCrumbs names the file at the absolute path file. A file in the project is named by
// its path in the project. A file outside it, such as a scratch file or a document a plugin
// provides, is named by its name alone. name is the name a document shows in its tab; when it is
// empty, the last element of the path is used.
func (a *App) statusFileCrumbs(p *project, file, name string) statusCrumbs {
s := &a.statusPath
if s.root != p.root || s.file != file {
s.root, s.file = p.root, file
s.rel, s.inside = projectRelative(p.root, file)
}
if s.inside {
return statusCrumbs{rel: s.rel}
}
if name == "" {
name = filepath.Base(file)
}
return statusCrumbs{name: name}
}

// statusPathValue is the accessible value of the path. It is the project and the crumbs after it
// joined by slashes, or a document's name when the document is not in the project. The value is
// made again only when the path changes.
func (a *App) statusPathValue(project string, crumbs statusCrumbs) string {
s := &a.statusPath
key := statusValueKey{project: project, crumbs: crumbs}
if s.valueOf == key {
return s.value
}
s.valueOf = key
switch {
case crumbs.name != "":
s.value = crumbs.name
case crumbs.rel == "":
s.value = project
default:
s.value = project + "/" + crumbs.rel
}
return s.value
}

// documentOwning is the open document whose editor holds the control focused, or nil when no
// editor holds it.
func (p *project) documentOwning(focused gfx.ID) *document {
for _, d := range p.docs.list {
if d.view.Owns(focused) {
return d
}
}
return nil
}

// rowFocused says whether focused is a row of the Project tool window's tree.
func (t *fileTree) rowFocused(focused gfx.ID) bool {
for i := range t.rows {
if t.view.RowID(i) == focused {
return true
}
}
return false
}

// rowFocused says whether focused is a row of the Problems tool window's tree.
func (v *problemsView) rowFocused(focused gfx.ID) bool {
for i := range v.result.Rows {
if v.view.RowID(i) == focused {
return true
}
}
return false
}

// selectedFile is the absolute path of the file the selected row belongs to. For a diagnostic,
// it is the diagnostic's file. For a file's node, it is that file. For a severity's node, it is
// empty. selected is false when no row is selected.
func (v *problemsView) selectedFile() (file string, selected bool) {
i := v.selectedRow
if i < 0 || i >= len(v.result.Rows) {
return "", false
}
if r := v.result.Rows[i]; r.Kind == core.ProblemsRowGroup {
g, ok := v.group(r.Group)
if !ok || g.Kind != core.ProblemsGroupByFile {
return "", ok
}
return g.FileKey, true
}
d, ok := v.shown(i)
return d.FileKey, ok
}
153 changes: 153 additions & 0 deletions desktop/internal/workbench/status_path_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
package workbench

import (
"os"
"path/filepath"
"testing"

"github.com/1lck/Lithe-IDEA/desktop/internal/core"
"github.com/vivaui/viva/gfx"
)

// statusPathValue is the accessible value of the navigation path the status bar showed in the
// last frame: the project and the crumbs after it, joined by slashes.
func (h *harness) statusPathValue() string {
h.t.Helper()
for _, hit := range h.driver.Snapshot().Hits {
if hit.Role == gfx.RoleStatus && hit.Label == h.app.text.Status.Path {
return hit.Value
}
}
h.t.Fatal("the status bar shows no navigation path")
return ""
}

// expectStatusPath paints what the last step did and fails unless the status bar's navigation
// path is then want.
func (h *harness) expectStatusPath(want string) {
h.t.Helper()
h.settle()
if got := h.statusPathValue(); got != want {
h.t.Fatalf("the status bar's path is %q, want %q", got, want)
}
}

// statusPathFixture opens a project with src/main.go and notes.txt in the Project tool window.
func statusPathFixture(t *testing.T, h *harness) (p *project, source string) {
t.Helper()
root := t.TempDir()
source = filepath.Join(root, "src", "main.go")
if err := os.MkdirAll(filepath.Dir(source), 0o755); err != nil {
t.Fatal(err)
}
for _, file := range []string{source, filepath.Join(root, "notes.txt")} {
if err := os.WriteFile(file, []byte("text\n"), 0o600); err != nil {
t.Fatal(err)
}
}
p = h.openTree(t, root)
// A click leaves the pointer on the editor; ask for the hover at once instead of after the
// pointer rests, so a step does not wait out the delay.
h.app.semantics.hoverDelay = -1
return p, source
}

// openStatusPathFile opens file in an editor and clicks into the editor.
func (h *harness) openStatusPathFile(p *project, file string) {
h.t.Helper()
h.app.openFile(p, file)
h.settle()
d := p.docs.byPath(file)
if d == nil {
h.t.Fatalf("%s did not open", filepath.Base(file))
}
h.press(h.topHit(d.hit))
}

// The path names the item of the area the user last clicked: the editor's file or the row
// selected in the Project tool window.
func TestStatusPathFollowsTheEditorAndTheProjectTree(t *testing.T) {
h := newHarness(t, &testKernel{Client: core.NewClient()})
p, source := statusPathFixture(t, h)
if got := h.statusPathValue(); got != p.name {
t.Fatalf("with no file open the path is %q, want the project %q", got, p.name)
}

h.openStatusPathFile(p, source)
h.expectStatusPath(p.name + "/src/main.go")
for _, crumb := range []string{"src", "main.go"} {
if !h.shows(crumb) {
t.Fatalf("the path does not show the crumb %q", crumb)
}
}

h.press(h.treeRowHit("src").Bounds)
h.expectStatusPath(p.name + "/src")
h.press(h.topHit(p.docs.byPath(source).hit))
h.expectStatusPath(p.name + "/src/main.go")
// A click on a file's row opens it in the preview tab; the path names the row.
h.press(h.treeRowHit("notes.txt").Bounds)
h.expectStatusPath(p.name + "/notes.txt")
// The project's own row names the project alone; the click also closes it.
h.press(h.treeRowHit(p.name).Bounds)
h.expectStatusPath(p.name)
}

// Focus on a terminal, which has no item, keeps the path, and a file outside the project is
// named by its name alone.
func TestStatusPathKeepsItsItemAndNamesAFileOutsideTheProject(t *testing.T) {
h := newHarness(t, &testKernel{Client: core.NewClient()})
p, source := statusPathFixture(t, h)
h.openStatusPathFile(p, source)
h.press(h.treeRowHit("src").Bounds)
h.expectStatusPath(p.name + "/src")

if !h.app.chrome.isOpen(catalogIndex[itemTerminal]) {
h.app.reduceChrome(core.SetChromeOpen(itemTerminal, true))
h.settle()
}
if len(p.terms.list) != 1 {
t.Fatalf("the Terminal tool window opened %d terminals", len(p.terms.list))
}
term := p.terms.list[0]
h.app.focusTerminal(term.id)
awaitDocumentState(t, h, func() bool { return term.view.Owns(h.driver.Snapshot().Focused) })
if got := h.statusPathValue(); got != p.name+"/src" {
t.Fatalf("with the terminal focused the path is %q, want the tree's row kept", got)
}

outside := filepath.Join(t.TempDir(), "outside.txt")
if err := os.WriteFile(outside, []byte("text\n"), 0o600); err != nil {
t.Fatal(err)
}
h.openStatusPathFile(p, outside)
h.expectStatusPath("outside.txt")
}

// In the Problems tool window the path names the file of the selected row: a diagnostic's file
// or a file's node.
func TestStatusPathFollowsTheProblemsSelection(t *testing.T) {
h, a, _ := problemsTreeFixture(t)
name := h.app.project.name
h.clickNamed(problemsRowNamed(t, h, problemsDiagnosticRow(t, h, "note")))
h.expectStatusPath(name + "/b.txt")
h.clickNamed(problemsRowNamed(t, h, a))
h.expectStatusPath(name + "/a.txt")
}

// A paint that names the same file as the paint before it makes no new strings.
func TestStatusPathReusesItsStringsWhileThePathStays(t *testing.T) {
root := t.TempDir()
a := &App{}
p := &project{root: root, name: filepath.Base(root)}
file := filepath.Join(root, "src", "main.go")
if got := a.statusPathValue(p.name, a.statusFileCrumbs(p, file, "main.go")); got != p.name+"/src/main.go" {
t.Fatalf("value = %q", got)
}
allocs := testing.AllocsPerRun(100, func() {
a.statusPathValue(p.name, a.statusFileCrumbs(p, file, "main.go"))
})
if allocs != 0 {
t.Fatalf("a paint of the same path allocated %v times", allocs)
}
}
Loading
Loading