Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable fuzzy matching for some sources #631

Open
rajaravivarma-r opened this issue May 8, 2024 · 0 comments
Open

Disable fuzzy matching for some sources #631

rajaravivarma-r opened this issue May 8, 2024 · 0 comments

Comments

@rajaravivarma-r
Copy link
Contributor

Thanks for this amazing plugin. Autocompletion was never this hackable and easy.

I'm writing a custom resource, which I'm filter using fzf. Though, I can see (from logs) that fzf is giving me some results, I don't see them in the autocomplete menu.

So can we avoid further filtering from Coq, for some custom resources, may be based on a flag or something?

The custom source,

local fzf = require('lib/fzf_helpers')
local log = require('lib/log')
local coq_3p_utils = require('coq_3p/utils')

log.outfile = "/tmp/coq_nvim_lua.log"
log.level = "trace"

-- `COQsources` is a global registry of sources
COQsources = COQsources or {}

COQsources["dd10e3499-7dde-4781-ab52-a7e894da7f7e"] = {
  name = "TagsFileUsingFzf", -- this is displayed to the client
  fn = function(args, callback)
    local _row, col = unpack(args.pos)
    local line = args.line
    local current_word = coq_3p_utils.cword(line, col)
    local items = {}

    log.info("TagsFileUsingFzf: Received word: " .. current_word)
    -- label      :: display label
    -- insertText :: string | null, default to `label` if null
    -- kind       :: int ∈ `vim.lsp.protocol.CompletionItemKind`
    -- detail     :: doc popup

    local file_path = "tags"
    local file = io.open(file_path, "r")

    -- Make sure the file exists
    if file then
      file:close()

      result = fzf.filter_from_file({file_path=file_path, query=current_word})
      log.info("TagsFileUsingFzf: Matches: " .. table.concat(result, "\n"))
      log.info("\n\n\n\n\n")

      for i, line in ipairs(result) do
        local tag_name, file_path, short_def, tag_type, parent_class

        for first_part, path, definition, type_info, class_info in string.gmatch(line, "(%S+)%s+(%S+)%s+(%S+)%s+(%S+)%s+(%S+)") do
            tag_name = first_part
            file_path = path
            short_def = definition:gsub("/^  ", ""):gsub("$/","")
            tag_type = type_info:gsub(";", "")
            parent_class = class_info:gsub("class:", "")
        end

        -- Capture the required parts using gmatch

        -- print(tag_name)         -- prints: <<
        -- print(file_path)    -- prints: app/migration/utils/logger.rb
        -- print(short_def)    -- prints: def <<(msg)
        -- print(tag_type)         -- prints: f
        -- print(parent_class)    -- prints: Utils.Logger

        -- Get the first word and the remaining words
        local detail = tag_name .. " [" .. tag_type .. "] " .. "\n" .. file_path .. "\n" .. short_def .. "\n" .. parent_class
        local item = {
          label = tag_name .. " " .. " [" .. tag_type .. "] " .. " " .. parent_class,
          insertText = tag_name, -- defaults to label if null
          kind = vim.lsp.protocol.CompletionItemKind.Text,
          detail = detail
        }
        table.insert(items, item)
      end
    end

    callback {
      isIncomplete = true,
      items = items
    }
  end,
  resolve = function()
  end,
  exec = function(...)
  end
}

fzf_helpers.lua

local fzf = {}

-- Accepts a table
-- { command = <command whose output will be piped to fzf>,
--   query = <used in the --filter flag of fzf> }
function fzf.filter(args)
  local command = args.command or "find . -type f"
  local query = args.query

  local handle = io.popen(command .. " | fzf --filter='" .. query .."'")
  local results = {}
  for line in handle:lines() do
    table.insert(results, line)
  end
  handle:close()
  return results
end


-- Accepts a table
-- { file_path = <file whose content will be piped to fzf>,
--   query = <used in the --filter flag of fzf> }
function fzf.filter_from_file(args)
  local file_path = args.file_path
  local query = args.query

  local handle = io.popen("fzf --filter='" .. query .."' < " .. file_path)
  local results = {}
  for line in handle:lines() do
    table.insert(results, line)
  end
  handle:close()
  return results
end
return fzf

log.lua

--
-- log.lua
--
-- Copyright (c) 2016 rxi
--
-- This library is free software; you can redistribute it and/or modify it
-- under the terms of the MIT license. See LICENSE for details.
--

local log = { _version = "0.1.0" }

log.usecolor = true
log.outfile = nil
log.level = "trace"


local modes = {
  { name = "trace", color = "\27[34m", },
  { name = "debug", color = "\27[36m", },
  { name = "info",  color = "\27[32m", },
  { name = "warn",  color = "\27[33m", },
  { name = "error", color = "\27[31m", },
  { name = "fatal", color = "\27[35m", },
}


local levels = {}
for i, v in ipairs(modes) do
  levels[v.name] = i
end


local round = function(x, increment)
  increment = increment or 1
  x = x / increment
  return (x > 0 and math.floor(x + .5) or math.ceil(x - .5)) * increment
end


local _tostring = tostring

local tostring = function(...)
  local t = {}
  for i = 1, select('#', ...) do
    local x = select(i, ...)
    if type(x) == "number" then
      x = round(x, .01)
    end
    t[#t + 1] = _tostring(x)
  end
  return table.concat(t, " ")
end


for i, x in ipairs(modes) do
  local nameupper = x.name:upper()
  log[x.name] = function(...)

    -- Return early if we're below the log level
    if i < levels[log.level] then
      return
    end

    local msg = tostring(...)
    local info = debug.getinfo(2, "Sl")
    local lineinfo = info.short_src .. ":" .. info.currentline

    -- Output to log file
    if log.outfile then
      local fp = io.open(log.outfile, "a")
      local str = string.format("[%-6s%s] %s: %s\n",
      nameupper, os.date(), lineinfo, msg)
      fp:write(str)
      fp:close()
    else
      -- Output to console
      print(string.format("%s[%-6s%s]%s %s: %s",
      log.usecolor and x.color or "",
      nameupper,
      os.date("%H:%M:%S"),
      log.usecolor and "\27[0m" or "",
      lineinfo,
      msg))

    end

  end
end


return log
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant