core

package
v0.0.1 Latest Latest
Warning

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

Go to latest
Published: Apr 1, 2024 License: MPL-2.0 Imports: 5 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Change

type Change struct {
	// The position at which the change was made.
	Position int64

	// The number of bytes that were removed at the position.
	Removed int64

	// The data inserted at the position. If longer than Removed, the extra
	// bytes will replace the bytes at Position + Removed.
	Data []byte
}

Change represents a change that has been made to a buffer. Changes are described by a position, a length, and the data that was inserted at that position. Removed bytes starting at the Position were removed, and the Data was inserted at the Position.

func (*Change) ReadSeeker

func (c *Change) ReadSeeker(r io.ReadSeeker) io.ReadSeeker

ReadSeeker returns a ReadSeeker with the Change applied to the given ReadSeeker.

type EditorBuffer

type EditorBuffer struct {
	// The name of the buffer. If the buffer is a file, this is the full path to
	// the file. If the buffer is not a file, this can be any string.
	Name string

	// The current cursor position.
	Cursor int64

	// The current selection start position. If there is no selection, this is
	// equal to Cursor.
	SelectionStart int64

	// The underlying buffer containing the actual data.
	Buffer io.ReadSeeker

	// Clipboard holds the current clipboard contents.
	Clipboard []byte

	// The undo stack. When changes are made to the buffer, they are pushed
	// here. This stack serves as the source of truth for the buffer's contents.
	UndoStack []Change

	// The redo stack. When undoing changes, they are popped from the undo stack
	// and pushed here.
	RedoStack []Change

	// Preview is a Change that is currently being edited. Once the user commits
	// the change, it will be pushed onto the UndoStack.
	Preview *Change

	// Regions is a list of user-defined regions in the buffer. This does not
	// include the selection and other internal regions.
	Regions []Region
}

EditorBuffer represents a file or buffer that is open in the editor.

func NewEditorBuffer

func NewEditorBuffer(name string, buffer io.ReadSeeker) *EditorBuffer

NewEditorBuffer creates a new EditorBuffer with the given name and buffer.

func (*EditorBuffer) CommitChange

func (b *EditorBuffer) CommitChange()

CommitChange commits the preview change to the buffer.

func (*EditorBuffer) CopySelection

func (b *EditorBuffer) CopySelection() (int, error)

CopySelection copies the current selection to the clipboard.

func (*EditorBuffer) GetRegions

func (b *EditorBuffer) GetRegions() []Region

GetRegions returns a combined list of user-defined regions and internal regions.

func (*EditorBuffer) GetSelectionRange

func (b *EditorBuffer) GetSelectionRange() (int64, int64)

GetSelectionRange returns the start and end of the current selection.

func (*EditorBuffer) IsDirty

func (b *EditorBuffer) IsDirty() bool

IsDirty returns true if the buffer contains unsaved changes.

func (*EditorBuffer) PreviewChange

func (b *EditorBuffer) PreviewChange(chg *Change)

PreviewChange applies the given change to the preview buffer.

func (*EditorBuffer) ReadSeeker

func (b *EditorBuffer) ReadSeeker() io.ReadSeeker

ReadSeeker returns a ReadSeeker with all changes applied to the underlying buffer.

func (*EditorBuffer) Redo

func (b *EditorBuffer) Redo() bool

Redo redoes the last change.

func (*EditorBuffer) Reload

func (b *EditorBuffer) Reload() error

Reload reloads the buffer from the file that is backing it.

func (*EditorBuffer) Save

func (b *EditorBuffer) Save(fileName string) (int64, error)

Save saves the buffer to the file that is backing it. It will also create a backup file. If fileName is empty, the buffer's name will be used.

func (*EditorBuffer) SaveInPlace

func (b *EditorBuffer) SaveInPlace() error

SaveInPlace will modify the edited file in-place. This will only work if the changes do not change the size of the file.

func (*EditorBuffer) Size

func (b *EditorBuffer) Size() int64

Size returns the size of the buffer.

func (*EditorBuffer) Undo

func (b *EditorBuffer) Undo() bool

Undo undoes the last change.

func (*EditorBuffer) WriteToFile

func (b *EditorBuffer) WriteToFile(filename string) (int64, error)

WriteToFile writes the buffer contents to the given file. Do not call this with the same file that is backing the buffer. To safely save the buffer to the same file, use Save.

type IndexedRange

type IndexedRange struct {
	Index int
	Range
}

type Range

type Range struct {
	Start int64
	End   int64
}

type Region

type Region struct {
	Type RegionType
	Range
}

func GetActiveRegions

func GetActiveRegions(regions []Region, pos int64) []Region

GetActiveRegions returns the list of regions that are active at the given position. Regions are assumed to be sorted by position.

type RegionType

type RegionType int
const (
	RegionTypeNone RegionType = iota
	RegionTypeSelection
	RegionTypeCursor
	RegionTypeDirty
	RegionTypeHighlight
)

Jump to

Keyboard shortcuts

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