Luce packages
LuceLuce BaseLuciaOS

dymokomi/luced

Source of release 0.4.1

0.4.1 / src / workspace / editors.luc

## Document-tab lifetime and selection. Docking itself stays in luce-ui; this
## component decides which document to open and whether a tab may close.
from luce_ui.ui import Command, DStack, DockPosition, DockRequest, Font, Panel, TextEditor, ContextMenu, ColorEditor
from interop import Connection
from editor.panel import EditorPanel
from editor.text_actions import TextActions
from workspace.document import Document
from language.appearance import SyntaxTheme
from language.grammars import GrammarSet
from luce_std import files
from luce_ui import ui
import workspace.paths as paths

pub class Editors:
    pub let dock: DStack
    let anchor: Panel
    let font: Font
    let commands: list[Command]
    let language_commands: list[Command]
    let editing: TextActions
    let tabs: list[EditorPanel]
    let connections: list[Connection]
    var active: EditorPanel? = none
    var last_focused: Panel? = none
    var syntax: SyntaxTheme? = none
    var grammars: GrammarSet? = none
    var wrap: bool = false
    var color: ColorEditor? = none
    var color_target: TextEditor? = none
    var color_offset: int = -1
    var color_wired: bool = false

    pub func init(self, dock: DStack, anchor: Panel, font: Font, commands: list[Command], language_commands: list[Command], editing: TextActions, wrap: bool = false) -> unit!:
        self.dock = dock
        self.anchor = anchor
        self.font = font
        self.wrap = wrap
        self.commands = commands
        self.language_commands = language_commands
        self.editing = editing
        self.tabs = []
        self.connections = []
        self.connections.append(dock.on_select(self.selected))
        self.connections.append(dock.on_add(self.add_requested))
        # The dock's + offers a new editor (a tab, or split beside the group);
        # editors carry no catalog id, so each pick makes another.
        dock.set_catalog(["editor"], ["New Editor"])
        self.connections.append(dock.on_close(self.removed))
        dock.set_close_handler(self.can_close)
        _ = self.create(anchor, DockPosition.top)
        dock.set_fraction(anchor, 0.77)

    ## Adopt the shared color editor and wire its live changes back to the source.
    pub func set_color_editor(self, color: ColorEditor) -> unit!:
        self.color = color
        if not self.color_wired:
            self.connections.append(color.on_change(self.apply_color))
            self.color_wired = true

    ## A color chip was clicked: open the editor under it, seeded from the token.
    func open_color(self, editor: TextEditor, offset: int) -> unit!:
        let color = self.color else return
        let token = editor.swatch_token(offset)
        let anchor = editor.swatch_anchor(offset)
        let bounds = editor.layout().bounds()
        self.color_target = editor
        self.color_offset = offset
        color.open_at(token, bounds.x + anchor.x, bounds.y + anchor.y)

    ## Replace the color token in place with the edited hex (its length may change).
    func apply_color(self, hex: str) -> unit!:
        let editor = self.color_target else return
        let token = editor.swatch_token(self.color_offset)
        editor.set_selection(self.color_offset, self.color_offset + token.length)
        editor.insert(hex)

    pub func documents(self) -> list[Document]:
        var documents: list[Document] = []
        for tab in self.tabs:
            if let document = tab.document: documents.append(document)
        return documents

    ## Refresh every pane's own status bar (caret position moves without a text change).
    pub func refresh_status(self) -> unit!:
        for tab in self.tabs:
            tab.refresh_status()

    func sync_focus(self):
        for tab in self.tabs:
            if tab.body.layout().is_visible() and tab.body.layout().contains_focus():
                let previous = self.last_focused
                if previous == none or not (previous is tab.body):
                    self.active = tab
                    self.last_focused = tab.body
                return

    pub func current_document(self) -> Document?:
        self.sync_focus()
        let active = self.active else return none
        return active.document

    pub func current_editor(self) -> TextEditor?:
        let document = self.current_document() else return none
        return document.editor

    pub func current_panel(self) -> Panel?:
        self.sync_focus()
        let active = self.active else return none
        return active.body

    pub func current_context(self) -> ContextMenu?:
        self.sync_focus()
        let active = self.active else return none
        return active.context

    pub func focus(self) -> unit!:
        if let active = self.active:
            self.dock.select(active.body)
        else:
            _ = self.create(self.anchor, DockPosition.top)

    pub func set_syntax(self, theme: SyntaxTheme) -> unit!:
        self.syntax = theme
        for document in self.documents(): document.set_syntax(theme)

    ## Grammars are loaded once at startup, before any file opens; store them for new documents.
    pub func set_grammars(self, grammars: GrammarSet) -> unit!:
        self.grammars = grammars

    pub func open(self, path: str) -> unit!:
        let identity = files.canonical(path)
        for tab in self.tabs:
            if let document = tab.document:
                if document.path == identity:
                    self.dock.select(tab.body)
                    return
        self.place(Document(identity, self.font, self.syntax, self.grammars, wrap = self.wrap))

    ## Open a fresh untitled buffer in a tab, ready to type into and Save As later.
    pub func new_untitled(self) -> unit!:
        self.place(Document("", self.font, self.syntax, self.grammars, blank = true, wrap = self.wrap))

    pub func wrapping(self) -> bool:
        return self.wrap

    ## Toggle soft wrap for every open document and for tabs opened afterward.
    pub func set_wrap_all(self, enabled: bool) -> unit!:
        self.wrap = enabled
        for document in self.documents():
            document.editor.set_wrap(enabled)

    ## Put a document in the reused empty tab if there is one, otherwise a new tab.
    func place(self, document: Document) -> unit!:
        self.sync_focus()
        var destination: EditorPanel? = none
        if let active = self.active:
            if active.document == none: destination = active
        if destination == none:
            if let active = self.active:
                destination = self.create(active.body, DockPosition.tab)
            else:
                destination = self.create(self.anchor, DockPosition.top)
        let tab = destination else return
        tab.open(document)
        tab.connections.append(document.editor.on_swatch_activate(func(offset: int) -> unit!:
            self.open_color(document.editor, offset)))
        self.dock.select(tab.body)

    func create(self, relative: Panel?, position: DockPosition) -> EditorPanel!:
        let tab = EditorPanel(self.commands, self.font, self.language_commands)
        tab.connections.append(tab.context.on_open(func() -> unit!:
            self.dock.select(tab.body)
            self.editing.bind(self.current_editor())))
        self.dock.add(tab.body, relative_to = relative, position = position)
        self.tabs.append(tab)
        self.active = tab
        return tab

    func add_requested(self, request: DockRequest) -> unit!:
        _ = self.create(request.panel, request.position)

    func selected(self, panel: Panel):
        self.sync_focus()
        for tab in self.tabs:
            if tab.body is panel:
                self.active = tab
                var editor: TextEditor? = none
                if let document = tab.document: editor = document.editor
                self.editing.bind(editor)
                return

    func can_close(self, panel: Panel) -> bool!:
        for tab in self.tabs:
            if tab.body is panel:
                if let document = tab.document:
                    if document.dirty():
                        error(ui.invalid, "Save or undo changes before closing " + paths.name(document.path))
        return true

    func removed(self, panel: Panel):
        var index = 0
        while index < self.tabs.length:
            let tab = self.tabs[index]
            if tab.body is panel:
                if let active = self.active:
                    if active is tab: self.active = none
                if let focused = self.last_focused:
                    if focused is panel: self.last_focused = none
                tab.close()
                _ = self.tabs.remove_at(index)
                return
            index += 1

    pub func remove(self, document: Document) -> unit!:
        for tab in self.tabs:
            if let current = tab.document:
                if current is document:
                    self.dock.remove(tab.body)
                    return

    pub func save_untitled(self, path: str) -> unit!:
        let document = self.current_document() else return
        document.save_as(path, self.syntax, self.grammars)

    pub func changed(self) -> unit!:
        for tab in self.tabs: tab.update()

    pub func rename(self, source: str, destination: str) -> unit!:
        for document in self.documents():
            if document.path == source or document.path.starts_with(source + "/"):
                document.rename(destination + document.path[source.length..], self.syntax, self.grammars)
        self.changed()

    pub func close(self):
        for connection in self.connections: connection.disconnect()
        self.connections.clear()
        for tab in self.tabs: tab.close()
        self.tabs.clear()
        self.active = none