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( """