Documentation
¶
Overview ¶
Package notes holds the desktop app's open notes: each note's text, the version it was read at, and the loop that saves it as it is typed and applies merges and other writers' changes (docs/design/app.md#documents-saves-and-merges, ADR-0022). It is the Go port of app/src/editor/document.ts and imports no UI: an editor attaches a Buffer, and tests drive it with a fake Env.
Index ¶
- Constants
- type Buffer
- type Change
- type ChangeSet
- func (cs ChangeSet) Apply(text string) (string, error)
- func (cs ChangeSet) Changes() []Change
- func (cs ChangeSet) Compose(next ChangeSet) (ChangeSet, error)
- func (cs ChangeSet) Empty() bool
- func (cs ChangeSet) Len() int
- func (cs ChangeSet) Map(other ChangeSet, before bool) (ChangeSet, error)
- func (cs ChangeSet) MapPos(pos, assoc int) int
- func (cs ChangeSet) NewLen() int
- type Document
- func (d *Document) Attach(b Buffer)
- func (d *Document) Base() string
- func (d *Document) CRLF() bool
- func (d *Document) Changed(hash string)
- func (d *Document) Dismiss()
- func (d *Document) Dispose()
- func (d *Document) Edit(cs ChangeSet)
- func (d *Document) EditIfText(base string, cs ChangeSet) bool
- func (d *Document) Flush(done func())
- func (d *Document) Freeze(reason string) (thaw func())
- func (d *Document) Load()
- func (d *Document) Loaded() bool
- func (d *Document) Local(cs ChangeSet)
- func (d *Document) Moved(to string)
- func (d *Document) Notice() *Notice
- func (d *Document) OnChange(fn func())
- func (d *Document) Path() string
- func (d *Document) ReadOnly() string
- func (d *Document) Reconnected()
- func (d *Document) Reload()
- func (d *Document) Removed()
- func (d *Document) Restore()
- func (d *Document) RetryNow()
- func (d *Document) Save()
- func (d *Document) Status() Status
- func (d *Document) Text() string
- func (d *Document) Unsaved() bool
- func (d *Document) Vault() int64
- type Env
- type NoteAPI
- type Notice
- type NoticeKind
- type Status
Constants ¶
const ( // SaveDelay and SaveMax are the debounce: 1 s after the last change, // at most 10 s after the first. SaveDelay = time.Second SaveMax = 10 * time.Second // RetryFirst and RetryMax bound a failed save's retries: 2, 4, 8 … // 60 s. RetryFirst = 2 * time.Second RetryMax = 60 * time.Second )
The save loop's timing, as the web app's (ADR-0022).
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Buffer ¶
type Buffer interface {
// Apply shows another writer's change or a merge in the editor's text,
// outside its undo history (ADR-0022). The buffer does not report it
// back as the user's edit.
Apply(cs ChangeSet)
// Edit shows a user's change made outside the editor, as one step of
// its undo. The buffer does not report it back either.
Edit(cs ChangeSet)
// Sync reports what the user typed since the last report, through
// Document.Local. The document calls it before it changes or sends
// the text, so its changes apply to the text the user sees.
Sync()
}
Buffer is the editor's side of a document: the text the user edits.
type Change ¶
Change is one replacement, in the coordinates of the text a set applies to: From to To is replaced by Insert.
type ChangeSet ¶
type ChangeSet struct {
// contains filtered or unexported fields
}
ChangeSet replaces ranges of a text with other text, as CodeMirror's ChangeSet does: an edit, a merge, another writer's change. It is a value; its methods return new sets. Offsets count bytes of UTF-8, as MyGo's editor buffer does.
sections holds pairs of (len, ins): a span of len bytes of the old text that is kept (ins == -1) or replaced by ins bytes (ins >= 0), whose text is inserted[i/2] for the pair at i. inserted may be shorter than the pairs; a missing entry is "".
func ChangesOf ¶
ChangesOf makes the set of changes, sorted and not overlapping, on a text of n bytes. Changes out of order or overlapping are an error.
func Diff ¶
Diff returns the changes that make b from a: whole lines first (Myers' algorithm), then each changed run of lines trimmed to the characters that differ, so a typed word is a change of a word. A change never starts or ends inside a character: mapped over another, it could make invalid UTF-8.
func EmptyChanges ¶
EmptyChanges is the set that keeps a text of n bytes as it is.
func (ChangeSet) Changes ¶
Changes lists the set's replacements, in the coordinates of the text it applies to.
func (ChangeSet) Compose ¶
Compose returns the set that applies cs, then next; next applies to the text cs makes.
func (ChangeSet) Map ¶
Map returns cs moved over other, so it applies to the text other makes: both apply to the same text. Where both insert at one place, before puts cs's insertion before other's.
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document is one open note, shared by every tab that shows it. Its methods run on the UI thread.
func NewDocument ¶
NewDocument makes the document of a note; client returns notesd's note domain, or nil while it is not connected. Load reads it.
func (*Document) Attach ¶
Attach gives the document its editor buffer; nil detaches it, which saves at once.
func (*Document) Changed ¶
Changed handles note.changed: another writer's content, unless it is the base.
func (*Document) Dispose ¶
func (d *Document) Dispose()
Dispose stops the document's timers; it does nothing more after.
func (*Document) Edit ¶
Edit makes a user's change from outside the editor, as a property in the panel: the buffer shows it, and its undo takes it back.
func (*Document) EditIfText ¶
EditIfText applies an outside edit only while the visible buffer still has base. Rendered controls use it to reject clicks from an obsolete rendering.
func (*Document) Flush ¶
func (d *Document) Flush(done func())
Flush saves now and calls done once nothing is unsaved, or a failed save waits to retry.
func (*Document) Freeze ¶
Freeze stops the user's edits, for reason, until the func it returns runs. Saves and other writers' changes go on.
func (*Document) Local ¶
Local takes an edit the user made in the editor: the buffer already shows it. cs applies to the document's text.
func (*Document) OnChange ¶
func (d *Document) OnChange(fn func())
OnChange calls fn on the UI thread whenever the status, the notice or the text changes.
func (*Document) Reconnected ¶
func (d *Document) Reconnected()
Reconnected catches up after the connection came back: saves what is unsaved, else reads again.
func (*Document) Reload ¶
func (d *Document) Reload()
Reload reads the note and applies the difference as another writer's change.
func (*Document) Removed ¶
func (d *Document) Removed()
Removed handles note.removed: unsaved text creates the note again, as an edit beats a delete (ADR-0005); otherwise the tabs say it is gone.
func (*Document) Restore ¶
func (d *Document) Restore()
Restore brings a deleted note back from its last version. What is typed meanwhile stays, on top of it.
func (*Document) Save ¶
func (d *Document) Save()
Save writes the text now, unless nothing is unsaved or a write is in flight.
type Env ¶
type Env interface {
// Post runs f on the UI thread.
Post(f func())
// After runs f on the UI thread after d; stop cancels it.
After(d time.Duration, f func()) (stop func())
// Now is the time.
Now() time.Time
}
Env is how a document reaches the UI thread and the clock. Its methods may be called from any goroutine.
type NoteAPI ¶
type NoteAPI interface {
Read(ctx context.Context, p *api.NoteReadParams) (*api.Note, error)
Write(ctx context.Context, p *api.NoteWriteParams) (*api.WriteResult, error)
Create(ctx context.Context, p *api.NoteCreateParams) (*api.WriteResult, error)
Versions(ctx context.Context, p *api.NoteVersionsParams) ([]api.Version, error)
Revert(ctx context.Context, p *api.NoteRevertParams) (*api.WriteResult, error)
}
NoteAPI is what a document needs of notesd; api.NoteClient has it.
type Notice ¶
type Notice struct {
Kind NoticeKind
Conflicts int64
By string
Version int64 // the conflict version, 0 for none
Text string // NoticeRefused: what the editor held
Reason string // NoticeFailed
}
Notice is what a document's tabs say about it.
type NoticeKind ¶
type NoticeKind int
NoticeKind says what a notice is about (docs/specs/ui.md#notices-in-a-tab).
const ( // NoticeConflict: a merge kept the other side of some hunks; the // writer's text is version Version. NoticeConflict NoticeKind = iota + 1 // NoticeRefused: notesd refused the base; the document now holds the // file, and Text what the editor held. NoticeRefused // NoticeFailed: a save failed and retries. NoticeFailed // NoticeDeleted: the note was deleted and nothing was unsaved. NoticeDeleted // NoticeLoadFailed: the note could not be read; Reason says why. NoticeLoadFailed )
The kinds of notice.