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

Fix incsearch with result before caret and wrapscan #923

Merged
merged 1 commit into from
Jun 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -210,12 +210,28 @@ private fun findClosestMatch(
return -1
}

val sortedResults = results.sortedBy { it.startOffset }.let { if (!forwards) it.reversed() else it }
val nextIndex = sortedResults.indexOfFirst {
if (forwards) it.startOffset > initialOffset else it.startOffset < initialOffset
val sortedResults = if (forwards) {
results.sortedBy { it.startOffset }
} else {
results.sortedByDescending { it.startOffset }
}
val toDrop = (nextIndex + count - 1).let { if (injector.globalOptions().wrapscan) it % results.size else it }
return sortedResults.drop(toDrop).firstOrNull()?.startOffset ?: -1
val closestIndex = if (forwards) {
sortedResults.indexOfFirst { it.startOffset > initialOffset }
}
else {
sortedResults.indexOfFirst { it.startOffset < initialOffset }
}

if (closestIndex == -1 && !injector.globalOptions().wrapscan) {
return -1
}

val nextIndex = closestIndex.coerceAtLeast(0) + (count - 1)
if (nextIndex >= sortedResults.size && !injector.globalOptions().wrapscan) {
return -1
}

return sortedResults[nextIndex % results.size].startOffset
}

internal fun highlightSearchResults(editor: Editor, pattern: String, results: List<TextRange>, currentMatchOffset: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1063,6 +1063,38 @@ class SearchGroupTest : VimTestCase() {
assertPosition(1, 14)
}

@Test
fun `test incsearch + hlsearch at bottom of file with wrapscan`() {
// Make sure the caret wraps during incsearch
configureByText(
"""
|Lorem ipsum dolor sit amet,
|consectetur adipiscing elit
|Sed in orci ${c}mauris.
|Cras id tellus in ex imperdiet egestas.
""".trimMargin()
)
enterCommand("set incsearch hlsearch wrapscan")
typeText("/it")
assertPosition(0, 19)
}

@Test
fun `test incsearch + hlsearch at bottom of file with nowrapscan`() {
// This will highlight the occurrences above/before the caret, but should not move the caret
configureByText(
"""
|Lorem ipsum dolor sit amet,
|consectetur adipiscing elit
|Sed in orci ${c}mauris.
|Cras id tellus in ex imperdiet egestas.
""".trimMargin()
)
enterCommand("set incsearch hlsearch nowrapscan")
typeText("/it")
assertPosition(2, 12)
}

@TestWithoutNeovim(SkipNeovimReason.OPTION)
@Test
fun `test incsearch moves caret to start of first match (backwards)`() {
Expand Down
Loading