1616local M = {}
1717
1818M .execute_anon = function ()
19- Debug :log (" execute_anon.lua" , " Executing anonymous Apex script......" )
19+ Debug :log (" execute_anon.lua" , " Executing anonymous Apex script..." )
20+ local disallowed_extensions = { " cls" , " trigger" }
2021 local path = vim .fn .expand (" %:p" )
22+ local extension = vim .fn .expand (" %:e" )
2123 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- if path == " " then
26- Debug :log (" execute_anon.lua" , " Executing anonymous Apex script for current buffer..." )
27- path = vim .fn .expand (" %" )
28- if not default_username then
29- Util .notify_default_org_not_set ()
30- return
31- end
32- else
33- Debug :log (" execute_anon.lua" , " Executing anonymous Apex script for 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 " )
3435 end
3536 else
36- local message = " Not an Apex script file "
37+ local message = " Not a valid Apex script"
3738 Debug :log (" execute_anon.lua" , message )
3839 vim .notify (message , vim .log .levels .ERROR )
3940 return
4041 end
4142
43+ if not default_username then
44+ Util .notify_default_org_not_set ()
45+ return
46+ end
47+
4248 Popup :create_popup ({})
4349 Popup :write_to_popup (" Executing anonymous Apex script..." )
4450 local executable = Config :get_options ().sf_executable
45- local command = string.format (" %s apex run -f '%s' -o %s" , executable , path , default_username )
46- 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
4764 local new_job = Job :new ({
4865 command = executable ,
4966 env = Util .get_env (),
50- args = { " apex" , " run" , " -f" , path , " -o" , default_username },
67+ args = args ,
68+ writer = writer ,
5169 on_exit = function (j , code )
5270 vim .schedule (function ()
5371 if code == 0 then
@@ -63,9 +81,11 @@ M.execute_anon = function()
6381 on_stderr = function (_ , data )
6482 vim .schedule (function ()
6583 Debug :log (" execute_anon.lua" , " Command stderr is: %s" , data )
66- local trimmed_data = vim .trim (data )
67- if string.len (trimmed_data ) > 0 then
68- 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
6989 end
7090 end )
7191 end ,
0 commit comments