Skip to content

Commit

Permalink
#2894 better test samples, better empty handling
Browse files Browse the repository at this point in the history
  • Loading branch information
hurricup committed Sep 15, 2024
1 parent c9257b6 commit 8720773
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -46,13 +50,23 @@ 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);

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));
}
Expand Down
54 changes: 31 additions & 23 deletions plugin/src/test/java/intellilang/PerlQuickEditTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ class PerlQuickEditTest : PerlLightTestCase() {
val (originalEditor, fragmentFile) = initFileWithTestSample()

myFixture.editor.caretModel.moveToOffset(fragmentFile.text.indexAfter("<html>"))
myFixture.type("\nhello there")
myFixture.type("\nhello\n there")
assertFalse(myFixture.editor.isDisposed)
assertEquals(
"""
<div>
<html>
hello there
hello
there
</html>
</div>""".trimIndent(), myFixture.editor.document.text.trim().replace(Regex("[ \t]+\n"), "\n")
)
Expand All @@ -52,7 +53,8 @@ class PerlQuickEditTest : PerlLightTestCase() {
say <<~HTML;
<div>
<html>
hello there
hello
there
</html>
</div>
HTML
Expand All @@ -65,7 +67,7 @@ class PerlQuickEditTest : PerlLightTestCase() {
val (originalEditor, fragmentFile) = initFileWithTestSample()

myFixture.editor.caretModel.moveToOffset(fragmentFile.text.indexAfter("</div>"))
myFixture.type("\n\n\nhello there\n")
myFixture.type("\n\n\nhello\n there\n")
assertFalse(myFixture.editor.isDisposed)
assertEquals(
"""
Expand All @@ -75,7 +77,8 @@ class PerlQuickEditTest : PerlLightTestCase() {
</div>
hello there
hello
there
""".trimIndent(), myFixture.editor.document.text.trim().replace(Regex("[ \t]+\n"), "\n")
)

Expand All @@ -91,7 +94,8 @@ class PerlQuickEditTest : PerlLightTestCase() {
</div>
hello there
hello
there
HTML
}""".trimIndent(), originalEditor.document.text
Expand All @@ -103,11 +107,12 @@ class PerlQuickEditTest : PerlLightTestCase() {
val (originalEditor, fragmentFile) = initFileWithTestSample()

myFixture.editor.caretModel.moveToOffset(fragmentFile.text.indexOf("<div>"))
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
<div>
<html>
Expand All @@ -124,7 +129,8 @@ class PerlQuickEditTest : PerlLightTestCase() {
hello there
hello
there
<div>
<html>
Expand All @@ -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")
)

Expand All @@ -154,15 +161,16 @@ class PerlQuickEditTest : PerlLightTestCase() {
sub foo{
say <<~HTML;
hello there
hello
there
HTML
}""".trimIndent(), originalEditor.document.text
)
}


private fun initFileWithTestSample(): Pair<Editor, PsiFile> {
initWithTextSmart(
private fun initFileWithTestSample(
testSample: String =
"""
use v5.36;

Expand All @@ -173,19 +181,19 @@ class PerlQuickEditTest : PerlLightTestCase() {
</html>
</div>
HTML
}""".trimIndent()
)
val originalEditor = injectionTestFixture.topLevelEditor
val quickEditHandler = QuickEditAction().invokeImpl(project, injectionTestFixture.topLevelEditor, injectionTestFixture.topLevelFile)
val fragmentFile = quickEditHandler.newFile
assertEquals(
"""
}""".trimIndent(),
injectedSample: String = """
<div>
<html>
</html>
</div>
""".trimIndent(), fragmentFile.text.trim()
)
""".trimIndent()
): Pair<Editor, PsiFile> {
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)
Expand Down

0 comments on commit 8720773

Please sign in to comment.