dymokomi/luced
Source of release 0.4.1
0.4.1 / src / workspace / controller.luc
## Application coordination stays in Luce. Native controls expose user intent;
## this controller handles workspace navigation, dirty files and command status.
from luce_ui.ui import Application, Text, TextEditor, TableView, Panel, ContextMenu, ConfirmDialog, Theme
from luce_gpu.gpu import Color
from workspace.explorer import Explorer
from workspace.project import Project
from luce_std.process import Command
from workspace.editors import Editors
from workspace.document import Document
from workspace.status import FileStatus
from commands.runner import Runner
from language.appearance import SyntaxTheme
from language.grammars import GrammarSet
from workspace.explorer import Entry
import workspace.operations as operations
import workspace.paths as paths
from luce_ui import ui
from luce_std import files
import editor.file_items as file_items
pub class Workspace:
let app: Application
let explorer: Explorer
pub let project: Project
var creating: Command? = none
var creating_root: str = ""
var creating_kind: str = ""
let listing: TableView
let directory_header: Panel
let status: Text
let runner: Runner
let editors: Editors
let dialog: ConfirmDialog
let file_status: FileStatus
var git_added: Color
var git_modified: Color
var status_started: bool = false
var dirty_sig: str = ""
var status_text: str = ""
var message_time: float = 0.0
pub func init(self, app: Application, explorer: Explorer, listing: TableView, directory_header: Panel,
status: Text, runner: Runner, editors: Editors, dialog: ConfirmDialog, theme: Theme):
self.app = app
self.explorer = explorer
self.project = Project(luc_program())
# Status is not wired yet; a project that cannot start describing itself just stays unloaded.
self.project.begin(explorer.directory) catch failure:
recover ()
self.listing = listing
self.directory_header = directory_header
self.status = status
self.runner = runner
self.editors = editors
self.dialog = dialog
self.file_status = FileStatus()
self.git_added = theme.vcs_added
self.git_modified = theme.vcs_modified
## The explorer rows reflect the applied theme's version-control accents; the
## configuration reload calls this so edited colors take effect immediately.
pub func set_theme_colors(self, theme: Theme) -> unit!:
self.git_added = theme.vcs_added
self.git_modified = theme.vcs_modified
self.rebuild()
## Rebuild the explorer rows from the current entries, background status and the
## set of files with unsaved edits, preserving the selection.
func rebuild(self) -> unit!:
self.listing.set_rows(file_items.present(self.explorer.entries, self.file_status, self.dirty_paths(), self.git_added, self.git_modified))
## Absolute paths of open files with unsaved changes (shown bold in the explorer).
func dirty_paths(self) -> list[str]!:
var edited: list[str] = []
for document in self.editors.documents():
if not document.is_untitled() and document.dirty():
edited.append(document.path)
return edited
## A cheap fingerprint of the unsaved set, so bold marks refresh only on a change.
func dirty_signature(self) -> str!:
var signature = ""
for path in self.dirty_paths():
signature += path + "\n"
return signature
## Start the off-thread git and line-count scans for the current directory.
func begin_status(self) -> unit!:
var code_files: list[str] = []
for entry in self.explorer.entries:
if not entry.directory and file_items.countable(entry.name):
code_files.append(entry.path)
self.file_status.begin(self.explorer.directory, code_files)
pub func has_document(self) -> bool:
return self.editors.current_document() != none
pub func current_editor(self) -> TextEditor?:
return self.editors.current_editor()
pub func current_document(self) -> Document?:
return self.editors.current_document()
pub func current_panel(self) -> Panel?:
return self.editors.current_panel()
pub func current_context(self) -> ContextMenu?:
return self.editors.current_context()
pub func set_syntax(self, theme: SyntaxTheme) -> unit!:
self.editors.set_syntax(theme)
pub func set_grammars(self, grammars: GrammarSet) -> unit!:
self.editors.set_grammars(grammars)
pub func focus_editor(self) -> unit!:
self.editors.focus()
pub func selected_entry(self) -> Entry?:
let index = self.listing.selected_index()
if index < 0 or index >= self.explorer.entries.length:
return none
let entry = self.explorer.entries[index]
return none if entry.name == ".." else entry
pub func directory(self) -> str:
return self.explorer.directory
pub func refresh_files(self) -> unit!:
self.explorer.open(self.explorer.directory)
self.begin_status()
self.rebuild()
pub func hidden_shown(self) -> bool:
return self.explorer.show_hidden
pub func toggle_hidden(self) -> unit!:
self.explorer.show_hidden = not self.explorer.show_hidden
self.refresh_files()
pub func paste_into(self, directory: str, source: str, move: bool) -> unit!:
let identity = files.canonical(source)
let target = operations.paste(source, directory, move)
if move:
self.editors.rename(identity, files.canonical(target))
self.refresh_files()
self.report(("Moved " if move else "Copied ") + paths.name(target))
## `luc new <name> --<kind>` in `directory`; the project opens when luc finishes.
pub func create_project(self, directory: str, name: str, kind: str) -> unit!:
if name == "" or "/" in name or name.starts_with("."):
error(ui.invalid, "A project name is a plain directory name.")
if self.creating != none:
error(ui.invalid, "A project is already being created.")
self.creating = Command(luc_program(), ["new", name, "--" + kind], directory = directory)
self.creating_root = paths.join(directory, name)
self.creating_kind = kind
self.report(f"Creating {kind} {name}…")
func poll_creation(self) -> unit!:
let command = self.creating else return
if not command.is_finished():
return
let code = command.exit_code() else -1
let out = command.output()
let failure = command.error_message()
command.close()
self.creating = none
if code != 0:
self.report(out.trim() if out.trim() != "" else (failure if failure != "" else "luc new failed"))
return
let root = self.creating_root
self.project.begin(root)
self.explorer.open(root)
self.begin_status()
self.listing.select(-1)
self.rebuild()
self.project_header()
self.report(f"Created {self.creating_kind} {paths.name(root)}")
pub func create_file(self, directory: str, name: str) -> unit!:
let path = operations.create_file(directory, name)
self.refresh_files()
self.open(path)
pub func create_folder(self, directory: str, name: str) -> unit!:
operations.create_folder(directory, name)
self.refresh_files()
pub func rename_entry(self, source: str, name: str) -> unit!:
let entry = paths.join(files.canonical(paths.parent(source)), paths.name(source))
let linked = files.metadata(entry, follow_symlinks = false).kind == files.FileKind.symlink
let identity = files.canonical(entry)
let destination = operations.child(paths.parent(entry), name)
if entry == destination:
return
files.rename(entry, destination, replace = false)
let renamed = files.canonical(destination)
if not linked: self.editors.rename(identity, renamed)
self.refresh_files()
self.changed()
pub func delete_entry(self, path: str) -> unit!:
let entry = paths.join(files.canonical(paths.parent(path)), paths.name(path))
let linked = files.metadata(entry, follow_symlinks = false).kind == files.FileKind.symlink
let identity = files.canonical(entry)
for document in self.editors.documents():
if not linked and (document.path == identity or document.path.starts_with(identity + "/")) and document.dirty():
error(ui.invalid, "Save unsaved changes before deleting " + paths.name(path))
files.remove_tree(entry)
for document in self.editors.documents():
if not linked and (document.path == identity or document.path.starts_with(identity + "/")):
self.editors.remove(document)
self.refresh_files()
pub func activate(self, index: int) -> unit!:
self.open_entry(index) catch failure:
self.report(failure.message)
func open_entry(self, index: int) -> unit!:
if index < 0 or index >= self.explorer.entries.length:
return
let entry = self.explorer.entries[index]
if entry.directory:
self.project.begin(entry.path)
self.explorer.open(entry.path)
self.begin_status()
self.listing.select(-1)
self.rebuild()
self.project_header()
else:
self.open(entry.path)
pub func open(self, path: str) -> unit!:
self.editors.open(path)
func project_header(self) -> unit!:
if self.project.loaded():
self.directory_header.set_detail(f"{self.project.name} · {self.project.kind}")
else:
self.directory_header.set_detail(paths.name(self.explorer.directory))
pub func new_tab(self) -> unit!:
self.editors.new_untitled()
## Write the active untitled buffer to `directory`/`name`, then adopt the file.
pub func save_untitled(self, directory: str, name: str) -> unit!:
let path = operations.child(directory, name)
self.editors.save_untitled(path)
self.refresh_files()
self.changed()
self.report("Saved " + paths.name(path))
pub func changed(self) -> unit!:
self.editors.changed()
pub func report(self, message: str) -> unit!:
self.status.set_text(paths.short(message, 108))
self.status_text = message
self.message_time = 5.0
pub func command(self, name: str) -> unit!:
self.perform(name) catch failure:
self.report(failure.message)
func perform(self, name: str) -> unit!:
if name == "stop":
self.runner.stop()
return
if name == "discard":
self.app.stop()
return
if name == "save_all":
for document in self.editors.documents():
if not document.is_untitled(): document.save()
self.changed()
self.begin_status()
self.report("All open files saved")
return
let document = self.editors.current_document() else return
if name == "fold":
_ = document.editor.toggle_fold(document.editor.line())
elif name == "fold_all":
document.editor.fold_all()
elif name == "unfold_all":
document.editor.unfold_all()
elif name == "save":
document.save()
self.changed()
self.begin_status()
self.report("Saved " + paths.name(document.path))
elif name == "build" or name == "run":
for open_document in self.editors.documents():
open_document.save()
# Inside a project the whole project builds through luc; a loose file builds alone.
if self.project.loaded():
self.runner.build_project(luc_program(), self.project.root, name == "run", self.project.language)
self.changed()
return
if not document.path.ends_with(".luc") and not document.path.ends_with(".lucb"):
error(ui.invalid, "Select a Luce or Luce Base source file to build.")
self.runner.build(document.path, name == "run")
self.changed()
pub func save(self) -> unit!:
self.command("save")
pub func save_all(self) -> unit!:
self.command("save_all")
pub func build(self) -> unit!:
self.command("build")
pub func run(self) -> unit!:
self.command("run")
pub func stop(self) -> unit!:
self.command("stop")
pub func discard_and_close(self) -> unit!:
self.command("discard")
pub func toggle_fold(self) -> unit!:
self.command("fold")
pub func fold_all(self) -> unit!:
self.command("fold_all")
pub func unfold_all(self) -> unit!:
self.command("unfold_all")
## Toggle soft wrap for every open editor and for tabs opened afterward.
pub func toggle_word_wrap(self) -> unit!:
let enabled = not self.editors.wrapping()
self.editors.set_wrap_all(enabled)
self.report("Word wrap " + ("on" if enabled else "off"))
## How many open documents have unsaved changes, for the close prompt (the Editor
## owns the prompt, since it holds the dialog view).
pub func unsaved_count(self) -> int!:
var count = 0
for document in self.editors.documents():
if document.dirty():
count += 1
return count
## The window close button and the Exit command both come here. Close now when
## nothing is unsaved, otherwise raise the save dialog and let close_choice decide.
pub func can_close(self) -> bool!:
let count = self.unsaved_count()
if count == 0:
return true
let noun = "file" if count == 1 else "files"
self.dialog.open(f"{count} unsaved {noun}. Save changes before closing?", "Save All", "Don't Save")
return false
## The dialog's answer: 0 saves everything then closes, 1 discards and closes, 2 stays.
pub func close_choice(self, role: int) -> unit!:
if role == 0:
self.command("save_all")
self.app.stop()
elif role == 1:
self.app.stop()
pub func request_exit(self) -> unit!:
if self.can_close():
self.app.stop()
pub func frame(self, elapsed: float) -> unit!:
self.tick(elapsed) catch failure:
self.report(failure.message)
func tick(self, elapsed: float) -> unit!:
self.runner.poll()
self.poll_creation()
if self.project.poll():
self.explorer.hidden_paths = self.project.absolute_generated()
self.refresh_files()
self.project_header()
if not self.status_started:
self.status_started = true
self.begin_status()
if self.file_status.poll():
self.rebuild()
let signature = self.dirty_signature()
if signature != self.dirty_sig:
self.dirty_sig = signature
self.rebuild()
# Each pane keeps its own syntax/caret/encoding status; the global bar below
# carries only workspace-wide state (build/run status and transient messages).
self.editors.refresh_status()
self.message_time -= elapsed
if self.message_time > 0.0:
return
# The idle status names the project this editor is in: "luced 0.1.1 · application · luce".
var text = self.runner.status
if text == "Ready" and self.project.loaded():
text = self.project.label()
if text != self.status_text:
self.status.set_text(text)
self.status_text = text
pub func close(self):
self.file_status.close()
self.runner.close()
self.project.close()
if let command = self.creating:
command.close()
self.creating = none
self.editors.close()
## FILES shows the project it is in ("luced · application") when there is one, else the
## directory name.
## The luc that goes with this editor: beside the compilers it was given, else on PATH.
func luc_program() -> str:
return "luc"