mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-20 01:49:40 +00:00
Add ECMAScript 5.1 global imports like Math/JSON/Date/Number
This commit is contained in:
1 parent
789c7f5317
commit
60fc7ec252
18 files changed
+1213
-18
No files matched your search
+8
@@ -217,6 +217,14 @@ public class JSAutocompleteProvider extends JavaAutocompleteProvider {
|
||||
|
||||
items.add(AutocompleteItem.fromField(fieldInfo));
|
||||
}
|
||||
|
||||
// Add global imports in JSTypeRegistry.globalEngineImports
|
||||
for (String importName : registry.getGlobalEngineImports().keySet()) {
|
||||
TypeInfo jsTypeInfo = document.resolveType(registry.getGlobalImportType(importName));
|
||||
if (jsTypeInfo != null) {
|
||||
items.add(AutocompleteItem.fromType(jsTypeInfo));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected UsageTracker getUsageTracker() {
|
||||
|
||||
@@ -7134,13 +7134,22 @@ public class ScriptDocument {
|
||||
continue;
|
||||
|
||||
// For JavaScript, first check synthetic types (Nashorn built-ins like Java, print, etc.)
|
||||
if (isJavaScript() && typeResolver.isSyntheticType(className)) {
|
||||
SyntheticType syntheticType = typeResolver.getSyntheticType(className);
|
||||
// Mark as IMPORTED_CLASS since it's a type reference
|
||||
// Pass the TypeInfo (which the hover system can display)
|
||||
marks.add(new ScriptLine.Mark(m.start(1), m.end(1), TokenType.IMPORTED_CLASS,
|
||||
syntheticType.getTypeInfo()));
|
||||
continue;
|
||||
if (isJavaScript()) {
|
||||
if (typeResolver.isSyntheticType(className)) {
|
||||
SyntheticType syntheticType = typeResolver.getSyntheticType(className);
|
||||
// Mark as IMPORTED_CLASS since it's a type reference
|
||||
// Pass the TypeInfo (which the hover system can display)
|
||||
marks.add(new ScriptLine.Mark(m.start(1), m.end(1), TokenType.IMPORTED_CLASS,
|
||||
syntheticType.getTypeInfo()));
|
||||
continue;
|
||||
}
|
||||
JSTypeRegistry jsRegistry = JSTypeRegistry.getInstance();
|
||||
if (jsRegistry.isGlobalImport(className)) {
|
||||
// Mark as IMPORTED_CLASS since it's a type reference
|
||||
TypeInfo globalType = typeResolver.resolveJSType(jsRegistry.getGlobalImportType(className));
|
||||
marks.add(new ScriptLine.Mark(m.start(1), m.end(1), TokenType.INTERFACE_DECL, globalType));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
// Try to resolve the class
|
||||
|
||||
+69
-1
@@ -52,6 +52,8 @@ public class JSTypeRegistry {
|
||||
// Global object instances: name -> type (e.g., "API" -> "AbstractNpcAPI")
|
||||
// These are treated as instance objects, not static classes
|
||||
private final Map<String, String> globalEngineObjects = new LinkedHashMap<>();
|
||||
// Math / Number / Json / etc. built-in globals from ECMAScript 5.1
|
||||
private final Map<String, String> globalEngineImports = new LinkedHashMap<>();
|
||||
|
||||
// Primitive types
|
||||
private static final Set<String> PRIMITIVES = new HashSet<>(Arrays.asList(
|
||||
@@ -119,6 +121,7 @@ public class JSTypeRegistry {
|
||||
|
||||
resolveInheritance();
|
||||
registerEngineGlobalObjects();
|
||||
registerES5Builtins();
|
||||
|
||||
initialized = true;
|
||||
System.out.println("[JSTypeRegistry] Loaded " + types.size() + " types, " + hooks.size() + " hooks from resources");
|
||||
@@ -449,6 +452,13 @@ public class JSTypeRegistry {
|
||||
return getType(resolved, visited);
|
||||
}
|
||||
|
||||
if (globalEngineImports.containsKey(baseName)){
|
||||
String resolved = globalEngineImports.get(baseName);
|
||||
if (types.containsKey(resolved)) {
|
||||
return types.get(resolved);
|
||||
}
|
||||
}
|
||||
|
||||
// Try simple name lookup (for types like "IEntity" without namespace)
|
||||
for (JSTypeInfo type : types.values()) {
|
||||
if (type.getSimpleName().equals(baseName)) {
|
||||
@@ -824,7 +834,64 @@ public class JSTypeRegistry {
|
||||
// Default: return the original name
|
||||
return concreteClassName;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Register ECMAScript 5.1 global built-in objects so that expressions like
|
||||
* `Math.floor(Math.random())`, `new Date()`, `Array.isArray(...)`, etc.
|
||||
* resolve properly with type checking and autocomplete.
|
||||
*
|
||||
* These globals are parsed from .d.ts patch files under `patches/ecmascript/`
|
||||
* and provide both static methods (e.g., Math.floor, Array.isArray, Object.keys)
|
||||
* and instance members (e.g., date.getTime(), array.length, string.charAt).
|
||||
*/
|
||||
private void registerES5Builtins() {
|
||||
// Singleton/namespace globals (accessed directly, not constructed)
|
||||
registerGlobalIfTypeExists("Math", "Math");
|
||||
registerGlobalIfTypeExists("JSON", "JSON");
|
||||
|
||||
// Constructor globals (can be called with `new` or used for static methods)
|
||||
registerGlobalIfTypeExists("Date", "Date");
|
||||
registerGlobalIfTypeExists("Array", "Array");
|
||||
registerGlobalIfTypeExists("Object", "Object");
|
||||
registerGlobalIfTypeExists("String", "String");
|
||||
registerGlobalIfTypeExists("Number", "Number");
|
||||
registerGlobalIfTypeExists("Boolean", "Boolean");
|
||||
registerGlobalIfTypeExists("RegExp", "RegExp");
|
||||
registerGlobalIfTypeExists("Error", "Error");
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a global object only if the target type is present in the registry.
|
||||
* Silently skips if the .d.ts patch hasn't been loaded yet (e.g. in unit tests).
|
||||
*/
|
||||
private void registerGlobalIfTypeExists(String globalName, String typeName) {
|
||||
if (types.containsKey(typeName)) {
|
||||
globalEngineImports.put(globalName, typeName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the type name for a global object.
|
||||
* @param name The global variable name
|
||||
* @return The type name, or null if not a registered global object
|
||||
*/
|
||||
public String getGlobalImportType(String name) {
|
||||
return globalEngineImports.get(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a name is a registered global object instance.
|
||||
*/
|
||||
public boolean isGlobalImport(String name) {
|
||||
return globalEngineImports.containsKey(name);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all registered global objects.
|
||||
*/
|
||||
public Map<String, String> getGlobalEngineImports() {
|
||||
return Collections.unmodifiableMap(globalEngineImports);
|
||||
}
|
||||
/**
|
||||
* Check if initialized.
|
||||
*/
|
||||
@@ -844,6 +911,7 @@ public class JSTypeRegistry {
|
||||
hooks.clear();
|
||||
contextHooks.clear();
|
||||
globalEngineObjects.clear();
|
||||
globalEngineImports.clear();
|
||||
DtsJavaBridge.clearCache();
|
||||
initialized = false;
|
||||
initializationAttempted = false;
|
||||
|
||||
+2
-2
@@ -41,10 +41,10 @@ public class TypeScriptDefinitionParser {
|
||||
"export\\s+type\\s+(\\w+)\\s*=\\s*([^;\\n]+);?");
|
||||
|
||||
private static final Pattern METHOD_PATTERN = Pattern.compile(
|
||||
"(?m)^\\s*(\\w+)\\s*\\((.*)\\)\\s*:\\s*([^;]+);", Pattern.MULTILINE);
|
||||
"(?m)^\\s*(\\w+)\\??\\s*\\((.*)\\)\\s*:\\s*([^;]+);", Pattern.MULTILINE);
|
||||
|
||||
private static final Pattern FIELD_PATTERN = Pattern.compile(
|
||||
"^\\s*(readonly\\s+)?(\\w+)\\s*:\\s*([^;]+);", Pattern.MULTILINE);
|
||||
"^\\s*(readonly\\s+)?(\\w+)\\??\\s*:\\s*([^;]+);", Pattern.MULTILINE);
|
||||
|
||||
private static final Pattern GLOBAL_FUNCTION_PATTERN = Pattern.compile(
|
||||
"function\\s+(\\w+)\\s*\\((.*)\\)\\s*:\\s*([^;]+);");
|
||||
|
||||
@@ -413,6 +413,13 @@ public class TypeResolver {
|
||||
SyntheticType syntheticType = getSyntheticType(baseName);
|
||||
return syntheticType.getTypeInfo();
|
||||
}
|
||||
|
||||
// Check JS type registry
|
||||
// Before primitive mapping, to allow .d.ts types to override primitive names for ECMAScript 5.1 globals (e.g., "String" in JS is not the same as "String" in Java))
|
||||
JSTypeInfo jsTypeInfo = getJSTypeRegistry().getType(baseName);
|
||||
if (jsTypeInfo != null) {
|
||||
return TypeInfo.fromJSTypeInfo(jsTypeInfo);
|
||||
}
|
||||
|
||||
// Check primitives and common types
|
||||
switch (baseName.toLowerCase()) {
|
||||
@@ -433,12 +440,6 @@ public class TypeResolver {
|
||||
return TypeInfo.ANY;
|
||||
}
|
||||
|
||||
// Check JS type registry
|
||||
JSTypeInfo jsTypeInfo = getJSTypeRegistry().getType(baseName);
|
||||
if (jsTypeInfo != null) {
|
||||
return TypeInfo.fromJSTypeInfo(jsTypeInfo);
|
||||
}
|
||||
|
||||
// Fall back to Java type resolution
|
||||
TypeInfo javaType = resolveFullName(baseName);
|
||||
if (javaType != null && javaType.isResolved()) {
|
||||
@@ -898,7 +899,7 @@ public class TypeResolver {
|
||||
|
||||
// Not a variable - check if it's a type name by trying to resolve as type
|
||||
TypeInfo typeCheck = document.resolveType(identifier);
|
||||
boolean isType = typeCheck != null && typeCheck.isResolved();
|
||||
boolean isType = typeCheck != null && typeCheck.isResolved() && !typeCheck.isJSType(); // JS types aren't static access in this context
|
||||
return isStaticAccess(typeCheck, isType);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user