-
-
Notifications
You must be signed in to change notification settings - Fork 370
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
🚀 Add Change Modes
to expressions in docs
#5687
Closed
Closed
Changes from all commits
Commits
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 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,40 @@ | ||
/** | ||
* This file is part of Skript. | ||
* | ||
* Skript is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Skript is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with Skript. If not, see <http://www.gnu.org/licenses/>. | ||
* | ||
* Copyright Peter Güttinger, SkriptLang team and contributors | ||
*/ | ||
package ch.njol.skript.doc; | ||
|
||
import ch.njol.skript.classes.Changer; | ||
|
||
import java.lang.annotation.Documented; | ||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Provides a list of accepted {@link ch.njol.skript.classes.Changer.ChangeMode}s to be used in documentation for annotated | ||
* elements. | ||
*/ | ||
@Target(ElementType.TYPE) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
@Documented | ||
public @interface AcceptedChangeModes { | ||
|
||
public Changer.ChangeMode[] value(); | ||
|
||
} |
This file contains 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 |
---|---|---|
|
@@ -20,6 +20,7 @@ | |
|
||
import ch.njol.skript.Skript; | ||
import ch.njol.skript.SkriptAPIException; | ||
import ch.njol.skript.classes.Changer; | ||
import ch.njol.skript.classes.ClassInfo; | ||
import ch.njol.skript.lang.Condition; | ||
import ch.njol.skript.lang.Effect; | ||
|
@@ -31,6 +32,7 @@ | |
import ch.njol.skript.lang.function.Functions; | ||
import ch.njol.skript.lang.function.JavaFunction; | ||
import ch.njol.skript.lang.function.Parameter; | ||
import ch.njol.skript.lang.util.SimpleExpression; | ||
import ch.njol.skript.registrations.Classes; | ||
import com.google.common.base.Joiner; | ||
import com.google.common.collect.Lists; | ||
|
@@ -46,6 +48,7 @@ | |
import java.nio.charset.StandardCharsets; | ||
import java.text.SimpleDateFormat; | ||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.Comparator; | ||
import java.util.Date; | ||
import java.util.Iterator; | ||
|
@@ -64,6 +67,8 @@ public class HTMLGenerator { | |
private static final String SKRIPT_VERSION = Skript.getVersion().toString().replaceAll("-(dev|alpha|beta)\\d*", ""); // Filter branches | ||
private static final Pattern NEW_TAG_PATTERN = Pattern.compile(SKRIPT_VERSION + "(?!\\.)"); // (?!\\.) to avoid matching 2.6 in 2.6.1 etc. | ||
private static final Pattern RETURN_TYPE_LINK_PATTERN = Pattern.compile("( ?href=\"(classes\\.html|)#|)\\$\\{element\\.return-type-linkcheck}"); | ||
private static final Pattern DOCS_PASSED_FOLDERS_PATTERN = Pattern.compile("css|js|assets"); | ||
private static final Pattern DOCS_ACCEPTED_FILES_PATTERN = Pattern.compile("(?i)(.*)\\.(html?|js|css|json)"); | ||
|
||
private final File template; | ||
private final File output; | ||
|
@@ -210,9 +215,9 @@ public int compare(@Nullable JavaFunction<?> o1, @Nullable JavaFunction<?> o2) { | |
*/ | ||
@SuppressWarnings("unchecked") | ||
public void generate() { | ||
for (File f : template.listFiles()) { | ||
if (f.getName().matches("css|js|assets")) { // Copy CSS/JS/Assets folders | ||
String slashName = "/" + f.getName(); | ||
for (File file : template.listFiles()) { | ||
if (DOCS_PASSED_FOLDERS_PATTERN.matcher(file.getName()).matches()) { // Copy CSS/JS/Assets folders | ||
String slashName = "/" + file.getName(); | ||
File fileTo = new File(output + slashName); | ||
fileTo.mkdirs(); | ||
for (File filesInside : new File(template + slashName).listFiles()) { | ||
|
@@ -223,7 +228,7 @@ public void generate() { | |
writeFile(new File(fileTo + "/" + filesInside.getName()), readFile(filesInside)); | ||
} | ||
|
||
else if (!filesInside.getName().matches("(?i)(.*)\\.(html?|js|css|json)")) { | ||
else if (!DOCS_ACCEPTED_FILES_PATTERN.matcher(filesInside.getName()).matches()) { | ||
try { | ||
Files.copy(filesInside, new File(fileTo + "/" + filesInside.getName())); | ||
} catch (IOException e) { | ||
|
@@ -233,23 +238,23 @@ else if (!filesInside.getName().matches("(?i)(.*)\\.(html?|js|css|json)")) { | |
} | ||
} | ||
continue; | ||
} else if (f.isDirectory()) // Ignore other directories | ||
} else if (file.isDirectory()) // Ignore other directories | ||
continue; | ||
if (f.getName().endsWith("template.html") || f.getName().endsWith(".md")) | ||
if (file.getName().endsWith("template.html") || file.getName().endsWith(".md")) | ||
continue; // Ignore skeleton and README | ||
|
||
Skript.info("Creating documentation for " + f.getName()); | ||
Skript.info("Creating documentation for " + file.getName()); | ||
|
||
String content = readFile(f); | ||
String content = readFile(file); | ||
String page; | ||
if (f.getName().endsWith(".html")) | ||
if (file.getName().endsWith(".html")) | ||
page = skeleton.replace("${content}", content); // Content to inside skeleton | ||
else // Not HTML, so don't even try to use template.html | ||
page = content; | ||
|
||
page = page.replace("${skript.version}", Skript.getVersion().toString()); // Skript version | ||
page = page.replace("${skript.build.date}", new SimpleDateFormat("dd/MM/yyyy").format(new Date())); // Build date | ||
page = page.replace("${pagename}", f.getName().replace(".html", "")); | ||
page = page.replace("${pagename}", file.getName().replace(".html", "")); | ||
|
||
List<String> replace = Lists.newArrayList(); | ||
int include = page.indexOf("${include"); // Single file includes | ||
|
@@ -375,7 +380,7 @@ else if (!filesInside.getName().matches("(?i)(.*)\\.(html?|js|css|json)")) { | |
generate = page.indexOf("${generate", nextBracket); | ||
} | ||
|
||
String name = f.getName(); | ||
String name = file.getName(); | ||
if (name.endsWith(".html")) { // Fix some stuff specially for HTML | ||
page = page.replace("\t", " "); // Tab to 4 non-collapsible spaces | ||
assert page != null; | ||
|
@@ -493,12 +498,12 @@ private String generateAnnotated(String descTemp, SyntaxElementInfo<?> info, @Nu | |
} | ||
desc = desc.replace("${element.events}", Joiner.on(", ").join(eventLinks)); | ||
} | ||
desc = desc.replace("${element.events-safe}", events == null ? "" : Joiner.on(", ").join((events != null ? events.value() : null))); | ||
desc = desc.replace("${element.events-safe}", events == null ? "" : Joiner.on(", ").join(events.value())); | ||
|
||
// RequiredPlugins | ||
RequiredPlugins plugins = c.getAnnotation(RequiredPlugins.class); | ||
desc = handleIf(desc, "${if required-plugins}", plugins != null); | ||
desc = desc.replace("${element.required-plugins}", plugins == null ? "" : Joiner.on(", ").join((plugins != null ? plugins.value() : null))); | ||
desc = desc.replace("${element.required-plugins}", plugins == null ? "" : Joiner.on(", ").join(plugins.value())); | ||
|
||
// Return Type | ||
ClassInfo<?> returnType = info instanceof ExpressionInfo ? Classes.getSuperClassInfo(((ExpressionInfo<?,?>) info).getReturnType()) : null; | ||
|
@@ -511,6 +516,25 @@ private String generateAnnotated(String descTemp, SyntaxElementInfo<?> info, @Nu | |
// desc = desc.replace("${element.by-addon}", addon); | ||
desc = handleIf(desc, "${if by-addon}", false); | ||
|
||
// Accepted Changers | ||
// TODO grab the accepted data type as well (find a way to include that in annotation as well) | ||
if (info instanceof ExpressionInfo<?,?>) { | ||
List<Changer.ChangeMode> acceptedChangeModes = new ArrayList<>(4); // 4 is common, SET/ADD/REMOVE/DELETE | ||
AcceptedChangeModes annotation = c.getAnnotation(AcceptedChangeModes.class); | ||
if (annotation != null) { | ||
acceptedChangeModes = Arrays.stream(annotation.value()).toList(); | ||
} else { | ||
try { | ||
acceptedChangeModes = ((SimpleExpression<?>) info.getElementClass().newInstance()).getAcceptedChangeModes().keySet().stream().toList(); | ||
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. i am a little weary of this given that expressions really should be initialized before having anything done on them - this may very well lead to inaccurate results |
||
} catch (Exception ignored) {} | ||
} | ||
|
||
desc = handleIf(desc, "${if accepted-change-modes}", acceptedChangeModes.size() > 0); | ||
desc = desc.replace("${element.accepted-change-modes}", acceptedChangeModes.size() == 0 ? "" :Joiner.on(", ").join(acceptedChangeModes.toArray())); | ||
} else { | ||
desc = handleIf(desc, "${if accepted-change-modes}", false); | ||
} | ||
|
||
// New Elements | ||
desc = handleIf(desc, "${if new-element}", NEW_TAG_PATTERN.matcher((since != null ? since.value() : "")).find()); | ||
|
||
|
@@ -625,12 +649,12 @@ private String generateEvent(String descTemp, SkriptEventInfo<?> info, @Nullable | |
} | ||
desc = desc.replace("${element.events}", Joiner.on(", ").join(eventLinks)); | ||
} | ||
desc = desc.replace("${element.events-safe}", events == null ? "" : Joiner.on(", ").join((events != null ? events.value() : null))); | ||
desc = desc.replace("${element.events-safe}", events == null ? "" : Joiner.on(", ").join(events.value())); | ||
|
||
// Required Plugins | ||
String[] requiredPlugins = info.getRequiredPlugins(); | ||
desc = handleIf(desc, "${if required-plugins}", requiredPlugins != null); | ||
desc = desc.replace("${element.required-plugins}", Joiner.on(", ").join(requiredPlugins == null ? new String[0] : requiredPlugins)); | ||
desc = desc.replace("${element.required-plugins}", requiredPlugins == null ? "" : Joiner.on(", ").join(requiredPlugins)); | ||
|
||
// New Elements | ||
desc = handleIf(desc, "${if new-element}", NEW_TAG_PATTERN.matcher(since).find()); | ||
|
@@ -729,12 +753,12 @@ private String generateClass(String descTemp, ClassInfo<?> info, @Nullable Strin | |
} | ||
desc = desc.replace("${element.events}", Joiner.on(", ").join(eventLinks)); | ||
} | ||
desc = desc.replace("${element.events-safe}", events == null ? "" : Joiner.on(", ").join((events != null ? events.value() : null))); | ||
desc = desc.replace("${element.events-safe}", events == null ? "" : Joiner.on(", ").join(events.value())); | ||
|
||
// Required Plugins | ||
String[] requiredPlugins = info.getRequiredPlugins(); | ||
desc = handleIf(desc, "${if required-plugins}", requiredPlugins != null); | ||
desc = desc.replace("${element.required-plugins}", Joiner.on(", ").join(requiredPlugins == null ? new String[0] : requiredPlugins)); | ||
desc = desc.replace("${element.required-plugins}", requiredPlugins == null ? "" : Joiner.on(", ").join(requiredPlugins)); | ||
|
||
// New Elements | ||
desc = handleIf(desc, "${if new-element}", NEW_TAG_PATTERN.matcher(since).find()); | ||
|
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you think about just taking in a string - might give developers more freedom for more complicated cases
for example, let's say we're annotating ExprName
@AcceptedChangeModes("set (only for display/tablist names)")
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer this approach over current solution for freedom and ease of implementation however, this means we have to add this annotation to all current expressions manually which I am okay with but just to keep in mind.