diff --git a/res/devtools/syntax/vcm.toml b/res/devtools/syntax/vcm.toml new file mode 100644 index 00000000..ca05b6a1 --- /dev/null +++ b/res/devtools/syntax/vcm.toml @@ -0,0 +1,3 @@ +language = "VCM" +extensions = ["vcm"] +line-comment-start = "#" diff --git a/res/devtools/syntax/xml.toml b/res/devtools/syntax/xml.toml new file mode 100644 index 00000000..ad591a70 --- /dev/null +++ b/res/devtools/syntax/xml.toml @@ -0,0 +1,4 @@ +language = "XML" +extensions = ["xml"] +multiline-comment-start = "" diff --git a/res/layouts/code_editor.xml b/res/layouts/code_editor.xml index 4336ee4f..4b61c4d8 100644 --- a/res/layouts/code_editor.xml +++ b/res/layouts/code_editor.xml @@ -1,12 +1,6 @@ - - - - - - + @@ -14,46 +8,49 @@ - - - - - - - - - + + + + + + + + + + + + - - + + diff --git a/res/layouts/code_editor.xml.lua b/res/layouts/code_editor.xml.lua index 6cb9ce68..de460f2e 100644 --- a/res/layouts/code_editor.xml.lua +++ b/res/layouts/code_editor.xml.lua @@ -1,6 +1,5 @@ local writeables = {} local registry -local filenames local current_file = { filename = "", @@ -49,6 +48,10 @@ events.on("core:error", function (msg, traceback) table.insert(errors_all, full) end) +events.on("core:open_in_editor", function(filename, linenum) + open_file_in_editor(filename, linenum) +end) + local function find_mutable(filename) local packid = file.prefix(filename) if packid == "core" then @@ -80,16 +83,6 @@ local function refresh_file_title() ..(edited and ' *' or '') end -function filter_files(text) - local filtered = {} - for _, filename in ipairs(filenames) do - if filename:find(text) then - table.insert(filtered, filename) - end - end - build_files_list(filtered, text) -end - function on_control_combination(keycode) if keycode == input.keycode("s") then save_current_file() @@ -111,10 +104,29 @@ function unlock_access() ) end +local function reload_model(filename, name) + assets.parse_model("xml", document.editor.text, name) +end + function run_current_file() if not current_file.filename then return end + + local info = registry.get_info(current_file.filename) + local script_type = info and info.type or "file" + local unit = info and info.unit + + if script_type == "model" then + print(current_file.filename) + clear_output() + local _, err = pcall(reload_model, current_file.filename, unit) + if err then + document.output:paste(string.format("\n[#FF0000]%s[#FFFFFF]", err)) + end + return + end + local chunk, err = loadstring(document.editor.text, current_file.filename) clear_output() if not chunk then @@ -126,9 +138,7 @@ function run_current_file() ) return end - local info = registry.get_info(current_file.filename) - local script_type = info and info.type or "file" - local unit = info and info.unit + save_current_file() local func = function() @@ -198,6 +208,7 @@ events.on("core:open_traceback", function(traceback_b64) tb_list.size = srcsize end) +--- Save the current file in the code editor if has writeable path. function save_current_file() if not current_file.mutable then return @@ -209,7 +220,22 @@ function save_current_file() document.editor.edited = false end +--- Open a file in the code editor. +--- @param filename string - the path to the file to open. +--- @param line integer - the line number to focus on (optional). +--- @param mutable string - writeable file path (optional). function open_file_in_editor(filename, line, mutable) + debug.log("opening file " .. string.escape(filename) .. " in editor") + + local ext = file.ext(filename) + if ext == "xml" or ext == "vcm" then + document.modelviewer.src = file.stem(filename) + document.modelviewer.visible = true + else + document.modelviewer.visible = false + end + document.codePanel:refresh() + local editor = document.editor local source = file.read(filename):gsub('\t', ' ') editor.scroll = 0 @@ -229,43 +255,11 @@ function open_file_in_editor(filename, line, mutable) document.saveIcon.enabled = current_file.modified end -function build_files_list(filenames, selected) - local files_list = document.filesList - files_list.scroll = 0 - files_list:clear() - - for _, actual_filename in ipairs(filenames) do - local filename = actual_filename - if selected then - filename = filename:gsub(selected, "**"..selected.."**") - end - local parent = file.parent(filename) - local info = registry.get_info(actual_filename) - local icon = "file" - if info then - icon = info.type == "component" and "entity" or info.type - end - files_list:add(gui.template("script_file", { - path = parent .. (parent[#parent] == ':' and '' or '/'), - name = file.name(filename), - icon = icon, - unit = info and info.unit or '', - filename = actual_filename - })) - end -end - function on_open(mode) registry = require "core:internal/scripts_registry" - - local files_list = document.filesList - document.editorContainer:setInterval(200, refresh_file_title) + document.codePanel:setInterval(200, refresh_file_title) clear_traceback() clear_output() - - filenames = registry.filenames - table.sort(filenames) - build_files_list(filenames) end diff --git a/res/layouts/console.xml b/res/layouts/console.xml index 16fbbb0b..3772d240 100644 --- a/res/layouts/console.xml +++ b/res/layouts/console.xml @@ -22,8 +22,7 @@ markup="md" > - + + + + + diff --git a/res/layouts/files_panel.xml.lua b/res/layouts/files_panel.xml.lua new file mode 100644 index 00000000..a4d6ca39 --- /dev/null +++ b/res/layouts/files_panel.xml.lua @@ -0,0 +1,54 @@ +local registry +local filenames + +function filter_files(text) + local pattern_safe = text:pattern_safe(); + local filtered = {} + for _, filename in ipairs(filenames) do + if filename:find(pattern_safe) then + table.insert(filtered, filename) + end + end + build_files_list(filtered, pattern_safe) +end + +function open_file_in_editor(filename, linenum) + events.emit("core:open_in_editor", filename, linenum) +end + +function build_files_list(filenames, highlighted_part) + local files_list = document.filesList + files_list.scroll = 0 + files_list:clear() + + for _, actual_filename in ipairs(filenames) do + local filename = actual_filename + if highlighted_part then + filename = filename:gsub(highlighted_part, "**"..highlighted_part.."**") + end + local parent = file.parent(filename) + local info = registry.get_info(actual_filename) + local icon = "file" + if info then + icon = info.type == "component" and "entity" or info.type + end + files_list:add(gui.template("script_file", { + path = parent .. (parent[#parent] == ':' and '' or '/'), + name = file.name(filename), + icon = icon, + unit = info and info.unit or '', + filename = actual_filename, + open_func = "open_file_in_editor", + })) + end +end + +function on_open(mode) + registry = require "core:internal/scripts_registry" + + local files_list = document.filesList + + filenames = registry.filenames + table.sort(filenames) + build_files_list(filenames) +end diff --git a/res/layouts/templates/script_file.xml b/res/layouts/templates/script_file.xml index 94e1afd2..f5d55729 100644 --- a/res/layouts/templates/script_file.xml +++ b/res/layouts/templates/script_file.xml @@ -3,7 +3,7 @@