754754
755755--- Open file in the appropriate window.
756756--- @param state table The state of the source
757- --- @param path string The file to open
757+ --- @param path string ? The file to open
758758--- @param open_cmd string ? The vimcommand to use to open the file
759- --- @param bufnr number | nil The buffer number to open
759+ --- @param bufnr integer ? The buffer number to open
760760M .open_file = function (state , path , open_cmd , bufnr )
761761 open_cmd = open_cmd or " edit"
762762 -- If the file is already open, switch to it.
@@ -774,65 +774,67 @@ M.open_file = function(state, path, open_cmd, bufnr)
774774 end
775775 end
776776
777- if M .truthy (path ) then
778- local relative = require (" neo-tree" ).config .open_files_using_relative_paths
779- local escaped_path = M .escape_path_for_cmd (relative and vim .fn .fnamemodify (path , " :." ) or path )
780- local bufnr_or_path = bufnr or escaped_path
781- local events = require (" neo-tree.events" )
782- local result = true
783- local err = nil
784- local event_result = events .fire_event (events .FILE_OPEN_REQUESTED , {
785- state = state ,
786- path = path ,
787- open_cmd = open_cmd ,
788- bufnr = bufnr ,
789- }) or {}
790- if event_result .handled then
791- events .fire_event (events .FILE_OPENED , path )
792- return
793- end
794- if state .current_position == " current" then
777+ if not path or not M .truthy (path ) then
778+ return
779+ end
780+
781+ local relative = require (" neo-tree" ).config .open_files_using_relative_paths
782+ local escaped_path = M .escape_path_for_cmd (relative and vim .fn .fnamemodify (path , " :." ) or path )
783+ local bufnr_or_path = bufnr or escaped_path
784+ local events = require (" neo-tree.events" )
785+ local result = true
786+ local err = nil
787+ local event_result = events .fire_event (events .FILE_OPEN_REQUESTED , {
788+ state = state ,
789+ path = path ,
790+ open_cmd = open_cmd ,
791+ bufnr = bufnr ,
792+ }) or {}
793+ if event_result .handled then
794+ events .fire_event (events .FILE_OPENED , path )
795+ return
796+ end
797+ if state .current_position == " current" then
798+ --- @diagnostic disable-next-line : param-type-mismatch
799+ result , err = pcall (vim .cmd , open_cmd .. " " .. bufnr_or_path )
800+ else
801+ local winid , is_neo_tree_window = M .get_appropriate_window (state )
802+ vim .api .nvim_set_current_win (winid )
803+ -- TODO: make this configurable, see issue #43
804+ if is_neo_tree_window then
805+ local width = vim .api .nvim_win_get_width (0 )
806+ if width == vim .o .columns then
807+ -- Neo-tree must be the only window, restore it's status as a sidebar
808+ width = M .get_value (state , " window.width" , 40 , false )
809+ width = M .resolve_width (width )
810+ end
811+ result , err = M .force_new_split (state .current_position , escaped_path )
812+ vim .api .nvim_win_set_width (winid , width )
813+ else
795814 --- @diagnostic disable-next-line : param-type-mismatch
796815 result , err = pcall (vim .cmd , open_cmd .. " " .. bufnr_or_path )
797- else
798- local winid , is_neo_tree_window = M .get_appropriate_window (state )
799- vim .api .nvim_set_current_win (winid )
800- -- TODO: make this configurable, see issue #43
801- if is_neo_tree_window then
802- local width = vim .api .nvim_win_get_width (0 )
803- if width == vim .o .columns then
804- -- Neo-tree must be the only window, restore it's status as a sidebar
805- width = M .get_value (state , " window.width" , 40 , false )
806- width = M .resolve_width (width )
807- end
808- result , err = M .force_new_split (state .current_position , escaped_path )
809- vim .api .nvim_win_set_width (winid , width )
810- else
811- --- @diagnostic disable-next-line : param-type-mismatch
812- result , err = pcall (vim .cmd , open_cmd .. " " .. bufnr_or_path )
813- end
814- end
815- if not result and string.find (err or " " , " winfixbuf" ) and M .is_winfixbuf () then
816- local winid , is_neo_tree_window = M .get_appropriate_window (state , true )
817- -- Rescan window list to find a window that is not winfixbuf.
818- -- If found, retry executing command in that window,
819- -- otherwise, all windows are either neo-tree or winfixbuf so we make a new split.
820- if not is_neo_tree_window and not M .is_winfixbuf (winid ) then
821- vim .api .nvim_set_current_win (winid )
822- --- @diagnostic disable-next-line : param-type-mismatch
823- result , err = pcall (vim .cmd , open_cmd .. " " .. bufnr_or_path )
824- else
825- result , err = M .force_new_split (state .current_position , escaped_path )
826- end
827816 end
828- if result or err == " Vim(edit):E325: ATTENTION" then
829- -- fixes #321
830- vim .bo [0 ].buflisted = true
831- events .fire_event (events .FILE_OPENED , path )
817+ end
818+ if not result and string.find (err or " " , " winfixbuf" ) and M .is_winfixbuf () then
819+ local winid , is_neo_tree_window = M .get_appropriate_window (state , true )
820+ -- Rescan window list to find a window that is not winfixbuf.
821+ -- If found, retry executing command in that window,
822+ -- otherwise, all windows are either neo-tree or winfixbuf so we make a new split.
823+ if not is_neo_tree_window and not M .is_winfixbuf (winid ) then
824+ vim .api .nvim_set_current_win (winid )
825+ --- @diagnostic disable-next-line : param-type-mismatch
826+ result , err = pcall (vim .cmd , open_cmd .. " " .. bufnr_or_path )
832827 else
833- log . error ( " Error opening file: " , err )
828+ result , err = M . force_new_split ( state . current_position , escaped_path )
834829 end
835830 end
831+ if result or err == " Vim(edit):E325: ATTENTION" then
832+ -- fixes #321
833+ vim .bo [0 ].buflisted = true
834+ events .fire_event (events .FILE_OPENED , path )
835+ else
836+ log .error (" Error opening file:" , err )
837+ end
836838end
837839
838840M .reduce = function (list , memo , func )
0 commit comments