Skip to content

Commit

Permalink
Merge branch 'ThePrimeagen:harpoon2' into harpoon2
Browse files Browse the repository at this point in the history
  • Loading branch information
NStefan002 authored Apr 17, 2024
2 parents c65a723 + 0378a6c commit 3f0b17c
Show file tree
Hide file tree
Showing 15 changed files with 767 additions and 172 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ test:
nvim --headless --noplugin -u scripts/tests/minimal.vim \
-c "PlenaryBustedDirectory lua/harpoon/test/ {minimal_init = 'scripts/tests/minimal.vim'}"

clean:
clean:
echo "===> Cleaning"
rm /tmp/lua_*

Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ local harpoon = require("harpoon")
harpoon:setup()
-- REQUIRED

vim.keymap.set("n", "<leader>a", function() harpoon:list():append() end)
vim.keymap.set("n", "<leader>a", function() harpoon:list():add() end)
vim.keymap.set("n", "<C-e>", function() harpoon.ui:toggle_quick_menu(harpoon:list()) end)

vim.keymap.set("n", "<C-h>", function() harpoon:list():select(1) end)
Expand All @@ -89,7 +89,7 @@ vim.keymap.set("n", "<C-S-N>", function() harpoon:list():next() end)

### Telescope

In order to use [Telescope](https://github.com/nvim-telescope/telescope.nvim) as a UI,
In order to use [Telescope](https://github.com/nvim-telescope/telescope.nvim) as a UI,
make sure to add `telescope` to your dependencies and paste this following snippet into your configuration.

```lua
Expand Down Expand Up @@ -137,7 +137,7 @@ harpoon:setup({
-- Setting up custom behavior for a list named "cmd"
"cmd" = {

-- When you call list:append() this function is called and the return
-- When you call list:add() this function is called and the return
-- value will be put in the list at the end.
--
-- which means same behavior for prepend except where in the list the
Expand Down Expand Up @@ -204,7 +204,7 @@ There is quite a bit of behavior you can configure via `harpoon:setup()`
* `display`: how to display the list item in the ui menu
* `select`: the action taken when selecting a list item. called from `list:select(idx, options)`
* `equals`: how to compare two list items for equality
* `create_list_item`: called when `list:append()` or `list:prepend()` is called. called with an item, which will be a string, when adding through the ui menu
* `create_list_item`: called when `list:add()` or `list:prepend()` is called. called with an item, which will be a string, when adding through the ui menu
* `BufLeave`: this function is called for every list on BufLeave. if you need custom behavior, this is the place
* `VimLeavePre`: this function is called for every list on VimLeavePre.
* `get_root_dir`: used for creating relative paths. defaults to `vim.loop.cwd()`
Expand Down Expand Up @@ -287,7 +287,7 @@ contribute start with an issue and I am totally willing for PRs, but I will be
very conservative on what I take. I don't want Harpoon _solving_ specific
issues, I want it to create the proper hooks to solve any problem

**Running Tests**
**Running Tests**
To run the tests make sure [plenary](https://github.com/nvim-lua/plenary.nvim) is checked out in the parent directory of *this* repository, then run `make test`.

## ⇁ Social
Expand Down
6 changes: 2 additions & 4 deletions lua/harpoon/buffer.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
local utils = require("harpoon.utils")
local HarpoonGroup = require("harpoon.autocmd")

local M = {}
Expand All @@ -16,6 +15,7 @@ local function get_harpoon_menu_name()
end

function M.run_select_command()
---@type Harpoon
local harpoon = require("harpoon")
harpoon.logger:log("select by keymap '<CR>'")
harpoon.ui:select_menu_item()
Expand Down Expand Up @@ -108,9 +108,7 @@ function M.get_contents(bufnr)
local indices = {}

for _, line in pairs(lines) do
if not utils.is_white_space(line) then
table.insert(indices, line)
end
table.insert(indices, line)
end

return indices
Expand Down
77 changes: 65 additions & 12 deletions lua/harpoon/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ local Path = require("plenary.path")
local function normalize_path(buf_name, root)
return Path:new(buf_name):make_relative(root)
end
local function to_exact_name(value)
return "^" .. value .. "$"
end

local M = {}
local DEFAULT_LIST = "__harpoon_files"
Expand Down Expand Up @@ -57,6 +60,7 @@ function M.get_default_config()
settings = {
save_on_toggle = false,
sync_on_ui_close = false,

key = function()
return vim.loop.cwd()
end,
Expand Down Expand Up @@ -96,16 +100,18 @@ function M.get_default_config()
list.name,
options
)
options = options or {}
if list_item == nil then
return
end

local bufnr = vim.fn.bufnr(list_item.value)
options = options or {}

local bufnr = vim.fn.bufnr(to_exact_name(list_item.value))
local set_position = false
if bufnr == -1 then
if bufnr == -1 then -- must create a buffer!
set_position = true
bufnr = vim.fn.bufnr(list_item.value, true)
-- bufnr = vim.fn.bufnr(list_item.value, true)
bufnr = vim.fn.bufadd(list_item.value)
end
if not vim.api.nvim_buf_is_loaded(bufnr) then
vim.fn.bufload(bufnr)
Expand All @@ -125,10 +131,37 @@ function M.get_default_config()
vim.api.nvim_set_current_buf(bufnr)

if set_position then
local lines = vim.api.nvim_buf_line_count(bufnr)

local edited = false
if list_item.context.row > lines then
list_item.context.row = lines
edited = true
end

local row = list_item.context.row
local row_text =
vim.api.nvim_buf_get_lines(0, row - 1, row, false)
local col = #row_text[1]

if list_item.context.col > col then
list_item.context.col = col
edited = true
end

vim.api.nvim_win_set_cursor(0, {
list_item.context.row or 1,
list_item.context.col or 0,
})

if edited then
Extensions.extensions:emit(
Extensions.event_names.POSITION_UPDATED,
{
list_item = list_item,
}
)
end
end

Extensions.extensions:emit(Extensions.event_names.NAVIGATE, {
Expand All @@ -139,6 +172,12 @@ function M.get_default_config()
---@param list_item_a HarpoonListItem
---@param list_item_b HarpoonListItem
equals = function(list_item_a, list_item_b)
if list_item_a == nil and list_item_b == nil then
return true
elseif list_item_a == nil or list_item_b == nil then
return false
end

return list_item_a.value == list_item_b.value
end,

Expand All @@ -151,11 +190,6 @@ function M.get_default_config()
---@return HarpoonListItem
create_list_item = function(config, name)
name = name
-- TODO: should we do path normalization???
-- i know i have seen sometimes it becoming an absolute
-- path, if that is the case we can use the context to
-- store the bufname and then have value be the normalized
-- value
or normalize_path(
vim.api.nvim_buf_get_name(
vim.api.nvim_get_current_buf()
Expand All @@ -181,10 +215,15 @@ function M.get_default_config()
}
end,

---@param arg {buf: number}
---@param list HarpoonList
BufLeave = function(arg, list)
local bufnr = arg.buf
local bufname = vim.api.nvim_buf_get_name(bufnr)
local item = list:get_by_display(bufname)
local bufname = normalize_path(
vim.api.nvim_buf_get_name(bufnr),
list.config.get_root_dir()
)
local item = list:get_by_value(bufname)

if item then
local pos = vim.api.nvim_win_get_cursor(0)
Expand All @@ -200,6 +239,11 @@ function M.get_default_config()

item.context.row = pos[1]
item.context.col = pos[2]

Extensions.extensions:emit(
Extensions.event_names.POSITION_UPDATED,
item
)
end
end,

Expand All @@ -208,7 +252,7 @@ function M.get_default_config()
}
end

---@param partial_config HarpoonPartialConfig
---@param partial_config HarpoonPartialConfig?
---@param latest_config HarpoonConfig?
---@return HarpoonConfig
function M.merge_config(partial_config, latest_config)
Expand All @@ -226,4 +270,13 @@ function M.merge_config(partial_config, latest_config)
return config
end

---@param settings HarpoonPartialSettings
function M.create_config(settings)
local config = M.get_default_config()
for k, v in ipairs(settings) do
config.settings[k] = v
end
return config
end

return M
Loading

0 comments on commit 3f0b17c

Please sign in to comment.