Mercurial Hosting > editor
annotate src/luan_editor/Window.luan @ 67:2c050fcf2614 default tip
add document count
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Fri, 06 Jun 2025 18:59:44 -0600 |
parents | 1c68552e8ac2 |
children |
rev | line source |
---|---|
37 | 1 local Luan = require "luan:Luan.luan" |
2 local error = Luan.error | |
3 local stringify = Luan.stringify or error() | |
55 | 4 local ipairs = Luan.ipairs or error() |
52 | 5 local Parsers = require "luan:Parsers.luan" |
6 local json_string = Parsers.json_string or error() | |
7 local json_parse = Parsers.json_parse or error() | |
37 | 8 local Math = require "luan:Math.luan" |
9 local min = Math.min or error() | |
10 local String = require "luan:String.luan" | |
11 local sub_string = String.sub or error() | |
12 local replace = String.replace or error() | |
13 local starts_with = String.starts_with or error() | |
14 local Io = require "luan:Io.luan" | |
56 | 15 local new_file = Io.schemes.file or error() |
37 | 16 local new_text_area = require("luan:swing/Text_area.luan").new or error() |
17 local new_frame = require("luan:swing/Frame.luan").new or error() | |
41 | 18 local new_dialog = require("luan:swing/Dialog.luan").new or error() |
37 | 19 local new_panel = require("luan:swing/Component.luan").new_panel or error() |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
20 local new_list = require("luan:swing/List.luan").new or error() |
37 | 21 local Layout = require "luan:swing/Layout.luan" |
22 local new_mig_layout = Layout.new_mig_layout or error() | |
23 local new_scroll_pane = require("luan:swing/Scroll_pane.luan").new or error() | |
24 local new_text_area_line_numbers = require("luan:swing/Text_area_line_numbers.luan").new or error() | |
25 local int_to_color = require("luan:swing/Color.luan").int_to_color or error() | |
26 local Border = require "luan:swing/Border.luan" | |
27 local create_empty_border = Border.create_empty_border or error() | |
28 local new_label = require("luan:swing/Label.luan").new or error() | |
29 local make_find_panel = require "classpath:luan_editor/find.luan" | |
30 local File_chooser = require "luan:swing/File_chooser.luan" | |
31 local choose_file = File_chooser.awt_choose_file or error() | |
39 | 32 local Option_pane = require "luan:swing/Option_pane.luan" |
33 local show_message_dialog = Option_pane.show_message_dialog or error() | |
55 | 34 local Clipboard = require "luan:swing/Clipboard.luan" |
53 | 35 local Java = require "classpath:luan_editor/Java.luan" |
37 | 36 local Logging = require "luan:logging/Logging.luan" |
56 | 37 local logger = Logging.logger "editor/Window" |
37 | 38 |
39 | |
56 | 40 local Window = {} |
41 | |
37 | 42 local n_windows = 0 |
43 local documents = {} | |
44 | |
45 local function bool(val,default) | |
46 if val ~= nil then | |
47 return val | |
48 else | |
49 return default | |
50 end | |
51 end | |
52 | |
53 | 53 local config_file = Io.uri("file:"..Java.home_dir.."/.luan_editor/config.json") |
37 | 54 local config = {} |
55 if config_file.exists() then | |
56 try | |
52 | 57 config = json_parse(config_file.read_text()) |
37 | 58 catch e |
59 logger.error(e) | |
60 end | |
61 end | |
62 config.size = config.size or { width=700, height=700 } | |
63 config.line_wrap = bool( config.line_wrap, true ) | |
64 config.whitespace_visible = bool( config.whitespace_visible, false ) | |
65 config.tab_size = config.tab_size or 4 | |
46 | 66 config.list_window = config.list_window or {} |
67 config.list_window.size = config.list_window.size or { width=200, height=400 } | |
37 | 68 |
69 local function save_config() | |
52 | 70 config_file.write_text( json_string(config).."\n" ) |
37 | 71 end |
72 | |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
73 local list_view = new_list{ |
41 | 74 --layout = new_mig_layout("insets 0,wrap,fill") |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
75 --layout = new_mig_layout("wrap","[grow]") |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
76 horizontal_alignment = "right" |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
77 hover_background = int_to_color(0xEEEEEE) |
41 | 78 } |
79 local list_scroll_pane = new_scroll_pane{ | |
80 view = list_view | |
81 } | |
82 local list_window = new_dialog{ | |
46 | 83 size = config.list_window.size |
41 | 84 content_pane = list_scroll_pane |
43 | 85 focusable_window_state = false |
41 | 86 } |
46 | 87 list_window.add_resize_stopped_listener( 200, function() |
88 --logger.info(stringify(list_window.size)) | |
89 config.list_window.size = list_window.size | |
90 save_config() | |
91 end) | |
92 list_window.add_move_stopped_listener( 200, function() | |
93 --logger.info(stringify(list_window.location)) | |
94 config.list_window.location = list_window.location | |
95 save_config() | |
96 end) | |
56 | 97 function Window.show_list_window() |
46 | 98 local location = config.list_window.location |
99 if location ~= nil then | |
100 list_window.location = location | |
101 end | |
44 | 102 list_window.visible = true |
103 list_scroll_pane.scroll_to_right() | |
104 end | |
105 | |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
106 local function add_list_window_item(item) |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
107 list_view.add_element(item) |
41 | 108 list_scroll_pane.scroll_to_right() |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
109 end |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
110 local function remove_list_window_item(item) |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
111 list_view.remove_element(item) --or error() |
41 | 112 end |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
113 list_view.add_list_selection_listener( function(item) |
49 | 114 if item ~= nil then |
115 item.window.text_area.request_focus() | |
116 end | |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
117 end ) |
41 | 118 |
44 | 119 local black = int_to_color(0x000000) |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
120 local dark_blue = int_to_color(0x0000C0) |
44 | 121 local grey = int_to_color(0x888888) |
122 | |
38 | 123 local function new_window(file,document) |
37 | 124 local window = {} |
39 | 125 if file == nil or not file.exists() then |
126 window.has_file = false | |
127 elseif file.is_file() then | |
128 window.has_file = true | |
129 else | |
130 show_message_dialog(nil,"Not a file") | |
131 if n_windows == 0 then | |
132 Luan.exit() | |
133 else | |
134 return | |
135 end | |
136 end | |
137 window.has_file = file~=nil and file.exists() | |
37 | 138 local text_area = new_text_area{ |
139 wrap_style_word = true | |
140 line_wrap = config.line_wrap | |
141 whitespace_visible = config.whitespace_visible | |
142 tab_size = config.tab_size | |
57 | 143 --font = { family="Monospaced", size=13 } |
144 font_changes = { family="Monospaced" } | |
37 | 145 } |
146 window.text_area = text_area | |
147 local title = file and file.canonical().to_string() or "new" | |
38 | 148 if document ~= nil then |
149 text_area.document = document | |
150 elseif file ~= nil then | |
67 | 151 local document_info = documents[title] |
152 if document_info ~= nil then | |
153 text_area.document = document_info.document | |
154 document_info.count = document_info.count + 1 | |
39 | 155 else |
67 | 156 document_info = { document = text_area.document, count = 1 } |
157 documents[title] = document_info | |
39 | 158 if file.exists() then |
159 text_area.text = file.read_text() | |
160 text_area.document.clear_unedited() | |
161 end | |
37 | 162 end |
163 end | |
164 text_area.set_selection(0) | |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
165 local list_window_item = { |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
166 window = window |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
167 } |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
168 add_list_window_item(list_window_item) |
37 | 169 local status_bar = new_label{ |
170 constraints = "span,growx" | |
171 text = " " | |
172 border = create_empty_border(2,16,4,16) | |
173 } | |
174 window.status_bar = status_bar | |
175 local find_panel = make_find_panel(window) | |
176 local frame = new_frame{ | |
177 preferred_size = config.size | |
178 content_pane = new_panel{ | |
179 layout = new_mig_layout("insets 0,wrap,fill,hidemode 3","","[][grow 0]") | |
180 children = { | |
181 new_scroll_pane{ | |
182 constraints = "grow" | |
183 view = text_area | |
184 row_header_view = new_text_area_line_numbers{ | |
185 text_area = text_area | |
44 | 186 foreground_color = grey |
37 | 187 border = create_empty_border(0,8,0,8) |
188 } | |
189 } | |
190 find_panel | |
191 status_bar | |
192 } | |
193 } | |
194 } | |
195 window.frame = frame | |
196 frame.add_close_listener(function() | |
197 n_windows = n_windows - 1 | |
198 if n_windows == 0 then | |
199 Luan.exit() | |
200 end | |
44 | 201 remove_list_window_item(list_window_item) |
67 | 202 local document_info = documents[title] or error() |
203 document_info.count = document_info.count - 1 | |
204 if document_info.count == 0 then | |
205 documents[title] = nil | |
206 end | |
37 | 207 end) |
43 | 208 frame.add_window_focus_listener(function() |
56 | 209 list_view.selected_value = list_window_item |
43 | 210 end) |
37 | 211 frame.add_resize_stopped_listener( 200, function() |
212 --logger.info(stringify(frame.size)) | |
213 config.size = frame.size | |
214 save_config() | |
215 end) | |
216 frame.add_move_stopped_listener( 200, function() | |
217 --logger.info(stringify(frame.location)) | |
218 config.location = frame.location | |
219 save_config() | |
220 end) | |
44 | 221 local function undo_listener() |
222 local is_unedited = text_area.document.is_unedited() | |
223 if window.is_unedited == is_unedited then | |
224 return | |
225 end | |
226 window.is_unedited = is_unedited | |
37 | 227 local s = title |
44 | 228 if not is_unedited then |
37 | 229 s = s.." *" |
230 end | |
231 frame.title = s | |
48 | 232 list_window_item.text = title |
47
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
233 list_window_item.foreground_color = is_unedited and black or dark_blue |
f66f704118e3
list window uses JList
Franklin Schmidt <fschmidt@gmail.com>
parents:
46
diff
changeset
|
234 list_view.repaint(list_window_item) |
37 | 235 end |
44 | 236 undo_listener() |
237 --window.undo_listener = undo_listener -- dont gc | |
238 text_area.document.add_undo_listener(undo_listener) | |
48 | 239 function window.title() |
240 return title | |
241 end | |
37 | 242 function window.open() |
243 local new_file = choose_file{ | |
244 action = "load" | |
245 parent = frame | |
246 directory = file and file.parent() | |
247 } | |
248 if new_file ~= nil then | |
249 new_window(new_file) | |
250 end | |
251 end | |
252 function window.save() | |
253 if file == nil then | |
254 file = choose_file{ | |
255 action = "save" | |
256 parent = frame | |
257 } | |
258 if file == nil then | |
259 return false | |
260 end | |
261 title = file.canonical().to_string() | |
67 | 262 documents[title] = { document = text_area.document, count = 1 } |
48 | 263 window.is_unedited = nil |
264 undo_listener() | |
37 | 265 end |
39 | 266 try |
267 file.write_text(text_area.text) | |
268 catch e | |
269 show_message_dialog( frame, e.get_message() ) | |
270 return false | |
271 end | |
37 | 272 text_area.document.set_unedited() |
273 return true | |
274 end | |
275 function window.revert() | |
276 local selection = text_area.get_selection() | |
277 local text = file.read_text() | |
278 text_area.text = text | |
279 text_area.set_selection(min(selection,#text+1)) | |
280 text_area.document.set_unedited() | |
281 status_bar.text = "Reverted" | |
282 end | |
283 local function selection_lines() | |
284 local start_seletion, end_selection = text_area.get_selection() | |
285 local end_ = end_selection == start_seletion and end_selection or end_selection - 1 | |
286 local start_line = text_area.get_line_from_position(start_seletion) | |
287 local end_line = text_area.get_line_from_position(end_) | |
288 local start_pos = text_area.get_line_start_position(start_line) | |
289 local end_pos = text_area.get_line_end_position(end_line) | |
290 local text = text_area.text | |
291 text = sub_string(text,start_pos,end_pos-2) | |
292 return { | |
293 text = text | |
294 start_pos = start_pos | |
295 length = #text | |
296 lines = end_line - start_line + 1 | |
297 start_seletion = start_seletion | |
298 end_selection = end_selection | |
299 } | |
300 end | |
301 function window.indent() | |
302 local r = selection_lines() | |
303 local text = r.text | |
304 local start_pos = r.start_pos | |
305 text = "\t"..replace(text,"\n","\n\t") | |
306 text_area.replace(start_pos,r.length,text) | |
307 --logger.info(stringify{text_area.get_selection()}) | |
308 text_area.set_selection( r.start_seletion+1, r.end_selection+r.lines ) | |
309 end | |
310 function window.unindent() | |
311 local r = selection_lines() | |
312 local text = r.text | |
313 text = "\n"..text | |
314 local start_seletion = r.start_seletion | |
315 if starts_with(text,"\n\t") then | |
316 start_seletion = start_seletion - 1 | |
317 end | |
318 local len1 = #text | |
319 text = replace(text,"\n\t","\n") | |
320 local len2 = #text | |
321 local end_selection = r.end_selection - (len1 - len2) | |
322 text = sub_string(text,2) | |
323 text_area.replace(r.start_pos,r.length,text) | |
324 text_area.set_selection(start_seletion,end_selection) | |
325 end | |
326 function window.cursor_column() | |
327 local cursor_pos = text_area.get_selection() | |
328 local line = text_area.get_line_from_position(cursor_pos) | |
329 local start_line_pos = text_area.get_line_start_position(line) | |
330 return cursor_pos - start_line_pos + 1 | |
331 end | |
332 function window.goto(line) | |
333 local pos = text_area.get_line_start_position(line) | |
334 text_area.set_selection(pos) | |
335 end | |
336 function window.set_line_wrap(line_wrap) | |
337 text_area.line_wrap = line_wrap | |
338 config.line_wrap = line_wrap | |
339 save_config() | |
340 end | |
341 function window.set_whitespace_visible(whitespace_visible) | |
342 text_area.whitespace_visible = whitespace_visible | |
343 config.whitespace_visible = whitespace_visible | |
344 save_config() | |
345 end | |
346 function window.set_tab_size(tab_size) | |
347 text_area.tab_size = tab_size | |
348 config.tab_size = tab_size | |
349 save_config() | |
350 end | |
38 | 351 function window.duplicate() |
352 local new = new_window(file,text_area.document) | |
353 new.text_area.set_selection( text_area.get_selection() ) | |
354 end | |
55 | 355 function window.paste_files() |
356 --logger.info("paste_files "..stringify(Clipboard.get_files())) | |
357 local files = Clipboard.get_files() | |
358 if files == nil then | |
359 return false | |
360 end | |
361 for _, file in ipairs(files) do | |
362 new_window(file) | |
363 end | |
364 return true | |
365 end | |
56 | 366 local add_menu_bar = require "classpath:luan_editor/menu.luan" |
37 | 367 add_menu_bar(window) |
368 frame.pack() | |
46 | 369 local location = config.location |
370 if location ~= nil then | |
371 frame.location = location | |
37 | 372 end |
373 frame.visible = true | |
374 text_area.request_focus_in_window() | |
375 n_windows = n_windows + 1 | |
38 | 376 return window |
37 | 377 end |
56 | 378 Window.new_window = new_window |
37 | 379 |
56 | 380 function Window.open_windows(file_paths) |
381 list_window.to_front() | |
382 for _, file_path in ipairs(file_paths) do | |
383 local file = new_file(file_path) | |
384 new_window(file) | |
385 end | |
386 if #file_paths == 0 then | |
387 local window = list_view.selected_value.window | |
388 window.frame.to_front() | |
389 window.text_area.request_focus_in_window() | |
390 end | |
391 end | |
392 | |
393 return Window |