Mercurial Hosting > editor
comparison src/luan_editor/window.luan @ 39:2a3092ca528e
error handling
| author | Franklin Schmidt <fschmidt@gmail.com> |
|---|---|
| date | Thu, 15 May 2025 16:24:04 -0600 |
| parents | 8c90e0077cfe |
| children | f7e8c1f532c8 |
comparison
equal
deleted
inserted
replaced
| 38:8c90e0077cfe | 39:2a3092ca528e |
|---|---|
| 22 local make_find_panel = require "classpath:luan_editor/find.luan" | 22 local make_find_panel = require "classpath:luan_editor/find.luan" |
| 23 local add_menu_bar = require "classpath:luan_editor/menu.luan" | 23 local add_menu_bar = require "classpath:luan_editor/menu.luan" |
| 24 local Swing = require "luan:swing/Swing.luan" | 24 local Swing = require "luan:swing/Swing.luan" |
| 25 local File_chooser = require "luan:swing/File_chooser.luan" | 25 local File_chooser = require "luan:swing/File_chooser.luan" |
| 26 local choose_file = File_chooser.awt_choose_file or error() | 26 local choose_file = File_chooser.awt_choose_file or error() |
| 27 local Option_pane = require "luan:swing/Option_pane.luan" | |
| 28 local show_message_dialog = Option_pane.show_message_dialog or error() | |
| 27 local Logging = require "luan:logging/Logging.luan" | 29 local Logging = require "luan:logging/Logging.luan" |
| 28 local logger = Logging.logger "editor/window" | 30 local logger = Logging.logger "editor/window" |
| 29 | 31 |
| 30 | 32 |
| 31 local n_windows = 0 | 33 local n_windows = 0 |
| 57 config_file.write_text( stringify(config).."\n" ) | 59 config_file.write_text( stringify(config).."\n" ) |
| 58 end | 60 end |
| 59 | 61 |
| 60 local function new_window(file,document) | 62 local function new_window(file,document) |
| 61 local window = {} | 63 local window = {} |
| 62 window.has_file = file~=nil and file.is_file() | 64 if file == nil or not file.exists() then |
| 65 window.has_file = false | |
| 66 elseif file.is_file() then | |
| 67 window.has_file = true | |
| 68 else | |
| 69 show_message_dialog(nil,"Not a file") | |
| 70 if n_windows == 0 then | |
| 71 Luan.exit() | |
| 72 else | |
| 73 return | |
| 74 end | |
| 75 end | |
| 76 window.has_file = file~=nil and file.exists() | |
| 63 local text_area = new_text_area{ | 77 local text_area = new_text_area{ |
| 64 wrap_style_word = true | 78 wrap_style_word = true |
| 65 line_wrap = config.line_wrap | 79 line_wrap = config.line_wrap |
| 66 whitespace_visible = config.whitespace_visible | 80 whitespace_visible = config.whitespace_visible |
| 67 tab_size = config.tab_size | 81 tab_size = config.tab_size |
| 71 local title = file and file.canonical().to_string() or "new" | 85 local title = file and file.canonical().to_string() or "new" |
| 72 if document ~= nil then | 86 if document ~= nil then |
| 73 text_area.document = document | 87 text_area.document = document |
| 74 elseif file ~= nil then | 88 elseif file ~= nil then |
| 75 local document = documents[title] | 89 local document = documents[title] |
| 76 if document == nil then | 90 if document ~= nil then |
| 91 text_area.document = document | |
| 92 else | |
| 77 documents[title] = text_area.document | 93 documents[title] = text_area.document |
| 78 else | 94 if file.exists() then |
| 79 text_area.document = document | 95 text_area.text = file.read_text() |
| 80 end | 96 text_area.document.clear_unedited() |
| 81 if file.is_file() then | 97 end |
| 82 text_area.text = file.read_text() | |
| 83 text_area.document.clear_unedited() | |
| 84 end | 98 end |
| 85 end | 99 end |
| 86 text_area.set_selection(0) | 100 text_area.set_selection(0) |
| 87 local status_bar = new_label{ | 101 local status_bar = new_label{ |
| 88 constraints = "span,growx" | 102 constraints = "span,growx" |
| 159 end | 173 end |
| 160 title = file.canonical().to_string() | 174 title = file.canonical().to_string() |
| 161 frame.title = title | 175 frame.title = title |
| 162 documents[title] = text_area.document | 176 documents[title] = text_area.document |
| 163 end | 177 end |
| 164 file.write_text(text_area.text) | 178 try |
| 179 file.write_text(text_area.text) | |
| 180 catch e | |
| 181 show_message_dialog( frame, e.get_message() ) | |
| 182 return false | |
| 183 end | |
| 165 text_area.document.set_unedited() | 184 text_area.document.set_unedited() |
| 166 return true | 185 return true |
| 167 end | 186 end |
| 168 function window.revert() | 187 function window.revert() |
| 169 local selection = text_area.get_selection() | 188 local selection = text_area.get_selection() |
