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

Add ripgrep support #16

Merged
merged 1 commit into from
May 15, 2021
Merged

Add ripgrep support #16

merged 1 commit into from
May 15, 2021

Conversation

timfjord
Copy link
Contributor

This PR adds support for ripgrep search tool

I haven't covered the ag edge case that is mentioned in the README but I am happy to do that if I have more details

@AndrewRadev AndrewRadev merged commit 594055d into AndrewRadev:master May 15, 2021
@AndrewRadev
Copy link
Owner

Thanks, this looks good! I would certainly appreciate help with ag, if I could only remember what the problem was :D. It might even be fixed already. If you'd like to help, maybe you could just enable it and try it out -- it might work fine now. I would keep an eye out on matches at the ends of files, because that's what past me has written down at least :). Really wish I'd left more info for my future self.

@AndrewRadev
Copy link
Owner

Trying to get ag to work, I'm running into trouble that's apparently quite recent. I'm not sure if the old issue is fixed, but this is a new one -- mixed delimiters.

Here's one result coming from ack:

bundle/csv/csv.vmb-2926-
bundle/csv/csv.vmb-2927-To have Vim automatically detect csv files, you need to do the following.
bundle/csv/csv.vmb-2928-
bundle/csv/csv.vmb:2929:   1) Create your user runtime directory if you do not have one yet. This
bundle/csv/csv.vmb:2930:      directory needs to be in your 'runtime' path. In Unix this would
bundle/csv/csv.vmb-2931-      typically the ~/.vim directory, while in Windows this is usually your
bundle/csv/csv.vmb-2932-      ~/vimfiles directory. Use :echo expand("~") to find out, what Vim thinks
bundle/csv/csv.vmb-2933-      your user directory is.
--

Now here's one coming from ag:

bundle/csv/csv.vmb:2926-
bundle/csv/csv.vmb:2927-To have Vim automatically detect csv files, you need to do the following.
bundle/csv/csv.vmb:2928-
bundle/csv/csv.vmb:2929:   1) Create your user runtime directory if you do not have one yet. This
bundle/csv/csv.vmb:2930:      directory needs to be in your 'runtime' path. In Unix this would
bundle/csv/csv.vmb:2931-      typically the ~/.vim directory, while in Windows this is usually your
bundle/csv/csv.vmb:2932-      ~/vimfiles directory. Use :echo expand("~") to find out, what Vim thinks
bundle/csv/csv.vmb:2933-      your user directory is.
--

They're very similar, but there's a subtle difference -- the lines that match (as opposed to being "context" lines from the -C flag) have mixed delimiters -- both : and -, like this: bundle/csv/csv.vmb:2927-To have....

The reason this is a problem is because the plugin tries to figure out what the delimiter is so it could validate whether the "filename" and "line number" that are parsed are actually valid filename + line number. This is to work around the issue of the filename having numbers and dashes in it (#15). It's an annoying problem and one that makes me think I need a separate backend setup for rg --json, but it's going to be a relatively large undertaking, maybe.

Anyway, I guess what I'd have to do is replace the split( with a series of matchstr calls and construct a data structure that holds both the pieces and the delimiters between them. The code is around here:

for delimiter in ['-', ':']
let parts = split(line, delimiter)
if line[len(line) - 1] == delimiter
" the line ended in a delimiter, so the split will end up one character
" short
call add(parts, '')
endif
if len(parts) < 3
" we expect [filename, line_number, text], so less than 3 parts means
" this is not a line we can parse, go on to the next delimiter
continue
endif
let delimiter_index = 0
while delimiter_index < len(parts) - 2
let filename = join(parts[0:delimiter_index], delimiter)
let line_number = parts[delimiter_index + 1]
if line_number !~ '^\d\+$'
" not a real number, so this isn't the right breakdown
let delimiter_index += 1
continue
endif
let text = join(parts[delimiter_index + 2:], delimiter)
if !filereadable(filename)
" not an existing file, not the right breakdown
let delimiter_index += 1
continue
endif
if !has_key(self.cache, filename)
let file_lines = readfile(filename)
let self.cache[filename] = {'lines': file_lines}
endif
let file_lines = self.cache[filename].lines
if len(file_lines) < line_number
" the file exists, but the lines don't match up
let delimiter_index += 1
continue
endif
if file_lines[line_number - 1] != text
" the file exists, the line exists, but the text doesn't match
let delimiter_index += 1
continue
endif
return [filename, line_number]
endwhile
endfor

If you'd like to try your hand with it, I'd certainly appreciate it! I think it will be kind of annoying to do, though. I'll write it down as a todo for myself for later, but let me know what you think.

@timfjord timfjord deleted the rg branch May 17, 2021 09:27
@timfjord
Copy link
Contributor Author

timfjord commented May 19, 2021

Hm, I was more worried if the ag case is also affecting rg 🙂

It is a bit tricky to dive into this as I am not 100% in it
But what I am definitely going to do is to keep an eye on how the rg integration performs(as I use this plugin as the main tool for serach)
Just a note here, I've added --vimgrep flag to the rg_command to make it works, but it is also available in ag so it might be a good idea to add it to the ag_command too (as far as I understand for ag it is a shortcut for --nogroup --nocolor --column) Most likely it won't fix the above issue but just something to keep in mind

So If I find something strange with the plugin I will let you know

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 this pull request may close these issues.

2 participants