Skip to content

Commit

Permalink
Add support for hlsearch variable
Browse files Browse the repository at this point in the history
  • Loading branch information
jphalip authored and AlexPl292 committed Dec 6, 2024
1 parent 2e550a0 commit 4962baa
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 1 deletion.
13 changes: 13 additions & 0 deletions src/main/java/com/maddyhome/idea/vim/newapi/IjVimSearchGroup.kt
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,19 @@ open class IjVimSearchGroup : VimSearchGroupBase(), PersistentStateComponent<Ele
showSearchHighlight = injector.globalOptions().hlsearch
}

override fun isSomeTextHighlighted(): Boolean {
val vimEditors = injector.editorGroup.getEditors().filter {
(injector.application.isUnitTest() || it.ij.component.isShowing)
}
for (vimEditor in vimEditors) {
val editor = vimEditor.ij
if (editor.vimLastHighlighters != null) {
return true
}
}
return false
}

override fun setShouldShowSearchHighlights() {
showSearchHighlight = injector.globalOptions().hlsearch
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/*
* Copyright 2003-2024 The IdeaVim authors
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE.txt file or at
* https://opensource.org/licenses/MIT.
*/

package org.jetbrains.plugins.ideavim.ex.implementation.variables

import org.jetbrains.plugins.ideavim.VimTestCase
import org.junit.jupiter.api.Test

class HighLightVariableTest : VimTestCase() {

@Test
fun `test v hlsearch is initially false`() {
configureByText(
"""
|one two one
|${c}three four three
|five six five
""".trimMargin()
)
enterCommand("set hlsearch")
enterCommand("echo v:hlsearch")
assertExOutput("0")
}

@Test
fun `test v hlsearch is true after successful search`() {
configureByText(
"""
|one two one
|${c}three four three
|five six five
""".trimMargin()
)
enterCommand("set hlsearch")
enterSearch("one")
enterCommand("echo v:hlsearch")
assertExOutput("1")
}

@Test
fun `test v hlsearch is false after search with no matches`() {
configureByText(
"""
|one two one
|${c}three four three
|five six five
""".trimMargin()
)
enterCommand("set hlsearch")
enterSearch("xyz")
enterCommand("echo v:hlsearch")
assertExOutput("0")
}

@Test
fun `test v hlsearch is false after nohlsearch command`() {
configureByText(
"""
|one two one
|${c}three four three
|five six five
""".trimMargin()
)
enterCommand("set hlsearch")
enterSearch("one")
enterCommand("nohlsearch")
enterCommand("echo v:hlsearch")
assertExOutput("0")
}

@Test
fun `test v hlsearch is true after n command reuses previous search`() {
configureByText(
"""
|one two one
|${c}three four three
|five six five
""".trimMargin()
)
enterCommand("set hlsearch")
enterSearch("one")
enterCommand("nohlsearch")
typeText("n")
enterCommand("echo v:hlsearch")
assertExOutput("1")
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -195,4 +195,9 @@ interface VimSearchGroup {
* Gets the direction lastly used in a search.
*/
fun getLastSearchDirection(): Direction

/**
* Returns true if any text is selected in the visible editors, false otherwise.
*/
fun isSomeTextHighlighted(): Boolean
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2003-2024 The IdeaVim authors
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE.txt file or at
* https://opensource.org/licenses/MIT.
*/

package com.maddyhome.idea.vim.vimscript.model.variables

import com.maddyhome.idea.vim.api.ExecutionContext
import com.maddyhome.idea.vim.api.VimEditor
import com.maddyhome.idea.vim.api.injector
import com.maddyhome.idea.vim.vimscript.model.VimLContext
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimDataType
import com.maddyhome.idea.vim.vimscript.model.datatypes.asVimInt

class HighLightVariable : Variable {

override fun evaluate(
name: String,
editor: VimEditor,
context: ExecutionContext,
vimContext: VimLContext,
): VimDataType {
return injector.searchGroup.isSomeTextHighlighted().asVimInt()
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,11 @@ import com.maddyhome.idea.vim.vimscript.model.ExecutableContext
import com.maddyhome.idea.vim.vimscript.model.VimLContext
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimDataType
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimInt
import com.maddyhome.idea.vim.vimscript.model.datatypes.VimString
import com.maddyhome.idea.vim.vimscript.model.expressions.Scope
import com.maddyhome.idea.vim.vimscript.model.expressions.Variable
import com.maddyhome.idea.vim.vimscript.model.statements.FunctionDeclaration
import com.maddyhome.idea.vim.vimscript.model.statements.FunctionFlag
import com.maddyhome.idea.vim.vimscript.model.variables.HighLightVariable
import com.maddyhome.idea.vim.vimscript.model.variables.RegisterVariable

abstract class VimVariableServiceBase : VariableService {
Expand Down Expand Up @@ -181,6 +181,9 @@ abstract class VimVariableServiceBase : VariableService {
"count" -> VimInt(KeyHandler.getInstance().keyHandlerState.commandBuilder.calculateCount0Snapshot())
"count1" -> VimInt(KeyHandler.getInstance().keyHandlerState.commandBuilder.calculateCount0Snapshot().coerceAtLeast(1))
"searchforward" -> VimInt(if (injector.searchGroup.getLastSearchDirection() == Direction.FORWARDS) 1 else 0)
"hlsearch" -> {
HighLightVariable().evaluate(name, editor, context, vimContext)
}
"register" -> {
RegisterVariable().evaluate(name, editor, context, vimContext)
}
Expand Down

0 comments on commit 4962baa

Please sign in to comment.