From c0bbb91d4bd1aa697af0a9f18b9eac5732dbbdb8 Mon Sep 17 00:00:00 2001 From: Lukas <67873524+lkalabis@users.noreply.github.com> Date: Mon, 15 Apr 2024 21:38:16 +0200 Subject: [PATCH 1/4] Check filetype for anon execution instead of file extension The current implementation checks an anonymous buffer for the extension: 'apex'. Only in this case its possible to run the current file in an anonymous execution. It makes more sense to check if the filetype is 'apex' since its not necessary to save a file for anonymous execution --- lua/salesforce/execute_anon.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/salesforce/execute_anon.lua b/lua/salesforce/execute_anon.lua index eed41b1..21c7f6b 100644 --- a/lua/salesforce/execute_anon.lua +++ b/lua/salesforce/execute_anon.lua @@ -18,7 +18,7 @@ local M = {} M.execute_anon = function() Debug:log("execute_anon.lua", "Executing anonymous Apex script...") local path = vim.fn.expand("%:p") - local file_type = vim.fn.expand("%:e") + local file_type = vim.bo.filetype local default_username = OrgManager:get_default_username() if file_type ~= "apex" then From 79561bc024ff86a89da4e8b65f111ceed5685dd8 Mon Sep 17 00:00:00 2001 From: Lukas <67873524+lkalabis@users.noreply.github.com> Date: Sun, 21 Apr 2024 14:12:06 +0200 Subject: [PATCH 2/4] Check if path is empty so buffer is treated as file --- lua/salesforce/execute_anon.lua | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/lua/salesforce/execute_anon.lua b/lua/salesforce/execute_anon.lua index 21c7f6b..1548fcb 100644 --- a/lua/salesforce/execute_anon.lua +++ b/lua/salesforce/execute_anon.lua @@ -21,18 +21,24 @@ M.execute_anon = function() local file_type = vim.bo.filetype local default_username = OrgManager:get_default_username() - if file_type ~= "apex" then + if file_type == "apex" then + if path == "" then + Debug:log("execute_anon.lua", "Executing anonymous Apex script for current buffer...") + path = vim.fn.expand("%") + if not default_username then + Util.notify_default_org_not_set() + return + end + else + Debug:log("execute_anon.lua", "Executing anonymous Apex script for file...") + end + else local message = "Not an Apex script file" Debug:log("execute_anon.lua", message) vim.notify(message, vim.log.levels.ERROR) return end - if not default_username then - Util.notify_default_org_not_set() - return - end - Popup:create_popup({}) Popup:write_to_popup("Executing anonymous Apex script...") local executable = Config:get_options().sf_executable @@ -73,4 +79,4 @@ M.execute_anon = function() end end -return M +return M \ No newline at end of file From e95d101deb1f0d1b7b52fd7d1203f4652efb2312 Mon Sep 17 00:00:00 2001 From: Lukas <67873524+lkalabis@users.noreply.github.com> Date: Sun, 21 Apr 2024 14:20:47 +0200 Subject: [PATCH 3/4] Update execute_anon.lua --- lua/salesforce/execute_anon.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lua/salesforce/execute_anon.lua b/lua/salesforce/execute_anon.lua index 1548fcb..0b7e332 100644 --- a/lua/salesforce/execute_anon.lua +++ b/lua/salesforce/execute_anon.lua @@ -16,7 +16,7 @@ end local M = {} M.execute_anon = function() - Debug:log("execute_anon.lua", "Executing anonymous Apex script...") + Debug:log("execute_anon.lua", "Executing anonymous Apex script......") local path = vim.fn.expand("%:p") local file_type = vim.bo.filetype local default_username = OrgManager:get_default_username() @@ -79,4 +79,4 @@ M.execute_anon = function() end end -return M \ No newline at end of file +return M From e472fe8be97b24a25280693c6bc9f1f0a4f6b8ed Mon Sep 17 00:00:00 2001 From: Jonathan Morris Date: Tue, 23 Apr 2024 00:27:18 -0400 Subject: [PATCH 4/4] feat: allow executing unsaved buffers anonymously --- lua/salesforce/execute_anon.lua | 56 ++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 18 deletions(-) diff --git a/lua/salesforce/execute_anon.lua b/lua/salesforce/execute_anon.lua index 0b7e332..8a81407 100644 --- a/lua/salesforce/execute_anon.lua +++ b/lua/salesforce/execute_anon.lua @@ -16,38 +16,56 @@ end local M = {} M.execute_anon = function() - Debug:log("execute_anon.lua", "Executing anonymous Apex script......") + Debug:log("execute_anon.lua", "Executing anonymous Apex script...") + local disallowed_extensions = { "cls", "trigger" } local path = vim.fn.expand("%:p") + local extension = vim.fn.expand("%:e") local file_type = vim.bo.filetype local default_username = OrgManager:get_default_username() + local file_contents = "" + local is_unsaved_buffer = false - if file_type == "apex" then - if path == "" then - Debug:log("execute_anon.lua", "Executing anonymous Apex script for current buffer...") - path = vim.fn.expand("%") - if not default_username then - Util.notify_default_org_not_set() - return - end - else - Debug:log("execute_anon.lua", "Executing anonymous Apex script for file...") + if file_type == "apex" and not vim.tbl_contains(disallowed_extensions, extension) then + if vim.fn.empty(path) == 1 then + Debug:log("execute_anon.lua", "File path is empty, retrieving buffer contents...") + is_unsaved_buffer = true + local bufnr = vim.api.nvim_get_current_buf() + local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false) + file_contents = vim.fn.join(lines, "\n") end else - local message = "Not an Apex script file" + local message = "Not a valid Apex script" Debug:log("execute_anon.lua", message) vim.notify(message, vim.log.levels.ERROR) return end + if not default_username then + Util.notify_default_org_not_set() + return + end + Popup:create_popup({}) Popup:write_to_popup("Executing anonymous Apex script...") local executable = Config:get_options().sf_executable - local command = string.format("%s apex run -f '%s' -o %s", executable, path, default_username) - Debug:log("execute_anon.lua", "Running " .. command .. "...") + local args = {} + local writer = nil + if not is_unsaved_buffer then + args = { "apex", "run", "-f", path, "-o", default_username } + local command = + string.format("%s apex run -f '%s' -o %s", executable, path, default_username) + Debug:log("execute_anon.lua", "Running " .. command .. "...") + else + writer = file_contents + args = { "apex", "run", "-o", default_username } + local command = string.format("%s apex run -o %s", executable, default_username) + Debug:log("execute_anon.lua", "Piping unsaved buffer contents into " .. command .. "...") + end local new_job = Job:new({ command = executable, env = Util.get_env(), - args = { "apex", "run", "-f", path, "-o", default_username }, + args = args, + writer = writer, on_exit = function(j, code) vim.schedule(function() if code == 0 then @@ -63,9 +81,11 @@ M.execute_anon = function() on_stderr = function(_, data) vim.schedule(function() Debug:log("execute_anon.lua", "Command stderr is: %s", data) - local trimmed_data = vim.trim(data) - if string.len(trimmed_data) > 0 then - Popup:write_to_popup(data) + if data then + local trimmed_data = vim.trim(data) + if string.len(trimmed_data) > 0 then + Popup:write_to_popup(data) + end end end) end,