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

Feature Request: Accept a custom prompt_parser #53

Open
zcag opened this issue Mar 31, 2023 · 2 comments · May be fixed by #49
Open

Feature Request: Accept a custom prompt_parser #53

zcag opened this issue Mar 31, 2023 · 2 comments · May be fixed by #49

Comments

@zcag
Copy link

zcag commented Mar 31, 2023

I would like to have the ability to provide a custom prompt_parser to further customize this plugin. It'll be extra nice if the parser can return some extra arguments to be passed on to vimgrep_args

Example Use case:

I want to just type what i want into the field and not think about quotes and globs and break the flow when I'm in the middle of debugging something.

Here's my test implementation of prompt_parser. Probably buggy and misses some corner cases but I used it on a whole day of work and it seemed fine.

I'll add the ability to escape / and starting - in the future.
Maybe we can have this kind of 'quoteless mode' as a feature on the plugin.

-- somewhere_in_my_config.lua
-- **
opts.prompt_parser = require('plugins.my-custom-prompt-parser')
require('telescope').extensions.live_grep_args.live_grep_args(opts)

-- plugins/my-custom-prompt-parser.lua
local M = {}

M.parse = function(prompt, _)
  local parts = ''
  local args = {}
  local path = nil

  path_index = prompt:find("/")

  if path_index then
    path_start_index = prompt:sub(0, path_index):find('%s[%S]*$')
    path_end_index = (prompt:find('%s', path_start_index+1) or 0)-1
    path = prompt:sub(path_start_index+1, path_end_index)
    if path:sub(-1) == '*' then path = path .. '/' end
    if path:sub(1, 1) == '/' then path = path:sub(2, -1)  end

    new_prompt = prompt:sub(0, path_start_index-1)
    if path_end_index ~= -1 then new_prompt = new_prompt .. prompt:sub(path_end_index+1, -1) end

    prompt = new_prompt
    table.insert(args, '--glob='.. path .. '**')
  end

  arg_index = prompt:find("%s%-")
  if arg_index then
    table.insert(args, prompt:sub(arg_index+1, -1))
    parts = prompt:sub(1, arg_index-1)
  else
    parts = prompt
  end

  return { args }, { parts }
end

return M

Demo:

demo.mp4

Example implementation:

I'm not sure if it's a good idea to do it like this, but just wanted to test the functionality, here's the PR: #52

@zcag zcag changed the title Feature Request: Take a custom prompt_parser Feature Request: Accept a custom prompt_parser Mar 31, 2023
@zcag
Copy link
Author

zcag commented Apr 3, 2023

Closing since we have a pr for this already #49

@zcag zcag closed this as completed Apr 3, 2023
@weeman1337
Copy link
Collaborator

Reopening, as it has not yet been merged

@weeman1337 weeman1337 reopened this Aug 28, 2023
@weeman1337 weeman1337 linked a pull request Aug 28, 2023 that will close this issue
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

Successfully merging a pull request may close this issue.

2 participants