765765--- @param state neotree.State
766766--- @param path string The file to open
767767--- @param open_cmd string ? The vimcommand to use to open the file
768- --- @param bufnr number | nil The buffer number to open
768+ --- @param bufnr integer ? The buffer number to open
769769M .open_file = function (state , path , open_cmd , bufnr )
770770 open_cmd = open_cmd or " edit"
771771 -- If the file is already open, switch to it.
@@ -783,65 +783,67 @@ M.open_file = function(state, path, open_cmd, bufnr)
783783 end
784784 end
785785
786- if M .truthy (path ) then
787- local relative = require (" neo-tree" ).config .open_files_using_relative_paths
788- local escaped_path = M .escape_path_for_cmd (relative and vim .fn .fnamemodify (path , " :." ) or path )
789- local bufnr_or_path = bufnr or escaped_path
790- local events = require (" neo-tree.events" )
791- local result = true
792- local err = nil
793- local event_result = events .fire_event (events .FILE_OPEN_REQUESTED , {
794- state = state ,
795- path = path ,
796- open_cmd = open_cmd ,
797- bufnr = bufnr ,
798- }) or {}
799- if event_result .handled then
800- events .fire_event (events .FILE_OPENED , path )
801- return
802- end
803- if state .current_position == " current" then
786+ if not path or not M .truthy (path ) then
787+ return
788+ end
789+
790+ local relative = require (" neo-tree" ).config .open_files_using_relative_paths
791+ local escaped_path = M .escape_path_for_cmd (relative and vim .fn .fnamemodify (path , " :." ) or path )
792+ local bufnr_or_path = bufnr or escaped_path
793+ local events = require (" neo-tree.events" )
794+ local result = true
795+ local err = nil
796+ local event_result = events .fire_event (events .FILE_OPEN_REQUESTED , {
797+ state = state ,
798+ path = path ,
799+ open_cmd = open_cmd ,
800+ bufnr = bufnr ,
801+ }) or {}
802+ if event_result .handled then
803+ events .fire_event (events .FILE_OPENED , path )
804+ return
805+ end
806+ if state .current_position == " current" then
807+ --- @diagnostic disable-next-line : param-type-mismatch
808+ result , err = pcall (vim .cmd , open_cmd .. " " .. bufnr_or_path )
809+ else
810+ local winid , is_neo_tree_window = M .get_appropriate_window (state )
811+ vim .api .nvim_set_current_win (winid )
812+ -- TODO: make this configurable, see issue #43
813+ if is_neo_tree_window then
814+ local width = vim .api .nvim_win_get_width (0 )
815+ if width == vim .o .columns then
816+ -- Neo-tree must be the only window, restore it's status as a sidebar
817+ width = M .get_value (state , " window.width" , 40 , false )
818+ width = M .resolve_width (width )
819+ end
820+ result , err = M .force_new_split (state .current_position , escaped_path )
821+ vim .api .nvim_win_set_width (winid , width )
822+ else
804823 --- @diagnostic disable-next-line : param-type-mismatch
805824 result , err = pcall (vim .cmd , open_cmd .. " " .. bufnr_or_path )
806- else
807- local winid , is_neo_tree_window = M .get_appropriate_window (state )
808- vim .api .nvim_set_current_win (winid )
809- -- TODO: make this configurable, see issue #43
810- if is_neo_tree_window then
811- local width = vim .api .nvim_win_get_width (0 )
812- if width == vim .o .columns then
813- -- Neo-tree must be the only window, restore it's status as a sidebar
814- width = M .get_value (state , " window.width" , 40 , false )
815- width = M .resolve_width (width )
816- end
817- result , err = M .force_new_split (state .current_position , escaped_path )
818- vim .api .nvim_win_set_width (winid , width )
819- else
820- --- @diagnostic disable-next-line : param-type-mismatch
821- result , err = pcall (vim .cmd , open_cmd .. " " .. bufnr_or_path )
822- end
823- end
824- if not result and string.find (err or " " , " winfixbuf" ) and M .is_winfixbuf () then
825- local winid , is_neo_tree_window = M .get_appropriate_window (state , true )
826- -- Rescan window list to find a window that is not winfixbuf.
827- -- If found, retry executing command in that window,
828- -- otherwise, all windows are either neo-tree or winfixbuf so we make a new split.
829- if not is_neo_tree_window and not M .is_winfixbuf (winid ) then
830- vim .api .nvim_set_current_win (winid )
831- --- @diagnostic disable-next-line : param-type-mismatch
832- result , err = pcall (vim .cmd , open_cmd .. " " .. bufnr_or_path )
833- else
834- result , err = M .force_new_split (state .current_position , escaped_path )
835- end
836825 end
837- if result or err == " Vim(edit):E325: ATTENTION" then
838- -- fixes #321
839- vim .bo [0 ].buflisted = true
840- events .fire_event (events .FILE_OPENED , path )
826+ end
827+ if not result and string.find (err or " " , " winfixbuf" ) and M .is_winfixbuf () then
828+ local winid , is_neo_tree_window = M .get_appropriate_window (state , true )
829+ -- Rescan window list to find a window that is not winfixbuf.
830+ -- If found, retry executing command in that window,
831+ -- otherwise, all windows are either neo-tree or winfixbuf so we make a new split.
832+ if not is_neo_tree_window and not M .is_winfixbuf (winid ) then
833+ vim .api .nvim_set_current_win (winid )
834+ --- @diagnostic disable-next-line : param-type-mismatch
835+ result , err = pcall (vim .cmd , open_cmd .. " " .. bufnr_or_path )
841836 else
842- log . error ( " Error opening file: " , err )
837+ result , err = M . force_new_split ( state . current_position , escaped_path )
843838 end
844839 end
840+ if result or err == " Vim(edit):E325: ATTENTION" then
841+ -- fixes #321
842+ vim .bo [0 ].buflisted = true
843+ events .fire_event (events .FILE_OPENED , path )
844+ else
845+ log .error (" Error opening file:" , err )
846+ end
845847end
846848
847849M .reduce = function (list , memo , func )
0 commit comments