From 3592b1f8b0ca207462bf945af52026e592afda30 Mon Sep 17 00:00:00 2001 From: TJ DeVries Date: Mon, 28 Sep 2020 17:03:59 -0400 Subject: [PATCH] fix: 126 --- lua/telescope/actions.lua | 18 +++++++---- lua/telescope/make_entry.lua | 14 ++------- lua/telescope/path.lua | 60 ++++++++++++++++++++++++++++++++++++ lua/telescope/pickers.lua | 11 +++++++ lua/telescope/utils.lua | 35 ++++----------------- 5 files changed, 92 insertions(+), 46 deletions(-) create mode 100644 lua/telescope/path.lua diff --git a/lua/telescope/actions.lua b/lua/telescope/actions.lua index 9f411e4..d24c683 100644 --- a/lua/telescope/actions.lua +++ b/lua/telescope/actions.lua @@ -3,6 +3,7 @@ local a = vim.api local log = require('telescope.log') +local path = require('telescope.path') local state = require('telescope.state') local actions = setmetatable({}, { @@ -89,11 +90,12 @@ local function goto_file_selection(prompt_bufnr, command) actions.close(prompt_bufnr) + filename = path.normalize(filename, vim.fn.getcwd()) + -- TODO: Sometimes we open something with missing line numbers and stuff... if entry_bufnr then if command == "e" then a.nvim_win_set_buf(original_win_id, entry_bufnr) - vim.api.nvim_command("doautocmd filetypedetect BufRead " .. vim.fn.fnameescape(filename)) else vim.cmd(string.format(":%s #%d", command, entry_bufnr)) end @@ -103,7 +105,6 @@ local function goto_file_selection(prompt_bufnr, command) vim.cmd(string.format(":%s %s", command, filename)) bufnr = vim.api.nvim_get_current_buf() a.nvim_buf_set_option(bufnr, "buflisted", true) - vim.api.nvim_command("doautocmd filetypedetect BufRead " .. vim.fn.fnameescape(filename)) end if row and col then @@ -113,23 +114,27 @@ local function goto_file_selection(prompt_bufnr, command) end end end + + if command == "edit" then + vim.api.nvim_command("doautocmd filetypedetect BufRead " .. vim.fn.fnameescape(filename)) + end end end function actions.goto_file_selection_edit(prompt_bufnr) - goto_file_selection(prompt_bufnr, "e") + goto_file_selection(prompt_bufnr, "edit") end function actions.goto_file_selection_split(prompt_bufnr) - goto_file_selection(prompt_bufnr, "sp") + goto_file_selection(prompt_bufnr, "new") end function actions.goto_file_selection_vsplit(prompt_bufnr) - goto_file_selection(prompt_bufnr, "vsp") + goto_file_selection(prompt_bufnr, "vnew") end function actions.goto_file_selection_tabedit(prompt_bufnr) - goto_file_selection(prompt_bufnr, "tabe") + goto_file_selection(prompt_bufnr, "tabedit") end function actions.close_pum(_) @@ -151,6 +156,7 @@ function actions.close(prompt_bufnr) vim.cmd [[stopinsert]] vim.api.nvim_win_close(prompt_win, true) + pcall(vim.cmd, string.format([[silent bdelete! %s]], prompt_bufnr)) a.nvim_set_current_win(original_win_id) diff --git a/lua/telescope/make_entry.lua b/lua/telescope/make_entry.lua index 1d159be..c62df79 100644 --- a/lua/telescope/make_entry.lua +++ b/lua/telescope/make_entry.lua @@ -1,9 +1,9 @@ local has_devicons, devicons = pcall(require, 'nvim-web-devicons') +local path = require('telescope.path') local utils = require('telescope.utils') local get_default = utils.get_default -local os_sep = utils.get_separator() local make_entry = {} @@ -198,7 +198,7 @@ function make_entry.gen_from_buffer(opts) local make_display = function(entry) local display_bufname if opts.shorten_path then - display_bufname = utils.path_shorten(entry.filename) + display_bufname = path.shorten(entry.filename) else display_bufname = entry.filename end @@ -209,17 +209,9 @@ function make_entry.gen_from_buffer(opts) return function(entry) local bufnr_str = tostring(entry) - local bufname = vim.api.nvim_buf_get_name(entry) + local bufname = path.normalize(vim.api.nvim_buf_get_name(entry), cwd) -- if bufname is inside the cwd, trim that part of the string - if bufname:sub(1, #cwd) == cwd then - local offset = 0 - -- if cwd does ends in the os separator, we need to take it off - if cwd:sub(#cwd, #cwd) ~= os_sep then - offset = 1 - end - bufname = bufname:sub(#cwd + 1 + offset, #bufname) - end local position = get_position(entry) diff --git a/lua/telescope/path.lua b/lua/telescope/path.lua new file mode 100644 index 0000000..8fdbc71 --- /dev/null +++ b/lua/telescope/path.lua @@ -0,0 +1,60 @@ +local path = {} + +-- TODO: Can we use vim.loop for this? +path.separator = package.config:sub(1, 1) +path.home = vim.fn.expand("~") + + +path.make_relative = function(filepath, cwd) + if not cwd or not filepath then return filepath end + + if filepath:sub(1, #cwd) == cwd then + local offset = 0 + -- if cwd does ends in the os separator, we need to take it off + if cwd:sub(#cwd, #cwd) ~= path.separator then + offset = 1 + end + + filepath = filepath:sub(#cwd + 1 + offset, #filepath) + end + + return filepath +end + +path.shorten = (function() + if jit then + local ffi = require('ffi') + ffi.cdef [[ + typedef unsigned char char_u; + char_u *shorten_dir(char_u *str); + ]] + + return function(filepath) + if not filepath then + return filepath + end + + local c_str = ffi.new("char[?]", #filepath + 1) + ffi.copy(c_str, filepath) + return ffi.string(ffi.C.shorten_dir(c_str)) + end + else + return function(filepath) + return filepath + end + end +end)() + +path.normalize = function(filepath, cwd) + filepath = path.make_relative(filepath, cwd) + + -- Substitute home directory w/ "~" + filepath = filepath:gsub("^" .. path.home, '~', 1) + + -- Remove double path separators, it's annoying + filepath = filepath:gsub(path.separator .. path.separator, path.separator) + + return filepath +end + +return path diff --git a/lua/telescope/pickers.lua b/lua/telescope/pickers.lua index b6fba4d..db6ef5d 100644 --- a/lua/telescope/pickers.lua +++ b/lua/telescope/pickers.lua @@ -361,8 +361,15 @@ function Picker:find() return end + if first_line > 0 or last_line > 1 then + return + end + local prompt = vim.api.nvim_buf_get_lines(prompt_bufnr, first_line, last_line, false)[1] + -- TODO: Statusbar possibilities here. + -- vim.api.nvim_buf_set_virtual_text(prompt_bufnr, 0, 1, { {"hello", "Error"} }, {}) + local filtered_amount = 0 local displayed_amount = 0 local displayed_fn_amount = 0 @@ -643,6 +650,10 @@ function Picker:set_selection(row) -- Probably something with setting a row that's too high for this? -- Not sure. local set_ok, set_errmsg = pcall(function() + if not a.nvim_buf_is_valid(results_bufnr) then + return + end + local prompt = vim.api.nvim_buf_get_lines(self.prompt_bufnr, 0, 1, false)[1] -- Handle adding '> ' to beginning of selections diff --git a/lua/telescope/utils.lua b/lua/telescope/utils.lua index 28e79e0..9d948ce 100644 --- a/lua/telescope/utils.lua +++ b/lua/telescope/utils.lua @@ -1,11 +1,10 @@ +local pathlib = require('telescope.path') + local utils = {} -utils.get_separator = (function() - local val = package.config:sub(1, 1) - return function() - return val - end -end)() +utils.get_separator = function() + return pathlib.separator +end utils.if_nil = function(x, was_nil, was_not_nil) if x == nil then @@ -97,29 +96,7 @@ end -- return result -- end or nil) -utils.path_shorten = (function() - if jit then - local ffi = require('ffi') - ffi.cdef [[ - typedef unsigned char char_u; - char_u *shorten_dir(char_u *str); - ]] - - return function(path) - if not path then - return path - end - - local c_str = ffi.new("char[?]", #path + 1) - ffi.copy(c_str, path) - return ffi.string(ffi.C.shorten_dir(c_str)) - end - else - return function(path) - return path - end - end -end)() +utils.path_shorten = pathlib.shorten utils.path_tail = (function() local os_sep = utils.get_separator()