Skip to content

Commit

Permalink
Remove the use of RegExp from ExEntryPanel
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexPl292 committed Jun 28, 2024
1 parent fb30e4e commit 736cb21
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 33 deletions.
6 changes: 2 additions & 4 deletions src/main/java/com/maddyhome/idea/vim/ui/ex/ExEntryPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,8 @@ protected void textChanged(@NotNull DocumentEvent e) {

if (labelText.equals("/") || labelText.equals("?") || searchCommand) {
final boolean forwards = !labelText.equals("?"); // :s, :g, :v are treated as forwards
final String pattern;
final CharPointer p = new CharPointer(searchText);
final CharPointer end = RegExp.skip_regexp(new CharPointer(searchText), separator, true);
pattern = p.substring(end.pointer() - p.pointer());
int pattenEnd = injector.getSearchGroup().findEndOfPattern(searchText, separator, 0);
final String pattern = searchText.substring(0, pattenEnd);

VimPlugin.getEditor().closeEditorSearchSession(editor);
final int matchOffset =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,17 @@ public interface VimSearchGroup {
*/
public fun searchWord(editor: VimEditor, caret: ImmutableVimCaret, count: Int, whole: Boolean, dir: Direction): Int

/**
* If [command] contains a pattern, this function finds the end of it that is marked with [delimiter].
*
* This is useful for commands like `:%s/123/321/s` to detect the end of `123` pattern. `/` will be a [delimiter].
*/
public fun findEndOfPattern(
command: String,
delimiter: Char,
startIndex: Int = 0
): Int

/**
* Parse and execute the substitute command
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,36 @@ public abstract class VimSearchGroupBase : VimSearchGroup {
return if (offset == -1) range.startOffset else offset
}

public override fun findEndOfPattern(
command: String,
delimiter: Char,
startIndex: Int,
): Int {
var magic = true

var i = startIndex
while (i < command.length) {
// delimiter found
if (command[i] == delimiter) break

// collection start found, ignore until end of collection
if (magic && command[i] == '[' ||
!magic && command[i] == '\\' && i + 1 < command.length && command[i + 1] == '[') {

i = findEndOfCollection(command, i)
// skip escaped char
} else if (command[i] == '\\' && i + 1 < command.length) {
i++
// update magic
if (command[i] == 'v' || command[i] == 'm') magic = true
if (command[i] == 'V' || command[i] == 'M') magic = false
}
i++
}
return i
}


private fun findNextSearchForGn(
editor: VimEditor,
count: Int,
Expand Down Expand Up @@ -526,35 +556,6 @@ public abstract class VimSearchGroupBase : VimSearchGroup {
return offset
}

private fun findEndOfPattern(
command: String,
delimiter: Char,
startIndex: Int = 0
): Int {
var magic = true

var i = startIndex
while (i < command.length) {
// delimiter found
if (command[i] == delimiter) break

// collection start found, ignore until end of collection
if (magic && command[i] == '[' ||
!magic && command[i] == '\\' && i + 1 < command.length && command[i + 1] == '[') {

i = findEndOfCollection(command, i)
// skip escaped char
} else if (command[i] == '\\' && i + 1 < command.length) {
i++
// update magic
if (command[i] == 'v' || command[i] == 'm') magic = true
if (command[i] == 'V' || command[i] == 'M') magic = false
}
i++
}
return i
}

private fun findEndOfCollection(
command: String,
startIndex: Int
Expand Down

0 comments on commit 736cb21

Please sign in to comment.