ThePrimeagen / harpoon

MIT License
6.85k stars 368 forks source link

Error on harpoon:setup() #362

Closed grappas closed 10 months ago

grappas commented 10 months ago
Failed to run `config` for harpoon

...ppas/.local/share/nvim/lazy/harpoon/lua/harpoon/init.lua:38: attempt to index local 'self' (a nil value)

# stacktrace:
  - /harpoon/lua/harpoon/init.lua:38 _in_ **setup**
  - ~/.config/nvim/lua/plugins/harpoon.lua:8 _in_ **values**
  - ~/.config/nvim/lua/config/lazy.lua:9
  - lazynvim/init.lua:2

Using NVIM v0.10.0-dev-1684+g570367ac83 via lazy.nvim

return {
    'ThePrimeagen/harpoon',
    branch = 'harpoon2',
    dependencies = {
        'nvim-lua/plenary.nvim',
    },
    opts = function()
        require("harpoon").setup()
    end,
}
roeeyn commented 10 months ago

From the README, it says the new approach is to write harpoon:setup(). Have you tried with that?

ThePrimeagen commented 10 months ago

also, .setup will fail, it has to be :setup() (luaism for passing a reference of harpoon into the function

grappas commented 10 months ago

@roeeyn that was my initial approach. The result was the same.

grappas commented 10 months ago

Little update

return {
    'ThePrimeagen/harpoon',
    branch = 'harpoon2',
    dependencies = {
        'nvim-lua/plenary.nvim'
    },
    config = function()
        local harpoon = require("harpoon")

        harpoon:setup()

        vim.keymap.set("n", "<leader>ha", function() harpoon:list():append() end)
        vim.keymap.set("n", "<leader>hh", function() harpoon.ui:toggle_quick_menu(harpoon:list()) end)

        vim.keymap.set("n", "<leader>hi", function() harpoon:list():select(1) end)
        vim.keymap.set("n", "<leader>hj", function() harpoon:list():select(2) end)
        vim.keymap.set("n", "<leader>hk", function() harpoon:list():select(3) end)
        vim.keymap.set("n", "<leader>hl", function() harpoon:list():select(4) end)

    end,
}

Now when I tried it works. Dunno why it haven't work before... 🤔

Can you add to README.md proper Lazy config?

roeeyn commented 10 months ago

I think it may be related to how Lazy works. From its README:

image

I noticed in your first comment you used opts instead of config as your second comment. But I have not used it before, so I may be wrong

grappas commented 10 months ago

@roeeyn that could be it. Trials and errors. ¯_(ツ)_/¯

ThePrimeagen commented 10 months ago

@grappas

to be clear, this code has a lua error in it

return {
    'ThePrimeagen/harpoon',
    branch = 'harpoon2',
    dependencies = {
        'nvim-lua/plenary.nvim',
    },
    opts = function()
        require("harpoon").setup()
    end,
}

specifically the line

        require("harpoon").setup()

it should be

        require("harpoon"):setup()

require("harpoon"):setup() is equivalent to require("harpoon").setup(require("harpoon"))

wookayin commented 10 months ago

BTW why :setup() instead of .setup()? It's an uncommon convention and I don't see any reason why one has to use the "OOP sugar" method.

ThePrimeagen commented 10 months ago

@wookayin this is poor comms

fork is you don't like it

wookayin commented 10 months ago

No offense, sorry if that didn't sound nice. I was just asking out of curiosity. Would you mind clarifying on why it's required? for autocmds (what README briefly says)?

grappas commented 10 months ago

@wookayin this is poor comms

fork is you don't like it

Ok. Lets talk conventions: why local? I can't bind it with which-key.nvim because of that.

jesseleite commented 9 months ago

For those using lazy.nvim plugin manager and want to utilize its lazy loading conventions by defining a keys object to trigger the loading of harpoon, you still totally can 🤘

Just make sure you local harpoon outside of your config func, and store to that in your config func, and then your keys will be able to access the same harpoon object 🤌

For example, in my lua/jl/plugins/harpoon.lua, I'm doing this...

local harpoon -- Ensure we only interact with local harpoon object 🦈

return {
  'ThePrimeagen/harpoon',
  branch = 'harpoon2', -- Temp; Will be merged soonish 🔥
  dependencies = {
    'nvim-lua/plenary.nvim',
  },
  keys = {
    { '<Leader>gh', function () harpoon.ui:toggle_quick_menu(harpoon:list()) end },
    { '<Leader>ga', function () harpoon:list():append() end },
    { '<Leader>gj', function () harpoon:list():select(1) end },
    { '<Leader>gk', function () harpoon:list():select(2) end },
    { '<Leader>gl', function () harpoon:list():select(3) end },
    { '<Leader>g;', function () harpoon:list():select(4) end },
  },
  config = function ()
    harpoon = require("harpoon"):setup() -- Notice we're storing to parent context 👆
  end,
}

I like the .setup() convention lazy.nvim expects too, but honestly this work just fine!

PS. Thank you @ThePrimeagen for your rad work BTW™️ 😎

ThePrimeagen commented 9 months ago

I've also made .setup(...) work (though it's a bit of a lie and makes you think it's not as state capturing as it is)

jesseleite commented 9 months ago

@ThePrimeagen Oh I totally understood why you went this direction though! Really cool to be able to configure separate list objects. Rad work on harpoon2 dude 🔥