@@ -16,12 +16,24 @@ local M = {}
1616
1717M .execute_anon = function ()
1818 Debug :log (" execute_anon.lua" , " Executing anonymous Apex script..." )
19+ local disallowed_extensions = { " cls" , " trigger" }
1920 local path = vim .fn .expand (" %:p" )
20- local file_type = vim .fn .expand (" %:e" )
21+ local extension = vim .fn .expand (" %:e" )
22+ local file_type = vim .bo .filetype
2123 local default_username = OrgManager :get_default_username ()
24+ local file_contents = " "
25+ local is_unsaved_buffer = false
2226
23- if file_type ~= " apex" then
24- local message = " Not an Apex script file"
27+ if file_type == " apex" and not vim .tbl_contains (disallowed_extensions , extension ) then
28+ if vim .fn .empty (path ) == 1 then
29+ Debug :log (" execute_anon.lua" , " File path is empty, retrieving buffer contents..." )
30+ is_unsaved_buffer = true
31+ local bufnr = vim .api .nvim_get_current_buf ()
32+ local lines = vim .api .nvim_buf_get_lines (bufnr , 0 , - 1 , false )
33+ file_contents = vim .fn .join (lines , " \n " )
34+ end
35+ else
36+ local message = " Not a valid Apex script"
2537 Debug :log (" execute_anon.lua" , message )
2638 vim .notify (message , vim .log .levels .ERROR )
2739 return
@@ -35,12 +47,24 @@ M.execute_anon = function()
3547 Popup :create_popup ({})
3648 Popup :write_to_popup (" Executing anonymous Apex script..." )
3749 local executable = Util .get_sf_executable ()
38- local command = string.format (" %s apex run -f '%s' -o %s" , executable , path , default_username )
39- Debug :log (" execute_anon.lua" , " Running " .. command .. " ..." )
50+ local args = {}
51+ local writer = nil
52+ if not is_unsaved_buffer then
53+ args = { " apex" , " run" , " -f" , path , " -o" , default_username }
54+ local command =
55+ string.format (" %s apex run -f '%s' -o %s" , executable , path , default_username )
56+ Debug :log (" execute_anon.lua" , " Running " .. command .. " ..." )
57+ else
58+ writer = file_contents
59+ args = { " apex" , " run" , " -o" , default_username }
60+ local command = string.format (" %s apex run -o %s" , executable , default_username )
61+ Debug :log (" execute_anon.lua" , " Piping unsaved buffer contents into " .. command .. " ..." )
62+ end
4063 local new_job = Job :new ({
4164 command = executable ,
4265 env = Util .get_env (),
43- args = { " apex" , " run" , " -f" , path , " -o" , default_username },
66+ args = args ,
67+ writer = writer ,
4468 on_exit = function (j , code )
4569 vim .schedule (function ()
4670 if code == 0 then
@@ -56,9 +80,11 @@ M.execute_anon = function()
5680 on_stderr = function (_ , data )
5781 vim .schedule (function ()
5882 Debug :log (" execute_anon.lua" , " Command stderr is: %s" , data )
59- local trimmed_data = vim .trim (data )
60- if string.len (trimmed_data ) > 0 then
61- Popup :write_to_popup (data )
83+ if data then
84+ local trimmed_data = vim .trim (data )
85+ if string.len (trimmed_data ) > 0 then
86+ Popup :write_to_popup (data )
87+ end
6288 end
6389 end )
6490 end ,
0 commit comments