notes

package
v0.7.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 22, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
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

type Change struct {
	From, To int
	Insert   string
}

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

func ChangesOf(n int, changes ...Change) (ChangeSet, error)

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

func Diff(a, b string) ChangeSet

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

func EmptyChanges(n int) ChangeSet

EmptyChanges is the set that keeps a text of n bytes as it is.

func (ChangeSet) Apply

func (cs ChangeSet) Apply(text string) (string, error)

Apply returns text with the set's changes. text must be Len bytes long.

func (ChangeSet) Changes

func (cs ChangeSet) Changes() []Change

Changes lists the set's replacements, in the coordinates of the text it applies to.

func (ChangeSet) Compose

func (cs ChangeSet) Compose(next ChangeSet) (ChangeSet, error)

Compose returns the set that applies cs, then next; next applies to the text cs makes.

func (ChangeSet) Empty

func (cs ChangeSet) Empty() bool

Empty reports whether the set changes nothing.

func (ChangeSet) Len

func (cs ChangeSet) Len() int

Len is the length of the text the set applies to.

func (ChangeSet) Map

func (cs ChangeSet) Map(other ChangeSet, before bool) (ChangeSet, error)

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.

func (ChangeSet) MapPos

func (cs ChangeSet) MapPos(pos, assoc int) int

MapPos returns where pos, an offset in the text cs applies to, is in the text it makes. assoc < 0 keeps a position at an insertion before it, assoc > 0 after it.

func (ChangeSet) NewLen

func (cs ChangeSet) NewLen() int

NewLen is the length of the text the set makes.

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

func NewDocument(env Env, client func() NoteAPI, vault int64, path string) *Document

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

func (d *Document) Attach(b Buffer)

Attach gives the document its editor buffer; nil detaches it, which saves at once.

func (*Document) Base

func (d *Document) Base() string

Base is the hash of the content notesd last confirmed.

func (*Document) CRLF

func (d *Document) CRLF() bool

CRLF reports whether the file has "\r\n" line endings.

func (*Document) Changed

func (d *Document) Changed(hash string)

Changed handles note.changed: another writer's content, unless it is the base.

func (*Document) Dismiss

func (d *Document) Dismiss()

Dismiss clears the notice.

func (*Document) Dispose

func (d *Document) Dispose()

Dispose stops the document's timers; it does nothing more after.

func (*Document) Edit

func (d *Document) Edit(cs ChangeSet)

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

func (d *Document) EditIfText(base string, cs ChangeSet) bool

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

func (d *Document) Freeze(reason string) (thaw func())

Freeze stops the user's edits, for reason, until the func it returns runs. Saves and other writers' changes go on.

func (*Document) Load

func (d *Document) Load()

Load reads the note.

func (*Document) Loaded

func (d *Document) Loaded() bool

Loaded reports whether the note has been read.

func (*Document) Local

func (d *Document) Local(cs ChangeSet)

Local takes an edit the user made in the editor: the buffer already shows it. cs applies to the document's text.

func (*Document) Moved

func (d *Document) Moved(to string)

Moved follows the note to a new path.

func (*Document) Notice

func (d *Document) Notice() *Notice

Notice is what the tabs say about the document, nil for nothing.

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) Path

func (d *Document) Path() string

Path is the note's path in its vault.

func (*Document) ReadOnly

func (d *Document) ReadOnly() string

ReadOnly says why the note cannot be edited here, "" when it can.

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) RetryNow

func (d *Document) RetryNow()

RetryNow runs a waiting retry at once.

func (*Document) Save

func (d *Document) Save()

Save writes the text now, unless nothing is unsaved or a write is in flight.

func (*Document) Status

func (d *Document) Status() Status

Status is where the document stands.

func (*Document) Text

func (d *Document) Text() string

Text is the document's text, with "\n" line endings.

func (*Document) Unsaved

func (d *Document) Unsaved() bool

Unsaved reports whether typed text has not reached notesd yet: text to save, or text notesd refused, which only its notice holds until the user merges or dismisses it.

func (*Document) Vault

func (d *Document) Vault() int64

Vault is the note's vault.

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

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.

type Status

type Status int

Status is where a document stands.

const (
	StatusLoading Status = iota
	StatusMissing
	StatusClean
	StatusDirty
	StatusSaving
)

The statuses.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL