Skip to content

Commit cf0df1b

Browse files
Merge pull request #37 from lkalabis/main
Check filetype for anon execution instead of file extension
2 parents 88b10f2 + e472fe8 commit cf0df1b

File tree

1 file changed

+35
-9
lines changed

1 file changed

+35
-9
lines changed

lua/salesforce/execute_anon.lua

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,24 @@ local M = {}
1717

1818
M.execute_anon = function()
1919
Debug:log("execute_anon.lua", "Executing anonymous Apex script...")
20+
local disallowed_extensions = { "cls", "trigger" }
2021
local path = vim.fn.expand("%:p")
21-
local file_type = vim.fn.expand("%:e")
22+
local extension = vim.fn.expand("%:e")
23+
local file_type = vim.bo.filetype
2224
local default_username = OrgManager:get_default_username()
25+
local file_contents = ""
26+
local is_unsaved_buffer = false
2327

24-
if file_type ~= "apex" then
25-
local message = "Not an Apex script file"
28+
if file_type == "apex" and not vim.tbl_contains(disallowed_extensions, extension) then
29+
if vim.fn.empty(path) == 1 then
30+
Debug:log("execute_anon.lua", "File path is empty, retrieving buffer contents...")
31+
is_unsaved_buffer = true
32+
local bufnr = vim.api.nvim_get_current_buf()
33+
local lines = vim.api.nvim_buf_get_lines(bufnr, 0, -1, false)
34+
file_contents = vim.fn.join(lines, "\n")
35+
end
36+
else
37+
local message = "Not a valid Apex script"
2638
Debug:log("execute_anon.lua", message)
2739
vim.notify(message, vim.log.levels.ERROR)
2840
return
@@ -36,12 +48,24 @@ M.execute_anon = function()
3648
Popup:create_popup({})
3749
Popup:write_to_popup("Executing anonymous Apex script...")
3850
local executable = Config:get_options().sf_executable
39-
local command = string.format("%s apex run -f '%s' -o %s", executable, path, default_username)
40-
Debug:log("execute_anon.lua", "Running " .. command .. "...")
51+
local args = {}
52+
local writer = nil
53+
if not is_unsaved_buffer then
54+
args = { "apex", "run", "-f", path, "-o", default_username }
55+
local command =
56+
string.format("%s apex run -f '%s' -o %s", executable, path, default_username)
57+
Debug:log("execute_anon.lua", "Running " .. command .. "...")
58+
else
59+
writer = file_contents
60+
args = { "apex", "run", "-o", default_username }
61+
local command = string.format("%s apex run -o %s", executable, default_username)
62+
Debug:log("execute_anon.lua", "Piping unsaved buffer contents into " .. command .. "...")
63+
end
4164
local new_job = Job:new({
4265
command = executable,
4366
env = Util.get_env(),
44-
args = { "apex", "run", "-f", path, "-o", default_username },
67+
args = args,
68+
writer = writer,
4569
on_exit = function(j, code)
4670
vim.schedule(function()
4771
if code == 0 then
@@ -57,9 +81,11 @@ M.execute_anon = function()
5781
on_stderr = function(_, data)
5882
vim.schedule(function()
5983
Debug:log("execute_anon.lua", "Command stderr is: %s", data)
60-
local trimmed_data = vim.trim(data)
61-
if string.len(trimmed_data) > 0 then
62-
Popup:write_to_popup(data)
84+
if data then
85+
local trimmed_data = vim.trim(data)
86+
if string.len(trimmed_data) > 0 then
87+
Popup:write_to_popup(data)
88+
end
6389
end
6490
end)
6591
end,

0 commit comments

Comments
 (0)