IDEA-146675 Permute reverse selection

GitOrigin-RevId: 4208981b342550bc4db6b801ba8d91aba1322f32
This commit is contained in:
filipp
2022-09-27 14:27:16 +00:00
committed by intellij-monorepo-bot
parent 4f81b923c6
commit e86a7e7cbb
15 changed files with 154 additions and 0 deletions
@@ -0,0 +1,4 @@
<body>
<selection>Lorem ipsum dolor sit amet<caret></selection>
<p><caret></p>
</body>
@@ -0,0 +1,4 @@
<body>
<p>Lorem ipsum dolor sit amet</p>
</body>
@@ -0,0 +1 @@
{<selection><caret>"age": 25</selection>, <selection><caret>"name": "John"</selection>, <selection><caret>"surname": "Smith"</selection>}
@@ -0,0 +1 @@
{"name": "John", "surname": "Smith", "age": 25}
@@ -0,0 +1 @@
{<selection><caret>"surname": "Smith"</selection>, <selection><caret>"age": 25</selection>, <selection><caret>"name": "John"</selection>}
@@ -0,0 +1 @@
{"name": "John", "surname": "Smith", "age": 25}
@@ -0,0 +1,11 @@
{
"days" : [
<selection>"Saturday"<caret></selection>,
<selection>"Sunday"<caret></selection>,
<selection>"Monday"<caret></selection>,
<selection>"Tuesday"<caret></selection>,
<selection>"Wednesday"<caret></selection>,
<selection>"Thursday"<caret></selection>,
<selection>"Friday"<caret></selection>
]
}
@@ -0,0 +1,11 @@
{
"days" : [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
]
}
@@ -0,0 +1,11 @@
{
"days" : [
<selection>"Monday"<caret></selection>,
<selection>"Tuesday"<caret></selection>,
<selection>"Wednesday"<caret></selection>,
<selection>"Thursday"<caret></selection>,
<selection>"Friday"<caret></selection>,
<selection>"Saturday"<caret></selection>,
<selection>"Sunday"<caret></selection>
]
}
@@ -0,0 +1,11 @@
{
"days" : [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
]
}
@@ -0,0 +1,9 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.editorActions.rotateSelection
import com.intellij.openapi.editor.actionSystem.EditorAction
class RotateSelectionsForwardAction : EditorAction(RotateSelectionsHandler(false))
class RotateSelectionsBackwardsAction : EditorAction(RotateSelectionsHandler(true))
@@ -0,0 +1,38 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.editorActions.rotateSelection
import com.intellij.openapi.actionSystem.DataContext
import com.intellij.openapi.editor.Caret
import com.intellij.openapi.editor.Editor
import com.intellij.openapi.editor.actionSystem.EditorWriteActionHandler
class RotateSelectionsHandler(private val isBackwards: Boolean) : EditorWriteActionHandler() {
override fun executeWriteAction(editor: Editor, caret: Caret?, dataContext: DataContext?) {
if (editor.caretModel.caretCount <= 1) return
val carets = editor.caretModel.allCarets
.sortedByDescending { it.selectionRange.endOffset }
val textToReplace = carets
.map { editor.document.getText(it.selectionRange) }
.rotateElements(!isBackwards) // isBackwards is inversed because carets are in the reverse order (sorted by descending offset)
assert(carets.size == textToReplace.size)
for ((i, caret) in carets.withIndex()) {
val selection = caret.selectionRange
val newText = textToReplace[i]
editor.document.replaceString(selection.startOffset, selection.endOffset, newText)
caret.setSelection(selection.startOffset, selection.startOffset + newText.length)
caret.moveToOffset(caret.selectionEnd)
}
}
private fun <T> List<T>.rotateElements(isBackwards: Boolean): List<T> {
if (this.size <= 1) return this.toList()
return if (isBackwards) {
this.subList(1, this.size) + this[0]
} else {
listOf(this[this.size - 1]) + this.subList(0, this.size - 1)
}
}
}
@@ -0,0 +1,42 @@
// Copyright 2000-2022 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.codeInsight.editorActions
import com.intellij.codeInsight.editorActions.rotateSelection.RotateSelectionsBackwardsAction
import com.intellij.codeInsight.editorActions.rotateSelection.RotateSelectionsForwardAction
import com.intellij.testFramework.LightPlatformCodeInsightTestCase
class RotateSelectionsHandlerTest : LightPlatformCodeInsightTestCase() {
fun `test inline forward rotation`() {
doTest(true, "json")
}
fun `test multiline forward rotation`() {
doTest(true, "json")
}
fun `test forward rotation with caret without selection`() {
doTest(true, "html")
}
fun `test inline backwards rotation`() {
doTest(false, "json")
}
fun `test multiline backwards rotation`() {
doTest(false, "json")
}
private fun doTest(isForward: Boolean, extension: String) {
val dir = "/codeInsight/rotateSelections"
val beforeFile = dir + "/" + getTestName(false).removePrefix(" ").replace(' ', '_') + "." + extension
configureByFile(beforeFile)
if (isForward) {
RotateSelectionsForwardAction().handler.execute(editor, null, null)
} else {
RotateSelectionsBackwardsAction().handler.execute(editor, null, null)
}
val afterFile = dir + "/" + getTestName(false).removePrefix(" ").replace(' ', '_') + "_after" + "." + extension
checkResultByFile(afterFile)
}
}
@@ -745,6 +745,10 @@ action.MoveLineDown.text=Move Line Do_wn
action.MoveLineDown.description=Move selected lines one line down
action.MoveLineUp.text=Move Line _Up
action.MoveLineUp.description=Move selected lines one line up
action.RotateSelectionsForward.text=Rotate selections forward
action.RotateSelectionsForward.description=Rotate selections forward
action.RotateSelectionsBackwards.text=Rotate selections backwards
action.RotateSelectionsBackwards.description=Rotate selections backwards
group.AnalyzeMenu.text=Analy_ze
action.CodeCleanup.text=_Code Cleanup...
action.CodeCleanup.description=Run cleanup inspections and apply quick fixes
@@ -581,6 +581,11 @@
<action id="MoveElementRight" class="com.intellij.codeInsight.editorActions.moveLeftRight.MoveElementRightAction"/>
<action id="MoveLineDown" class="com.intellij.codeInsight.editorActions.moveUpDown.MoveLineDownAction"/>
<action id="MoveLineUp" class="com.intellij.codeInsight.editorActions.moveUpDown.MoveLineUpAction"/>
<separator/>
<action id="RotateSelectionsForward" class="com.intellij.codeInsight.editorActions.rotateSelection.RotateSelectionsForwardAction"/>
<action id="RotateSelectionsBackwards" class="com.intellij.codeInsight.editorActions.rotateSelection.RotateSelectionsBackwardsAction"/>
<separator/>
<add-to-group group-id="MainMenu" anchor="after" relative-to-action="GoToMenu"/>
</group>