Added DataScript global definitions i.e. npc, world, event to syntax highlighter as global vars

This commit is contained in:
bigguy345 committed 2026-01-26 03:49:38 +02:00
1 parent df0d46e63d
commit aae2fdb23a
5 files changed
+155 -24

No files matched your search

@@ -24,6 +24,7 @@ import noppes.npcs.client.gui.util.script.interpreter.type.synthetic.SyntheticFi
import noppes.npcs.client.gui.util.script.interpreter.type.synthetic.SyntheticMethod;
import noppes.npcs.client.gui.util.script.interpreter.type.synthetic.SyntheticType;
import noppes.npcs.constants.ScriptContext;
import noppes.npcs.controllers.data.DataScript;
import java.util.*;
import java.util.regex.Matcher;
@@ -130,6 +131,9 @@ public class ScriptDocument {
// Determines which hooks and event types are available
private ScriptContext scriptContext = ScriptContext.GLOBAL;
// Editor-injected globals (name -> type name) provided by the handler
private Map<String, String> editorGlobals = Collections.emptyMap();
// Implicit imports from JaninoScript default imports and hook parameter types
// These are types that should be resolved even without explicit import statements
private final Set<String> implicitImports = new HashSet<>();
@@ -198,6 +202,26 @@ public class ScriptDocument {
return scriptContext;
}
/**
* Set editor-injected globals (name -> type name).
* i.e. DataScript global definitions {@link DataScript#getEditorGlobals(String)}
* This is provided by the script handler at editor init time.
*/
public void setEditorGlobals(Map<String, String> globals) {
if (globals == null || globals.isEmpty()) {
this.editorGlobals = Collections.emptyMap();
return;
}
this.editorGlobals = new LinkedHashMap<>(globals);
}
/**
* Get editor-injected globals (name -> type name).
*/
public Map<String, String> getEditorGlobals() {
return Collections.unmodifiableMap(editorGlobals);
}
/**
* Add implicit imports that should be resolved without explicit import statements.
* Used for JaninoScript default imports and hook parameter types.
@@ -4688,6 +4712,16 @@ public class ScriptDocument {
continue;
}
}
// Mark DataScript global variable definitions
if (!editorGlobals.isEmpty() && editorGlobals.containsKey(name)) {
FieldInfo fieldInfo = resolveVariable(name, position);
if (fieldInfo != null) {
Object metadata = callInfo != null ? new FieldInfo.ArgInfo(fieldInfo, callInfo) : fieldInfo;
marks.add(new ScriptLine.Mark(m.start(1), m.end(1), TokenType.GLOBAL_FIELD, metadata));
continue;
}
}
}
// Skip uppercase if not a known field - type handling will deal with it
@@ -5664,6 +5698,16 @@ public class ScriptDocument {
return FieldInfo.globalField(name, typeInfo, -1);
}
}
// Check if is DataScript global variable definition
String editorGlobalType = editorGlobals.get(name);
if (editorGlobalType != null) {
TypeInfo typeInfo = resolveType(editorGlobalType);
if (typeInfo == null || !typeInfo.isResolved()) {
typeInfo = TypeInfo.ANY;
}
return FieldInfo.globalField(name, typeInfo, -1);
}
}
return null;
@@ -2,12 +2,10 @@ package noppes.npcs.client.gui.util.script.interpreter;
import noppes.npcs.client.ClientProxy;
import noppes.npcs.client.gui.util.script.JavaTextContainer;
import noppes.npcs.client.gui.util.script.interpreter.field.FieldInfo;
import noppes.npcs.client.gui.util.script.interpreter.method.MethodInfo;
import noppes.npcs.constants.ScriptContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
import java.util.Map;
/**
* New interpreter-based text container that replaces JavaTextContainer.
@@ -39,6 +37,9 @@ public class ScriptTextContainer extends JavaTextContainer {
/** The script context: NPC, PLAYER, BLOCK, ITEM, etc. */
private ScriptContext scriptContext = ScriptContext.GLOBAL;
/** Editor-injected globals (name -> type name) */
private Map<String, String> editorGlobals = Collections.emptyMap();
public ScriptTextContainer(String text) {
super(text);
if (USE_NEW_INTERPRETER) {
@@ -94,6 +95,16 @@ public class ScriptTextContainer extends JavaTextContainer {
return scriptContext;
}
/**
* Set editor-injected globals (name -> type name).
*/
public void setEditorGlobalsMap(java.util.Map<String, String> globals) {
this.editorGlobals = globals != null ? globals : java.util.Collections.emptyMap();
if (document != null) {
document.setEditorGlobals(this.editorGlobals);
}
}
/**
* Add implicit imports that should be resolved without explicit import statements.
* Used for JaninoScript default imports and hook parameter types.
@@ -143,6 +154,7 @@ public class ScriptTextContainer extends JavaTextContainer {
if (document == null) {
document = new ScriptDocument(this.text, this.language);
document.setScriptContext(this.scriptContext);
document.setEditorGlobals(this.editorGlobals);
}
init(width, height);