{ ... }:
{
  programs.nixvim = {
    plugins = {
      cmp-cmdline.enable = true;
      cmp-git.enable = true;
      cmp-npm.enable = true;
      cmp-path.enable = true;
      cmp-rg.enable = true;
      cmp-tmux.enable = true;
      cmp-treesitter.enable = true;
      cmp-zsh.enable = true;
      cmp_yanky.enable = true;
      copilot-cmp = {
        enable = true;
        settings = {
          event = ["InsertEnter" "LspAttach"]; # When to trigger Copilot
          fix_pairs = true;  # Fix parentheses/brackets completion
        };
      };

      cmp = {
        enable = true;
        autoEnableSources = true;
        settings = {
          enabled.__raw = ''
            function()
              local filetype = vim.api.nvim_buf_get_option(0, "filetype")
                if filetype == "TelescopePrompt" then
                  return false
                end
              return true
            end
          '';

          sources = [
            { name = "nvim_lua"; }
            { name = "nvim_lsp"; }
            { name = "emoji"; }
            {
              name = "buffer"; # text within current buffer
              option.get_bufnrs.__raw = "vim.api.nvim_list_bufs";
              keywordLength = 3;
            }
            { name = "copilot"; }
            {
              name = "path"; # file system paths
              keywordLength = 3;
            }
            {
              name = "luasnip"; # snippets
              keywordLength = 3;
            }
            { name = "cmdline"; }
          ];

          formatting = {
            fields = [ "kind" "abbr" "menu" ];
            format = ''
              function(entry, vim_item)
                local kind_icons = {
                  Text = "󰊄",
                  Method = "",
                  Function = "󰡱",
                  Constructor = "",
                  Field = "",
                  Variable = "󱀍",
                  Class = "",
                  Interface = "",
                  Module = "󰕳",
                  Property = "",
                  Unit = "",
                  Value = "",
                  Enum = "",
                  Keyword = "",
                  Snippet = "",
                  Color = "",
                  File = "",
                  Reference = "",
                  Folder = "",
                  EnumMember = "",
                  Constant = "",
                  Struct = "",
                  Event = "",
                  Operator = "",
                  TypeParameter = "",
                }
                vim_item.kind = string.format("%s", kind_icons[vim_item.kind])
                vim_item.menu = ({
                  path = "[Path]",
                  nvim_lua = "[NVIM_LUA]",
                  nvim_lsp = "[LSP]",
                  luasnip = "[Snippet]",
                  buffer = "[Buffer]",
                })[entry.source.name]
                return vim_item
              end
            '';
          };

          completion = {
            completeopt = "menuone,noselect";
          };

          autoEnableSources = true;

          experimental = { ghost_text = true; };

          performance = {
            debounce = 60;
            fetchingTimeout = 200;
            maxViewEntries = 30;
          };

          snippet = {
            expand = ''
              function(args)
                require('luasnip').lsp_expand(args.body)
              end
            '';
          };

          window = {
            completion = { border = "solid"; };
            documentation = { border = "solid"; };
          };

          mapping = {
            "<C-j>" = "cmp.mapping.select_next_item()";
            "<C-n>" = "cmp.mapping.select_next_item()";
            "<C-k>" = "cmp.mapping.select_prev_item()";
            "<C-p>" = "cmp.mapping.select_prev_item()";
            "<C-e>" = "cmp.mapping.abort()";
            "<C-b>" = "cmp.mapping.scroll_docs(-4)";
            "<C-f>" = "cmp.mapping.scroll_docs(4)";
            "<C-c>" = "cmp.mapping.complete()";
            "<Tab>" = "cmp.mapping.confirm({ select = false })";
            # "<Tab>" = ''
            #   cmp.mapping(function(fallback)
            #     -- local context = require("cmp.config.context")
            #     -- local is_comment = context.in_treesitter_capture("comment") == true or context.in_syntax_group("Comment")
            #
            #     local col = vim.fn.col('.') - 1
            #     local line = vim.fn.getline('.')
            #     local char_under_cursor = string.sub(line, col, col)
            #
            #     if col == 0 or string.match(char_under_cursor, '%s') then
            #       fallback()
            #     elseif cmp.visible() then
            #       cmp.confirm({ select = true })
            #     else
            #       fallback()
            #     end
            #   end, { "i", "s" })
            # '';
            "<S-Tab>" = "cmp.mapping.confirm({ behavior = cmp.ConfirmBehavior.Replace, select = true })";
            "<C-l>" = ''
              cmp.mapping(function()
                if luasnip.expand_or_locally_jumpable() then
                  luasnip.expand_or_jump()
                end
              end, { 'i', 's' })
            '';
            "<C-h>" = ''
              cmp.mapping(function()
                if luasnip.locally_jumpable(-1) then
                  luasnip.jump(-1)
                end
              end, { 'i', 's' })
            '';
          };
        };
      };
    };
  };
}