<commit_before>-- Custom nvim-cmp source for GitHub handles.

local handles = {}

local registered = false

handles.setup = function()
  if registered then
    return
  end
  registered = true

  local has_cmp, cmp = pcall(require, 'cmp')

  if not has_cmp then
    return
  end

  local config = vim.fn.expand('~/.github-handles.json')
  if vim.fn.filereadable(config) == 0 then
    return
  end
  local addresses = vim.fn.json_decode(vim.fn.readfile(config))

  local source = {}

  source.new = function()
    return setmetatable({}, {__index = source})
  end

  source.get_trigger_characters = function()
    return { '@' }
  end

  source.complete = function(self, request, callback)
    local input = string.sub(request.context.cursor_before_line, request.offset - 1)
    local prefix = string.sub(request.context.cursor_before_line, 1, request.offset - 1)

    if vim.startswith(input, '@') and (prefix == '@' or vim.endswith(prefix, ' @')) then
      local items = {}
      for handle, address in pairs(addresses) do
        table.insert(items, {
            label = address,
            textEdit = {
              newText = address,
              range = {
                start = {
                  line = request.context.cursor.row - 1,
                  character = request.context.cursor.col - 1 - #input,
                },
                ['end'] = {
                  line = request.context.cursor.row - 1,
                  character = request.context.cursor.col - 1,
                },
              },
            },
          }
        )
      end
      callback {
        items = items,
        isIncomplete = true,
      }
    else
      callback({isIncomplete = true})
    end
  end

  cmp.register_source('handles', source.new())

  cmp.setup.filetype('gitcommit', {
    sources = cmp.config.sources({
      { name = 'luasnip' },
      { name = 'buffer' },
      { name = 'calc' },
      { name = 'emoji' },
      { name = 'path' },

      -- My custom sources.
      { name = 'handles' }, -- GitHub handles; eg. @wincent → <NAME> <<EMAIL>>
    }),
  })
end

return handles
<commit_msg>fix(nvim): include handle text in handle completion autocomplete<commit_after>-- Custom nvim-cmp source for GitHub handles.

local handles = {}

local registered = false

handles.setup = function()
  if registered then
    return
  end
  registered = true

  local has_cmp, cmp = pcall(require, 'cmp')

  if not has_cmp then
    return
  end

  local config = vim.fn.expand('~/.github-handles.json')
  if vim.fn.filereadable(config) == 0 then
    return
  end
  local addresses = vim.fn.json_decode(vim.fn.readfile(config))

  local source = {}

  source.new = function()
    return setmetatable({}, {__index = source})
  end

  source.get_trigger_characters = function()
    return { '@' }
  end

  source.complete = function(self, request, callback)
    local input = string.sub(request.context.cursor_before_line, request.offset - 1)
    local prefix = string.sub(request.context.cursor_before_line, 1, request.offset - 1)

    if vim.startswith(input, '@') and (prefix == '@' or vim.endswith(prefix, ' @')) then
      local items = {}
      for handle, address in pairs(addresses) do
        table.insert(items, {
            filterText = handle .. ' ' .. address,
            label = address,
            textEdit = {
              newText = address,
              range = {
                start = {
                  line = request.context.cursor.row - 1,
                  character = request.context.cursor.col - 1 - #input,
                },
                ['end'] = {
                  line = request.context.cursor.row - 1,
                  character = request.context.cursor.col - 1,
                },
              },
            },
          }
        )
      end
      callback {
        items = items,
        isIncomplete = true,
      }
    else
      callback({isIncomplete = true})
    end
  end

  cmp.register_source('handles', source.new())

  cmp.setup.filetype('gitcommit', {
    sources = cmp.config.sources({
      { name = 'luasnip' },
      { name = 'buffer' },
      { name = 'calc' },
      { name = 'emoji' },
      { name = 'path' },

      -- My custom sources.
      { name = 'handles' }, -- GitHub handles; eg. @wincent → <NAME> <<EMAIL>>
    }),
  })
end

return handles

{'fraction_non_alphanumeric': 0.12742830712303424, 'fraction_numerical': 0.0037002775208140612, 'mean_word_length': 1.8871829105473965, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3225218', 'n_tokens_mistral': 1348, 'n_tokens_neox': 1274, 'n_words': 310}