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

Replace regex within visual-box \%V is not working #848

Open
kenmizrahi opened this issue Feb 23, 2022 · 2 comments
Open

Replace regex within visual-box \%V is not working #848

kenmizrahi opened this issue Feb 23, 2022 · 2 comments

Comments

@kenmizrahi
Copy link

As I tried to use that feature, only first line in the box was properly replaced only within the box.
In all the other lines a-l-l the instances of the word-to-replace were changed (not only in the box)

 %V to search within visually-selected area only
regex used for example: s/%Vphy/dhy/g - both with g and without g did not work as expected from gvim-like behavior.

@keforbes
Copy link
Contributor

keforbes commented Mar 19, 2022

I think I see the issue. We use the presence of \%V to tell us to use the selection bounds for the replace. But we have to remove the \%V characters before performing the replace since they aren't part of the actual regex (we use Ecilpse's regex engine, which isn't aware of Vim special cases).

if(find.contains("\\%V")) { //select only within visual area (not lines)
find = find.replaceAll("\\\\%V", "");
Selection selection = editorAdaptor.getSelection();
if(selection != null) {
start = selection.getLeftBound().getModelOffset();
end = selection.getRightBound().getModelOffset();
}
}
//let Eclipse handle the regex
SearchAndReplaceService searchAndReplace = editorAdaptor.getSearchAndReplaceService();
return searchAndReplace.replace(start, end, find, replace, flags);

Then, since we have a multi-line replace, we loop through the other lines in the selection, re-using the regex provided. Unfortunately, at that point we've already removed the \%V characters from the command so subsequent lines don't know to stay within the bounds of the visual-block and treat the operation like a regular multi-line selection.

//perform search individually on each line in the range
//(so :%s without 'g' flag runs once on each line)
editorAdaptor.getHistory().beginCompoundChange();
for(int i=range.getStartLine(); i <= endLine; i++) {
line = model.getLineInformation(i);
lineChanges = performReplace(line, subDef.find, subDef.replace, subDef.flags, editorAdaptor);

So the solution would be to remove the \%V from the string sent to the Eclipse API but continue using the un-modified string when looping through the other lines. Basically, we shouldn't be re-using the find variable here:

@keforbes
Copy link
Contributor

I should be able to fix this sometime this weekend. Sorry it took me a month before I found the time to look at 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

No branches or pull requests

2 participants