Skip to content

Commit

Permalink
fix: edit grep (#92)
Browse files Browse the repository at this point in the history
* WIP

* WIP: revert

* fix: edit grep
  • Loading branch information
linrongbin16 authored Aug 21, 2023
1 parent 2663cd4 commit 5f2f017
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 68 deletions.
2 changes: 1 addition & 1 deletion bin/buffers/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ shell_helpers.log_debug(
if type(buffers) == "table" and #buffers > 0 then
for _, line in ipairs(buffers) do
-- shell_helpers.log_debug("line:%s", vim.inspect(line))
local line_with_icon = shell_helpers.render_line_with_icon(line)
local line_with_icon = shell_helpers.render_filepath_line(line)
io.write(string.format("%s\n", line_with_icon))
end
end
2 changes: 1 addition & 1 deletion bin/files/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ shell_helpers.log_ensure(
--- @diagnostic disable-next-line: need-check-nil
for line in p:lines("*line") do
-- shell_helpers.log_debug("line:%s", vim.inspect(line))
local line_with_icon = shell_helpers.render_line_with_icon(line)
local line_with_icon = shell_helpers.render_filepath_line(line)
io.write(string.format("%s\n", line_with_icon))
end
--- @diagnostic disable-next-line: need-check-nil
Expand Down
2 changes: 1 addition & 1 deletion bin/git_files/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ shell_helpers.log_ensure(
--- @diagnostic disable-next-line: need-check-nil
for line in p:lines("*line") do
-- shell_helpers.log_debug("line:%s", vim.inspect(line))
local line_with_icon = shell_helpers.render_line_with_icon(line)
local line_with_icon = shell_helpers.render_filepath_line(line)
io.write(string.format("%s\n", line_with_icon))
end
--- @diagnostic disable-next-line: need-check-nil
Expand Down
3 changes: 1 addition & 2 deletions bin/live_grep/provider.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ shell_helpers.log_ensure(
)
--- @diagnostic disable-next-line: need-check-nil
for line in p:lines("*line") do
local line_with_icon =
shell_helpers.render_delimiter_line_with_icon(line, ":", 1)
local line_with_icon = shell_helpers.render_filepath_line(line, ":", 1)
io.write(string.format("%s\n", line_with_icon))
end
--- @diagnostic disable-next-line: need-check-nil
Expand Down
39 changes: 39 additions & 0 deletions lua/fzfx/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ local function edit_rg(lines)
local splits = vim.fn.split(line, ":")
local filename = env.icon_enable() and vim.fn.split(splits[1])[2]
or splits[1]
log.debug("|fzfx.actions - edit_rg| filename:%s", vim.inspect(filename))
filename = path.normalize(filename)
local row = tonumber(splits[2])
local col = tonumber(splits[3])
Expand Down Expand Up @@ -50,6 +51,43 @@ local function edit_rg(lines)
end
end

local function edit_grep(lines)
log.debug("|fzfx.actions - edit_grep| lines:%s", vim.inspect(lines))
for i, line in ipairs(lines) do
local splits = vim.fn.split(line, ":")
local filename = env.icon_enable() and vim.fn.split(splits[1])[2]
or splits[1]
log.debug(
"|fzfx.actions - edit_grep| filename:%s",
vim.inspect(filename)
)
filename = path.normalize(filename)
local row = tonumber(splits[2])
local col = 0
local edit_cmd = string.format("edit %s", vim.fn.expand(filename))
local setpos_cmd =
string.format("call setpos('.', [0, %d, %d])", row, col)
log.debug(
"|fzfx.actions - edit_grep| line[%d] - splits:%s, filename:%s, row:%d, col:%d",
i,
vim.inspect(splits),
vim.inspect(filename),
vim.inspect(row),
vim.inspect(col)
)
log.debug(
"|fzfx.actions - edit_grep| line[%d] - edit_cmd:[%s], setpos_cmd:[%s]",
i,
edit_cmd,
setpos_cmd
)
vim.cmd(edit_cmd)
if i == #lines then
vim.cmd(setpos_cmd)
end
end
end

local function buffer(lines)
log.debug("|fzfx.actions - buffer| lines:%s", vim.inspect(lines))
for i, line in ipairs(lines) do
Expand Down Expand Up @@ -127,6 +165,7 @@ local M = {
nop = nop,
edit = edit,
edit_rg = edit_rg,
edit_grep = edit_grep,
buffer = buffer,
buffer_rg = buffer_rg,
bdelete = bdelete,
Expand Down
7 changes: 5 additions & 2 deletions lua/fzfx/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -264,8 +264,11 @@ local Defaults = {
},
actions = {
["esc"] = require("fzfx.actions").nop,
["enter"] = require("fzfx.actions").edit_rg,
["double-click"] = require("fzfx.actions").edit_rg,
["enter"] = constants.has_rg and require("fzfx.actions").edit_rg
or require("fzfx.actions").edit_grep,
["double-click"] = constants.has_rg
and require("fzfx.actions").edit_rg
or require("fzfx.actions").edit_grep,
},
fzf_opts = {
default_fzf_options.multi,
Expand Down
2 changes: 2 additions & 0 deletions lua/fzfx/constants.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-- Zero Dependency

--- @type boolean
local is_windows = vim.fn.has("win32") > 0 or vim.fn.has("win64") > 0
--- @type boolean
Expand Down
120 changes: 59 additions & 61 deletions lua/fzfx/shell_helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -164,72 +164,71 @@ local function csi(color, fg)
end
end

local function render_line_with_icon(line)
if DEVICONS ~= nil then
local ext = vim.fn.fnamemodify(line, ":e")
local icon, icon_color = DEVICONS.get_icon_color(line, ext)
-- if DEBUG_ENABLE then
-- log_debug(
-- "|fzfx.shell_helpers - render_line_with_icon| line:%s, ext:%s, icon:%s, color:%s\n",
-- vim.inspect(line),
-- vim.inspect(ext),
-- vim.inspect(icon),
-- vim.inspect(color)
-- )
-- end
if type(icon) == "string" and string.len(icon) > 0 then
local colorfmt = csi(icon_color, true)
if colorfmt then
return string.format("[%sm%s %s", colorfmt, icon, line)
else
return string.format("%s %s", icon, line)
end
else
if vim.fn.isdirectory(line) > 0 then
return string.format("%s %s", FOLDER_ICON, line)
else
return string.format("%s %s", UNKNOWN_FILE_ICON, line)
end
end
else
return line
--- @param p string?
local function normalize_filepath(p)
if type(p) ~= "string" or string.len(p) == 0 then
return p
end
if p:sub(1, 2) == [[./]] or p:sub(1, 2) == [[.\]] then
return p:sub(3, #p)
end
return p
end

local function render_delimiter_line_with_icon(line, delimiter, pos)
if DEVICONS ~= nil then
--- @param line string
--- @param delimiter string?
--- @param pos integer?
local function render_filepath_line(line, delimiter, pos)
if DEVICONS == nil then
return normalize_filepath(line)
end
local filename = nil
if
type(delimiter) == "string"
and string.len(delimiter) > 0
and type(pos) == "number"
then
local splits = vim.fn.split(line, delimiter)
local filename = splits[pos]
if type(filename) == "string" and string.len(filename) > 0 then
filename = filename:gsub("\x1b%[%d+m", "")
end
local ext = vim.fn.fnamemodify(filename, ":e")
local icon, color = DEVICONS.get_icon_color(filename, ext)
-- if DEBUG_ENABLE then
-- log_debug(
-- "|fzfx.shell_helpers - render_line_with_icon| line:%s, ext:%s, icon:%s, color:%s\n",
-- vim.inspect(line),
-- vim.inspect(ext),
-- vim.inspect(icon),
-- vim.inspect(color)
-- )
-- end
if type(icon) == "string" and string.len(icon) > 0 then
local colorfmt = csi(color, true)
if colorfmt then
return string.format("[%sm%s %s", colorfmt, icon, line)
else
return string.format("%s %s", icon, line)
end
filename = splits[pos]
else
filename = line
end
if type(filename) == "string" and string.len(filename) > 0 then
filename = filename:gsub("\x1b%[%d+m", "")
end
local ext = vim.fn.fnamemodify(filename, ":e")
local icon, icon_color = DEVICONS.get_icon_color(filename, ext)
-- if DEBUG_ENABLE then
-- log_debug(
-- "|fzfx.shell_helpers - render_line_with_icon| line:%s, ext:%s, icon:%s, color:%s\n",
-- vim.inspect(line),
-- vim.inspect(ext),
-- vim.inspect(icon),
-- vim.inspect(color)
-- )
-- end
if type(icon) == "string" and string.len(icon) > 0 then
local colorfmt = csi(icon_color, true)
if colorfmt then
return string.format(
"[%sm%s %s",
colorfmt,
icon,
normalize_filepath(line)
)
else
if vim.fn.isdirectory(filename) > 0 then
return string.format("%s %s", FOLDER_ICON, line)
else
return string.format("%s %s", UNKNOWN_FILE_ICON, line)
end
return string.format("%s %s", icon, normalize_filepath(line))
end
else
return line
if vim.fn.isdirectory(filename) > 0 then
return string.format("%s %s", FOLDER_ICON, normalize_filepath(line))
else
return string.format(
"%s %s",
UNKNOWN_FILE_ICON,
normalize_filepath(line)
)
end
end
end

Expand Down Expand Up @@ -273,8 +272,7 @@ local M = {
log_ensure = log_ensure,
read_provider_command = read_provider_command,
color_csi = csi,
render_line_with_icon = render_line_with_icon,
render_delimiter_line_with_icon = render_delimiter_line_with_icon,
render_filepath_line = render_filepath_line,
parse_query = parse_query,
Command = require("fzfx.command").Command,
GitRootCommand = require("fzfx.git_helpers").GitRootCommand,
Expand Down
2 changes: 2 additions & 0 deletions lua/fzfx/utils.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
-- Zero Dependency

local constants = require("fzfx.constants")

local function table_filter(f, t)
Expand Down

0 comments on commit 5f2f017

Please sign in to comment.