forked from vimichael/floatingtodo.nvim
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.lua
More file actions
148 lines (119 loc) · 4.03 KB
/
Copy pathbuffer.lua
File metadata and controls
148 lines (119 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
local picker = require("quickbuffer.picker")
local window = require("quickbuffer.window")
local utils = require("quickbuffer.utils")
local M = {}
local win = nil
M.buf = nil
M.keybinds = {
gotopicker = "",
closebuffer = ""
}
local function get_keybinds(opts)
local kbs = nil
if opts.keybinds then
kbs = opts.keybinds
end
if not kbs or kbs.gotopicker == "" then
M.keybinds.gotopicker = "p"
else
M.keybinds.gotopicker = kbs.gotopicker
end
if not kbs or kbs.closebuffer == "" then
M.keybinds.closebuffer = "<Esc>"
else
M.keybinds.closebuffer = kbs.closebuffer
end
end
local function set_buf_keymaps(buf, opts)
-- close buffer
vim.api.nvim_buf_set_keymap(buf, "n", M.keybinds.closebuffer, "", {
noremap = true,
silent = true,
callback = function()
if vim.api.nvim_get_option_value("modified", { buf = buf }) then
vim.cmd("w")
end
vim.api.nvim_win_close(0, true)
win = nil
if picker.buf ~= nil and vim.api.nvim_buf_is_valid(picker.buf) and picker.win ~= nil then
vim.api.nvim_win_call(picker.win, function ()
vim.api.nvim_win_close(0, true)
picker.win = nil
end)
end
end,
})
-- go to picker
vim.api.nvim_buf_set_keymap(buf, "n", M.keybinds.gotopicker, "", {
noremap = true,
silent = true,
callback = function()
picker.open(opts, function(path)
M.switchBufFile(path, opts)
end, M.buf)
end,
})
end
function M.switchBufFile(path, opts)
if vim.api.nvim_get_option_value("modified", {buf = M.buf}) then
vim.api.nvim_buf_call(M.buf, function()
vim.cmd("w")
end)
end
path = utils.Expand_path(path)
if win == nil or not vim.api.nvim_win_is_valid(win) then return end
local buf = vim.fn.bufnr(path, true)
if not vim.api.nvim_buf_is_loaded(buf) then
vim.fn.bufload(buf)
end
vim.api.nvim_win_set_buf(win, buf)
M.buf = buf
set_buf_keymaps(M.buf, opts)
-- Trigger markview refresh if it's loaded
local ok = pcall(require, "markview")
if ok then
vim.api.nvim_exec_autocmds("BufWinEnter", { buffer = buf })
end
end
function M.open_floating_file(opts)
if win ~= nil and vim.api.nvim_win_is_valid(win) then
vim.api.nvim_set_current_win(win)
return
end
local expanded_path = utils.Expand_path(opts.target_file)
-- See if we are in a project folder
local cwd = utils.Expand_path(vim.fn.getcwd())
-- We need to go through all folders just in case we encounter something like "/projects/code" at the start but actually there is "/projects/code/anotherFolder" and if we are in that second folder we want that one even though we are still technicallt in /projects/code
local best_match = nil
local best_len = 0
if opts.project_folders then
for _, project in ipairs(opts.project_folders) do
local dir = utils.Expand_path(project.path)
-- normalize (remove trailing slash)
dir = dir:gsub("/+$", "")
if cwd == dir or cwd:sub(1, #dir + 1) == dir .. "/" then
if #dir > best_len then
best_match = project
best_len = #dir
end
end
end
end
if best_match then
expanded_path = utils.Expand_path(best_match.target_file)
end
if vim.fn.filereadable(expanded_path) == 0 then
vim.notify("todo file does not exist at directory: " .. expanded_path, vim.log.levels.ERROR)
return
end
M.buf = vim.fn.bufnr(expanded_path, true)
if M.buf == -1 then
M.buf = vim.api.nvim_create_buf(false, false)
vim.api.nvim_buf_set_name(M.buf, expanded_path)
end
vim.bo[M.buf].swapfile = false
win = vim.api.nvim_open_win(M.buf, true, window.Win_config(opts.width, opts.height, opts.bufferposition, opts.border))
get_keybinds(opts)
set_buf_keymaps(M.buf, opts)
end
return M