Skip to content

Commit

Permalink
Runtime error highlighting (#7402)
Browse files Browse the repository at this point in the history
* Add slightly more custom highlighting control

* Removes RuntimeErrorProducer#toHighlight()
- Highlighting defaults to null now, if error(String) is used

* Removes toHighlight() override in SPE

* Adds getRawExpr() method to SPE

* Makes rawExpr field protected instead of having a public method to access it
- Cheers Sovde

---------

Co-authored-by: Moderocky <[email protected]>
  • Loading branch information
cheeezburga and Moderocky authored Jan 7, 2025
1 parent 34a0bc6 commit 42b6198
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
public abstract class SimplePropertyExpression<F, T> extends PropertyExpression<F, T> implements Converter<F, T>, SyntaxRuntimeErrorProducer {

private Node node;
private String rawExpr;
protected String rawExpr;

@Override
@SuppressWarnings("unchecked")
Expand Down Expand Up @@ -48,11 +48,6 @@ public Node getNode() {
return node;
}

@Override
public String toHighlight() {
return rawExpr;
}

/**
* Used to collect the property type used in the register method.
* This forms the toString of this SimplePropertyExpression.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
package org.skriptlang.skript.log.runtime;

import ch.njol.skript.Skript;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser;
import ch.njol.skript.lang.SyntaxElement;
import ch.njol.util.Kleenean;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.logging.Level;

Expand All @@ -26,33 +21,33 @@ public interface RuntimeErrorProducer {
@NotNull ErrorSource getErrorSource();

/**
* Gets the text that should be underlined within the line contents. This should match the text the user wrote that
* was parsed as the syntax that threw the runtime issue. For example, if the skull expression in
* {@code give skull of player to all players} throws a runtime error, this method should return
* {@code "skull of player"}
* <br>
* An example implementation for {@link Expression}s is to store {@link SkriptParser.ParseResult#expr} during
* {@link SyntaxElement#init(Expression[], int, Kleenean, SkriptParser.ParseResult)} and return that.
* Dispatches a runtime error with the given text.
* Metadata will be provided along with the message, including line number, the docs name of the producer,
* and the line content.
* <br>
* For other syntax types, this may vary. Effects, for example, may underline the whole line.
* Implementations should ensure they call super() to print the error.
*
* @return The text to underline in the line that produced a runtime error. This may be null if no highlighting
* is desired or possible.
* @param message The text to display as the error message.
*/
@Nullable String toHighlight();
default void error(String message) {
getRuntimeErrorManager().error(
new RuntimeError(Level.SEVERE, getErrorSource(), message, null)
);
}

/**
* Dispatches a runtime error with the given text.
* Dispatches a runtime error with the given text and syntax highlighting.
* Metadata will be provided along with the message, including line number, the docs name of the producer,
* and the line content.
* <br>
* Implementations should ensure they call super() to print the error.
*
* @param message The text to display as the error message.
* @param highlight The text to highlight in the parsed syntax.
*/
default void error(String message) {
default void error(String message, String highlight) {
getRuntimeErrorManager().error(
new RuntimeError(Level.SEVERE, getErrorSource(), message, toHighlight())
new RuntimeError(Level.SEVERE, getErrorSource(), message, highlight)
);
}

Expand All @@ -67,7 +62,23 @@ default void error(String message) {
*/
default void warning(String message) {
getRuntimeErrorManager().error(
new RuntimeError(Level.WARNING, getErrorSource(), message, toHighlight())
new RuntimeError(Level.WARNING, getErrorSource(), message, null)
);
}

/**
* Dispatches a runtime warning with the given text and syntax highlighting.
* Metadata will be provided along with the message, including line number, the docs name of the producer,
* and the line content.
* <br>
* Implementations should ensure they call super() to print the warning.
*
* @param message The text to display as the error message.
* @param highlight The text to highlight in the parsed syntax.
*/
default void warning(String message, String highlight) {
getRuntimeErrorManager().error(
new RuntimeError(Level.WARNING, getErrorSource(), message, highlight)
);
}

Expand Down

0 comments on commit 42b6198

Please sign in to comment.