Skip to content

Commit

Permalink
add: global event structure, and refactoring.
Browse files Browse the repository at this point in the history
  • Loading branch information
obaland committed Aug 3, 2023
1 parent f4fe43f commit 61d49d6
Show file tree
Hide file tree
Showing 43 changed files with 604 additions and 281 deletions.
3 changes: 3 additions & 0 deletions lua/vfiler.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local config = require('vfiler/config')
local core = require('vfiler/libs/core')
local event = require('vfiler/event')
local vim = require('vfiler/libs/vim')

local VFiler = require('vfiler/vfiler')
Expand Down Expand Up @@ -68,7 +69,9 @@ function M.start(dirpath, configs)
local merged_configs = core.table.copy(config.configs)
core.table.merge(merged_configs, configs or {})

-- Preparation before starting
VFiler.cleanup()
event.start()

-- Correction of option values
local options = merged_configs.options
Expand Down
1 change: 1 addition & 0 deletions lua/vfiler/action.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local action_modules = {
'yank',
}

-- Load each action APIs.
local M = setmetatable({}, {
__index = function(t, key)
for _, name in ipairs(action_modules) do
Expand Down
6 changes: 3 additions & 3 deletions lua/vfiler/actions/bookmark.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local api = require('vfiler/actions/api')
local utils = require('vfiler/actions/utilities')

local M = {}

Expand All @@ -11,10 +11,10 @@ function M.list_bookmark(vfiler, context, view)
local Bookmark = require('vfiler/extensions/bookmark')
local bookmark = Bookmark.new(vfiler, {
on_selected = function(filer, c, v, path, open_type)
api.open_file(filer, c, v, path, open_type)
utils.open_file(filer, c, v, path, open_type)
end,
})
api.start_extension(vfiler, context, view, bookmark)
utils.start_extension(vfiler, context, view, bookmark)
end

return M
21 changes: 7 additions & 14 deletions lua/vfiler/actions/buffer.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local api = require('vfiler/actions/api')
local utils = require('vfiler/actions/utilities')
local core = require('vfiler/libs/core')
local vim = require('vfiler/libs/vim')

Expand All @@ -12,13 +12,13 @@ local M = {}

function M.quit(vfiler, context, view)
if context.options.quit then
api.close_preview(vfiler, context, view)
utils.close_preview(vfiler, context, view)
vfiler:quit()
end
end

function M.quit_force(vfiler, context, view)
api.close_preview(vfiler, context, view)
utils.close_preview(vfiler, context, view)
vfiler:quit()
end

Expand All @@ -43,7 +43,7 @@ function M.switch_to_filer(vfiler, context, view)
end

-- close preview window
api.close_preview(vfiler, context, view)
utils.close_preview(vfiler, context, view)

local linked = context.linked
-- already linked
Expand All @@ -53,7 +53,7 @@ function M.switch_to_filer(vfiler, context, view)
else
linked:open('right')
end
linked:do_action(api.open_preview)
linked:do_action(utils.open_preview)
return
end

Expand All @@ -65,24 +65,17 @@ function M.switch_to_filer(vfiler, context, view)
newfiler:start(context.root.path)
core.cursor.move(lnum)

-- redraw current
vfiler:focus()
view:draw(context)

newfiler:focus() -- return other filer
newfiler:do_action(api.open_preview)
newfiler:do_action(utils.open_preview)
end

function M.sync_with_current_filer(vfiler, context, view)
local linked = context.linked
if not (linked and linked:visible()) then
return
end

linked:focus()
linked:update(context)
linked:do_action(api.cd, context.root.path)
vfiler:focus() -- return current window
linked:do_action(utils.cd, context.root.path)
end

return M
18 changes: 9 additions & 9 deletions lua/vfiler/actions/directory.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local api = require('vfiler/actions/api')
local utils = require('vfiler/actions/utilities')
local cmdline = require('vfiler/libs/cmdline')
local core = require('vfiler/libs/core')

Expand Down Expand Up @@ -31,7 +31,7 @@ end

function M.change_to_parent(vfiler, context, view)
local current_path = context.root.path
api.cd(vfiler, context, view, context:parent_path())
utils.cd(vfiler, context, view, context:parent_path())
view:move_cursor(current_path)
end

Expand All @@ -45,7 +45,7 @@ function M.jump_to_directory(vfiler, context, view)
core.message.error('Not exists the "%s" path.', dirpath)
return
end
api.cd(vfiler, context, view, dirpath)
utils.cd(vfiler, context, view, dirpath)
end

function M.jump_to_history_directory(vfiler, context, view)
Expand All @@ -59,20 +59,20 @@ function M.jump_to_history_directory(vfiler, context, view)
initial_items = history,

on_selected = function(filer, ctx, v, path)
api.cd(filer, ctx, v, path)
utils.cd(filer, ctx, v, path)
end,
})
api.start_extension(vfiler, context, view, menu)
utils.start_extension(vfiler, context, view, menu)
end

function M.jump_to_home(vfiler, context, view)
local dirpath = vim.fn.expand('~')
api.cd(vfiler, context, view, dirpath)
utils.cd(vfiler, context, view, dirpath)
end

function M.jump_to_root(vfiler, context, view)
local dirpath = core.path.root(context.root.path)
api.cd(vfiler, context, view, dirpath)
utils.cd(vfiler, context, view, dirpath)
end

function M.close_tree(vfiler, context, view)
Expand All @@ -90,7 +90,7 @@ function M.close_tree_or_cd(vfiler, context, view)
local level = item and item.level or 0
if level == 0 or (level <= 1 and not item.opened) then
local path = context.root.path
api.cd(vfiler, context, view, context:parent_path())
utils.cd(vfiler, context, view, context:parent_path())
view:move_cursor(path)
else
M.close_tree(vfiler, context, view)
Expand Down Expand Up @@ -151,7 +151,7 @@ function M.switch_to_drive(vfiler, context, view)
v:move_cursor(focus_path)
end,
})
api.start_extension(vfiler, context, view, menu)
utils.start_extension(vfiler, context, view, menu)
end

return M
59 changes: 0 additions & 59 deletions lua/vfiler/actions/event.lua

This file was deleted.

4 changes: 2 additions & 2 deletions lua/vfiler/actions/file_operation.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local api = require('vfiler/actions/api')
local utils = require('vfiler/actions/utilities')
local buffer = require('vfiler/actions/buffer')
local clipboard = require('vfiler/clipboard')
local cmdline = require('vfiler/libs/cmdline')
Expand Down Expand Up @@ -58,7 +58,7 @@ local function rename_files(vfiler, context, view, targets)
end
end,
})
api.start_extension(vfiler, context, view, rename)
utils.start_extension(vfiler, context, view, rename)
end

local function rename_one_file(vfiler, context, view, target)
Expand Down
16 changes: 8 additions & 8 deletions lua/vfiler/actions/open.lua
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
local api = require('vfiler/actions/api')
local utils = require('vfiler/actions/utilities')

local M = {}

function M.open(vfiler, context, view)
local path = view:get_item().path
api.open_file(vfiler, context, view, path)
utils.open_file(vfiler, context, view, path)
end

function M.open_by_choose(vfiler, context, view)
local path = view:get_item().path
api.open_file(vfiler, context, view, path, 'choose')
utils.open_file(vfiler, context, view, path, 'choose')
end

function M.open_by_choose_or_cd(vfiler, context, view)
local item = view:get_item()
if item.type == 'directory' then
api.cd(vfiler, context, view, item.path)
utils.cd(vfiler, context, view, item.path)
else
api.open_file(vfiler, context, view, item.path, 'choose')
utils.open_file(vfiler, context, view, item.path, 'choose')
end
end

function M.open_by_split(vfiler, context, view)
local path = view:get_item().path
api.open_file(vfiler, context, view, path, 'bottom')
utils.open_file(vfiler, context, view, path, 'bottom')
end

function M.open_by_tabpage(vfiler, context, view)
local path = view:get_item().path
api.open_file(vfiler, context, view, path, 'tab')
utils.open_file(vfiler, context, view, path, 'tab')
end

function M.open_by_vsplit(vfiler, context, view)
local path = view:get_item().path
api.open_file(vfiler, context, view, path, 'right')
utils.open_file(vfiler, context, view, path, 'right')
end

return M
14 changes: 7 additions & 7 deletions lua/vfiler/actions/preview.lua
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
local api = require('vfiler/actions/api')
local utils = require('vfiler/actions/utilities')
local vim = require('vfiler/libs/vim')

local Preview = require('vfiler/preview')

local M = {}

function M.close_preview(vfiler, context, view)
api.close_preview(vfiler, context, view)
utils.close_preview(vfiler, context, view)
end

function M.preview_cursor_moved(vfiler, context, view)
Expand All @@ -19,9 +19,9 @@ function M.preview_cursor_moved(vfiler, context, view)
local line = vim.fn.line('.')
if preview.line ~= line then
if in_preview.once then
api.close_preview(vfiler, context, view)
utils.close_preview(vfiler, context, view)
else
api.open_preview(vfiler, context, view)
utils.open_preview(vfiler, context, view)
end
preview.line = line
end
Expand All @@ -41,20 +41,20 @@ function M.toggle_auto_preview(vfiler, context, view)
in_preview.preview = Preview.new(context.options.preview)
end
in_preview.once = false
api.open_preview(vfiler, context, view)
utils.open_preview(vfiler, context, view)
end

function M.toggle_preview(vfiler, context, view)
local in_preview = context.in_preview
if api.close_preview(vfiler, context, view) then
if utils.close_preview(vfiler, context, view) then
in_preview.preview = nil
return
end
if not in_preview.preview then
in_preview.preview = Preview.new(context.options.preview)
in_preview.once = true
end
api.open_preview(vfiler, context, view)
utils.open_preview(vfiler, context, view)
end

return M
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,7 @@ local function open_file(vfiler, context, view, path, layout)
local dest_winid = vim.fn.win_getid()
--if layout ~= 'tab' and dest_winid ~= view:winid() then
if dest_winid ~= view:winid() then
vfiler:focus()
view:redraw()
core.window.move(dest_winid)
end
end

Expand Down
4 changes: 2 additions & 2 deletions lua/vfiler/actions/view.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
local api = require('vfiler/actions/api')
local utils = require('vfiler/actions/utilities')
local sort = require('vfiler/sort')

local M = {}
Expand All @@ -20,7 +20,7 @@ function M.change_sort(vfiler, context, view)
v:move_cursor(item.path)
end,
})
api.start_extension(vfiler, context, view, menu)
utils.start_extension(vfiler, context, view, menu)
end

function M.toggle_show_hidden(vfiler, context, view)
Expand Down
Loading

0 comments on commit 61d49d6

Please sign in to comment.