Added documentation for method/field declarations and displayed it in hover info

This commit is contained in:
bigguy345 committed 2025-12-27 14:51:00 +02:00
1 parent 4d670f31de
commit 448329386e
4 files changed
+204 -19

No files matched your search

@@ -17,42 +17,48 @@ public final class FieldInfo {
private final TypeInfo declaredType; // The declared type (e.g., String, List<Integer>)
private final int declarationOffset; // Where this field was declared in the source
private final boolean resolved;
private final String documentation; // Javadoc/comment documentation for this field
// For local/parameter fields, track the containing method
private final MethodInfo containingMethod;
private FieldInfo(String name, Scope scope, TypeInfo declaredType,
int declarationOffset, boolean resolved, MethodInfo containingMethod) {
int declarationOffset, boolean resolved, MethodInfo containingMethod, String documentation) {
this.name = name;
this.scope = scope;
this.declaredType = declaredType;
this.declarationOffset = declarationOffset;
this.resolved = resolved;
this.containingMethod = containingMethod;
this.documentation = documentation;
}
// Factory methods
public static FieldInfo globalField(String name, TypeInfo type, int declOffset) {
return new FieldInfo(name, Scope.GLOBAL, type, declOffset, type != null && type.isResolved(), null);
return new FieldInfo(name, Scope.GLOBAL, type, declOffset, type != null && type.isResolved(), null, null);
}
public static FieldInfo globalField(String name, TypeInfo type, int declOffset, String documentation) {
return new FieldInfo(name, Scope.GLOBAL, type, declOffset, type != null && type.isResolved(), null, documentation);
}
public static FieldInfo localField(String name, TypeInfo type, int declOffset, MethodInfo method) {
return new FieldInfo(name, Scope.LOCAL, type, declOffset, type != null && type.isResolved(), method);
return new FieldInfo(name, Scope.LOCAL, type, declOffset, type != null && type.isResolved(), method, null);
}
public static FieldInfo parameter(String name, TypeInfo type, int declOffset, MethodInfo method) {
return new FieldInfo(name, Scope.PARAMETER, type, declOffset, type != null && type.isResolved(), method);
return new FieldInfo(name, Scope.PARAMETER, type, declOffset, type != null && type.isResolved(), method, null);
}
public static FieldInfo unresolved(String name, Scope scope) {
return new FieldInfo(name, scope, null, -1, false, null);
return new FieldInfo(name, scope, null, -1, false, null, null);
}
/**
* Create a FieldInfo from reflection data for method parameters.
*/
public static FieldInfo reflectionParam(String name, TypeInfo type) {
return new FieldInfo(name, Scope.PARAMETER, type, -1, true, null);
return new FieldInfo(name, Scope.PARAMETER, type, -1, true, null, null);
}
/**
@@ -61,7 +67,7 @@ public final class FieldInfo {
public static FieldInfo fromReflection(java.lang.reflect.Field field, TypeInfo containingType) {
String name = field.getName();
TypeInfo type = TypeInfo.fromClass(field.getType());
return new FieldInfo(name, Scope.GLOBAL, type, -1, true, null);
return new FieldInfo(name, Scope.GLOBAL, type, -1, true, null, null);
}
// Getters
@@ -72,6 +78,7 @@ public final class FieldInfo {
public int getDeclarationOffset() { return declarationOffset; }
public boolean isResolved() { return resolved; }
public MethodInfo getContainingMethod() { return containingMethod; }
public String getDocumentation() { return documentation; }
public boolean isGlobal() { return scope == Scope.GLOBAL; }
public boolean isLocal() { return scope == Scope.LOCAL; }
@@ -21,11 +21,12 @@ public final class MethodInfo {
private final boolean resolved;
private final boolean isDeclaration; // true if this is a declaration, false if it's a call
private final boolean isStatic; // true if this is a static method
private final String documentation; // Javadoc/comment documentation for this method
private MethodInfo(String name, TypeInfo returnType, TypeInfo containingType,
List<FieldInfo> parameters, int declarationOffset,
int bodyStart, int bodyEnd, boolean resolved, boolean isDeclaration,
boolean isStatic) {
boolean isStatic, String documentation) {
this.name = name;
this.returnType = returnType;
this.containingType = containingType;
@@ -36,17 +37,23 @@ public final class MethodInfo {
this.resolved = resolved;
this.isDeclaration = isDeclaration;
this.isStatic = isStatic;
this.documentation = documentation;
}
// Factory methods
public static MethodInfo declaration(String name, TypeInfo returnType, List<FieldInfo> params,
int declOffset, int bodyStart, int bodyEnd) {
return new MethodInfo(name, returnType, null, params, declOffset, bodyStart, bodyEnd, true, true, false);
return new MethodInfo(name, returnType, null, params, declOffset, bodyStart, bodyEnd, true, true, false, null);
}
public static MethodInfo declaration(String name, TypeInfo returnType, List<FieldInfo> params,
int declOffset, int bodyStart, int bodyEnd, boolean isStatic) {
return new MethodInfo(name, returnType, null, params, declOffset, bodyStart, bodyEnd, true, true, isStatic);
return new MethodInfo(name, returnType, null, params, declOffset, bodyStart, bodyEnd, true, true, isStatic, null);
}
public static MethodInfo declaration(String name, TypeInfo returnType, List<FieldInfo> params,
int declOffset, int bodyStart, int bodyEnd, boolean isStatic, String documentation) {
return new MethodInfo(name, returnType, null, params, declOffset, bodyStart, bodyEnd, true, true, isStatic, documentation);
}
public static MethodInfo call(String name, TypeInfo containingType, int paramCount) {
@@ -56,7 +63,7 @@ public final class MethodInfo {
for (int i = 0; i < paramCount; i++) {
params.add(FieldInfo.unresolved("arg" + i, FieldInfo.Scope.PARAMETER));
}
return new MethodInfo(name, null, containingType, params, -1, -1, -1, resolved, false, false);
return new MethodInfo(name, null, containingType, params, -1, -1, -1, resolved, false, false, null);
}
public static MethodInfo unresolvedCall(String name, int paramCount) {
@@ -64,7 +71,7 @@ public final class MethodInfo {
for (int i = 0; i < paramCount; i++) {
params.add(FieldInfo.unresolved("arg" + i, FieldInfo.Scope.PARAMETER));
}
return new MethodInfo(name, null, null, params, -1, -1, -1, false, false, false);
return new MethodInfo(name, null, null, params, -1, -1, -1, false, false, false, null);
}
/**
@@ -83,7 +90,7 @@ public final class MethodInfo {
params.add(FieldInfo.reflectionParam("arg" + i, paramType));
}
return new MethodInfo(name, returnType, containingType, params, -1, -1, -1, true, false, isStatic);
return new MethodInfo(name, returnType, containingType, params, -1, -1, -1, true, false, isStatic, null);
}
// Getters
@@ -99,6 +106,7 @@ public final class MethodInfo {
public boolean isDeclaration() { return isDeclaration; }
public boolean isCall() { return !isDeclaration; }
public boolean isStatic() { return isStatic; }
public String getDocumentation() { return documentation; }
/**
* Check if a position is inside this method's body.
@@ -393,8 +393,11 @@ public class ScriptDocument {
continue;
}
// Extract documentation before this field
String documentation = extractDocumentationBefore(absPos);
TypeInfo fieldType = resolveType(typeName);
FieldInfo fieldInfo = FieldInfo.globalField(fieldName, fieldType, absPos);
FieldInfo fieldInfo = FieldInfo.globalField(fieldName, fieldType, absPos, documentation);
scriptType.addField(fieldInfo);
}
@@ -420,12 +423,15 @@ public class ScriptDocument {
int methodBodyEnd = findMatchingBrace(methodBodyStart);
if (methodBodyEnd < 0) methodBodyEnd = bodyEnd;
// Extract documentation before this method
String documentation = extractDocumentationBefore(absPos);
TypeInfo returnType = resolveType(returnTypeName);
List<FieldInfo> params = parseParametersWithPositions(paramList,
bodyStart + 1 + mm.start(3));
MethodInfo methodInfo = MethodInfo.declaration(
methodName, returnType, params, absPos, methodBodyStart, methodBodyEnd);
methodName, returnType, params, absPos, methodBodyStart, methodBodyEnd, false, documentation);
scriptType.addMethod(methodInfo);
}
}
@@ -472,6 +478,9 @@ public class ScriptDocument {
if (bodyEnd < 0)
bodyEnd = text.length();
// Extract documentation before this method
String documentation = extractDocumentationBefore(m.start());
// Parse parameters with their actual positions
List<FieldInfo> params = parseParametersWithPositions(paramList, m.start(3));
@@ -481,7 +490,9 @@ public class ScriptDocument {
params,
m.start(),
bodyStart,
bodyEnd
bodyEnd,
false,
documentation
);
methods.add(methodInfo);
}
@@ -781,8 +792,11 @@ public class ScriptDocument {
}
if (!insideMethod) {
// Extract documentation before this field
String documentation = extractDocumentationBefore(m.start());
TypeInfo typeInfo = resolveType(typeName);
FieldInfo fieldInfo = FieldInfo.globalField(fieldName, typeInfo, position);
FieldInfo fieldInfo = FieldInfo.globalField(fieldName, typeInfo, position, documentation);
globalFields.put(fieldName, fieldInfo);
}
}
@@ -807,7 +821,131 @@ public class ScriptDocument {
return result.toString();
}
/**
* Extract documentation comment immediately preceding a position.
* Supports both single-line (//) and multi-line (/* *\/) comment styles.
* Returns null if no documentation is found.
*
* @param position The position of the declaration (e.g., start of method/field)
* @return The documentation text, or null if none found
*/
private String extractDocumentationBefore(int position) {
if (position <= 0) return null;
// First, skip backwards to get off the current declaration line
// (declarations might have modifiers like 'public' before the matched position)
int searchStart = position - 1;
// Skip backwards to the previous newline
while (searchStart >= 0 && text.charAt(searchStart) != '\n') {
searchStart--;
}
if (searchStart < 0) return null;
// Now we're at a newline before the declaration line
// Skip backwards through any additional whitespace
searchStart--;
while (searchStart >= 0 && Character.isWhitespace(text.charAt(searchStart))) {
searchStart--;
}
if (searchStart < 0) return null;
// Now searchStart should be at the last non-whitespace character before the declaration line
// Check if we're at the end of a comment
// Case 1: Multi-line comment ending with */
if (searchStart > 0 && text.charAt(searchStart) == '/' && text.charAt(searchStart - 1) == '*') {
// Find the start of this comment
int commentEnd = searchStart + 1;
int commentStart = text.lastIndexOf("/*", searchStart - 1);
if (commentStart >= 0) {
String comment = text.substring(commentStart, commentEnd);
return cleanDocumentation(comment);
}
}
// Case 2: Single-line comments (may be multiple consecutive lines)
StringBuilder doc = new StringBuilder();
int currentPos = searchStart;
// Walk backward through consecutive comment lines
while (currentPos >= 0) {
// Find start of current line
int lineStart = currentPos;
while (lineStart > 0 && text.charAt(lineStart - 1) != '\n') {
lineStart--;
}
// Find end of current line
int lineEnd = currentPos;
while (lineEnd < text.length() && text.charAt(lineEnd) != '\n') {
lineEnd++;
}
// Extract the line and trim
String line = text.substring(lineStart, lineEnd).trim();
// Check if this line is a comment
if (line.startsWith("//") || line.startsWith("#")) {
// Prepend to doc (we're going backwards)
String commentText = line.substring(2).trim();
if (doc.length() > 0) {
doc.insert(0, "\n");
}
doc.insert(0, commentText);
// Move to previous line
if (lineStart > 0) {
currentPos = lineStart - 1;
// Skip to previous line (skip the newline)
while (currentPos >= 0 && text.charAt(currentPos) == '\n') {
currentPos--;
}
} else {
break;
}
} else {
// Not a comment line, stop
break;
}
}
if (doc.length() > 0) {
return doc.toString();
}
return null;
}
/**
* Clean up documentation comment text by removing comment markers and extra formatting.
*/
private String cleanDocumentation(String comment) {
if (comment == null || comment.isEmpty()) return null;
// Remove /* and */ markers
comment = comment.replaceAll("^/\\*+\\s*", "").replaceAll("\\s*\\*+/$", "");
// Split into lines and clean each one
String[] lines = comment.split("\n");
StringBuilder cleaned = new StringBuilder();
for (String line : lines) {
// Remove leading asterisks and whitespace
line = line.trim().replaceAll("^\\*+\\s*", "");
if (cleaned.length() > 0 && !line.isEmpty()) {
cleaned.append("\n");
}
if (!line.isEmpty()) {
cleaned.append(line);
}
}
String result = cleaned.toString().trim();
return result.isEmpty() ? null : result;
}
private TypeInfo resolveType(String typeName) {
return resolveTypeAndTrackUsage(typeName);
@@ -411,6 +411,13 @@ public class TokenHoverInfo {
iconIndicator = "f";
// Add documentation if available
if (fieldInfo.getDocumentation() != null && !fieldInfo.getDocumentation().isEmpty()) {
String[] docLines = fieldInfo.getDocumentation().split("\n");
for (String line : docLines) {
documentation.add(line);
}
}
TypeInfo declaredType = fieldInfo.getDeclaredType();
@@ -465,6 +472,14 @@ public class TokenHoverInfo {
iconIndicator = "v";
// Add documentation if available (though local vars rarely have docs)
if (fieldInfo.getDocumentation() != null && !fieldInfo.getDocumentation().isEmpty()) {
String[] docLines = fieldInfo.getDocumentation().split("\n");
for (String line : docLines) {
documentation.add(line);
}
}
TypeInfo declaredType = fieldInfo.getDeclaredType();
if (declaredType != null) {
int typeColor = getColorForTypeInfo(declaredType);
@@ -484,6 +499,14 @@ public class TokenHoverInfo {
iconIndicator = "p";
// Add documentation if available (args/parameters might have docs from method javadoc)
if (fieldInfo.getDocumentation() != null && !fieldInfo.getDocumentation().isEmpty()) {
String[] docLines = fieldInfo.getDocumentation().split("\n");
for (String line : docLines) {
documentation.add(line);
}
}
TypeInfo declaredType = fieldInfo.getDeclaredType();
if (declaredType != null) {
int typeColor = getColorForTypeInfo(declaredType);
@@ -621,8 +644,17 @@ public class TokenHoverInfo {
addSegment(param.getName(), TokenType.PARAMETER.getHexColor());
}
addSegment(")", TokenType.DEFAULT.getHexColor());
}
// Add documentation if available
if (methodInfo.getDocumentation() != null && !methodInfo.getDocumentation().isEmpty()) {
String[] docLines = methodInfo.getDocumentation().split("\n");
for (String line : docLines) {
documentation.add(line);
}
}
}
private void extractJavadoc(Method method) {
// Java reflection doesn't provide Javadoc at runtime
// We could potentially load it from source files or external documentation