Skip to content

Commit

Permalink
feat: add support for OnEnterRule#previousLineText
Browse files Browse the repository at this point in the history
  • Loading branch information
sebthom committed Nov 6, 2023
1 parent 3b09e2c commit 36d666e
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 132 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -66,13 +66,14 @@ public static LanguageConfiguration load(@NonNull final Reader reader) {
final var indentActionString = getAsString(actionJsonObj.get("indent")); //$NON-NLS-1$
if (indentActionString != null) {
final var afterText = getAsPattern(jsonObj.get("afterText")); //$NON-NLS-1$
final var previousLineText = getAsPattern(jsonObj.get("previousLineText")); //$NON-NLS-1$
final var indentAction = IndentAction.get(indentActionString);
final var removeText = getAsInteger(actionJsonObj.get("removeText")); //$NON-NLS-1$
final var appendText = getAsString(actionJsonObj.get("appendText")); //$NON-NLS-1$
final var action = new EnterAction(indentAction);
action.appendText = appendText;
action.removeText = removeText;
return new OnEnterRule(beforeText, afterText, action);
return new OnEnterRule(beforeText, afterText, previousLineText, action);
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
* Sebastian Thomschke (Vegard IT GmbH) - add previousLineText support
*/
package org.eclipse.tm4e.languageconfiguration.internal.model;

Expand Down Expand Up @@ -35,16 +36,21 @@ public final class OnEnterRule {
@Nullable
public final Pattern afterText;

// TODO @Nullable public final Pattern previousLineText;
/**
* This rule will only execute if the text above the current line matches this regular expression.
*/
@Nullable
public final Pattern previousLineText;

/**
* The action to execute.
*/
public final EnterAction action;

public OnEnterRule(final Pattern beforeText, @Nullable final Pattern afterText, final EnterAction action) {
public OnEnterRule(final Pattern beforeText, @Nullable final Pattern afterText, @Nullable final Pattern previousLineText, final EnterAction action) {
this.beforeText = beforeText;
this.afterText = afterText;
this.previousLineText = previousLineText;
this.action = action;
}

Expand All @@ -53,9 +59,10 @@ public OnEnterRule(final Pattern beforeText, @Nullable final Pattern afterText,
*
* @throws PatternSyntaxException if beforeText or afterText contain invalid regex pattern
*/
OnEnterRule(final String beforeText, @Nullable final String afterText, final EnterAction action) {
OnEnterRule(final String beforeText, @Nullable final String afterText, @Nullable final String previousLineText, final EnterAction action) {
this.beforeText = Pattern.compile(beforeText);
this.afterText = afterText == null ? null : Pattern.compile(afterText);
this.previousLineText = previousLineText == null ? null : Pattern.compile(previousLineText);
this.action = action;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.eclipse.tm4e.languageconfiguration.internal.supports.CommentSupport;
import org.eclipse.tm4e.languageconfiguration.internal.supports.OnEnterSupport;
import org.eclipse.tm4e.languageconfiguration.internal.utils.TextUtils;
import org.eclipse.tm4e.ui.internal.model.DocumentHelper;
import org.osgi.service.prefs.BackingStoreException;

public final class LanguageConfigurationRegistryManager extends AbstractLanguageConfigurationRegistryManager {
Expand Down Expand Up @@ -159,34 +160,31 @@ public CompleteEnterAction getEnterAction(final IDocument document, final int of

// String scopeLineText = DocumentHelper.getLineTextOfOffset(document, offset, false);
final String beforeEnterText = document.get(lineInfo.getOffset(), offset - lineInfo.getOffset());
String afterEnterText = null;

// selection support
String afterEnterText = null;
// if (range.isEmpty()) {
afterEnterText = document.get(offset, lineInfo.getLength() - (offset - lineInfo.getOffset()));
// afterEnterText = scopedLineText.substr(range.startColumn - 1 - scopedLineTokens.firstCharOffset);
// afterEnterText = scopedLineText.substr(range.startColumn - 1 - scopedLineTokens.firstCharOffset);
// } else {
// const endScopedLineTokens = this.getScopedLineTokens(model,
// range.endLineNumber, range.endColumn);
// afterEnterText = endScopedLineTokens.getLineContent().substr(range.endColumn - 1 -
// scopedLineTokens.firstCharOffset);
// const endScopedLineTokens = this.getScopedLineTokens(model, range.endLineNumber, range.endColumn);
// afterEnterText = endScopedLineTokens.getLineContent().substr(range.endColumn - 1 - scopedLineTokens.firstCharOffset);
// }

/*
* let lineNumber = range.startLineNumber; let oneLineAboveText = '';
*
* if (lineNumber > 1 && scopedLineTokens.firstCharOffset === 0) { // This is
* not the first line and the entire line belongs to this mode let
* oneLineAboveScopedLineTokens = this.getScopedLineTokens(model, lineNumber -
* 1); if (oneLineAboveScopedLineTokens.languageId ===
* scopedLineTokens.languageId) { // The line above ends with text belonging to
* the same mode oneLineAboveText =
* oneLineAboveScopedLineTokens.getLineContent(); } }
*/
String previousLineText = "";
// if (range.startLineNumber > 1 && scopedLineTokens.firstCharOffset === 0) {
// // This is not the first line and the entire line belongs to this mode
// const oneLineAboveScopedLineTokens = getScopedLineTokens(model, range.startLineNumber - 1);
// if (oneLineAboveScopedLineTokens.languageId === scopedLineTokens.languageId) {
// // The line above ends with text belonging to the same mode
// previousLineText = oneLineAboveScopedLineTokens.getLineContent();
previousLineText = DocumentHelper.getLineText(document, document.getLineOfOffset(offset), false);
// }
// }

EnterAction enterResult = null;
try {
enterResult = onEnterSupport.onEnter(beforeEnterText, afterEnterText);
enterResult = onEnterSupport.onEnter(previousLineText, beforeEnterText, afterEnterText);
} catch (final Exception e) {
// onUnexpectedError(e);
}
Expand All @@ -195,8 +193,7 @@ public CompleteEnterAction getEnterAction(final IDocument document, final int of
return null;
}

// Here we add `\t` to appendText first because enterAction is leveraging
// appendText and removeText to change indentation.
// Here we add `\t` to appendText first because enterAction is leveraging appendText and removeText to change indentation.
if (enterResult.appendText == null) {
if (enterResult.indentAction == IndentAction.Indent
|| enterResult.indentAction == IndentAction.IndentOutdent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
*
* Contributors:
* Angelo Zerr <angelo.zerr@gmail.com> - initial API and implementation
* Sebastian Thomschke (Vegard IT GmbH) - add previousLineText support
*/
package org.eclipse.tm4e.languageconfiguration.internal.supports;

Expand Down Expand Up @@ -52,22 +53,24 @@ public OnEnterSupport(@Nullable final List<CharacterPair> brackets, @Nullable fi
@Nullable
public EnterAction onEnter(
// TODO autoIndent: EditorAutoIndentStrategy,
// TODO final String previousLineText,
final String previousLineText,
final String beforeEnterText,
final String afterEnterText) {
// (1): `regExpRules`
for (final OnEnterRule rule : regExpRules) {
final var beforeText = rule.beforeText;
if (beforeText.matcher(beforeEnterText).find()) {
final var afterText = rule.afterText;
if (afterText != null) {
if (afterText.matcher(afterEnterText).find()) {
return rule.action;
}
} else {
return rule.action;
}
}
final var beforeTextPattern = rule.beforeText;
if (!beforeTextPattern.matcher(beforeEnterText).find())
continue;

final var afterTextPattern = rule.afterText;
if (afterTextPattern != null && !afterTextPattern.matcher(afterEnterText).find())
continue;

final var previousLinePattern = rule.previousLineText;
if (previousLinePattern != null && !previousLinePattern.matcher(previousLineText).find())
continue;

return rule.action;
}

// (2): Special indent-outdent
Expand Down
Loading

0 comments on commit 36d666e

Please sign in to comment.