From e8211ce00cf59e5e54ab03dc7154535ef7222d04 Mon Sep 17 00:00:00 2001 From: Alexandr Evstigneev Date: Sat, 21 Sep 2024 18:19:02 +0400 Subject: [PATCH] #2894 do not inject into last newline --- .../intellilang/PerlHeredocLiteralEscaper.java | 7 +------ .../PerlHeredocLanguageInjector.java | 18 ++++++++++++++---- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/plugin/core/src/main/java/com/perl5/lang/perl/idea/intellilang/PerlHeredocLiteralEscaper.java b/plugin/core/src/main/java/com/perl5/lang/perl/idea/intellilang/PerlHeredocLiteralEscaper.java index 9033341de5..46e5b310b5 100644 --- a/plugin/core/src/main/java/com/perl5/lang/perl/idea/intellilang/PerlHeredocLiteralEscaper.java +++ b/plugin/core/src/main/java/com/perl5/lang/perl/idea/intellilang/PerlHeredocLiteralEscaper.java @@ -35,12 +35,7 @@ public boolean isOneLine() { @Override public boolean decode(@NotNull TextRange rangeInsideHost, @NotNull StringBuilder outChars) { - var hostTextLength = myHost.getTextLength(); - var endOffset = rangeInsideHost.getEndOffset(); - if (endOffset == hostTextLength) { - endOffset--; - } - outChars.append(myHost.getNode().getChars().subSequence(rangeInsideHost.getStartOffset(), endOffset)); + outChars.append(rangeInsideHost.subSequence(myHost.getNode().getChars())); return true; } diff --git a/plugin/intelliLang/src/main/java/com/perl5/lang/perl/intellilang/PerlHeredocLanguageInjector.java b/plugin/intelliLang/src/main/java/com/perl5/lang/perl/intellilang/PerlHeredocLanguageInjector.java index 9a34eb7239..6587a5d9d7 100644 --- a/plugin/intelliLang/src/main/java/com/perl5/lang/perl/intellilang/PerlHeredocLanguageInjector.java +++ b/plugin/intelliLang/src/main/java/com/perl5/lang/perl/intellilang/PerlHeredocLanguageInjector.java @@ -94,7 +94,8 @@ public void getLanguagesToInject(@NotNull MultiHostRegistrar registrar, @NotNull private void addPlace(@NotNull PerlHeredocElementImpl heredocElement, @NotNull MultiHostRegistrar registrar) { int indentSize = heredocElement.getRealIndentSize(); if (indentSize == 0) { - registrar.addPlace(null, null, heredocElement, ElementManipulators.getValueTextRange(heredocElement)); + var elementRange = ElementManipulators.getValueTextRange(heredocElement); + registrar.addPlace(null, null, heredocElement, TextRange.create(elementRange.getStartOffset(), elementRange.getEndOffset() - 1)); return; } @@ -107,8 +108,13 @@ private void addPlace(@NotNull PerlHeredocElementImpl heredocElement, @NotNull M while (sourceOffset < sourceLength) { char currentChar = sourceText.charAt(sourceOffset); if (currentChar == '\n') { - var suffix = sourceOffset + 1 < sourceLength ? null : "\n"; - registrar.addPlace(null, suffix, heredocElement, TextRange.from(sourceOffset, 1)); + String suffix = null; + int endOffset = sourceOffset + 1; + if (endOffset == sourceLength) { + suffix = "\n"; + endOffset--; + } + registrar.addPlace(null, suffix, heredocElement, TextRange.create(sourceOffset, endOffset)); currentLineIndent = 0; } else if (Character.isWhitespace(currentChar) && currentLineIndent < indentSize) { @@ -125,7 +131,11 @@ else if (Character.isWhitespace(currentChar) && currentLineIndent < indentSize) } } - var suffix = sourceEnd < sourceLength ? null : "\n"; + String suffix = null; + if (sourceEnd == sourceLength) { + suffix = "\n"; + sourceEnd--; + } registrar.addPlace(null, suffix, heredocElement, TextRange.create(sourceOffset, sourceEnd)); sourceOffset = sourceEnd;