JoinLineProcessor.getSpacesToAdd: properly handle end-of-file

Fixes EA-213710 - [JoinLines] IOOBE: ImmutableText.outOfRange

GitOrigin-RevId: 13ab2f27bbcf5457671331c8b41e5897d9d82255
This commit is contained in:
Tagir Valeev
2019-10-02 03:38:05 +00:00
committed by intellij-monorepo-bot
parent 51b1d72348
commit 7cc4dc505b
4 changed files with 12 additions and 3 deletions
@@ -0,0 +1,5 @@
class Foo {
void test() {
}
// this test must have a single newline at the end-of-file
<caret>}
@@ -0,0 +1,5 @@
class Foo {
void test() {
}
// this test must have a single newline at the end-of-file
}
@@ -16,13 +16,11 @@
package com.intellij.java.codeInsight;
import com.intellij.JavaTestUtil;
import com.intellij.application.options.CodeStyle;
import com.intellij.ide.DataManager;
import com.intellij.lang.java.JavaLanguage;
import com.intellij.openapi.actionSystem.IdeActions;
import com.intellij.openapi.editor.actionSystem.EditorActionHandler;
import com.intellij.openapi.editor.actionSystem.EditorActionManager;
import com.intellij.psi.codeStyle.CodeStyleSettings;
import com.intellij.psi.codeStyle.CodeStyleSettingsManager;
import com.intellij.psi.codeStyle.CommonCodeStyleSettings;
import com.intellij.testFramework.LightJavaCodeInsightTestCase;
@@ -64,6 +62,7 @@ public class JoinLinesTest extends LightJavaCodeInsightTestCase {
public void testIfChainNoBraces() { doTest(); }
public void testIfChainElse() { doTest(); }
public void testIfChainSelection() { doTest(); }
public void testAtEOF() { doTest(); }
public void testSCR3493() {
CommonCodeStyleSettings settings = getJavaSettings();
@@ -340,7 +340,7 @@ public class JoinLinesHandler extends EditorActionHandler {
RangeMarker marker = markers.get(i);
if (!marker.isValid()) continue;
int end = StringUtil.skipWhitespaceForward(text, marker.getStartOffset());
int spacesToCreate = text.charAt(end) == '\n' ? 0 :
int spacesToCreate = end == text.length() || text.charAt(end) == '\n' ? 0 :
model == null ? 1 : formatter.getSpacingForBlockAtOffset(model, end);
spacesToAdd[i] = spacesToCreate < 0 ? 1 : spacesToCreate;
}