telescope.nvim icon indicating copy to clipboard operation
telescope.nvim copied to clipboard

Support for preprocessing of line in finders

Open psiska opened this issue 3 years ago • 5 comments

Rationale: Different command line tools outputs single line of json as result of operation (hoogle - https://github.com/ndmitchell/hoogle) or (neuron - https://github.com/srid/neuron). Currently there is no way to create multiple results from one line

Solution: If fn_preprocess function from line to {item} is provided (is not nil) then run it and call entry_maker and process_result for each item in this result. If not provided behave as before.

Example: Given the program which outputs single json line for single invocation we can use following preprocess function

local function do_preprocess(line)
  local result = {}
  local t = string.sub(line, 1, 1)
  -- Filter out non json lines
  if t ~= '[' and t ~= '{' then
    return {}
  end
  local pJson = json.decode(line)
  for _, v in ipairs(pJson) do
    table.insert(result, v)
  end
  return result
end

In this case entry maker and process result will be called for each result.

psiska avatar Mar 22 '21 08:03 psiska

Okay the linting is complaining about

  • lua/telescope/pickers/highlights.lua:46:39: (W612) line contains trailing whitespace
  • lua/telescope/builtin/files.lua:404:121: (W631) line is too long (138 > 120)

Also, it would be nice if you can expose the above snippet if it's required to process json through utils.lua.

kkharji avatar Mar 29 '21 11:03 kkharji

you could either wait for https://github.com/nvim-telescope/telescope.nvim/pull/676 to fix lint error or do the same thing I did there and I'll close that PR, either way is fine for me.

The reason I didn't merge that PR is because the docgen is kinda broken so it removes the docs and no one will like that :laughing:

elianiva avatar Mar 29 '21 11:03 elianiva

We should probably ping @oberblastmeister for this PR because of the upcoming async await changes

Conni2461 avatar Mar 31 '21 20:03 Conni2461

Yes, let's wait til after asyncify pr lands. This will change this quite a bit.

I also have to think about this a bit more as well.

tjdevries avatar Apr 04 '21 03:04 tjdevries

Currently I am updating tools I need to support the JSONL support in order not to need to land this PR. However until those changes are merged is there an option to run tools via pipe and process with jq to format suitable for telescope?

Something along the lines cmd = "tool | jq" in such case I will get one line == one entry. In other words mode which telescope is used to. If there is no such possibility I would rework the PR and rename preprocess to line_processor to better explain the purpose.

psiska avatar May 16 '21 18:05 psiska

I find this still useful but the PR has been closed unfortunately. Can we consider adding this feature or have we decided to not accept preprocessors?

wookayin avatar Sep 11 '22 06:09 wookayin