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: 1 addition & 1 deletion desktop/internal/workbench/catalog.go
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@ func (a *App) reduceChrome(action core.ChromeAction) {
// The Problems list copies the diagnostics it submits to Core. Drop that copy
// when the tool window closes; opening it again asks Core for a fresh answer.
if !a.chrome.isOpen(catalogIndex[itemProblems]) {
a.problems.reset()
a.resetProblems()
}
if !changed {
return
Expand Down
4 changes: 3 additions & 1 deletion desktop/internal/workbench/difftab.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,10 @@ type diffSet struct {
// ignoreSpace reads the diffs ignoring whitespace, for the tabs opened after it is set.
ignoreSpace bool
// mirroring counts the edits the Diff tabs and the documents are mirroring into each other
// (difftab_edit.go); a mirrored edit is not mirrored back.
// (difftab_edit.go); a mirrored edit is not mirrored back. origin is the second view whose
// edit a document is taking, so the document's mirror passes over the view it came from.
mirroring int
origin *editorview.View
}

// find is the tab with id, nil when there is none.
Expand Down
58 changes: 37 additions & 21 deletions desktop/internal/workbench/difftab_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package workbench
import (
"path/filepath"
"runtime"
"strings"

"github.com/1lck/Lithe-IDEA/desktop/internal/core"
"github.com/1lck/Lithe-IDEA/desktop/internal/editorview"
Expand Down Expand Up @@ -52,23 +53,31 @@ func (a *App) diffEdited(t *diffTab, edit editorview.Edit) {
if d == nil {
return
}
p.diffs.mirroring++
ok := d.view.ApplyEdit(edit, true)
p.diffs.mirroring--
if !ok {
if !a.editDocumentFrom(p, d, t.after.view, edit) {
// The document and the side disagree on the text; the side shows the document again.
a.rebuildDiff(t)
}
}

// mirrorDocumentEdit mirrors an edit of document d into the right side of every Diff tab that
// shows its file. The document's own undo already holds the edit, so the side does not record it.
// editDocumentFrom applies an edit the user made in from, a second view of document d (a Diff
// tab's side or the Problems preview), to the document as one undo step of its own. The
// document then mirrors it into its other second views, but not back into from. It reports
// false when the two disagree on the text, and the caller shows the document again.
func (a *App) editDocumentFrom(p *project, d *document, from *editorview.View, edit editorview.Edit) bool {
origin := p.diffs.origin
p.diffs.origin = from
ok := d.view.ApplyEdit(edit, true)
p.diffs.origin = origin
return ok
}

// mirrorDocumentEdit mirrors an edit of document d into its second views: the right side of every
// Diff tab that shows its file and the editable preview that shows it, except the view the edit
// came from. The document's own undo already holds the edit, so a second view does not record it,
// and the report each view makes of the mirrored edit is not mirrored back.
func (a *App) mirrorDocumentEdit(p *project, d *document, edit editorview.Edit) {
if p.diffs.mirroring > 0 {
return
}
for _, t := range p.diffs.list {
if t.after == nil || !diffWritable(t) || diffTabPath(t) != d.path {
if t.after == nil || t.after.view == p.diffs.origin || !diffWritable(t) || diffTabPath(t) != d.path {
continue
}
p.diffs.mirroring++
Expand All @@ -78,6 +87,7 @@ func (a *App) mirrorDocumentEdit(p *project, d *document, edit editorview.Edit)
a.rebuildDiff(t)
}
}
a.mirrorPreviewEdit(p, d, edit)
}

// diffUndoChord takes the undo and redo chords of an editable side for its document, which owns
Expand All @@ -87,21 +97,27 @@ func (a *App) diffUndoChord(t *diffTab, event gfx.KeyEvent) bool {
if event.Release || event.Key != gfx.KeyUnknown {
return false
}
d := a.diffDocument(t)
if d == nil {
return false
}
modifier := gfx.ModifierControl
if runtime.GOOS == "darwin" {
modifier = gfx.ModifierMeta
}
if event.Modifiers&^gfx.ModifierShift != modifier || event.Text != "z" && event.Text != "Z" {
return documentUndoChord(a.diffDocument(t), event)
}

// documentUndoChord takes back or does again in document d the edit an undo or redo chord pressed
// in a second view of it asks for, as the document holds the undo of every edit made in its views:
// Command+Z and Shift+Command+Z on macOS, Control+Z, Shift+Control+Z and Control+Y elsewhere. It
// reports false for any other key.
func documentUndoChord(d *document, event gfx.KeyEvent) bool {
if d == nil || event.Release || event.Key != gfx.KeyUnknown || event.Modifiers&^gfx.ModifierShift != primaryModifier() {
return false
}
if event.Modifiers&gfx.ModifierShift != 0 {
shift := event.Modifiers&gfx.ModifierShift != 0
switch {
case strings.EqualFold(event.Text, "z") && shift:
d.view.Redo()
} else {
case strings.EqualFold(event.Text, "z"):
d.view.Undo()
case runtime.GOOS != "darwin" && strings.EqualFold(event.Text, "y") && !shift:
d.view.Redo()
default:
return false
}
return true
}
Expand Down
18 changes: 18 additions & 0 deletions desktop/internal/workbench/documents.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ type document struct {
path, name string
languageID string
view *editorview.View
// lens is a second view of the text that the user last typed or clicked in: the editable
// preview of the Problems tool window (source_preview.go). The language features follow its
// caret while it is set, so completion, hover and the rest work there as in the editor; nil
// when the document's own editor was the last to take a key or a press.
lens *editorview.View
// conflict is the notice shown while the file changed on disk under unsaved edits.
conflict conflictBanner
// sync is the document's state in its language-server session.
Expand Down Expand Up @@ -828,6 +833,7 @@ func (a *App) closeDocument(p *project, id int) {
// removeDocument takes a tab away without asking about its text. The caller has saved it or
// decided that it is going away: deleting its file, for one.
func (a *App) removeDocument(p *project, d *document) {
a.closePreviewsOfDocument(d)
d.find.dispose()
d.cancelAutoSave()
a.flushCheckpoint(p, d, false)
Expand All @@ -839,6 +845,8 @@ func (a *App) removeDocument(p *project, d *document) {
a.semanticTokensDropped(d)
d.closeEditorView()
p.docs.list = slices.DeleteFunc(p.docs.list, func(x *document) bool { return x.id == d.id })
// The preview's language features may have opened it in a server.
a.closeDocumentInSession(p, d)
}
return
}
Expand Down Expand Up @@ -1017,6 +1025,16 @@ func (a *App) focusedDocument(id gfx.ID) *editorview.View {
return nil
}

// caretView is the view whose caret, selection and geometry the language features follow: the
// preview the user last worked in, or the document's own editor. Both show the same text, so a
// position or an offset is the same in either; the document's own view keeps the revision.
func (d *document) caretView() *editorview.View {
if d.lens != nil {
return d.lens
}
return d.view
}

func (d *document) closeEditorView() {
if d.markerCancel != nil {
d.markerCancel()
Expand Down
82 changes: 51 additions & 31 deletions desktop/internal/workbench/documents_tabless.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ import (
)

// This file keeps the documents that have no tab (matrix id git-diff-review). The right side of a
// working-copy Diff tab edits the file's text, and an unopened file has no document to edit. Such a
// document is read in the background and joins the project's documents: it is saved, autosaved and
// checked on close like any other, but no group holds it and it takes no focus. The editor attaches a
// tab to it when the file is opened (selectDocument), and it is released when no Diff tab shows it
// and it is saved (releaseIdleTablessDocument).
// working-copy Diff tab and the Problems preview edit the file's text, and an unopened file has no
// document to edit. Such a document is read in the background and joins the project's documents: it
// is saved, autosaved and checked on close like any other, but no group holds it and it takes no
// focus. The editor attaches a tab to it when the file is opened (selectDocument), and it is released
// when no Diff tab and no editable preview shows it and it is saved (releaseIdleTablessDocument).

// ensureTablessDocument makes sure the working-copy file of tab t has a document, opening it in the
// background when none is open, and lays the tab out again when it has one.
Expand All @@ -40,40 +40,60 @@ func (a *App) openTablessDocument(p *project, path string) {
}
p.docs.opening[path] = true
registry := a.syntaxRegistry()
name := filepath.Base(path)
kernel, _ := a.kernel.(editorKernel)
a.background(p, 0, func(ctx context.Context) func(*App) {
read, err := readDocumentEncoded(ctx, kernel, path)
var content editorview.Content
var grammar *syntax.Syntax
languageID := ""
if err == nil {
content = editorview.Prepare(read.text)
grammar = registry.SyntaxForPath(path)
if language, ok := registry.ForPath(path); ok {
languageID = language.ID
}
}
file := readTablessFile(ctx, kernel, registry, path)
return func(a *App) {
delete(p.docs.opening, path)
// A file that is not text, or that cannot be read, has no document; its Diff stays read only.
if err != nil || a.project != p || p.docs.byPath(path) != nil {
if file.err != nil || a.project != p || p.docs.byPath(path) != nil {
return
}
p.docs.next++
d := a.newTablessDocument(p, p.docs.next, path, name, content, grammar, languageID)
d.encoding, d.hasBOM, d.diskBytes = read.encoding, read.hasBOM, read.disk
d.tabless = true
p.docs.list = append(p.docs.list, d)
for _, t := range p.diffs.list {
if diffWritable(t) && diffTabPath(t) == path {
a.rebuildDiff(t)
}
}
a.addTablessDocument(p, path, file)
}
})
}

// tablessFile is what a worker read of a file that becomes a document without a tab.
type tablessFile struct {
read documentRead
content editorview.Content
grammar *syntax.Syntax
languageID string
err error
}

// readTablessFile reads path and prepares its text and grammar; it runs on a worker.
func readTablessFile(ctx context.Context, kernel editorKernel, registry *syntax.Registry, path string) tablessFile {
read, err := readDocumentEncoded(ctx, kernel, path)
file := tablessFile{read: read, err: err}
if err != nil {
return file
}
file.content = editorview.Prepare(read.text)
file.grammar = registry.SyntaxForPath(path)
if language, ok := registry.ForPath(path); ok {
file.languageID = language.ID
}
return file
}

// addTablessDocument makes file the document of path without a tab and lays the Diff tabs of the
// file out again, so their right sides become editable.
func (a *App) addTablessDocument(p *project, path string, file tablessFile) *document {
p.docs.next++
d := a.newTablessDocument(p, p.docs.next, path, filepath.Base(path), file.content, file.grammar, file.languageID)
d.encoding, d.hasBOM, d.diskBytes = file.read.encoding, file.read.hasBOM, file.read.disk
d.tabless = true
p.docs.list = append(p.docs.list, d)
for _, t := range p.diffs.list {
if diffWritable(t) && diffTabPath(t) == path {
a.rebuildDiff(t)
}
}
return d
}

// newTablessDocument makes the document of path with its editor, wired the way a tab's document is,
// except for its tab, its input and its watch, which attachDocumentTab adds.
func (a *App) newTablessDocument(p *project, id int, path, name string, content editorview.Content, grammar *syntax.Syntax, languageID string) *document {
Expand Down Expand Up @@ -114,11 +134,11 @@ func (a *App) attachDocumentTab(p *project, d *document) bool {
return true
}

// releaseIdleTablessDocument releases the tabless document of path once no Diff tab shows the file
// and the document has nothing unsaved.
// releaseIdleTablessDocument releases the tabless document of path once no Diff tab and no editable
// preview shows the file and the document has nothing unsaved.
func (a *App) releaseIdleTablessDocument(p *project, path string) {
d := p.docs.byPath(path)
if d == nil || !d.tabless {
if d == nil || !d.tabless || a.previewShowsDocument(d) {
return
}
for _, t := range p.diffs.list {
Expand Down
27 changes: 24 additions & 3 deletions desktop/internal/workbench/editor_view.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (

"github.com/1lck/Lithe-IDEA/desktop/internal/editorview"
"github.com/1lck/Lithe-IDEA/desktop/internal/icons"
"github.com/vivaui/viva/app"
"github.com/vivaui/viva/gfx"
"github.com/vivaui/viva/ui"
)
Expand Down Expand Up @@ -203,7 +204,11 @@ func (a *App) documentInput(p *project, d *document) {
result, _ := d.view.Scroll(event)
return result
},
Text: d.view.TextInput,
// Typing in the document's own editor points the language features back at it.
Text: func(ctx app.TextInputContext) *app.TextInput {
d.lens = nil
return d.view.TextInput(ctx)
},
}
d.draw = func(dc *ui.DrawContext) {
v := d.view
Expand Down Expand Up @@ -240,6 +245,13 @@ func (a *App) documentInput(p *project, d *document) {
// documentKey gives a key pressed while the file's control has the keyboard to the language
// popups, the language shortcuts and the editor.
func (a *App) documentKey(p *project, d *document, event gfx.KeyEvent) gfx.EventResult {
d.lens = nil
return a.documentViewKey(p, d, d.view, event)
}

// documentViewKey gives a key pressed in view, the document's own editor or its editable
// preview, to the language popups, the language shortcuts and view.
func (a *App) documentViewKey(p *project, d *document, view *editorview.View, event gfx.KeyEvent) gfx.EventResult {
// A semantic request belongs to the caret it was placed at. Every key path ends here, so
// one sync covers arrows, edits and commands.
defer a.pluginSemanticsCaretSync()
Expand All @@ -252,7 +264,7 @@ func (a *App) documentKey(p *project, d *document, event gfx.KeyEvent) gfx.Event
if result, handled := a.lineEditKey(p, d, event); handled {
return result
}
if result, handled := d.view.Key(event); handled {
if result, handled := view.Key(event); handled {
return result
}
return gfx.EventResult{}
Expand All @@ -262,6 +274,11 @@ func (a *App) documentKey(p *project, d *document, event gfx.KeyEvent) gfx.Event
// editor: the press on a text and the moves and the release of the selection it starts,
// wherever they go.
func (a *App) documentPointer(p *project, d *document, event gfx.PointerEvent) gfx.EventResult {
// A press puts the caret in the document's own editor; a move over it leaves the language
// features where the user works, so a list opened in the preview stays open.
if event.Kind == gfx.PointerDown {
d.lens = nil
}
if event.Kind == gfx.PointerDown && event.Button == gfx.PointerButtonSecondary {
if line, column, ok := d.view.PositionAt(event.X, event.Y); ok {
a.editor.copyReference.click = &referenceClick{documentID: d.id, line: line, column: column}
Expand All @@ -273,7 +290,7 @@ func (a *App) documentPointer(p *project, d *document, event gfx.PointerEvent) g
if result, handled := a.documentLinkPointer(event, d); handled {
return result
}
if result, handled := a.pluginSemanticsPointer(event, p, d); handled {
if result, handled := a.pluginSemanticsPointer(event, p, d, d.view); handled {
return result
}
result, _ := d.view.Pointer(event)
Expand Down Expand Up @@ -396,6 +413,7 @@ func (a *App) terminalToolBody(c ui.C, p *project) {
// (see windowShortcuts).
func (a *App) onShortcut(event gfx.KeyEvent) gfx.EventResult {
if !event.Release {
a.followKeyboardView(event.Target)
if result := a.pluginSemanticsShortcut(event); result.Handled {
return result
}
Expand All @@ -411,6 +429,9 @@ func (a *App) onShortcut(event gfx.KeyEvent) gfx.EventResult {
if result := a.lineEditShortcut(event); result.Handled {
return result
}
if result, handled := a.sourcePreviewShortcut(event); handled {
return result
}
if v := a.focusedDocument(event.Target); v != nil {
if result, handled := v.Shortcut(event); handled {
return result
Expand Down
Loading
Loading