Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check filetype for anon execution instead of file extension #37

Merged
merged 4 commits into from
Apr 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 35 additions & 9 deletions lua/salesforce/execute_anon.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,24 @@ local M = {}

M.execute_anon = function()
Debug:log("execute_anon.lua", "Executing anonymous Apex script...")
local disallowed_extensions = { "cls", "trigger" }
local path = vim.fn.expand("%:p")
local file_type = vim.fn.expand("%:e")
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
local message = "Not an Apex script 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 a valid Apex script"
Debug:log("execute_anon.lua", message)
vim.notify(message, vim.log.levels.ERROR)
return
Expand All @@ -36,12 +48,24 @@ M.execute_anon = function()
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
Expand All @@ -57,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,
Expand Down
Loading