From c0a022c5b5dd68922fb5ff296cf3b0fa8396626a Mon Sep 17 00:00:00 2001 From: bigguy345 Date: Fri, 13 Mar 2026 19:49:30 +0200 Subject: [PATCH] Tightened excluded range checks in receiver chain resolution (fixes stupid . in comments fkin up next line bug) --- .../autocomplete/AutocompleteManager.java | 36 ++++++++++++++++--- .../script/interpreter/FieldChainMarker.java | 8 ++--- .../script/interpreter/ScriptDocument.java | 30 +++++++++++----- 3 files changed, 58 insertions(+), 16 deletions(-) diff --git a/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/AutocompleteManager.java b/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/AutocompleteManager.java index 976688865..729a8dd74 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/AutocompleteManager.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/autocomplete/AutocompleteManager.java @@ -389,6 +389,17 @@ public class AutocompleteManager { // Walk left across a dotted receiver chain while (pos >= 0) { + // Skip excluded regions (comments/strings) to avoid reading dots from inside them + if (document != null && document.isExcluded(pos)) { + int jumped = jumpBeforeExcluded(pos); + if (jumped == pos) { + pos--; + } else { + pos = jumped; + } + continue; + } + char c = text.charAt(pos); if (Character.isWhitespace(c)) { @@ -414,10 +425,24 @@ public class AutocompleteManager { // Consume identifier while (pos >= 0 && Character.isJavaIdentifierPart(text.charAt(pos))) pos--; - // If preceded by a dot, keep going + // If preceded by a dot, keep going (skip excluded ranges when looking for the dot) int checkPos = pos; - while (checkPos >= 0 && Character.isWhitespace(text.charAt(checkPos))) { - checkPos--; + while (checkPos >= 0) { + char ch = text.charAt(checkPos); + if (Character.isWhitespace(ch)) { + checkPos--; + continue; + } + if (document != null && document.isExcluded(checkPos)) { + int jumped = jumpBeforeExcluded(checkPos); + if (jumped == checkPos) { + checkPos--; + } else { + checkPos = jumped; + } + continue; + } + break; } if (checkPos >= 0 && text.charAt(checkPos) == '.') { pos = checkPos - 1; @@ -1044,13 +1069,16 @@ public class AutocompleteManager { // Check if we found a dot if (pos >= 0 && text.charAt(pos) == '.') { + if (document != null && document.isExcluded(pos)) return -1; return pos; } else { while (pos >= 0 && Character.isJavaIdentifierPart(text.charAt(pos))) pos--; - if (pos >= 0 && text.charAt(pos) == '.') + if (pos >= 0 && text.charAt(pos) == '.') { + if (document != null && document.isExcluded(pos)) return -1; return pos; + } } return -1; } diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/FieldChainMarker.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/FieldChainMarker.java index 2eeff85c0..5659c7f56 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/FieldChainMarker.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/FieldChainMarker.java @@ -151,11 +151,11 @@ public class FieldChainMarker { int pos = m.end(2); while (pos < text.length()) { pos = document.skipWhitespace(pos); - if (pos >= text.length() || text.charAt(pos) != '.') break; + if (pos >= text.length() || document.isExcluded(pos) || text.charAt(pos) != '.') break; pos++; // Skip dot pos = document.skipWhitespace(pos); - if (pos >= text.length() || !Character.isJavaIdentifierStart(text.charAt(pos))) break; + if (pos >= text.length() || document.isExcluded(pos) || !Character.isJavaIdentifierStart(text.charAt(pos))) break; // Read identifier int identStart = pos; @@ -452,11 +452,11 @@ public class FieldChainMarker { int pos = identEnd; while (pos < text.length()) { pos = document.skipWhitespace(pos); - if (pos >= text.length() || text.charAt(pos) != '.') break; + if (pos >= text.length() || document.isExcluded(pos) || text.charAt(pos) != '.') break; pos++; // Skip dot pos = document.skipWhitespace(pos); - if (pos >= text.length() || !Character.isJavaIdentifierStart(text.charAt(pos))) break; + if (pos >= text.length() || document.isExcluded(pos) || !Character.isJavaIdentifierStart(text.charAt(pos))) break; // Read identifier int nStart = pos; diff --git a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java index f7089bedd..e9d6d7cf2 100644 --- a/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java +++ b/src/main/java/noppes/npcs/client/gui/util/script/interpreter/ScriptDocument.java @@ -6467,7 +6467,7 @@ public class ScriptDocument { while (pos >= 0 && Character.isWhitespace(text.charAt(pos))) pos--; - if (pos < 0 || text.charAt(pos) != '.') + if (pos < 0 || isExcluded(pos) || text.charAt(pos) != '.') return false; // Skip the dot and any whitespace @@ -6475,7 +6475,7 @@ public class ScriptDocument { while (pos >= 0 && Character.isWhitespace(text.charAt(pos))) pos--; - if (pos < 0) + if (pos < 0 || isExcluded(pos)) return false; // Check what's before the dot - could be: @@ -6506,7 +6506,7 @@ public class ScriptDocument { pos--; // If preceded by a dot, this is part of a chain - treat as instance for now - if (pos >= 0 && text.charAt(pos) == '.') { + if (pos >= 0 && !isExcluded(pos) && text.charAt(pos) == '.') { return false; } @@ -7199,7 +7199,8 @@ public class ScriptDocument { while (scanPos >= 0 && Character.isWhitespace(text.charAt(scanPos))) scanPos--; - if (scanPos < 0 || text.charAt(scanPos) != '.') { + // Guard: dot inside a comment/string must not be treated as a receiver chain separator + if (scanPos < 0 || isExcluded(scanPos) || text.charAt(scanPos) != '.') { return null; // No receiver } @@ -8130,11 +8131,24 @@ public class ScriptDocument { if (Character.isJavaIdentifierPart(c)) { while (pos >= 0 && Character.isJavaIdentifierPart(text.charAt(pos))) pos--; - // Skip whitespace to check for chained identifier (preceded by a dot) + // Skip whitespace AND excluded ranges to check for chained identifier (preceded by a dot). + // Without skipping excluded ranges, a dot inside a comment (e.g. "//event.\n player") + // would be mistaken for a chain continuation dot. int checkPos = pos; - while (checkPos >= 0 && Character.isWhitespace(text.charAt(checkPos))) checkPos--; - // If it's part of a chained identifier (preceded by a dot), continue - if (checkPos >= 0 && text.charAt(checkPos) == '.') { pos = checkPos - 1; continue; } + while (checkPos >= 0) { + if (isExcluded(checkPos)) { + for (int[] range : excludedRanges) { + if (checkPos >= range[0] && checkPos < range[1]) { + checkPos = range[0] - 1; + break; + } + } + continue; + } + if (Character.isWhitespace(text.charAt(checkPos))) { checkPos--; continue; } + break; + } + if (checkPos >= 0 && !isExcluded(checkPos) && text.charAt(checkPos) == '.') { pos = checkPos - 1; continue; } break; }