mirror of
https://github.com/KAMKEEL/CustomNPC-Plus.git
synced 2026-11-25 00:34:28 +00:00
Improved <> formatting and auto insertion
This commit is contained in:
1 parent
bcd60d80ea
commit
254655cb1d
2 files changed
+144
-1
No files matched your search
@@ -1181,13 +1181,15 @@ public class FormatHelper {
|
||||
return settings.spaceBeforeSemicolon ? " " : "";
|
||||
}
|
||||
|
||||
// ---- COLON spacing (ternary : always gets space, label : uses settings) ----
|
||||
// ---- COLON spacing (ternary : always gets space, enhanced-for : always gets space, label : uses settings) ----
|
||||
if (prev.type == TokenType.COLON) {
|
||||
if (isTernaryColon(allTokens, currIdx - 1)) return " ";
|
||||
if (isEnhancedForLoopColon(allTokens, currIdx - 1)) return " ";
|
||||
return settings.spaceAfterColon ? " " : "";
|
||||
}
|
||||
if (curr.type == TokenType.COLON) {
|
||||
if (isTernaryColon(allTokens, currIdx)) return " ";
|
||||
if (isEnhancedForLoopColon(allTokens, currIdx)) return " ";
|
||||
return settings.spaceBeforeColon ? " " : "";
|
||||
}
|
||||
|
||||
@@ -1398,6 +1400,39 @@ public class FormatHelper {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if a colon at the given index is the separator in an enhanced for-loop
|
||||
* ({@code for (Type var : collection)}) by scanning backwards from the colon
|
||||
* for an opening paren preceded by the {@code for} keyword.
|
||||
*/
|
||||
private boolean isEnhancedForLoopColon(List<Token> tokens, int colonIdx) {
|
||||
int depth = 0;
|
||||
for (int i = colonIdx - 1; i >= 0; i--) {
|
||||
Token t = tokens.get(i);
|
||||
if (t.type == TokenType.PAREN_CLOSE) depth++;
|
||||
else if (t.type == TokenType.PAREN_OPEN) {
|
||||
if (depth > 0) {
|
||||
depth--;
|
||||
} else {
|
||||
// Found the opening paren at our depth — check if preceded by 'for'
|
||||
for (int j = i - 1; j >= 0; j--) {
|
||||
if (tokens.get(j).type != TokenType.WHITESPACE) {
|
||||
return tokens.get(j).type == TokenType.KEYWORD
|
||||
&& tokens.get(j).value.equals("for");
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
// Stop at statement boundaries
|
||||
if (t.type == TokenType.SEMICOLON || t.type == TokenType.BRACE_OPEN ||
|
||||
t.type == TokenType.BRACE_CLOSE) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
private boolean isAssignmentOperator(String op) {
|
||||
switch (op) {
|
||||
case "=": case "+=": case "-=": case "*=": case "/=": case "%=":
|
||||
|
||||
Reference in new issue
Block a user