nix-community / nixvim

Configure Neovim with Nix! [maintainers=@GaetanLepage, @traxys, @mattsturgeon, @khaneliman]
https://nix-community.github.io/nixvim
MIT License
1.73k stars 265 forks source link

[BUG] Can't use actions in conjunction (`+` operator) to `telescope.settings.defaults` #1431

Closed becknik closed 6 months ago

becknik commented 6 months ago
Field Description
Plugin telescope-unstable-2024-04-02
Nixpkgs unstable
Home Manager -

Description

I'm unable to add the conjunction of two functions using the + operator to the plugins.telescope.settings.defaults.mappings.[i,n]."<c-f>" attribute. See telescope documentation of how it is intended to be used.

I tried out to use the qualifier actions. before the mapped functions, but it wouldn't work for a single mapped function without the operator +. Adding actions. before both of the conjunction functions wouldn't work either...

  plugins.telescope = {
    enable = true;
    keymapsSilent = true;

    # https://youtu.be/u_OORAL_xSM?feature=shared&t=442
    extensions = {
      fzf-native.enable = true;
      ui-select.enable = true; # uses zoxide with the global above TODO doesn't work...
    };

    settings = {
      defaults = {
        file_ignore_patterns = [ "^.git/" ];

        mappings = {
          i = {
            "<c-f>" = "add_selected_to_qflist + open_qflist"; # <- THIS LINE
            # also tried the following, without success:
            /* "<c-f>" = ''
              function(prompt_bufnr)
                require('telescope.actions').add_selected_to_qflist({prompt_bufnr})
                require('telescope.actions').open_qflist({prompt_bufnr})
              end
              ''; */
          };
          n = {
            "<esc>" = "close";
            "<c-k>" = "cycle_history_next";
            "<C-j>" = "cycle_history_prev";
          };
        };
      };
    keymaps = {
      "<leader>ff" = "find_files";
      # --snip--
      };
    };
  };

When launching nvim with this set & opening telesope with e.g. <leader>ff, I'm getting the following error:

E5108: Error executing lua: ...ages/start/telescope.nvim/lua/telescope/actions/init.lua:74: Key does not exist for 'telescope.action
s': add_selected_to_qflist + open_qflist
stack traceback:
        [C]: in function 'error'
        ...ages/start/telescope.nvim/lua/telescope/actions/init.lua:74: in function '__index'
        ...Packages/start/telescope.nvim/lua/telescope/mappings.lua:270: in function 'telescope_map'
        ...Packages/start/telescope.nvim/lua/telescope/mappings.lua:345: in function 'apply_keymap'
        ...mPackages/start/telescope.nvim/lua/telescope/pickers.lua:740: in function 'find'
        ...s/start/telescope.nvim/lua/telescope/builtin/__files.lua:392: in function 'v'
        ...s/start/telescope.nvim/lua/telescope/builtin/__files.lua:641: in function 'v'
        ...ages/start/telescope.nvim/lua/telescope/builtin/init.lua:580: in function <...ages/start/telescope.nvim/lua/telescope/bui
ltin/init.lua:539>

The telescope-related part of the init.lua (with some extra formatting for readability) looks like this:

require('telescope').setup(
    {
        ["defaults"] = {
            ["file_ignore_patterns"] = { "^.git/" },
            ["mappings"] = {
                ["i"] = { ["<c-f>"] = "add_selected_to_qflist + open_qflist" },
                ["n"] = {
                    ["<C-j>"] = "cycle_history_prev",
                    ["<c-k>"] = "cycle_history_next",
                    ["<esc>"] = "close"
                }
            }
        }
    })

local __telescopeExtensions = { "ui-select", "fzf" }
for i, extension in ipairs(__telescopeExtensions) do
    require('telescope').load_extension(extension)
end
further keybindings (not in example above) ```lua do local __nixvim_binds = { { ["action"] = require('telescope.builtin').lsp_dynamic_workspace_symbols, ["key"] = "S", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = require('telescope.builtin').buffers, ["key"] = "fb", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = require('telescope.builtin').commands, ["key"] = "fc", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = require('telescope.builtin').diagnostics, ["key"] = "fd", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = require('telescope.builtin').find_files, ["key"] = "ff", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = require('telescope.builtin').live_grep, ["key"] = "fg", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = require('telescope.builtin').help_tags, ["key"] = "fh", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = require('telescope.builtin').keymaps, ["key"] = "fk", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = require('telescope.builtin').marks, ["key"] = "fm", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = require('telescope.builtin').oldfiles, ["key"] = "fo", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = require('telescope.builtin').registers, ["key"] = "fr", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = require('telescope.builtin').grep_string, ["key"] = "fs", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = require('telescope.builtin').git_branches, ["key"] = "gb", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = require('telescope.builtin').git_commits, ["key"] = "gc", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = require('telescope.builtin').lsp_document_symbols, ["key"] = "s", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = require('telescope.builtin').lsp_incoming_calls, ["key"] = "gci", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = require('telescope.builtin').lsp_outgoing_calls, ["key"] = "gco", ["mode"] = "n", ["options"] = { ["silent"] = true } }, { ["action"] = "function() require(\"treesitter-context\").go_to_context(vim.v.count1) end", ["key"] = "c", ["mode"] = "", ["options"] = { ["noremap"] = true, ["nowait"] = true, ["silent"] = true, ["unique"] = false } } } for i, map in ipairs(__nixvim_binds) do vim.keymap.set(map.mode, map.key, map.action, map.options) end end ```

Minimal, Reproducible Example (MRE)

See example provided above.

MattSturgeon commented 6 months ago

I believe you need to use helpers.mkRaw (or just set .__raw manually) when assigning lua code

becknik commented 6 months ago

Thanks a lot.

Solution is "<C-f>" = helpers.mkRaw "require('telescope.actions').add_selected_to_qflist + require('telescope.actions').open_qflist";