Java/Janino editor fixes and improvements:

- Fixed clicking and adding hooks not properly resolving added types like INpcEvent.CollideEvent

- Imported wildcards of all available events in the given context.
  i.e ScriptContext NPC has INpcEvent,IProjectileEvent,IAbilityEvent,etc.. as available wildcard imports

- Added nested types to autocomplete of outer classes so INpcEvent. shows InitEvent, UpdateEvent, InteractEvent,etc...

- Added wildcard imports to autocomplete
This commit is contained in:
bigguy345 committed 2026-03-04 15:08:30 +02:00
1 parent 784fb8da65
commit 24af603ce1
4 files changed
+89 -20

No files matched your search

@@ -284,22 +284,7 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
activeArea.setScriptContext(getScriptContext());
activeArea.enableCodeHighlighting();
// For JaninoScripts, add implicit imports (default imports + hook signature types)
// These allow the syntax highlighter to resolve types without explicit import statements
if (container instanceof JaninoScript) {
JaninoScript<?> janinoScript = (JaninoScript<?>) container;
// Add default imports (e.g., noppes.npcs.api.*, noppes.npcs.api.entity.*, etc.)
activeArea.addImplicitImports(janinoScript.getDefaultImports());
// Add hook types from signatures (parameters + return types)
// e.g., INpcEvent.InitEvent, Color, String, IOverlayContext, etc.
Set<String> hookTypes = janinoScript.getHookTypes();
activeArea.addImplicitImports(hookTypes.toArray(new String[0]));
// Format to update which implicit imports are actually used
activeArea.formatCodeText();
}
updateScriptDocumentImports(activeArea, container);
// Setup fullscreen key binding
GuiScriptTextArea.KEYS.FULLSCREEN.setTask(e -> {
@@ -343,7 +328,6 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
this.addScroll(scroll);
}
}
/**
* Initialize the settings tab layout (console view).
*/
@@ -433,11 +417,40 @@ public class GuiScriptInterface extends GuiNPCInterface implements GuiYesNoCallb
this.getTextField(2).setText(this.getTextField(2).getText() + addString);
previousHookClicked = "";
updateScriptDocumentImports(getActiveScriptArea(), container);
} else {
previousHookClicked = hook;
}
}
public void updateScriptDocumentImports(GuiScriptTextArea activeArea, IScriptUnit container) {
// For JaninoScripts, add implicit imports (default imports + hook signature types)
// These allow the syntax highlighter to resolve types without explicit import statements
if (container instanceof JaninoScript) {
JaninoScript<?> janinoScript = (JaninoScript<?>) container;
ScriptContext ctx = getScriptContext();
// Add default imports (e.g., noppes.npcs.api.*, noppes.npcs.api.entity.*, etc.)
activeArea.addImplicitImports(janinoScript.getDefaultImports());
// Add hook types from signatures (parameters + return types)
// e.g., INpcEvent.InitEvent, Color, String, IOverlayContext, etc.
Set<String> hookTypes = janinoScript.getHookTypes();
activeArea.addImplicitImports(hookTypes.toArray(new String[0]));
// Add wildcard imports for each event class in the script context: INpcEvent.*, IProjectileEvent.*
// so nested types like INpcEvent.CollideEvent resolve without explicit import
Set<String> wildcardImports = new HashSet<>();
for (String fqn : ctx.getNamespaceFQNs())
wildcardImports.add(fqn + ".*");
activeArea.addImplicitImports(wildcardImports.toArray(new String[0]));
// Format to update which implicit imports are actually used
activeArea.formatCodeText();
}
}
@Override
public void unFocused(GuiNpcTextField textfield) {
@@ -152,6 +152,25 @@ public class JavaAutocompleteProvider implements AutocompleteProvider {
}
}
// Add nested types (inner classes/interfaces) in static context
if (isStaticContext && !context.methodsOnly) {
try {
for (Class<?> nested : clazz.getDeclaredClasses()) {
if (!Modifier.isPublic(nested.getModifiers())) {
continue;
}
// Convert $ notation to dot notation for resolution
String nestedFullName = nested.getName().replace('$', '.');
TypeInfo nestedType = TypeResolver.getInstance().resolveFullName(nestedFullName);
if (nestedType != null && nestedType.isResolved()) {
items.add(AutocompleteItem.fromType(nestedType));
}
}
} catch (SecurityException ignored) {
// Some runtimes may restrict getDeclaredClasses()
}
}
}
/**
@@ -225,6 +244,13 @@ public class JavaAutocompleteProvider implements AutocompleteProvider {
for (EnumConstantInfo enumConstant : scriptType.getEnumConstants().values()) {
items.add(AutocompleteItem.fromField(enumConstant.getFieldInfo()));
}
// Add nested types (inner classes/interfaces) in static context
if (isStaticContext) {
for (ScriptTypeInfo inner : scriptType.getInnerClasses()) {
items.add(AutocompleteItem.fromType(inner));
}
}
}
// Add parent class members
@@ -8072,8 +8072,8 @@ public class ScriptDocument {
* Get all imported types that have been resolved.
* Used by autocomplete to show available types.
*/
public List<TypeInfo> getImportedTypes() {
List<TypeInfo> types = new ArrayList<>();
public Set<TypeInfo> getImportedTypes() {
Set<TypeInfo> types = new HashSet<>();
for (ImportData imp : imports) {
if (!imp.isWildcard() && imp.isResolved()) {
TypeInfo type = typeResolver.resolveSimpleName(imp.getSimpleName(), importsBySimpleName, wildcardPackages);
@@ -8082,6 +8082,16 @@ public class ScriptDocument {
}
}
}
// For class-level wildcard imports (e.g. INpcEvent.*), add the outer class itself.
// Package-level wildcards (e.g. noppes.npcs.api.event.*) are skipped — those are
// already covered by addUnimportedClassSuggestions via ClassIndex.
for (String pkg : wildcardPackages) {
TypeInfo outerType = typeResolver.resolveFullName(pkg);
if (outerType != null && outerType.isResolved()) {
// resolveFullName succeeded → pkg is a class, not a package
types.add(outerType);
}
}
return types;
}
@@ -136,6 +136,12 @@ public class ScriptContext {
*/
private final List<String> namespaces;
/**
* Fully qualified names of the event interface classes this context supports.
* Only populated when registered via the Class<?> overload.
*/
private final List<String> namespaceFQNs = new ArrayList<>();
// ==================== CONSTRUCTOR ====================
private ScriptContext(String id, String hookContext, String... namespaces) {
@@ -155,6 +161,16 @@ public class ScriptContext {
return Collections.unmodifiableList(namespaces);
}
/**
* Get the fully qualified names of event interface classes this context supports.
* Only populated for contexts registered with Class<?> references.
*
* @return Unmodifiable list of FQN strings (e.g., "noppes.npcs.api.event.INpcEvent")
*/
public List<String> getNamespaceFQNs() {
return Collections.unmodifiableList(namespaceFQNs);
}
/**
* Check if this context includes a specific namespace.
*
@@ -220,7 +236,11 @@ public class ScriptContext {
for (int i = 0; i < eventClasses.length; i++) {
namespaces[i] = eventClasses[i].getSimpleName();
}
return register(id, hookContext, namespaces);
ScriptContext context = register(id, hookContext, namespaces);
for (Class<?> clazz : eventClasses) {
context.namespaceFQNs.add(clazz.getName().replace('$', '.'));
}
return context;
}
/**