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

Emulate matchit.vim #343

Merged
merged 1 commit into from
Oct 7, 2021
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
7 changes: 7 additions & 0 deletions resources/META-INF/includes/VimExtensions.xml
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,13 @@
<alias name="vim-indent-object"/>
</aliases>
</vimExtension>

<vimExtension implementation="com.maddyhome.idea.vim.extension.matchit.Matchit" name="matchit">
<aliases>
<alias name="vim-matchit"/>
<alias name="chrisbra/matchit"/>
</aliases>
</vimExtension>
</extensions>

<!-- IdeaVim extensions-->
Expand Down
4 changes: 4 additions & 0 deletions src/com/maddyhome/idea/vim/command/CommandBuilder.kt
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ class CommandBuilder(private var currentCommandPartNode: CommandPartNode<ActionB
prevExpectedArgumentType = null
}

fun resetCount() {
count = 0
}

fun resetInProgressCommandPart(commandPartNode: CommandPartNode<ActionBeanClass>) {
count = 0
setCurrentCommandPartNode(commandPartNode)
Expand Down
579 changes: 579 additions & 0 deletions src/com/maddyhome/idea/vim/extension/matchit/Matchit.kt

Large diffs are not rendered by default.

43 changes: 43 additions & 0 deletions test/org/jetbrains/plugins/ideavim/VimTestCase.kt
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,13 @@ abstract class VimTestCase : UsefulTestCase() {
return myFixture.editor
}

private fun configureByText(fileName: String, content: String): Editor {
@Suppress("IdeaVimAssertState")
myFixture.configureByText(fileName, content)
setEditorVisibleSize(screenWidth, screenHeight)
return myFixture.editor
}

protected fun configureByFileName(fileName: String): Editor {
@Suppress("IdeaVimAssertState")
myFixture.configureByText(fileName, "\n")
Expand Down Expand Up @@ -461,6 +468,42 @@ abstract class VimTestCase : UsefulTestCase() {
NeovimTesting.assertState(myFixture.editor, this)
}

fun doTest(
keys: String,
before: String,
after: String,
modeAfter: CommandState.Mode,
subModeAfter: SubMode,
fileType: FileType
) {
configureByText(fileType, before)

NeovimTesting.setupEditor(myFixture.editor, this)
NeovimTesting.typeCommand(keys, this)

performTest(keys, after, modeAfter, subModeAfter)

NeovimTesting.assertState(myFixture.editor, this)
}

fun doTest(
keys: String,
before: String,
after: String,
modeAfter: CommandState.Mode,
subModeAfter: SubMode,
fileName: String
) {
configureByText(fileName, before)

NeovimTesting.setupEditor(myFixture.editor, this)
NeovimTesting.typeCommand(keys, this)

performTest(keys, after, modeAfter, subModeAfter)

NeovimTesting.assertState(myFixture.editor, this)
}

private fun performTest(keys: String, after: String, modeAfter: CommandState.Mode, subModeAfter: SubMode) {
typeText(parseKeys(keys))
@Suppress("IdeaVimAssertState")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,267 @@
/*
* IdeaVim - Vim emulator for IDEs based on the IntelliJ platform
* Copyright (C) 2003-2020 The IdeaVim authors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

package org.jetbrains.plugins.ideavim.extension.matchit

import com.intellij.ide.highlighter.HtmlFileType
import com.intellij.ide.highlighter.JavaFileType
import com.maddyhome.idea.vim.command.CommandState
import org.jetbrains.plugins.ideavim.SkipNeovimReason
import org.jetbrains.plugins.ideavim.TestWithoutNeovim
import org.jetbrains.plugins.ideavim.VimTestCase

class MatchitGeneralTest : VimTestCase() {
@Throws(Exception::class)
override fun setUp() {
super.setUp()
enableExtensions("matchit")
}

/*
* Tests to make sure we didn't break the default % motion
*/

@TestWithoutNeovim(reason = SkipNeovimReason.PLUGIN)
fun `test jump from Java comment start to end`() {
doTest("%",
"""
/${c}**
*
*/
""".trimIndent(),
"""
/**
*
*${c}/
""".trimIndent(), CommandState.Mode.COMMAND, CommandState.SubMode.NONE, JavaFileType.INSTANCE
)
}

@TestWithoutNeovim(reason = SkipNeovimReason.PLUGIN)
fun `test jump from Java comment end to start`() {
doTest("%",
"""
/**
*
*${c}/
""".trimIndent(),
"""
${c}/**
*
*/
""".trimIndent(), CommandState.Mode.COMMAND, CommandState.SubMode.NONE, JavaFileType.INSTANCE
)
}

@TestWithoutNeovim(reason = SkipNeovimReason.PLUGIN)
fun `test 25 percent jump`() {
doTest("25%",
"""
int a;
int b;
in${c}t c;
int d;
""".trimIndent(),
"""
${c}int a;
int b;
int c;
int d;
""".trimIndent(), CommandState.Mode.COMMAND, CommandState.SubMode.NONE, HtmlFileType.INSTANCE
)
}

fun `test jump from visual end of line to opening parenthesis`() {
doTest("v$%",
"""foo(${c}bar)""",
"""foo${s}${c}(b${se}ar)""",
CommandState.Mode.VISUAL, CommandState.SubMode.VISUAL_CHARACTER, HtmlFileType.INSTANCE
)
}

fun `test jump from visual end of line to opening parenthesis then back to closing`() {
doTest("v$%%",
"""foo(${c}bar)""",
"""foo(${s}bar${c})${se}""",
CommandState.Mode.VISUAL, CommandState.SubMode.VISUAL_CHARACTER, HtmlFileType.INSTANCE
)
}

@TestWithoutNeovim(reason = SkipNeovimReason.PLUGIN)
fun `test delete everything from opening parenthesis to closing parenthesis`() {
doTest("d%",
"${c}(x == 123)", "", CommandState.Mode.COMMAND, CommandState.SubMode.NONE, HtmlFileType.INSTANCE
)
}

@TestWithoutNeovim(reason = SkipNeovimReason.PLUGIN)
fun `test delete everything from closing parenthesis to opening parenthesis`() {
doTest("d%",
"(x == 123${c})", "", CommandState.Mode.COMMAND, CommandState.SubMode.NONE, HtmlFileType.INSTANCE
)
}

@TestWithoutNeovim(reason = SkipNeovimReason.PLUGIN)
fun `test delete everything from opening curly brace to closing curly brace`() {
doTest("d%",
"${c}{ foo: 123 }", "", CommandState.Mode.COMMAND, CommandState.SubMode.NONE, HtmlFileType.INSTANCE
)
}

@TestWithoutNeovim(reason = SkipNeovimReason.PLUGIN)
fun `test delete everything from closing curly brace to opening curly brace`() {
doTest("d%",
"{ foo: 123 ${c}}", "", CommandState.Mode.COMMAND, CommandState.SubMode.NONE, HtmlFileType.INSTANCE
)
}

@TestWithoutNeovim(reason = SkipNeovimReason.PLUGIN)
fun `test delete everything from opening square bracket to closing square bracket`() {
doTest("d%",
"${c}[1, 2, 3]", "", CommandState.Mode.COMMAND, CommandState.SubMode.NONE, HtmlFileType.INSTANCE
)
}

@TestWithoutNeovim(reason = SkipNeovimReason.PLUGIN)
fun `test delete everything from closing square bracket to opening square bracket`() {
doTest("d%",
"[1, 2, 3${c}]", "", CommandState.Mode.COMMAND, CommandState.SubMode.NONE, HtmlFileType.INSTANCE
)
}

/*
* Tests for visual mode and deleting on the new Matchit patterns.
*/

fun `test jump from visual end of line to opening angle bracket`() {
doTest("v$%",
"""</h${c}tml>""",
"""${s}${c}</ht${se}ml>""",
CommandState.Mode.VISUAL, CommandState.SubMode.VISUAL_CHARACTER, HtmlFileType.INSTANCE
)
}

fun `test jump from visual end of line to start of for loop`() {
doTest("v$%",
"""
for n in [1, 2, 3]
puts n
e${c}nd
""".trimIndent(),
"""
${s}${c}for n in [1, 2, 3]
puts n
en${se}d
""".trimIndent(),
CommandState.Mode.VISUAL, CommandState.SubMode.VISUAL_CHARACTER, "ruby.rb"
)
}

@TestWithoutNeovim(reason = SkipNeovimReason.PLUGIN)
fun `test delete from elseif to else`() {
doTest("d%",
"""
if x == 0
puts "Zero"
${c}elsif x < -1
puts "Negative"
else
puts "Positive"
end
""".trimIndent(),
"""
if x == 0
puts "Zero"
$c
puts "Positive"
end
""".trimIndent(), CommandState.Mode.COMMAND, CommandState.SubMode.NONE, "ruby.rb"
)
}

@TestWithoutNeovim(reason = SkipNeovimReason.PLUGIN)
fun `test delete from opening to closing div`() {
doTest("d%",
"""
<${c}div>
<img src="fff">
</div>
""".trimIndent(),
"${c}<", CommandState.Mode.COMMAND, CommandState.SubMode.NONE, HtmlFileType.INSTANCE
)
}

@TestWithoutNeovim(reason = SkipNeovimReason.PLUGIN)
fun `test delete from opening angle bracket to closing angle bracket`() {
doTest("d%",
"""
${c}<div></div>
""".trimIndent(),
"${c}</div>", CommandState.Mode.COMMAND, CommandState.SubMode.NONE, HtmlFileType.INSTANCE
)
}

@TestWithoutNeovim(reason = SkipNeovimReason.PLUGIN)
fun `test delete whole function from def`() {
doTest("d%",
"""
${c}def function
puts "hello"
end
""".trimIndent(),
"", CommandState.Mode.COMMAND, CommandState.SubMode.NONE, "ruby.rb"
)
}

@TestWithoutNeovim(reason = SkipNeovimReason.PLUGIN)
fun `test delete whole function from def with reverse motion`() {
doTest("dg%",
"""
${c}def function
puts "hello"
end
""".trimIndent(),
"", CommandState.Mode.COMMAND, CommandState.SubMode.NONE, "ruby.rb"
)
}

@TestWithoutNeovim(reason = SkipNeovimReason.PLUGIN)
fun `test delete whole function from end`() {
doTest("d%",
"""
def function
puts "hello"
en${c}d
""".trimIndent(),
"", CommandState.Mode.COMMAND, CommandState.SubMode.NONE, "ruby.rb"
)
}

@TestWithoutNeovim(reason = SkipNeovimReason.PLUGIN)
fun `test delete whole function from end with reverse motion`() {
doTest("dg%",
"""
def function
puts "hello"
en${c}d
""".trimIndent(),
"", CommandState.Mode.COMMAND, CommandState.SubMode.NONE, "ruby.rb"
)
}

}
Loading