diff --git a/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/model/OnEnterRule.java b/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/model/OnEnterRule.java index 590e07d98..fc9848188 100644 --- a/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/model/OnEnterRule.java +++ b/org.eclipse.tm4e.languageconfiguration/src/main/java/org/eclipse/tm4e/languageconfiguration/internal/model/OnEnterRule.java @@ -13,7 +13,6 @@ package org.eclipse.tm4e.languageconfiguration.internal.model; import org.eclipse.jdt.annotation.Nullable; -import org.eclipse.tm4e.core.TMException; import org.eclipse.tm4e.core.internal.utils.StringUtils; /** @@ -54,19 +53,6 @@ public OnEnterRule(final RegExPattern beforeText, final @Nullable RegExPattern a this.action = action; } - /** - * Only for unit tests - * - * @throws TMException if beforeText, afterText or previousLineText contain invalid regex pattern - */ - OnEnterRule(final String beforeText, final @Nullable String afterText, final @Nullable String previousLineText, - final EnterAction action) { - this.beforeText = RegExPattern.of(beforeText); - this.afterText = afterText == null ? null : RegExPattern.of(afterText); - this.previousLineText = previousLineText == null ? null : RegExPattern.of(previousLineText); - this.action = action; - } - @Override public String toString() { return StringUtils.toString(this, sb -> sb diff --git a/org.eclipse.tm4e.languageconfiguration/src/test/java/org/eclipse/tm4e/languageconfiguration/internal/supports/ParsingTest.java b/org.eclipse.tm4e.languageconfiguration/src/test/java/org/eclipse/tm4e/languageconfiguration/internal/model/ParsingTest.java similarity index 97% rename from org.eclipse.tm4e.languageconfiguration/src/test/java/org/eclipse/tm4e/languageconfiguration/internal/supports/ParsingTest.java rename to org.eclipse.tm4e.languageconfiguration/src/test/java/org/eclipse/tm4e/languageconfiguration/internal/model/ParsingTest.java index d63e34aab..6771f78be 100644 --- a/org.eclipse.tm4e.languageconfiguration/src/test/java/org/eclipse/tm4e/languageconfiguration/internal/supports/ParsingTest.java +++ b/org.eclipse.tm4e.languageconfiguration/src/test/java/org/eclipse/tm4e/languageconfiguration/internal/model/ParsingTest.java @@ -7,7 +7,7 @@ * * SPDX-License-Identifier: EPL-2.0 **********************************************************************/ -package org.eclipse.tm4e.languageconfiguration.internal.supports; +package org.eclipse.tm4e.languageconfiguration.internal.model; import static org.junit.jupiter.api.Assertions.*; @@ -24,7 +24,6 @@ import java.util.concurrent.atomic.AtomicInteger; import org.eclipse.jdt.annotation.Nullable; -import org.eclipse.tm4e.languageconfiguration.internal.model.LanguageConfiguration; import org.junit.jupiter.api.Test; class ParsingTest { diff --git a/org.eclipse.tm4e.languageconfiguration/src/test/java/org/eclipse/tm4e/languageconfiguration/internal/model/OnEnterSupportTest.java b/org.eclipse.tm4e.languageconfiguration/src/test/java/org/eclipse/tm4e/languageconfiguration/internal/supports/OnEnterSupportTest.java similarity index 69% rename from org.eclipse.tm4e.languageconfiguration/src/test/java/org/eclipse/tm4e/languageconfiguration/internal/model/OnEnterSupportTest.java rename to org.eclipse.tm4e.languageconfiguration/src/test/java/org/eclipse/tm4e/languageconfiguration/internal/supports/OnEnterSupportTest.java index 20e348ff0..1c3058bd1 100644 --- a/org.eclipse.tm4e.languageconfiguration/src/test/java/org/eclipse/tm4e/languageconfiguration/internal/model/OnEnterSupportTest.java +++ b/org.eclipse.tm4e.languageconfiguration/src/test/java/org/eclipse/tm4e/languageconfiguration/internal/supports/OnEnterSupportTest.java @@ -1,34 +1,77 @@ /** * Copyright (c) 2015-2017 Angelo ZERR. + * * This program and the accompanying materials are made * available under the terms of the Eclipse Public License 2.0 * which is available at https://www.eclipse.org/legal/epl-2.0/ * * SPDX-License-Identifier: EPL-2.0 * + * Initial code from https://github.com/microsoft/vscode/ + * Initial copyright Copyright (C) Microsoft Corporation. All rights reserved. + * Initial license: MIT + * * Contributors: - * Angelo Zerr - initial API and implementation - * Sebastian Thomschke (Vegard IT GmbH) - refactor and extend test cases + * - Microsoft Corporation: Initial code, written in TypeScript, licensed under MIT license + * - Angelo Zerr - translation and adaptation to Java + * - Sebastian Thomschke (Vegard IT GmbH) - refactor and extend test cases */ -package org.eclipse.tm4e.languageconfiguration.internal.model; +package org.eclipse.tm4e.languageconfiguration.internal.supports; import static org.junit.jupiter.api.Assertions.*; import java.util.List; import org.eclipse.jdt.annotation.Nullable; +import org.eclipse.tm4e.languageconfiguration.internal.model.CharacterPair; +import org.eclipse.tm4e.languageconfiguration.internal.model.EnterAction; import org.eclipse.tm4e.languageconfiguration.internal.model.EnterAction.IndentAction; -import org.eclipse.tm4e.languageconfiguration.internal.supports.OnEnterSupport; +import org.eclipse.tm4e.languageconfiguration.internal.model.OnEnterRule; +import org.eclipse.tm4e.languageconfiguration.internal.model.RegExPattern; import org.junit.jupiter.api.Test; /** * {@link OnEnterSupport} tests. * - * @see - * https://github.com/microsoft/vscode/blob/main/src/vs/editor/test/common/modes/supports/onEnter.test.ts + * @see + * github.com/microsoft/vscode/blob/main/src/vs/editor/test/common/modes/supports/onEnter.test.ts */ class OnEnterSupportTest { + /** + * @see + * github.com/microsoft/vscode/blob/main/src/vs/editor/test/common/modes/supports/javascriptOnEnterRules.ts + */ + static final List javaScriptOnEnterRules = List.of( + new OnEnterRule( // e.g. /** | */ + RegExPattern.of("^\\s*\\/\\*\\*(?!\\/)([^\\*]|\\*(?!\\/))*$"), + RegExPattern.of("^\\s*\\*\\/$"), + null, + new EnterAction(IndentAction.IndentOutdent, " * ", null)), + new OnEnterRule( // e.g. /** ...| + RegExPattern.of("^\\s*\\/\\*\\*(?!\\/)([^\\*]|\\*(?!\\/))*$"), + null, + null, + new EnterAction(IndentAction.None, " * ", null)), + new OnEnterRule( + // e.g. * ...| + RegExPattern.of("^(\\t|(\\ \\ ))*\\ \\*(\\ ([^\\*]|\\*(?!\\/))*)?$"), + null, + RegExPattern.of("(?=^(\\s*(\\/\\*\\*|\\*)).*)(?=(?!(\\s*\\*\\/)))"), + new EnterAction(IndentAction.None, "* ", null)), + new OnEnterRule( // e.g. */| + RegExPattern.of("^(\\t|(\\ \\ ))*\\ \\*\\/\\s*$"), + null, + null, + new EnterAction(IndentAction.None, null, 1)), + new OnEnterRule( // e.g. *-----*/| + RegExPattern.of("^(\\t|(\\ \\ ))*\\ \\*[^/]*\\*\\/\\s*$"), + null, + null, + new EnterAction(IndentAction.None, null, 1))); + @Test void testUseBrackets() { @@ -74,42 +117,20 @@ void testIndentAction(final String beforeText, final String afterText, final Ind } @Test - void testRegExpRules() { + void testUseRegExpRules() { class Support extends OnEnterSupport { Support() { - super(null, List.of( - // see https://github.com/microsoft/vscode/blob/main/src/vs/editor/test/common/modes/supports/javascriptOnEnterRules.ts - new OnEnterRule( // e.g. /** | */ - "^\\s*\\/\\*\\*(?!\\/)([^\\*]|\\*(?!\\/))*$", - "^\\s*\\*\\/$", null, - new EnterAction(IndentAction.IndentOutdent, " * ", null)), - new OnEnterRule( // e.g. /** ...| - "^\\s*\\/\\*\\*(?!\\/)([^\\*]|\\*(?!\\/))*$", - null, null, - new EnterAction(IndentAction.None, " * ", null)), - new OnEnterRule( - // e.g. * ...| - "^(\\t|(\\ \\ ))*\\ \\*(\\ ([^\\*]|\\*(?!\\/))*)?$", - null, "(?=^(\\s*(\\/\\*\\*|\\*)).*)(?=(?!(\\s*\\*\\/)))", - new EnterAction(IndentAction.None, "* ", null)), - new OnEnterRule( // e.g. */| - "^(\\t|(\\ \\ ))*\\ \\*\\/\\s*$", - null, null, - new EnterAction(IndentAction.None, null, 1)), - new OnEnterRule( // e.g. *-----*/| - "^(\\t|(\\ \\ ))*\\ \\*[^/]*\\*\\/\\s*$", - null, null, - new EnterAction(IndentAction.None, null, 1)))); + super(null, javaScriptOnEnterRules); } void testIndentAction(final String previousLineText, final String beforeText, final String afterText, - @Nullable final IndentAction expectedIndentAction, @Nullable final String expectedAppendText) { + final @Nullable IndentAction expectedIndentAction, final @Nullable String expectedAppendText) { testIndentAction(previousLineText, beforeText, afterText, expectedIndentAction, expectedAppendText, 0); } void testIndentAction(final String previousLineText, final String beforeText, final String afterText, - @Nullable final IndentAction expectedIndentAction, @Nullable final String expectedAppendText, + final @Nullable IndentAction expectedIndentAction, final @Nullable String expectedAppendText, final int removeText) { final EnterAction actual = onEnter(previousLineText, beforeText, afterText); if (expectedIndentAction == null) { @@ -197,4 +218,29 @@ void testIndentAction(final String previousLineText, final String beforeText, fi support.testIndentAction("", " * test() {", "", IndentAction.Indent, null, 0); support.testIndentAction(" ", " * test() {", "", IndentAction.Indent, null, 0); } + + @Test + void testVscodeIssue43469() { + + class Support extends OnEnterSupport { + Support() { + super(null, javaScriptOnEnterRules); + } + + void testIndentAction(final String beforeText, final String afterText, final @Nullable IndentAction expected) { + final EnterAction actual = onEnter("", beforeText, afterText); + if (expected == IndentAction.None) { + assertNull(actual); + } else { + assertNotNull(actual); + assertEquals(expected, actual.indentAction); + } + } + } + + final var support = new Support(); + support.testIndentAction("const r = /{/;", "", IndentAction.None); + support.testIndentAction("const r = /{[0-9]/;", "", IndentAction.None); + support.testIndentAction("const r = /[a-zA-Z]{/;", "", IndentAction.None); + } }