Skip to content

Commit

Permalink
Revert changes to SearchGroup
Browse files Browse the repository at this point in the history
Wrong branch. The changes should be merged to master only after review in the Fleet branch
  • Loading branch information
lippfi committed Mar 3, 2024
1 parent 00cbf18 commit 00ccddf
Show file tree
Hide file tree
Showing 17 changed files with 156 additions and 853 deletions.
61 changes: 0 additions & 61 deletions src/main/java/com/maddyhome/idea/vim/group/IjVimPsiService.kt

This file was deleted.

2 changes: 1 addition & 1 deletion src/main/java/com/maddyhome/idea/vim/group/MotionGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ internal class MotionGroup : VimMotionGroupBase() {
VimPlugin.getFile().selectEditor(project, file)

override fun moveCaretToMatchingPair(editor: VimEditor, caret: ImmutableVimCaret): Motion {
return findMatchingPairOnCurrentLine(editor, caret)?.toMotionOrError() ?: Motion.Error
return SearchHelper.findMatchingPairOnCurrentLine(editor.ij, caret.ij).toMotionOrError()
}

override fun moveCaretToFirstDisplayLine(
Expand Down
107 changes: 107 additions & 0 deletions src/main/java/com/maddyhome/idea/vim/helper/SearchHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,113 @@ public static int findUnmatchedBlock(@NotNull Editor editor, @NotNull Caret care
return new TextRange(bstart, bend + 1);
}

private static int findMatchingBlockCommentPair(@NotNull PsiComment comment,
int pos,
@Nullable String prefix,
@Nullable String suffix) {
if (prefix != null && suffix != null) {
// TODO: Try to get rid of `getText()` because it takes a lot of time to calculate the string
final String commentText = comment.getText();
if (commentText.startsWith(prefix) && commentText.endsWith(suffix)) {
final int endOffset = comment.getTextOffset() + comment.getTextLength();
if (pos < comment.getTextOffset() + prefix.length()) {
return endOffset;
}
else if (pos >= endOffset - suffix.length()) {
return comment.getTextOffset();
}
}
}
return -1;
}

private static int findMatchingBlockCommentPair(@NotNull PsiElement element, int pos) {
final Language language = element.getLanguage();
final Commenter commenter = LanguageCommenters.INSTANCE.forLanguage(language);
final PsiComment comment = PsiTreeUtil.getParentOfType(element, PsiComment.class, false);
if (comment != null) {
final int ret = findMatchingBlockCommentPair(comment, pos, commenter.getBlockCommentPrefix(),
commenter.getBlockCommentSuffix());
if (ret >= 0) {
return ret;
}
if (commenter instanceof CodeDocumentationAwareCommenter docCommenter) {
return findMatchingBlockCommentPair(comment, pos, docCommenter.getDocumentationCommentPrefix(),
docCommenter.getDocumentationCommentSuffix());
}
}
return -1;
}

/**
* This looks on the current line, starting at the cursor position for one of {, }, (, ), [, or ]. It then searches
* forward or backward, as appropriate for the associated match pair. String in double quotes are skipped over.
* Single characters in single quotes are skipped too.
*
* @param editor The editor to search in
* @return The offset within the editor of the found character or -1 if no match was found or none of the characters
* were found on the remainder of the current line.
*/
public static int findMatchingPairOnCurrentLine(@NotNull Editor editor, @NotNull Caret caret) {
int pos = caret.getOffset();

final int commentPos = findMatchingComment(editor, pos);
if (commentPos >= 0) {
return commentPos;
}

int line = caret.getLogicalPosition().line;
final IjVimEditor vimEditor = new IjVimEditor(editor);
int end = EngineEditorHelperKt.getLineEndOffset(vimEditor, line, true);

// To handle the case where visual mode allows the user to go past the end of the line,
// which will prevent loc from finding a pairable character below
if (pos > 0 && pos == end) {
pos = end - 1;
}

final String pairChars = parseMatchPairsOption(vimEditor);

CharSequence chars = editor.getDocument().getCharsSequence();
int loc = -1;
// Search the remainder of the current line for one of the candidate characters
while (pos < end) {
loc = pairChars.indexOf(chars.charAt(pos));
if (loc >= 0) {
break;
}

pos++;
}

int res = -1;
// If we found one ...
if (loc >= 0) {
// What direction should we go now (-1 is backward, 1 is forward)
Direction dir = loc % 2 == 0 ? Direction.FORWARDS : Direction.BACKWARDS;
// Which character did we find and which should we now search for
char found = pairChars.charAt(loc);
char match = pairChars.charAt(loc + dir.toInt());
res = findBlockLocation(chars, found, match, dir, pos, 1, true);
}

return res;
}

/**
* If on the start/end of a block comment, jump to the matching of that comment, or vice versa.
*/
private static int findMatchingComment(@NotNull Editor editor, int pos) {
final PsiFile psiFile = PsiHelper.getFile(editor);
if (psiFile != null) {
final PsiElement element = psiFile.findElementAt(pos);
if (element != null) {
return findMatchingBlockCommentPair(element, pos);
}
}
return -1;
}

private static int findBlockLocation(@NotNull CharSequence chars,
char found,
char match,
Expand Down
3 changes: 0 additions & 3 deletions src/main/java/com/maddyhome/idea/vim/newapi/IjVimEditor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,3 @@ public val Editor.vim: VimEditor
get() = IjVimEditor(this)
public val VimEditor.ij: Editor
get() = (this as IjVimEditor).editor

public val com.intellij.openapi.util.TextRange.vim: TextRange
get() = TextRange(this.startOffset, this.endOffset)
4 changes: 0 additions & 4 deletions src/main/java/com/maddyhome/idea/vim/newapi/IjVimInjector.kt
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import com.maddyhome.idea.vim.api.VimMessages
import com.maddyhome.idea.vim.api.VimMotionGroup
import com.maddyhome.idea.vim.api.VimOptionGroup
import com.maddyhome.idea.vim.api.VimProcessGroup
import com.maddyhome.idea.vim.api.VimPsiService
import com.maddyhome.idea.vim.api.VimRegexpService
import com.maddyhome.idea.vim.api.VimScrollGroup
import com.maddyhome.idea.vim.api.VimSearchGroup
Expand All @@ -66,7 +65,6 @@ import com.maddyhome.idea.vim.group.FileGroup
import com.maddyhome.idea.vim.group.GlobalIjOptions
import com.maddyhome.idea.vim.group.HistoryGroup
import com.maddyhome.idea.vim.group.IjVimOptionGroup
import com.maddyhome.idea.vim.group.IjVimPsiService
import com.maddyhome.idea.vim.group.MacroGroup
import com.maddyhome.idea.vim.group.MotionGroup
import com.maddyhome.idea.vim.group.SearchGroup
Expand Down Expand Up @@ -149,8 +147,6 @@ internal class IjVimInjector : VimInjectorBase() {
get() = service<MacroGroup>()
override val undo: VimUndoRedo
get() = service<UndoRedoHelper>()
override val psiService: VimPsiService
get() = service<IjVimPsiService>()
override val commandLineHelper: VimCommandLineHelper
get() = service<CommandLineHelper>()
override val nativeActionManager: NativeActionManager
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/com/maddyhome/idea/vim/newapi/IjVimSearchHelper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,22 @@ internal class IjVimSearchHelper : VimSearchHelperBase() {
return PsiHelper.findMethodStart(editor.ij, caret.ij.offset, count)
}

override fun findUnmatchedBlock(editor: VimEditor, caret: ImmutableVimCaret, type: Char, count: Int): Int {
val chars: CharSequence = editor.ij.document.charsSequence
var pos: Int = caret.ij.offset
val loc = BLOCK_CHARS.indexOf(type)
// What direction should we go now (-1 is backward, 1 is forward)
val dir = if (loc % 2 == 0) Direction.BACKWARDS else Direction.FORWARDS
// Which character did we find and which should we now search for
val match = BLOCK_CHARS[loc]
val found = BLOCK_CHARS[loc - dir.toInt()]

if (pos < chars.length && chars[pos] == type) {
pos += dir.toInt()
}
return findBlockLocation(chars, found, match, dir, pos, count)
}

private fun findBlockLocation(
chars: CharSequence,
found: Char,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,15 @@ class MotionUnmatchedBraceOpenActionTest : VimTestCase() {
)
}

@VimBehaviorDiffers(
originalVimAfter = """
class Xxx $c{
int main() {
}
}
""",
)
@Test
fun `test go to next next bracket with great count`() {
doTest(
Expand All @@ -110,9 +119,9 @@ class MotionUnmatchedBraceOpenActionTest : VimTestCase() {
}
""".trimIndent(),
"""
class Xxx $c{
class Xxx {
int main() {
$c
}
}
""".trimIndent(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,10 @@

package org.jetbrains.plugins.ideavim.action.motion.updown

import com.intellij.idea.TestFor
import com.maddyhome.idea.vim.state.mode.Mode
import org.jetbrains.plugins.ideavim.SkipNeovimReason
import org.jetbrains.plugins.ideavim.TestWithoutNeovim
import org.jetbrains.plugins.ideavim.VimTestCase
import org.junit.jupiter.api.Disabled
import org.junit.jupiter.api.Test

/**
Expand Down Expand Up @@ -145,7 +143,6 @@ class MotionPercentOrMatchActionTest : VimTestCase() {
}

@Test
@Disabled("It will work after implementing all of the methods in VimPsiService")
fun `test motion outside text`() {
doTest(
"%",
Expand Down Expand Up @@ -210,45 +207,41 @@ class MotionPercentOrMatchActionTest : VimTestCase() {
}

@Test
@TestWithoutNeovim(SkipNeovimReason.BUG_IN_NEOVIM)
fun `test motion in text with escape (outer forward)`() {
doTest(
"%",
""" debugPrint$c(\(var)) """,
""" debugPrint(\(var$c)) """,
""" debugPrint(\(var)$c) """,
Mode.NORMAL(),
)
}

@Test
@TestWithoutNeovim(SkipNeovimReason.BUG_IN_NEOVIM)
fun `test motion in text with escape (outer backward)`() {
doTest(
"%",
""" debugPrint(\(var)$c) """,
""" debugPrint(\(var)$c) """,
""" debugPrint$c(\(var)) """,
Mode.NORMAL(),
)
}

@Test
@TestWithoutNeovim(SkipNeovimReason.BUG_IN_NEOVIM)
fun `test motion in text with escape (inner forward)`() {
doTest(
"%",
""" debugPrint(\$c(var)) """,
""" debugPrint(\$c(var)) """,
""" debugPrint(\(var$c)) """,
Mode.NORMAL(),
)
}

@Test
@TestWithoutNeovim(SkipNeovimReason.BUG_IN_NEOVIM)
fun `test motion in text with escape (inner backward)`() {
doTest(
"%",
""" debugPrint(\$c(var)) """,
""" debugPrint(\$c(var)) """,
""" debugPrint(\(var$c)) """,
Mode.NORMAL(),
)
}
Expand Down Expand Up @@ -339,28 +332,4 @@ class MotionPercentOrMatchActionTest : VimTestCase() {
)
assertOffset(10)
}

@Test
@TestFor(issues = ["VIM-3294"])
fun `test matching with braces inside of string`() {
configureByText("""
$c("("")")
""".trimIndent())
typeText("%")
assertState("""
("("")"$c)
""".trimIndent())
}

@Test
@TestFor(issues = ["VIM-3294"])
fun `test matching with braces inside of string 2`() {
configureByText("""
("("")"$c)
""".trimIndent())
typeText("%")
assertState("""
$c("("")")
""".trimIndent())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -245,9 +245,6 @@ enum class SkipNeovimReason {

GUARDED_BLOCKS,
CTRL_CODES,

BUG_IN_NEOVIM,
PSI,
}

fun LogicalPosition.toVimCoords(): VimCoords {
Expand Down
Loading

0 comments on commit 00ccddf

Please sign in to comment.