dymokomi/luced
Source of release 0.4.1
0.4.1 / src / workspace / document.luc
## Each open file owns its editor, so switching files preserves text, selection,
## scroll position, folding and undo history. Disk conflict checks precede atomic saves.
from luce_ui.ui import Font, TextEditor
from language.folding import Folder
from language.tm import TmHighlighter
from language.swatches import SwatchHighlighter
from language.grammars import GrammarSet
from language.appearance import SyntaxTheme
from interop import Connection
from luce_std import files
from luce_ui import ui
pub class Document:
pub var path: str
pub let editor: TextEditor
var original: str
var baseline: str
let crlf: bool
var highlighting: Connection?
var folding: Connection?
var swatching: Connection?
var tm: TmHighlighter?
var folder: Folder?
var swatcher: SwatchHighlighter?
## The name of the active highlighter (for the status bar), and the set + theme kept so the
## user can switch highlighter by hand from the status bar's language menu.
var language: str
var grammars: GrammarSet? = none
var syntax: SyntaxTheme? = none
pub func init(self, path: str, font: Font? = none, syntax: SyntaxTheme? = none, grammars: GrammarSet? = none, blank: bool = false, wrap: bool = false) -> unit!:
let text = "" if blank else files.read_text(path)
self.path = "" if blank else files.canonical(path)
self.original = text
self.crlf = text.contains("\r\n")
let editor = TextEditor(text, font = font, wrap = wrap)
self.editor = editor
self.baseline = editor.text()
editor.set_baseline(self.baseline)
self.highlighting = none
self.folding = none
self.swatching = none
self.tm = none
self.folder = none
self.swatcher = none
self.language = "Text"
self.attach(syntax, grammars)
## Connect the highlighter that fits the current path: a grammar from the set when one claims
## the extension, otherwise the built-in Luce lexer for `.luc`/`.lucb`, otherwise nothing.
## Every language uses the same two passes: a TextMate grammar for color and indentation-based
## folding. No file type has its own highlighter — one system, from `.luc` to `.cpp`.
func attach(self, syntax: SyntaxTheme?, grammars: GrammarSet?) -> unit!:
if let connection = self.highlighting: connection.disconnect()
if let connection = self.folding: connection.disconnect()
if let connection = self.swatching: connection.disconnect()
self.highlighting = none
self.folding = none
self.swatching = none
self.tm = none
self.folder = none
self.swatcher = none
self.grammars = grammars
self.syntax = syntax
self.language = "Text"
if self.path == "":
return
let folder = Folder(self.editor)
folder.update()
self.folding = self.editor.on_change(folder.update)
self.folder = folder
# Color chips beside hex literals, refreshed on each edit — for themes and CSS.
let swatcher = SwatchHighlighter(self.editor)
self.swatching = self.editor.on_change(swatcher.update)
self.swatcher = swatcher
self.language = language_from_path(self.path)
if let registry = grammars:
if let grammar = registry.grammar_for(self.path):
let service = TmHighlighter(self.editor, grammar, syntax)
service.update()
self.highlighting = self.editor.on_change(service.update)
self.tm = service
let named = grammar.name()
if named != "":
self.language = named
## The language label for the status bar.
pub func language_label(self) -> str:
return self.language
## The line-ending style shown in the pane status bar.
pub func line_ending(self) -> str:
return "CRLF" if self.crlf else "LF"
## Switch the highlighter to the grammar in `filename` (from the status-bar language menu).
pub func set_language(self, filename: str) -> unit!:
let registry = self.grammars else return
let grammar = registry.grammar_named(filename) else return
if let connection = self.highlighting: connection.disconnect()
self.highlighting = none
self.tm = none
let service = TmHighlighter(self.editor, grammar, self.syntax)
service.update()
self.highlighting = self.editor.on_change(service.update)
self.tm = service
let named = grammar.name()
self.language = named if named != "" else filename
pub func set_syntax(self, theme: SyntaxTheme) -> unit!:
if let tm = self.tm:
tm.set_theme(theme)
pub func rename(self, path: str, syntax: SyntaxTheme?, grammars: GrammarSet?) -> unit!:
self.path = path
self.attach(syntax, grammars)
if self.tm == none:
self.editor.set_highlights([])
if self.folder == none:
_ = self.editor.set_folds([], self.editor.version())
## An untitled buffer has no path on disk yet; saving it needs a destination.
pub func is_untitled(self) -> bool:
return self.path == ""
## Write an untitled buffer to a new file, then adopt it: refuse an existing
## path, keep the edits, and enable language features for a source extension.
pub func save_as(self, path: str, syntax: SyntaxTheme?, grammars: GrammarSet?) -> unit!:
let text = self.encoded_text()
files.create_text(path, text)
self.original = text
self.baseline = self.editor.text()
self.editor.set_baseline(self.baseline)
self.rename(path, syntax, grammars)
pub func dirty(self) -> bool!:
return self.editor.text() != self.baseline
func encoded_text(self) -> str!:
let text = self.editor.text()
return text.replace("\n", "\r\n") if self.crlf else text
pub func save(self) -> unit!:
let text = self.encoded_text()
if not self.dirty():
return
if files.read_text(self.path) != self.original:
error(ui.invalid, "The file changed on disk. Your edits are still open; save a copy before reloading.")
files.write_text(self.path, text)
self.original = text
self.baseline = self.editor.text()
self.editor.set_baseline(self.baseline)
## A fallback language label from a path's extension, for files no grammar claims.
func language_from_path(path: str) -> str:
let base = path.replace("\\", "/").split("/").last else path
let parts = base.split(".")
if parts.length < 2:
return "Text"
let ext = parts.last else ""
return ext if ext != "" else "Text"