Skip to content

Commit

Permalink
enable collapse dirs
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon McLean committed Sep 15, 2024
1 parent 94f3089 commit 615795f
Show file tree
Hide file tree
Showing 8 changed files with 188 additions and 125 deletions.
23 changes: 22 additions & 1 deletion lua/triptych/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,16 @@ function Actions.new(State, refresh_view)
refresh_view()
end

---@return nil
M.toggle_collapse_dirs = function()
if State.collapse_dirs then
State.collapse_dirs = false
else
State.collapse_dirs = true
end
refresh_view()
end

---@return nil
M.jump_to_cwd = function()
local cwd = vim.fn.getcwd()
Expand All @@ -385,6 +395,12 @@ function Actions.new(State, refresh_view)
end
end

---@param path string
---@return nil
M.jump_to_dir = function(path)
view.set_primary_and_parent_window_targets(State, path)
end

M.nav_left = function()
local parent_path = State.windows.parent.path
if parent_path ~= '/' then
Expand All @@ -396,7 +412,12 @@ function Actions.new(State, refresh_view)
local target = view.get_target_under_cursor(State)
if target then
if target.is_dir then
view.set_primary_and_parent_window_targets(State, target.path)
local target_path
if State.collapse_dirs and target.collapse_path then
view.set_primary_and_parent_window_targets(State, target.collapse_path)
else
view.set_primary_and_parent_window_targets(State, target.path)
end
else
edit_file(target.path, 'in-place')
end
Expand Down
88 changes: 88 additions & 0 deletions lua/triptych/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,92 @@ local function config_warn(config_prop_name)
end
end

---@class TriptychConfig
---@field debug boolean
---@field mappings TriptychConfigMappings
---@field extension_mappings { [string]: ExtensionMapping }
---@field options TriptychConfigOptions
---@field git_signs TriptychConfigGitSigns
---@field diagnostic_signs TriptychConfigDiagnostic

---@class TriptychConfigMappings
---@field show_help KeyMapping
---@field jump_to_cwd KeyMapping
---@field nav_left KeyMapping
---@field nav_right KeyMapping
---@field open_vsplit KeyMapping
---@field open_hsplit KeyMapping
---@field open_tab KeyMapping
---@field cd KeyMapping
---@field delete KeyMapping
---@field add KeyMapping
---@field copy KeyMapping
---@field rename KeyMapping
---@field cut KeyMapping
---@field paste KeyMapping
---@field quit KeyMapping
---@field toggle_hidden KeyMapping
---@field toggle_collapse_dirs KeyMapping

---@class ExtensionMapping
---@field mode string
---@field fn fun(contents?: PathDetails, refresh_fn: fun(): nil): nil

---@class TriptychConfigOptions
---@field dirs_first boolean
---@field show_hidden boolean
---@field line_numbers TriptychConfigLineNumbers
---@field file_icons TriptychConfigFileIcons
---@field responsive_column_widths { [string]: number[] }
---@field highlights TriptychConfigHighlights
---@field syntax_highlighting TriptychConfigSyntaxHighlighting
---@field backdrop number
---@field border string | table
---@field max_height number
---@field max_width number
---@field margin_x number
---@field margin_y number

---@class TriptychConfigHighlights
---@field file_names string
---@field directory_names string

---@class TriptychConfigSyntaxHighlighting
---@field enabled boolean
---@field debounce_ms number

---@class TriptychConfigLineNumbers
---@field enabled boolean
---@field relative boolean

---@class TriptychConfigFileIcons
---@field enabled boolean
---@field directory_icon string
---@field fallback_file_icon string

---@class TriptychConfigGitSigns
---@field enabled boolean
---@field signs TriptychConfigGitSignsSigns

---@class TriptychConfigGitSignsSigns
---@field add string | TriptychConfigGitSignDefineOptions
---@field modify string | TriptychConfigGitSignDefineOptions
---@field rename string | TriptychConfigGitSignDefineOptions
---@field untracked string | TriptychConfigGitSignDefineOptions

---@class TriptychConfigGitSignDefineOptions
---@field icon? string
---@field linehl? string
---@field numhl? string
---@field text? string
---@field texthl? string
---@field culhl? string

---@class TriptychConfigDiagnostic
---@field enabled boolean

---@alias KeyMapping (string | string[])

---@return TriptychConfig
local function default_config()
return {
Expand All @@ -26,12 +112,14 @@ local function default_config()
rename = 'r',
cut = 'x',
paste = 'p',
toggle_collapse_dirs = 'z',
quit = 'q',
toggle_hidden = '<leader>.',
},
extension_mappings = {},
options = {
dirs_first = true,
collapse_dirs = true,
show_hidden = false,
line_numbers = {
enabled = true,
Expand Down
8 changes: 7 additions & 1 deletion lua/triptych/event_handlers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ local log = require 'triptych.logger'
local float = require 'triptych.float'
local autocmds = require 'triptych.autocmds'
local view = require 'triptych.view'
local fs = require 'triptych.fs'

local M = {}

Expand All @@ -16,7 +17,12 @@ function M.handle_cursor_moved(State)
local line_number = vim.api.nvim_win_get_cursor(0)[1]
State.path_to_line_map[current_dir] = line_number
if target then
view.set_child_window_target(State, target)
if State.collapse_dirs and target.is_dir and target.collapse_path then
local path_details = fs.read_path(target.collapse_path, State.show_hidden)
view.set_child_window_target(State, path_details)
else
view.set_child_window_target(State, target)
end
end
end

Expand Down
51 changes: 37 additions & 14 deletions lua/triptych/fs.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,31 @@ M.read_file_async = plenary_async.wrap(function(file_path, callback)
end)
end, 2)

local function drill(path, display_name)
local handle, _ = vim.loop.fs_scandir(path)
if not handle then
return path, display_name
end

local dirs = {}
while true do
local name, type = vim.loop.fs_scandir_next(handle)
if not name or type ~= 'directory' then
break
end
table.insert(dirs, name)
end

if #dirs == 1 then
return drill(path .. '/' .. dirs[1], display_name .. dirs[1] .. '/')
end

return path, display_name
end

---@param _path string
---@param show_hidden boolean
---@param callback fun(path_details: PathDetails): nil
function M.get_path_details(_path, show_hidden, callback)
function M.read_path(_path, show_hidden)
local path = vim.fs.normalize(_path)

local tree = {
Expand All @@ -56,8 +77,7 @@ function M.get_path_details(_path, show_hidden, callback)
local handle, _ = vim.loop.fs_scandir(path)
if not handle then
-- On error fallback to cwd
M.get_path_details(vim.fn.getcwd(), show_hidden, callback)
return
return M.get_path_details(vim.fn.getcwd(), show_hidden)
end

while true do
Expand All @@ -66,24 +86,27 @@ function M.get_path_details(_path, show_hidden, callback)
break
end
local entry_path = path .. '/' .. name
table.insert(tree.children, {
display_name = u.eval(function()
if type == 'directory' then
return name .. '/'
end
return name
end),
local is_dir = type == 'directory'
local display_name = is_dir and (name .. '/') or name
local entry = {
display_name = display_name,
path = entry_path,
dirname = path,
is_dir = type == 'directory',
is_dir = is_dir,
filetype = u.eval(function()
if type == 'directory' then
return
end
return M.get_filetype_from_path(entry_path)
end),
children = {},
})
}
if is_dir then
local collapsed_path, collapsed_display_name = drill(entry_path, display_name)
entry.collapse_path = collapsed_path
entry.collapse_display_name = collapsed_display_name
end
table.insert(tree.children, entry)
end

if vim.g.triptych_config.options.dirs_first then
Expand All @@ -98,7 +121,7 @@ function M.get_path_details(_path, show_hidden, callback)
end)
end

callback(tree)
return tree
end

return M
1 change: 1 addition & 0 deletions lua/triptych/mappings.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ function Mappings.new(State, actions, refresh_fn)
map('n', mappings.paste, actions.paste)
map('n', mappings.show_help, actions.help)
map('n', mappings.toggle_hidden, actions.toggle_hidden)
map('n', mappings.toggle_collapse_dirs, actions.toggle_collapse_dirs)
map('n', mappings.quit, function()
vim.g.triptych_close() -- TODO: Move to actions
end)
Expand Down
16 changes: 16 additions & 0 deletions lua/triptych/state.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
local u = require 'triptych.utils'

---@class TriptychState
---@field new fun(config: TriptychConfig, opening_win: integer): TriptychState
---@field list_add fun(self: TriptychState, list_type: 'cut' | 'copy', item: PathDetails): nil
---@field list_remove fun(self: TriptychState, list_type: 'cut' | 'copy', item: PathDetails): nil
---@field list_remove_all fun(self: TriptychState, list_type: 'cut' | 'copy'): nil
---@field list_toggle fun(self: TriptychState, list_type: 'cut' | 'copy', item: PathDetails): nil
---@field list_contains fun(self: TriptychState, list_type: 'cut' | 'copy', item: PathDetails): nil
---@field windows ViewState
---@field cut_list PathDetails[]
---@field copy_list PathDetails[]
---@field path_to_line_map { [string]: integer }
---@field opening_win integer
---@field show_hidden boolean
---@field collapse_dirs boolean
---@field has_initial_cursor_pos_been_set boolean
local TriptychState = {}

---@return TriptychState
Expand All @@ -8,6 +23,7 @@ function TriptychState.new(config, opening_win)
setmetatable(instance, { __index = TriptychState })

instance.show_hidden = config.options.show_hidden
instance.collapse_dirs = config.options.collapse_dirs
instance.windows = {
parent = {
path = '',
Expand Down
Loading

0 comments on commit 615795f

Please sign in to comment.