Skip to content

Commit

Permalink
textcore: completer and update file info from lines logic
Browse files Browse the repository at this point in the history
  • Loading branch information
rcoreilly committed Feb 22, 2025
1 parent 7c96144 commit fe8169e
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 34 deletions.
6 changes: 0 additions & 6 deletions text/lines/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,12 +264,6 @@ func (ls *Lines) stat() error {
// Returns true if supported.
func (ls *Lines) configKnown() bool {
if ls.fileInfo.Known != fileinfo.Unknown {
// if ls.spell == nil {
// ls.setSpell()
// }
// if ls.Complete == nil {
// ls.setCompleter(&ls.ParseState, completeParse, completeEditParse, lookupParse)
// }
return ls.Settings.ConfigKnown(ls.fileInfo.Known)
}
return false
Expand Down
33 changes: 15 additions & 18 deletions text/textcore/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,6 @@ func (ed *Base) Init() {
ed.OnClose(func(e events.Event) {
ed.editDone()
})

// ed.Updater(ed.NeedsRender) // todo: delete me
}

func (ed *Base) Destroy() {
Expand Down Expand Up @@ -306,48 +304,47 @@ func (ed *Base) SendClose() {

// SetLines sets the [lines.Lines] that this is an editor of,
// creating a new view for this editor and connecting to events.
func (ed *Base) SetLines(buf *lines.Lines) *Base {
oldbuf := ed.Lines
if ed == nil || (buf != nil && oldbuf == buf) {
func (ed *Base) SetLines(ln *lines.Lines) *Base {
oldln := ed.Lines
if ed == nil || (ln != nil && oldln == ln) {
return ed
}
if oldbuf != nil {
oldbuf.DeleteView(ed.viewId)
if oldln != nil {
oldln.DeleteView(ed.viewId)
ed.viewId = -1
}
ed.Lines = buf
ed.Lines = ln
ed.resetState()
if buf != nil {
buf.Settings.EditorSettings = core.SystemSettings.Editor
if ln != nil {
ln.Settings.EditorSettings = core.SystemSettings.Editor
wd := ed.linesSize.X
if wd == 0 {
wd = 80
}
ed.viewId = buf.NewView(wd)
buf.OnChange(ed.viewId, func(e events.Event) {
ed.viewId = ln.NewView(wd)
ln.OnChange(ed.viewId, func(e events.Event) {
ed.NeedsRender()
ed.SendChange()
})
buf.OnInput(ed.viewId, func(e events.Event) {
ln.OnInput(ed.viewId, func(e events.Event) {
if ed.AutoscrollOnInput {
ed.cursorEndDoc()
}
ed.NeedsRender()
ed.SendInput()
})
buf.OnClose(ed.viewId, func(e events.Event) {
ln.OnClose(ed.viewId, func(e events.Event) {
ed.SetLines(nil)
ed.SendClose()
})
phl := buf.PosHistoryLen()
phl := ln.PosHistoryLen()
if phl > 0 {
cp, _ := buf.PosHistoryAt(phl - 1)
cp, _ := ln.PosHistoryAt(phl - 1)
ed.posHistoryIndex = phl - 1
ed.SetCursorShow(cp)
} else {
ed.SetCursorShow(textpos.Pos{})
}
} else {
ed.viewId = -1
}
ed.NeedsRender()
return ed
Expand Down
1 change: 1 addition & 0 deletions text/textcore/complete.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func (ed *Editor) deleteCompleter() {
if ed.Complete == nil {
return
}
ed.Complete.Cancel()
ed.Complete = nil
}

Expand Down
55 changes: 46 additions & 9 deletions text/textcore/editor.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"unicode"

"cogentcore.org/core/base/fileinfo"
"cogentcore.org/core/base/indent"
"cogentcore.org/core/base/reflectx"
"cogentcore.org/core/core"
Expand Down Expand Up @@ -38,8 +39,6 @@ import (
type Editor struct { //core:embedder
Base

// todo: unexport below, pending cogent code update

// ISearch is the interactive search data.
ISearch ISearch `set:"-" edit:"-" json:"-" xml:"-"`

Expand All @@ -51,29 +50,67 @@ type Editor struct { //core:embedder

// spell is the functions and data for spelling correction.
spell *spellCheck

// curFilename is the current filename from Lines. Used to detect changed file.
curFilename string
}

func (ed *Editor) Init() {
ed.Base.Init()
ed.editorSetLines(ed.Lines)
ed.setSpell()
ed.AddContextMenu(ed.contextMenu)
ed.handleKeyChord()
ed.handleMouse()
ed.handleLinkCursor()
ed.handleFocus()
}

// updateNewFile checks if there is a new file in the Lines editor and updates
// any relevant editor settings accordingly.
func (ed *Editor) updateNewFile() {
ln := ed.Lines
if ln == nil {
ed.curFilename = ""
ed.viewId = -1
return
}
fnm := ln.Filename()
if ed.curFilename == fnm {
return
}
ed.curFilename = fnm
if ln.FileInfo().Known != fileinfo.Unknown {
_, ps := ln.ParseState()
fmt.Println("set completer")
ed.setCompleter(ps, completeParse, completeEditParse, lookupParse)
} else {
ed.deleteCompleter()
}
}

// SetLines sets the [lines.Lines] that this is an editor of,
// creating a new view for this editor and connecting to events.
func (ed *Editor) SetLines(buf *lines.Lines) *Editor {
ed.Base.SetLines(buf)
if ed.Lines != nil {
ed.Lines.FileModPromptFunc = func() {
FileModPrompt(ed.Scene, ed.Lines)
}
}
func (ed *Editor) SetLines(ln *lines.Lines) *Editor {
ed.Base.SetLines(ln)
ed.editorSetLines(ln)
return ed
}

// editorSetLines does the editor specific part of SetLines.
func (ed *Editor) editorSetLines(ln *lines.Lines) {
if ln == nil {
ed.curFilename = ""
return
}
ln.OnChange(ed.viewId, func(e events.Event) {
ed.updateNewFile()
})
ln.FileModPromptFunc = func() {
FileModPrompt(ed.Scene, ln)
}
}

// SaveAs saves the current text into given file; does an editDone first to save edits
// and checks for an existing file; if it does exist then prompts to overwrite or not.
func (ed *Editor) SaveAs(filename core.Filename) { //types:add
Expand Down
2 changes: 1 addition & 1 deletion text/textcore/spell.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (ed *Editor) spellCheck(reg *textpos.Edit) bool {
ld := len(wb) - len(lwb)
reg.Region.Start.Char += widx
reg.Region.End.Char += widx - ld
//

sugs, knwn := ed.spell.checkWord(lwb)
if knwn {
ed.Lines.RemoveTag(reg.Region.Start, token.TextSpellErr)
Expand Down

0 comments on commit fe8169e

Please sign in to comment.