Skip to content

Commit d9f16fe

Browse files
committed
Stored MethodCallInfo and FieldCallInfo properly for method call args, and underlined them when errored for 2 error types; wrong method call args count and wrong arg type
1 parent 9cb4c96 commit d9f16fe

File tree

5 files changed

+83
-44
lines changed

5 files changed

+83
-44
lines changed

src/main/java/noppes/npcs/client/gui/util/script/interpreter/FieldInfo.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,4 +125,18 @@ public boolean equals(Object o) {
125125
public int hashCode() {
126126
return name.hashCode() * 31 + scope.ordinal();
127127
}
128+
129+
/**
130+
* Wrapper class that holds both FieldInfo and MethodCallInfo for a variable usage.
131+
* This is used when a variable is used as an argument to a method call.
132+
*/
133+
public static class ArgInfo {
134+
public final FieldInfo fieldInfo;
135+
public final MethodCallInfo methodCallInfo;
136+
137+
public ArgInfo(FieldInfo fieldInfo, MethodCallInfo methodCallInfo) {
138+
this.fieldInfo = fieldInfo;
139+
this.methodCallInfo = methodCallInfo;
140+
}
141+
}
128142
}

src/main/java/noppes/npcs/client/gui/util/script/interpreter/MethodCallInfo.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ public boolean hasArgCountError() {
202202
public boolean hasArgTypeError() {
203203
return !this.argumentTypeErrors.isEmpty();
204204
}
205+
205206
public boolean hasArgTypeError(Token t) {
206207
for (ArgumentTypeError ate : this.argumentTypeErrors) {
207208
if (ate.getArg().equals(t)) {
@@ -211,6 +212,10 @@ public boolean hasArgTypeError(Token t) {
211212
return false;
212213
}
213214

215+
public boolean hasArgCountError(Token t) {
216+
return hasArgCountError() && t.isMethodCall();
217+
}
218+
214219
/**
215220
* Check if this is a static access error (underline method name).
216221
*/

src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package noppes.npcs.client.gui.util.script.interpreter;
22

3-
import net.minecraft.client.Minecraft;
43
import noppes.npcs.client.ClientProxy;
54

65
import java.util.*;
@@ -74,6 +73,9 @@ public class ScriptDocument {
7473
// Local variables per method (methodStartOffset -> {varName -> FieldInfo})
7574
private final Map<Integer, Map<String, FieldInfo>> methodLocals = new HashMap<>();
7675

76+
// Method calls - stores all parsed method call information
77+
private final List<MethodCallInfo> methodCalls = new ArrayList<>();
78+
7779
// Excluded regions (strings/comments) - positions where other patterns shouldn't match
7880
private final List<int[]> excludedRanges = new ArrayList<>();
7981

@@ -180,6 +182,7 @@ public void formatCodeText() {
180182
excludedRanges.clear();
181183
methodLocals.clear();
182184
scriptTypes.clear();
185+
methodCalls.clear();
183186

184187
// Phase 1: Find excluded regions (strings/comments)
185188
findExcludedRanges();
@@ -858,13 +861,14 @@ private List<ScriptLine.Mark> buildMarks() {
858861
// Numbers
859862
addPatternMarks(marks, NUMBER_PATTERN, TokenType.NUMBER);
860863

864+
// Method calls (parse before variables so we can attach context)
865+
markMethodCalls(marks);
866+
861867
// Variables and fields
862868
markVariables(marks);
863869

864870
// Chained field accesses (e.g., mc.player.world, this.field)
865871
markChainedFieldAccesses(marks);
866-
867-
markMethodCalls(marks);
868872

869873
// Imported class usages
870874
markImportedClassUsages(marks);
@@ -1321,6 +1325,9 @@ private void markMethodCalls(List<ScriptLine.Mark> marks) {
13211325
arguments, receiverType, resolvedMethod, isStaticAccess
13221326
);
13231327
callInfo.validate();
1328+
1329+
// Store in collection for later lookup
1330+
methodCalls.add(callInfo);
13241331

13251332
// Always mark method call as green (METHOD_CALL), with error info attached for underline
13261333
// The MethodCallInfo is attached so rendering can draw curly underline if there's an error
@@ -1349,6 +1356,9 @@ private void markMethodCalls(List<ScriptLine.Mark> marks) {
13491356
arguments, null, resolvedMethod
13501357
);
13511358
callInfo.validate();
1359+
1360+
// Store in collection for later lookup
1361+
methodCalls.add(callInfo);
13521362

13531363
// Always mark method call as green (METHOD_CALL), with error info attached for underline
13541364
marks.add(new ScriptLine.Mark(nameStart, nameEnd, TokenType.METHOD_CALL, callInfo));
@@ -1422,6 +1432,25 @@ private boolean isStaticAccessCall(int methodNameStart) {
14221432
return !ident.isEmpty() && Character.isUpperCase(ident.charAt(0));
14231433
}
14241434

1435+
/**
1436+
* Find the MethodCallInfo that contains the given position as an argument.
1437+
* Returns null if the position is not inside any method call's argument list.
1438+
*/
1439+
private MethodCallInfo findMethodCallContainingPosition(int position) {
1440+
for (MethodCallInfo call : methodCalls) {
1441+
// Check if position is within the argument list (between open and close parens)
1442+
if (position >= call.getOpenParenOffset() && position <= call.getCloseParenOffset()) {
1443+
// Check if it's within any of the arguments
1444+
for (MethodCallInfo.Argument arg : call.getArguments()) {
1445+
if (position >= arg.getStartOffset() && position <= arg.getEndOffset()) {
1446+
return call;
1447+
}
1448+
}
1449+
}
1450+
}
1451+
return null;
1452+
}
1453+
14251454
/**
14261455
* Find the matching closing parenthesis for an opening parenthesis.
14271456
* Handles nested parentheses, strings, and comments.
@@ -1951,6 +1980,8 @@ private void markVariables(List<ScriptLine.Mark> marks) {
19511980

19521981
// Find containing method
19531982
MethodInfo containingMethod = findMethodAtPosition(position);
1983+
// Check if this variable is an argument to a method call
1984+
MethodCallInfo callInfo = findMethodCallContainingPosition(position);
19541985

19551986
// For uppercase identifiers, only process if it's a known field
19561987
// Otherwise, let type handling (markImportedClassUsages) handle it
@@ -1970,39 +2001,26 @@ private void markVariables(List<ScriptLine.Mark> marks) {
19702001
FieldInfo localInfo = locals.get(name);
19712002
// Only highlight if the position is after the declaration
19722003
if (localInfo.isVisibleAt(position)) {
1973-
marks.add(new ScriptLine.Mark(m.start(1), m.end(1), TokenType.LOCAL_FIELD, localInfo));
2004+
Object metadata = callInfo != null ? new FieldInfo.ArgInfo(localInfo, callInfo) : localInfo;
2005+
marks.add(new ScriptLine.Mark(m.start(1), m.end(1), TokenType.LOCAL_FIELD, metadata));
19742006
continue;
19752007
}
19762008
}
1977-
1978-
// Check global fields
1979-
if (globalFields.containsKey(name)) {
1980-
FieldInfo fieldInfo = globalFields.get(name);
1981-
marks.add(new ScriptLine.Mark(m.start(1), m.end(1), TokenType.GLOBAL_FIELD, fieldInfo));
1982-
continue;
1983-
}
1984-
1985-
// Skip uppercase if not a known field - type handling will deal with it
1986-
if (isUppercase)
1987-
continue;
1988-
1989-
// Unknown variable inside method - mark as undefined
1990-
marks.add(new ScriptLine.Mark(m.start(1), m.end(1), TokenType.UNDEFINED_VAR));
19912009
} else {
1992-
// Outside any method
19932010
// Check global fields
19942011
if (globalFields.containsKey(name)) {
19952012
FieldInfo fieldInfo = globalFields.get(name);
1996-
marks.add(new ScriptLine.Mark(m.start(1), m.end(1), TokenType.GLOBAL_FIELD, fieldInfo));
2013+
Object metadata = callInfo != null ? new FieldInfo.ArgInfo(fieldInfo, callInfo) : fieldInfo;
2014+
marks.add(new ScriptLine.Mark(m.start(1), m.end(1), TokenType.GLOBAL_FIELD, metadata));
19972015
continue;
19982016
}
19992017

20002018
// Skip uppercase if not a known field - type handling will deal with it
20012019
if (isUppercase)
20022020
continue;
20032021

2004-
// Unknown variable outside method - mark as undefined
2005-
marks.add(new ScriptLine.Mark(m.start(1), m.end(1), TokenType.UNDEFINED_VAR));
2022+
// Unknown variable inside method - mark as undefined
2023+
marks.add(new ScriptLine.Mark(m.start(1), m.end(1), TokenType.UNDEFINED_VAR, callInfo));
20062024
}
20072025
}
20082026
}

src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptLine.java

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package noppes.npcs.client.gui.util.script.interpreter;
22

3-
import net.minecraft.client.gui.Gui;
43
import noppes.npcs.client.ClientProxy;
54
import org.lwjgl.opengl.GL11;
65

@@ -144,6 +143,10 @@ public void buildTokensFromMarks(List<Mark> marks, String fullText, ScriptDocume
144143
if (mark.metadata != null) {
145144
if (mark.metadata instanceof TypeInfo) {
146145
token.setTypeInfo((TypeInfo) mark.metadata);
146+
} else if (mark.metadata instanceof FieldInfo.ArgInfo) {
147+
FieldInfo.ArgInfo ctx = (FieldInfo.ArgInfo) mark.metadata;
148+
token.setFieldInfo(ctx.fieldInfo);
149+
token.setMethodCallInfo(ctx.methodCallInfo);
147150
} else if (mark.metadata instanceof FieldInfo) {
148151
token.setFieldInfo((FieldInfo) mark.metadata);
149152
} else if (mark.metadata instanceof MethodInfo) {
@@ -206,7 +209,6 @@ public void drawString(int x, int y, int defaultColor) {
206209
int lastIndex = 0;
207210

208211
// Track positions for underlines
209-
List<int[]> underlines = new ArrayList<>(); // [startX, endX, color]
210212
int currentX = x;
211213

212214
for (Token t : tokens) {
@@ -222,18 +224,10 @@ public void drawString(int x, int y, int defaultColor) {
222224
// Track underline position if token has one
223225
if (t.hasUnderline()) {
224226
int tokenWidth = ClientProxy.Font.width(t.getText());
225-
underlines.add(new int[]{currentX, currentX + tokenWidth, t.getUnderlineColor()});
227+
int currentY = y + ClientProxy.Font.height() - 1;
228+
drawCurlyUnderline(currentX + 1, currentY, tokenWidth + 1, t.getUnderlineColor());
226229
}
227230

228-
if (t.getMethodCallInfo() != null) {
229-
MethodCallInfo callInfo = t.getMethodCallInfo();
230-
if (callInfo.hasArgTypeError(t)) {
231-
underlines.add(
232-
new int[]{currentX, currentX + ClientProxy.Font.width(t.getText()), t.getUnderlineColor()});
233-
}
234-
}
235-
236-
237231
// Append the colored token
238232
builder.append(COLOR_CHAR)
239233
.append(t.getColorCode())
@@ -252,11 +246,6 @@ public void drawString(int x, int y, int defaultColor) {
252246

253247
// Draw the text
254248
ClientProxy.Font.drawString(builder.toString(), x, y, defaultColor);
255-
256-
// Draw curly underlines for error tokens
257-
for (int[] ul : underlines) {
258-
drawCurlyUnderline(ul[0], y + ClientProxy.Font.height() - 1, ul[1] - ul[0], ul[2]);
259-
}
260249
}
261250

262251
/**
@@ -288,12 +277,12 @@ private void drawCurlyUnderline(int x, int y, int width, int color) {
288277

289278
GL11.glBegin(GL11.GL_LINE_STRIP);
290279
// Wave parameters: 2 pixels amplitude, 4 pixels wavelength
291-
int waveHeight = 2;
280+
int waveHeight = 1;
292281
int waveLength = 4;
293282
for (int i = 0; i <= width; i++) {
294283
// Create a sine-like wave pattern
295284
double phase = (double) i / waveLength * Math.PI * 2;
296-
int yOffset = (int) (Math.sin(phase) * waveHeight);
285+
float yOffset = (float) (Math.sin(phase) * waveHeight) - 0.5f;
297286
GL11.glVertex2f(x + i, y + yOffset);
298287
}
299288
GL11.glEnd();

src/main/java/noppes/npcs/client/gui/util/script/interpreter/Token.java

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public class Token {
2626

2727
// Rendering flags
2828
private boolean hasUnderline; // True if this token should be underlined (for errors)
29-
private int underlineColor; // Color of the underline (if any)
29+
private int underlineColor = 0xFF5555; // Color of the underline (if any)
3030

3131
// Navigation - set by ScriptLine
3232
private Token prev;
@@ -119,8 +119,21 @@ public static Token undefined(String text, int start, int end) {
119119
public MethodCallInfo getMethodCallInfo() { return methodCallInfo; }
120120
public ImportData getImportData() { return importData; }
121121
public ScriptLine getParentLine() { return parentLine; }
122-
public boolean hasUnderline() { return hasUnderline; }
123-
public int getUnderlineColor() { return underlineColor; }
122+
123+
public boolean hasUnderline() {
124+
if (methodCallInfo != null) {
125+
if (methodCallInfo.hasArgCountError(this))
126+
return true;
127+
else if (methodCallInfo.hasArgTypeError(this)) {
128+
return true;
129+
}
130+
}
131+
return false;
132+
}
133+
134+
public int getUnderlineColor() {
135+
return underlineColor;
136+
}
124137

125138
// ==================== SETTERS ====================
126139

0 commit comments

Comments
 (0)