-
-
Notifications
You must be signed in to change notification settings - Fork 400
Meta-syntax and annotations. #6571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
6324dc6
The basics of annotations.
Moderocky 544a17a
Auto stash before cherry pick of "Add better annotation presence tests."
Moderocky cfb9e7b
Add better annotation presence tests.
Moderocky cc84624
Make annotations properly scoped for section headers.
Moderocky 4bba461
Scope annotations properly in section loadCode.
Moderocky 681ccb1
Add root-level annotations for structures.
Moderocky a098977
Make annotations visible in current section/structure.
Moderocky 9c4a8d9
Apply suggestions from code review
Moderocky cbefcc0
Fix thing from sovde.
Moderocky 0351eb4
Just remove the annotation all together, never really liked it anyway :(
Moderocky f2a8e0c
Nice message.
Moderocky 00a7a0a
Update src/main/java/ch/njol/skript/effects/EffAnnotate.java
Moderocky 9e08f81
Apply suggestions from code review
Moderocky 0d64e4f
Fix changes.
Moderocky 1f477f8
Remove example.
Moderocky e02f17b
Fix some bits and pieces.
Moderocky ecd7655
Apply suggestions from code review
Moderocky 54362bb
Fix string sequencing.
Moderocky 4324810
Add tests.
Moderocky cd6135d
Add new test class.
Moderocky c10e4d5
Move structural annotations.
Moderocky acbe8e0
Add no-doc.
Moderocky File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package ch.njol.skript.effects; | ||
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.doc.Description; | ||
import ch.njol.skript.doc.Examples; | ||
import ch.njol.skript.doc.Name; | ||
import ch.njol.skript.doc.Since; | ||
import ch.njol.skript.lang.Effect; | ||
import ch.njol.skript.lang.Expression; | ||
import ch.njol.skript.lang.MetaSyntaxElement; | ||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
import ch.njol.util.Kleenean; | ||
import org.bukkit.event.Event; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.jetbrains.annotations.UnknownNullability; | ||
import org.skriptlang.skript.lang.script.Annotation; | ||
|
||
@Name("Annotation (Code)") | ||
@Description({ | ||
"A special metadata note visible to the next real line of code.", | ||
"This does nothing by itself, but may change the behaviour of the following line.", | ||
"If the annotation does not exist (or the following line does not use it) then it will have no effect.", | ||
"There is no penalty for using annotations that do not exist." | ||
}) | ||
@Examples({ | ||
"on join:", | ||
"\t@suppress warnings", | ||
"\t@suppress errors", | ||
"\tbroadcast \"hello there!\"" | ||
}) | ||
@Since("INSERT VERSION") | ||
public class EffAnnotate extends Effect implements MetaSyntaxElement { | ||
|
||
static { | ||
Skript.registerEffect(EffAnnotate.class, "@<.+>"); | ||
} | ||
|
||
private @UnknownNullability Annotation annotation; | ||
|
||
@Override | ||
public boolean init(Expression<?>[] expressions, int pattern, Kleenean delayed, ParseResult result) { | ||
String text = result.regexes.get(0).group().trim(); | ||
if (text.isEmpty()) { | ||
Skript.error("An empty annotation (@) is not allowed. Please specify an annotation."); | ||
return false; | ||
} | ||
this.annotation = Annotation.create(text); | ||
this.getParser().addAnnotation(annotation); | ||
Skript.debug("found annotation: " + annotation); | ||
return true; | ||
} | ||
|
||
@Override | ||
protected void execute(Event event) { | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return '@' + annotation.value(); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package ch.njol.skript.lang; | ||
|
||
/** | ||
* Represents a type of syntax element used to modify other syntax elements rather than to provide any function | ||
* itself. | ||
*/ | ||
public interface MetaSyntaxElement extends SyntaxElement { | ||
|
||
@Override | ||
default boolean consumeAnnotations() { | ||
return false; | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,10 +10,10 @@ | |
import org.jetbrains.annotations.ApiStatus; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.jetbrains.annotations.Nullable; | ||
import org.skriptlang.skript.lang.script.Annotation; | ||
import org.skriptlang.skript.lang.structure.Structure; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.*; | ||
import java.util.function.Supplier; | ||
|
||
/** | ||
|
@@ -48,6 +48,7 @@ public abstract class Section extends TriggerSection implements SyntaxElement { | |
@Override | ||
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
SectionContext sectionContext = getParser().getData(SectionContext.class); | ||
sectionContext.setAnnotations(this.getParser().copyAnnotations()); | ||
return init(expressions, matchedPattern, isDelayed, parseResult, sectionContext.sectionNode, sectionContext.triggerItems) | ||
&& sectionContext.claim(this); | ||
} | ||
|
@@ -67,7 +68,10 @@ public abstract boolean init(Expression<?>[] expressions, | |
* (although the loaded code may change it), the calling code must deal with this. | ||
*/ | ||
protected void loadCode(SectionNode sectionNode) { | ||
ParserInstance parser = getParser(); | ||
ParserInstance parser = this.getParser(); | ||
Set<Annotation> annotations = parser.copyAnnotations(); | ||
parser.forgetAnnotations(); // scope annotations correctly for section headers | ||
APickledWalrus marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
List<TriggerSection> previousSections = parser.getCurrentSections(); | ||
|
||
List<TriggerSection> sections = new ArrayList<>(previousSections); | ||
|
@@ -78,6 +82,7 @@ protected void loadCode(SectionNode sectionNode) { | |
setTriggerItems(ScriptLoader.loadItems(sectionNode)); | ||
} finally { | ||
parser.setCurrentSections(previousSections); | ||
parser.replaceAnnotations(annotations); | ||
} | ||
} | ||
|
||
|
@@ -158,10 +163,17 @@ protected void loadOptionalCode(SectionNode sectionNode) { | |
|
||
@Nullable | ||
public static Section parse(String expr, @Nullable String defaultError, SectionNode sectionNode, List<TriggerItem> triggerItems) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the goal of the changes for this method? It doesn't actually seem to do anything, as the resetting of annotations occurs before the SkriptParser is invoked. That is, nothing happens between saving them and resetting them. |
||
SectionContext sectionContext = ParserInstance.get().getData(SectionContext.class); | ||
//noinspection unchecked,rawtypes | ||
ParserInstance parser = ParserInstance.get(); | ||
SectionContext sectionContext = parser.getData(SectionContext.class); | ||
Set<Annotation> annotations = parser.copyAnnotations(); | ||
parser.forgetAnnotations(); | ||
return sectionContext.modify(sectionNode, triggerItems, | ||
() -> (Section) SkriptParser.parse(expr, (Iterator) Skript.getSections().iterator(), defaultError)); | ||
() -> { | ||
ParserInstance local = ParserInstance.get(); | ||
local.forgetAnnotations(); | ||
local.replaceAnnotations(annotations); | ||
return (Section) SkriptParser.parse(expr, (Iterator) Skript.getSections().iterator(), defaultError); | ||
}); | ||
} | ||
|
||
static { | ||
|
@@ -173,6 +185,7 @@ protected static class SectionContext extends ParserInstance.Data { | |
|
||
protected SectionNode sectionNode; | ||
protected List<TriggerItem> triggerItems; | ||
protected @Nullable Collection<Annotation> annotations; | ||
protected @Nullable Debuggable owner; | ||
|
||
public SectionContext(ParserInstance parserInstance) { | ||
|
@@ -244,6 +257,27 @@ public boolean claimed() { | |
return owner != null; | ||
} | ||
|
||
/** | ||
* Sets the annotation container for this section. | ||
* | ||
* @param annotations The annotations container | ||
*/ | ||
public void setAnnotations(@Nullable Collection<Annotation> annotations) { | ||
this.annotations = annotations; | ||
} | ||
|
||
/** | ||
* Returns the annotations visible to the section header (the line that it started from). | ||
* The collection may be empty if no annotations were placed before the header line. | ||
* | ||
* @return The annotation applied to the current section | ||
*/ | ||
public @NotNull Collection<Annotation> getAnnotations() { | ||
if (annotations == null) | ||
return Collections.emptySet(); | ||
return annotations; | ||
} | ||
|
||
} | ||
|
||
@Override | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.