Skip to content
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

Switch to tab indentation #129

Open
wants to merge 3 commits into
base: 2.x
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
root = true

[*]
indent_style = space
indent_size = 2
indent_style = tab
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
Expand Down
46 changes: 23 additions & 23 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,35 @@ version '2.5.1'
apply plugin: 'java'

repositories {
mavenCentral()
maven {
url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
}
maven {
url 'https://oss.sonatype.org/content/groups/public/'
}
maven {
url 'https://repo.destroystokyo.com/repository/maven-public/'
}
maven {
url 'https://repo.skriptlang.org/releases'
}
mavenCentral()
maven {
url 'https://hub.spigotmc.org/nexus/content/repositories/snapshots/'
}
maven {
url 'https://oss.sonatype.org/content/groups/public/'
}
maven {
url 'https://repo.destroystokyo.com/repository/maven-public/'
}
maven {
url 'https://repo.skriptlang.org/releases'
}
}

processResources {
filter ReplaceTokens, tokens: [
"version": version
]
filter ReplaceTokens, tokens: [
"version": version
]
}

compileJava {
sourceCompatibility = '11'
targetCompatibility = '11'
options.encoding = 'UTF-8'
compileJava {
sourceCompatibility = '11'
targetCompatibility = '11'
options.encoding = 'UTF-8'
}

dependencies {
implementation 'org.spigotmc:spigot-api:1.13.2-R0.1-SNAPSHOT'
implementation 'com.github.SkriptLang:Skript:2.9.0'
implementation 'org.eclipse.jdt:org.eclipse.jdt.annotation:1.1.0'
implementation 'org.spigotmc:spigot-api:1.13.2-R0.1-SNAPSHOT'
implementation 'com.github.SkriptLang:Skript:2.9.0'
implementation 'org.eclipse.jdt:org.eclipse.jdt.annotation:1.1.0'
}
294 changes: 147 additions & 147 deletions src/main/java/com/btk5h/skriptmirror/Descriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,152 +20,152 @@
*/
public final class Descriptor {

/**
* A regex string for a list of array symbols, e.g. {@code [][][]}.
*/
private static final String PACKAGE_ARRAY = SkriptMirrorUtil.PACKAGE + "(?:\\[])*";

/**
* A regex {@link Pattern} for a single array symbol, matches only {@code []}.
*/
private static final Pattern PACKAGE_ARRAY_SINGLE = Pattern.compile("\\[]");

/**
* A regex {@link Pattern} for a {@link Descriptor}.
*/
private static final Pattern DESCRIPTOR =
Pattern.compile("" +
"(?:\\[(" + SkriptMirrorUtil.PACKAGE + ")])?" +
"(" + SkriptMirrorUtil.IDENTIFIER + ")" +
"(?:\\[((?:" + PACKAGE_ARRAY + "\\s*,\\s*)*(?:" + PACKAGE_ARRAY + "))])?"
);

private final Class<?> javaClass;
private final String name;
private final Class<?>[] parameterTypes;

public Descriptor(Class<?> javaClass, String name, Class<?>[] parameterTypes) {
this.javaClass = javaClass;
this.name = name;
this.parameterTypes = parameterTypes;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Descriptor that = (Descriptor) o;
return Objects.equals(javaClass, that.javaClass) &&
Objects.equals(name, that.name);
}

@Override
public int hashCode() {
return Objects.hash(javaClass, name);
}

public Class<?> getJavaClass() {
return javaClass;
}

public String getName() {
return name;
}

public Class<?>[] getParameterTypes() {
return parameterTypes;
}

@Override
public String toString() {
return toString(false);
}

public String toString(boolean isStatic) {
return javaClass == null ? "(unspecified)" : SkriptMirrorUtil.getDebugName(javaClass)
+ (isStatic ? "." : "#")
+ name;
}

/**
* Returns a new descriptor with a new {@link #javaClass}.
* If this Descriptors {@link #javaClass} is not null, it will instead return itself.
*/
public Descriptor orDefaultClass(Class<?> cls) {
if (javaClass != null) {
return this;
}

return new Descriptor(cls, name, parameterTypes);
}

/**
* Parses the given {@link String} as a {@link Descriptor}. The script parameter is to get the imports.
*/
public static Descriptor parse(String desc, Script script) throws ImportNotFoundException {
Matcher m = DESCRIPTOR.matcher(desc);

if (m.matches()) {
String cls = m.group(1);
Class<?> javaClass = cls == null ? null : lookupClass(script, cls);

String name = m.group(2);

String args = m.group(3);
Class<?>[] parameterTypes = args == null ? null : parseParams(args, script);

return new Descriptor(javaClass, name, parameterTypes);
}

return null;
}

/**
* Parses a list of imported names, returning a class array containing the classes in the given string.
*/
private static Class<?>[] parseParams(String args, Script script) throws ImportNotFoundException {
String[] rawClasses = args.split(",");

Class<?>[] parsedClasses = new Class<?>[rawClasses.length];

for (int i = 0; i < rawClasses.length; i++) {
String userType = rawClasses[i].trim();

// Calculate array depth with regex
Matcher arrayDepthMatcher = PACKAGE_ARRAY_SINGLE.matcher(userType);
int arrayDepth = 0;
while (arrayDepthMatcher.find()) {
arrayDepth++;
}

// Remove array's square brackets
userType = userType.substring(0, userType.length() - (2 * arrayDepth));

Class<?> cls;
if (JavaUtil.PRIMITIVE_CLASS_NAMES.containsKey(userType)) {
cls = JavaUtil.PRIMITIVE_CLASS_NAMES.get(userType);
} else {
cls = lookupClass(script, userType);
}

// Convert class to array class with calculated depth
cls = JavaUtil.getArrayClass(cls, arrayDepth);

parsedClasses[i] = cls;
}

return parsedClasses;
}

/**
* Looks up a class from its imported name in the given file.
*/
private static Class<?> lookupClass(Script script, String userType) throws ImportNotFoundException {
JavaType customImport = StructImport.lookup(script, userType);
if (customImport == null)
throw new ImportNotFoundException(userType);

return customImport.getJavaClass();
}
/**
* A regex string for a list of array symbols, e.g. {@code [][][]}.
*/
private static final String PACKAGE_ARRAY = SkriptMirrorUtil.PACKAGE + "(?:\\[])*";

/**
* A regex {@link Pattern} for a single array symbol, matches only {@code []}.
*/
private static final Pattern PACKAGE_ARRAY_SINGLE = Pattern.compile("\\[]");

/**
* A regex {@link Pattern} for a {@link Descriptor}.
*/
private static final Pattern DESCRIPTOR =
Pattern.compile("" +
"(?:\\[(" + SkriptMirrorUtil.PACKAGE + ")])?" +
"(" + SkriptMirrorUtil.IDENTIFIER + ")" +
"(?:\\[((?:" + PACKAGE_ARRAY + "\\s*,\\s*)*(?:" + PACKAGE_ARRAY + "))])?"
);

private final Class<?> javaClass;
private final String name;
private final Class<?>[] parameterTypes;

public Descriptor(Class<?> javaClass, String name, Class<?>[] parameterTypes) {
this.javaClass = javaClass;
this.name = name;
this.parameterTypes = parameterTypes;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Descriptor that = (Descriptor) o;
return Objects.equals(javaClass, that.javaClass) &&
Objects.equals(name, that.name);
}

@Override
public int hashCode() {
return Objects.hash(javaClass, name);
}

public Class<?> getJavaClass() {
return javaClass;
}

public String getName() {
return name;
}

public Class<?>[] getParameterTypes() {
return parameterTypes;
}

@Override
public String toString() {
return toString(false);
}

public String toString(boolean isStatic) {
return javaClass == null ? "(unspecified)" : SkriptMirrorUtil.getDebugName(javaClass)
+ (isStatic ? "." : "#")
+ name;
}

/**
* Returns a new descriptor with a new {@link #javaClass}.
* If this Descriptors {@link #javaClass} is not null, it will instead return itself.
*/
public Descriptor orDefaultClass(Class<?> cls) {
if (javaClass != null) {
return this;
}

return new Descriptor(cls, name, parameterTypes);
}

/**
* Parses the given {@link String} as a {@link Descriptor}. The script parameter is to get the imports.
*/
public static Descriptor parse(String desc, Script script) throws ImportNotFoundException {
Matcher m = DESCRIPTOR.matcher(desc);

if (m.matches()) {
String cls = m.group(1);
Class<?> javaClass = cls == null ? null : lookupClass(script, cls);

String name = m.group(2);

String args = m.group(3);
Class<?>[] parameterTypes = args == null ? null : parseParams(args, script);

return new Descriptor(javaClass, name, parameterTypes);
}

return null;
}

/**
* Parses a list of imported names, returning a class array containing the classes in the given string.
*/
private static Class<?>[] parseParams(String args, Script script) throws ImportNotFoundException {
String[] rawClasses = args.split(",");

Class<?>[] parsedClasses = new Class<?>[rawClasses.length];

for (int i = 0; i < rawClasses.length; i++) {
String userType = rawClasses[i].trim();

// Calculate array depth with regex
Matcher arrayDepthMatcher = PACKAGE_ARRAY_SINGLE.matcher(userType);
int arrayDepth = 0;
while (arrayDepthMatcher.find()) {
arrayDepth++;
}

// Remove array's square brackets
userType = userType.substring(0, userType.length() - (2 * arrayDepth));

Class<?> cls;
if (JavaUtil.PRIMITIVE_CLASS_NAMES.containsKey(userType)) {
cls = JavaUtil.PRIMITIVE_CLASS_NAMES.get(userType);
} else {
cls = lookupClass(script, userType);
}

// Convert class to array class with calculated depth
cls = JavaUtil.getArrayClass(cls, arrayDepth);

parsedClasses[i] = cls;
}

return parsedClasses;
}

/**
* Looks up a class from its imported name in the given file.
*/
private static Class<?> lookupClass(Script script, String userType) throws ImportNotFoundException {
JavaType customImport = StructImport.lookup(script, userType);
if (customImport == null)
throw new ImportNotFoundException(userType);

return customImport.getJavaClass();
}

}
Loading
Loading