From 8720773a4bfc813ce2f68a28f5d00a14d6fd50c9 Mon Sep 17 00:00:00 2001 From: Alexandr Evstigneev Date: Sun, 15 Sep 2024 13:01:41 +0400 Subject: [PATCH] #2894 better test samples, better empty handling --- .../PerlHeredocElementManipulator.java | 14 +++++ .../java/intellilang/PerlQuickEditTest.kt | 54 +++++++++++-------- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/plugin/core/src/main/java/com/perl5/lang/perl/idea/manipulators/PerlHeredocElementManipulator.java b/plugin/core/src/main/java/com/perl5/lang/perl/idea/manipulators/PerlHeredocElementManipulator.java index 419d14bfce..250a4c6ed6 100644 --- a/plugin/core/src/main/java/com/perl5/lang/perl/idea/manipulators/PerlHeredocElementManipulator.java +++ b/plugin/core/src/main/java/com/perl5/lang/perl/idea/manipulators/PerlHeredocElementManipulator.java @@ -16,10 +16,14 @@ package com.perl5.lang.perl.idea.manipulators; +import com.intellij.application.options.CodeStyle; +import com.intellij.openapi.project.Project; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.text.StringUtil; import com.intellij.psi.AbstractElementManipulator; +import com.intellij.psi.codeStyle.CommonCodeStyleSettings; import com.intellij.util.IncorrectOperationException; +import com.perl5.lang.perl.PerlLanguage; import com.perl5.lang.perl.psi.impl.PerlHeredocElementImpl; import com.perl5.lang.perl.psi.utils.PerlElementFactory; import org.jetbrains.annotations.NotNull; @@ -46,6 +50,9 @@ public PerlHeredocElementImpl handleContentChange(@NotNull PerlHeredocElementImp range = TextRange.create(lineStart, range.getEndOffset()); } } + else if (range.getStartOffset() == 0) { + newContent = prependLines(newContent, getIndenter(element.getProject(), element.getRealIndentSize())); + } String newElementText = range.replace(elementText, newContent); PerlHeredocElementImpl replacement = PerlElementFactory.createHeredocBodyReplacement(element, newElementText); @@ -53,6 +60,13 @@ public PerlHeredocElementImpl handleContentChange(@NotNull PerlHeredocElementImp return (PerlHeredocElementImpl)element.replace(replacement); } + private static @NotNull String getIndenter(@NotNull Project project, int indentSize) { + CommonCodeStyleSettings.IndentOptions indentOptions = + CodeStyle.getSettings(project).getCommonSettings(PerlLanguage.INSTANCE).getIndentOptions(); + + return StringUtil.repeat(indentOptions != null && indentOptions.USE_TAB_CHARACTER ? "\t" : " ", indentSize); + } + private static @NotNull String prependLines(@NotNull String newContent, @NotNull String prefix) { return prefix + String.join(prefix, StringUtil.split(newContent, "\n", false, true)); } diff --git a/plugin/src/test/java/intellilang/PerlQuickEditTest.kt b/plugin/src/test/java/intellilang/PerlQuickEditTest.kt index 54404389d1..d563850fb2 100644 --- a/plugin/src/test/java/intellilang/PerlQuickEditTest.kt +++ b/plugin/src/test/java/intellilang/PerlQuickEditTest.kt @@ -33,13 +33,14 @@ class PerlQuickEditTest : PerlLightTestCase() { val (originalEditor, fragmentFile) = initFileWithTestSample() myFixture.editor.caretModel.moveToOffset(fragmentFile.text.indexAfter("")) - myFixture.type("\nhello there") + myFixture.type("\nhello\n there") assertFalse(myFixture.editor.isDisposed) assertEquals( """
- hello there + hello + there
""".trimIndent(), myFixture.editor.document.text.trim().replace(Regex("[ \t]+\n"), "\n") ) @@ -52,7 +53,8 @@ class PerlQuickEditTest : PerlLightTestCase() { say <<~HTML;
- hello there + hello + there
HTML @@ -65,7 +67,7 @@ class PerlQuickEditTest : PerlLightTestCase() { val (originalEditor, fragmentFile) = initFileWithTestSample() myFixture.editor.caretModel.moveToOffset(fragmentFile.text.indexAfter("")) - myFixture.type("\n\n\nhello there\n") + myFixture.type("\n\n\nhello\n there\n") assertFalse(myFixture.editor.isDisposed) assertEquals( """ @@ -75,7 +77,8 @@ class PerlQuickEditTest : PerlLightTestCase() { - hello there + hello + there """.trimIndent(), myFixture.editor.document.text.trim().replace(Regex("[ \t]+\n"), "\n") ) @@ -91,7 +94,8 @@ class PerlQuickEditTest : PerlLightTestCase() { - hello there + hello + there HTML }""".trimIndent(), originalEditor.document.text @@ -103,11 +107,12 @@ class PerlQuickEditTest : PerlLightTestCase() { val (originalEditor, fragmentFile) = initFileWithTestSample() myFixture.editor.caretModel.moveToOffset(fragmentFile.text.indexOf("
")) - myFixture.type("\n\n\nhello there\n\n") + myFixture.type("\n\n\nhello\n there\n\n") assertFalse(myFixture.editor.isDisposed) assertEquals( """ - hello there + hello + there
@@ -124,7 +129,8 @@ class PerlQuickEditTest : PerlLightTestCase() { - hello there + hello + there
@@ -140,11 +146,12 @@ class PerlQuickEditTest : PerlLightTestCase() { val (originalEditor, fragmentFile) = initFileWithTestSample() myFixture.editor.selectionModel.setSelection(0, myFixture.editor.document.text.length) - myFixture.type("hello there") + myFixture.type("hello\n there") assertFalse(myFixture.editor.isDisposed) assertEquals( """ - hello there + hello + there """.trimIndent(), myFixture.editor.document.text.trim().replace(Regex("[ \t]+\n"), "\n") ) @@ -154,15 +161,16 @@ class PerlQuickEditTest : PerlLightTestCase() { sub foo{ say <<~HTML; - hello there + hello + there HTML }""".trimIndent(), originalEditor.document.text ) } - private fun initFileWithTestSample(): Pair { - initWithTextSmart( + private fun initFileWithTestSample( + testSample: String = """ use v5.36; @@ -173,19 +181,19 @@ class PerlQuickEditTest : PerlLightTestCase() {
HTML - }""".trimIndent() - ) - val originalEditor = injectionTestFixture.topLevelEditor - val quickEditHandler = QuickEditAction().invokeImpl(project, injectionTestFixture.topLevelEditor, injectionTestFixture.topLevelFile) - val fragmentFile = quickEditHandler.newFile - assertEquals( - """ + }""".trimIndent(), + injectedSample: String = """
- """.trimIndent(), fragmentFile.text.trim() - ) + """.trimIndent() + ): Pair { + initWithTextSmart(testSample) + val originalEditor = injectionTestFixture.topLevelEditor + val quickEditHandler = QuickEditAction().invokeImpl(project, injectionTestFixture.topLevelEditor, injectionTestFixture.topLevelFile) + val fragmentFile = quickEditHandler.newFile + assertEquals(injectedSample, fragmentFile.text.trim()) myFixture.openFileInEditor(fragmentFile.virtualFile) return Pair(originalEditor, fragmentFile)