From fe8169e89a8afb0c6eff2d7d751d71bdcf242bbe Mon Sep 17 00:00:00 2001 From: "Randall C. O'Reilly" Date: Sat, 22 Feb 2025 12:54:34 -0800 Subject: [PATCH] textcore: completer and update file info from lines logic --- text/lines/file.go | 6 ----- text/textcore/base.go | 33 +++++++++++------------ text/textcore/complete.go | 1 + text/textcore/editor.go | 55 ++++++++++++++++++++++++++++++++------- text/textcore/spell.go | 2 +- 5 files changed, 63 insertions(+), 34 deletions(-) diff --git a/text/lines/file.go b/text/lines/file.go index 11867817f1..17e26e5672 100644 --- a/text/lines/file.go +++ b/text/lines/file.go @@ -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 diff --git a/text/textcore/base.go b/text/textcore/base.go index 9b7b3cc8f3..e1970a8e9b 100644 --- a/text/textcore/base.go +++ b/text/textcore/base.go @@ -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() { @@ -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 diff --git a/text/textcore/complete.go b/text/textcore/complete.go index e6caf6457d..5f1ef9a7f0 100644 --- a/text/textcore/complete.go +++ b/text/textcore/complete.go @@ -52,6 +52,7 @@ func (ed *Editor) deleteCompleter() { if ed.Complete == nil { return } + ed.Complete.Cancel() ed.Complete = nil } diff --git a/text/textcore/editor.go b/text/textcore/editor.go index 978770455a..29bdd859a6 100644 --- a/text/textcore/editor.go +++ b/text/textcore/editor.go @@ -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" @@ -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:"-"` @@ -51,10 +50,15 @@ 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() @@ -62,18 +66,51 @@ func (ed *Editor) Init() { 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 diff --git a/text/textcore/spell.go b/text/textcore/spell.go index 58113af2a0..91a44a3298 100644 --- a/text/textcore/spell.go +++ b/text/textcore/spell.go @@ -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)