diff src/luan_editor/window.luan @ 37:b7ff52d45b9a default tip

copy from luan
author Franklin Schmidt <fschmidt@gmail.com>
date Mon, 21 Apr 2025 13:07:29 -0600
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/luan_editor/window.luan	Mon Apr 21 13:07:29 2025 -0600
@@ -0,0 +1,252 @@
+local Luan = require "luan:Luan.luan"
+local error = Luan.error
+local stringify = Luan.stringify or error()
+local Math = require "luan:Math.luan"
+local min = Math.min or error()
+local String = require "luan:String.luan"
+local sub_string = String.sub or error()
+local replace = String.replace or error()
+local starts_with = String.starts_with or error()
+local Io = require "luan:Io.luan"
+local new_text_area = require("luan:swing/Text_area.luan").new or error()
+local new_frame = require("luan:swing/Frame.luan").new or error()
+local new_panel = require("luan:swing/Component.luan").new_panel or error()
+local Layout = require "luan:swing/Layout.luan"
+local new_mig_layout = Layout.new_mig_layout or error()
+local new_scroll_pane = require("luan:swing/Scroll_pane.luan").new or error()
+local new_text_area_line_numbers = require("luan:swing/Text_area_line_numbers.luan").new or error()
+local int_to_color = require("luan:swing/Color.luan").int_to_color or error()
+local Border = require "luan:swing/Border.luan"
+local create_empty_border = Border.create_empty_border or error()
+local new_label = require("luan:swing/Label.luan").new or error()
+local make_find_panel = require "classpath:luan_editor/find.luan"
+local add_menu_bar = require "classpath:luan_editor/menu.luan"
+local Swing = require "luan:swing/Swing.luan"
+local File_chooser = require "luan:swing/File_chooser.luan"
+local choose_file = File_chooser.awt_choose_file or error()
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "editor/window"
+
+
+local n_windows = 0
+local documents = {}
+
+local function bool(val,default)
+	if val ~= nil then
+		return val
+	else
+		return default
+	end
+end
+
+local config_file = Io.uri("file:"..Swing.home_dir.."/.luan_editor")
+local config = {}
+if config_file.exists() then
+	try
+		config = Luan.parse(config_file.read_text())
+	catch e
+		logger.error(e)
+	end
+end
+config.size = config.size or { width=700, height=700 }
+config.line_wrap = bool( config.line_wrap, true )
+config.whitespace_visible = bool( config.whitespace_visible, false )
+config.tab_size = config.tab_size or 4
+
+local function save_config()
+	config_file.write_text( stringify(config).."\n" )
+end
+
+local function new_window(file)
+	local window = {}
+	window.has_file = file~=nil and file.is_file()
+	local text_area = new_text_area{
+		wrap_style_word = true
+		line_wrap = config.line_wrap
+		whitespace_visible = config.whitespace_visible
+		tab_size = config.tab_size
+		font = { family="Monospaced", size=13 }
+	}
+	window.text_area = text_area
+	local title = file and file.canonical().to_string() or "new"
+	if file ~= nil then
+		local document = documents[title]
+		if document == nil then
+			documents[title] = text_area.document
+		else
+			text_area.document = document
+		end
+		if file.is_file() then
+			text_area.text = file.read_text()
+			text_area.document.clear_unedited()
+		end
+	end
+	text_area.set_selection(0)
+	local status_bar = new_label{
+		constraints = "span,growx"
+		text = " "
+		border = create_empty_border(2,16,4,16)
+	}
+	window.status_bar = status_bar
+	local find_panel = make_find_panel(window)
+	local frame = new_frame{
+		preferred_size = config.size
+		content_pane = new_panel{
+			layout = new_mig_layout("insets 0,wrap,fill,hidemode 3","","[][grow 0]")
+			children = {
+				new_scroll_pane{
+					constraints = "grow"
+					view = text_area
+					row_header_view = new_text_area_line_numbers{
+						text_area = text_area
+						foreground_color = int_to_color(0x888888)
+						border = create_empty_border(0,8,0,8)
+					}
+				}
+				find_panel
+				status_bar
+			}
+		}
+	}
+	window.frame = frame
+	frame.add_close_listener(function()
+		n_windows = n_windows - 1
+		if n_windows == 0 then
+			Luan.exit()
+		end
+	end)
+	frame.add_resize_stopped_listener( 200, function()
+		--logger.info(stringify(frame.size))
+		config.size = frame.size
+		save_config()
+	end)
+	frame.add_move_stopped_listener( 200, function()
+		--logger.info(stringify(frame.location))
+		config.location = frame.location
+		save_config()
+	end)
+	local function set_title()
+		local s = title
+		if not text_area.document.is_unedited() then
+			s = s.." *"
+		end
+		frame.title = s
+	end
+	set_title()
+	window.set_title = set_title  -- dont gc
+	text_area.document.add_undo_listener(set_title)
+	window.new = new_window
+	function window.open()
+		local new_file = choose_file{
+			action = "load"
+			parent = frame
+			directory = file and file.parent()
+		}
+		if new_file ~= nil then
+			new_window(new_file)
+		end
+	end
+	function window.save()
+		if file == nil then
+			file = choose_file{
+				action = "save"
+				parent = frame
+			}
+			if file == nil then
+				return false
+			end
+			title = file.canonical().to_string()
+			frame.title = title
+			documents[title] = text_area.document
+		end
+		file.write_text(text_area.text)
+		text_area.document.set_unedited()
+		return true
+	end
+	function window.revert()
+		local selection = text_area.get_selection()
+		local text = file.read_text()
+		text_area.text = text
+		text_area.set_selection(min(selection,#text+1))
+		text_area.document.set_unedited()
+		status_bar.text = "Reverted"
+	end
+	local function selection_lines()
+		local start_seletion, end_selection = text_area.get_selection()
+		local end_ = end_selection == start_seletion and end_selection or end_selection - 1
+		local start_line = text_area.get_line_from_position(start_seletion)
+		local end_line = text_area.get_line_from_position(end_)
+		local start_pos = text_area.get_line_start_position(start_line)
+		local end_pos = text_area.get_line_end_position(end_line)
+		local text = text_area.text
+		text = sub_string(text,start_pos,end_pos-2)
+		return {
+			text = text
+			start_pos = start_pos
+			length = #text
+			lines = end_line - start_line + 1
+			start_seletion = start_seletion
+			end_selection = end_selection
+		}
+	end
+	function window.indent()
+		local r = selection_lines()
+		local text = r.text
+		local start_pos = r.start_pos
+		text = "\t"..replace(text,"\n","\n\t")
+		text_area.replace(start_pos,r.length,text)
+		--logger.info(stringify{text_area.get_selection()})
+		text_area.set_selection( r.start_seletion+1, r.end_selection+r.lines )
+	end
+	function window.unindent()
+		local r = selection_lines()
+		local text = r.text
+		text = "\n"..text
+		local start_seletion = r.start_seletion
+		if starts_with(text,"\n\t") then
+			start_seletion = start_seletion - 1
+		end
+		local len1 = #text
+		text = replace(text,"\n\t","\n")
+		local len2 = #text
+		local end_selection = r.end_selection - (len1 - len2)
+		text = sub_string(text,2)
+		text_area.replace(r.start_pos,r.length,text)
+		text_area.set_selection(start_seletion,end_selection)
+	end
+	function window.cursor_column()
+		local cursor_pos = text_area.get_selection()
+		local line = text_area.get_line_from_position(cursor_pos)
+		local start_line_pos = text_area.get_line_start_position(line)
+		return cursor_pos - start_line_pos + 1
+	end
+	function window.goto(line)
+		local pos = text_area.get_line_start_position(line)
+		text_area.set_selection(pos)
+	end
+	function window.set_line_wrap(line_wrap)
+		text_area.line_wrap = line_wrap
+		config.line_wrap = line_wrap
+		save_config()
+	end
+	function window.set_whitespace_visible(whitespace_visible)
+		text_area.whitespace_visible = whitespace_visible
+		config.whitespace_visible = whitespace_visible
+		save_config()
+	end
+	function window.set_tab_size(tab_size)
+		text_area.tab_size = tab_size
+		config.tab_size = tab_size
+		save_config()
+	end
+	add_menu_bar(window)
+	frame.pack()
+	if config.location ~= nil then
+		frame.location = config.location
+	end
+	frame.visible = true
+	text_area.request_focus_in_window()
+	n_windows = n_windows + 1
+end
+
+return new_window