workspace

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package workspace models an IDE session: the versioned, on-disk record of what a window had open — the project, the editor tabs (with their cursor, scroll position and folded regions), the split/pane layout, dock visibility, and the text of buffers that were never written to disk.

It exists to replace silkide's original session format, a bare list of paths persisted through preferences and rebuilt by ranging a Go map, so the restored tab order differed on every launch (cmd/silkide/main.go currentSessionPaths / restoreSession). Everything here is ordered: Tabs is a slice, ActiveTab indexes it, and Save normalizes the parts a host naturally builds from maps (fold lines, dock state, unsaved buffers) so saving the same session twice yields byte-identical files.

Sessions are versioned and Load never rejects an older layout. A file with no version is v0 and is migrated forward, which covers both historical shapes: a bare JSON array of paths (what preferences held) and the flat object ged.SessionState wrote.

Index

Constants

View Source
const Version = 1

Version is the session layout this package writes. Save stamps it and Load migrates anything lower.

Variables

This section is empty.

Functions

This section is empty.

Types

type Pane

type Pane struct {
	Tabs   []int `json:"tabs"`
	Active int   `json:"active"`
}

Pane is one editor pane of a split layout. Tabs holds indices into Session.Tabs in that pane's tab-bar order, and Active is the index within Tabs of its focused tab (-1 when the pane is empty). An empty Session.Panes means "a single pane holding every tab", which is what a migrated v0 session gets.

type Pos

type Pos struct {
	Line int `json:"line"`
	Col  int `json:"col"`
}

Pos is a caret position inside a file. Both fields are 0-based — the convention gui.CodeEditor.CursorLine / CursorCol report — so a host round-trips them without arithmetic.

type Session

type Session struct {
	// Version is the layout this session was written with. Save always
	// stamps the current Version; Load migrates older values forward.
	Version int `json:"version"`

	// Project is the project / .silkui file the window was working on,
	// "" when none.
	Project string `json:"project,omitempty"`

	// Tabs are the open editor tabs in tab-bar order. That order is the
	// point of this type — it is what the previous format lost.
	Tabs []Tab `json:"tabs,omitempty"`

	// ActiveTab indexes Tabs, or -1 when nothing is open.
	ActiveTab int `json:"active_tab"`

	// Panes and Split describe the editor split layout.
	Panes []Pane `json:"panes,omitempty"`
	Split Split  `json:"split"`

	// DockState maps a tool-view id (gui.ToolViewDef.Id) to whether its
	// dock was visible.
	DockState map[string]bool `json:"dock_state,omitempty"`

	// Unsaved carries the buffer text of tabs with unsaved edits, keyed
	// by path, so restoring a session does not silently drop work. Tabs
	// absent here are re-read from disk.
	Unsaved map[string]string `json:"unsaved,omitempty"`
}

Session is the restorable state of one IDE window.

func FromPaths

func FromPaths(paths []string) *Session

FromPaths builds a session from an ordered path list — the v0 shape, and what a caller migrating silkide's preferences-backed session already holds. The first path becomes the active tab and per-tab state starts zeroed. Empty entries and repeats are dropped, keeping the first occurrence: the v0 producer appended the design canvas' file and then every editor path, so duplicates are expected input.

func Load

func Load(path string) (*Session, error)

Load reads a session file. A file written by an older silkide is migrated instead of rejected, so upgrading never costs the user their session:

v0  a bare JSON array of paths (preferences' OpenSession list), or a
    flat object with no "version" key (what ged.SaveSession wrote:
    open_files / active_file / last_project)
v1  this package's Session

A missing file is returned as an error — check os.IsNotExist to tell "first run" from "unreadable session". An empty file yields an empty session. A version newer than Version loads best-effort: the fields this build knows are kept as they are, and the version is left alone so a newer build's file is not silently downgraded.

func New

func New() *Session

New returns an empty session stamped with the current Version and no active tab.

func (*Session) Paths

func (s *Session) Paths() []string

Paths returns the tab paths in tab-bar order.

func (*Session) Save

func (s *Session) Save(path string) error

Save writes the session to path as JSON, creating the parent directory when needed. The bytes go to a sibling temp file that is renamed into place, so a crash mid-write cannot leave a truncated session behind.

The output is normalized first: the current Version is stamped, ActiveTab is clamped into range and every tab's Folds is sorted and de-duped. Together with encoding/json emitting map keys in sorted order, that makes the file a pure function of the session — it changes only when the session does, never because a map iterated differently. The receiver itself is left untouched.

type Split

type Split struct {
	Orientation string  `json:"orientation,omitempty"`
	Ratio       float64 `json:"ratio,omitempty"`
}

Split records how the editor area is divided between Session.Panes. Orientation is "" for a single pane, "h" for side-by-side and "v" for stacked; Ratio is the fraction of the area given to the first pane.

type Tab

type Tab struct {
	Path   string `json:"path"`
	Cursor Pos    `json:"cursor"`
	Scroll int    `json:"scroll"`
	Folds  []int  `json:"folds,omitempty"`
}

Tab is one open editor tab.

Scroll is the first visible line index, not a pixel offset, so a restored tab lands on the same code after a font-size or window-height change. Folds lists the start lines of the collapsed regions (the key gui.CodeEditor.IsFolded takes); the editor holds those in a map, so Save sorts and de-dupes the list on the way out.

Jump to

Keyboard shortcuts

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