preparing to change alignment

This commit is contained in:
Denis Molony 2021-02-26 08:16:14 +10:00
parent a20f026cf2
commit d71075a16c
2 changed files with 63 additions and 22 deletions

View File

@ -304,7 +304,7 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
} }
// Are we joining REM lines with the previous subline? // Are we joining REM lines with the previous subline?
if (!basicPreferences.splitRem && subline.isJoinableRem ()) if (joinableRem (subline))
{ {
// Join this REM statement to the previous line, so no indenting // Join this REM statement to the previous line, so no indenting
fullText.deleteCharAt (fullText.length () - 1); // remove newline fullText.deleteCharAt (fullText.length () - 1); // remove newline
@ -781,8 +781,12 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
if (subline.equalsPosition == 0) // if the line has no equals sign if (subline.equalsPosition == 0) // if the line has no equals sign
return 0; // reset it return 0; // reset it
if (currentAlignPosition == 0) if (currentAlignPosition == 0) // examine following sublines
currentAlignPosition = findHighest (subline); // examine following sublines {
Alignment alignment = new Alignment (subline);
findHighest (subline, alignment);
currentAlignPosition = alignment.equalsPosition;
}
return currentAlignPosition; return currentAlignPosition;
} }
@ -790,11 +794,11 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
// The IF processing is so that any assignment that is being aligned doesn't continue // The IF processing is so that any assignment that is being aligned doesn't continue
// to the next full line (because the indentation has changed). // to the next full line (because the indentation has changed).
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
private int findHighest (SubLine startSubline) private void findHighest (SubLine startSubline, Alignment alignment)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
boolean started = false; boolean started = false;
int highestAssign = startSubline.equalsPosition; // int highestAssign = startSubline.equalsPosition;
outerLoop: for (int i = sourceLines.indexOf (startSubline.parent); i < sourceLines outerLoop: for (int i = sourceLines.indexOf (startSubline.parent); i < sourceLines
.size (); i++) .size (); i++)
@ -809,8 +813,10 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
if (subline.equalsPosition == 0 && !joinableRem (subline)) if (subline.equalsPosition == 0 && !joinableRem (subline))
break outerLoop; break outerLoop;
if (subline.equalsPosition > highestAssign) // if (subline.equalsPosition > highestAssign)
highestAssign = subline.equalsPosition; // highestAssign = subline.equalsPosition;
if (subline.equalsPosition > 0)
alignment.check (subline);
} }
else if (subline == startSubline) else if (subline == startSubline)
started = true; started = true;
@ -822,7 +828,10 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
break; // don't continue with following SourceLine break; // don't continue with following SourceLine
} }
return highestAssign; System.out.printf (" %d %d%n",
alignment.equalsPosition, alignment.targetLength);
// return highestAssign;
// return alignment;
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -1134,4 +1143,29 @@ public class ApplesoftBasicProgram extends BasicProgram implements ApplesoftCons
lines.add (lineNumber); lines.add (lineNumber);
} }
} }
// ---------------------------------------------------------------------------------//
private class Alignment
// ---------------------------------------------------------------------------------//
{
int equalsPosition;
int targetLength;
Alignment (SubLine subline)
{
check (subline);
}
void check (SubLine subline)
{
System.out.printf ("%-20s %d %d%n", subline, subline.equalsPosition,
subline.endPosition - subline.equalsPosition);
if (equalsPosition < subline.equalsPosition)
equalsPosition = subline.equalsPosition;
int temp = subline.endPosition - subline.equalsPosition;
if (targetLength < temp)
targetLength = temp;
}
}
} }

View File

@ -38,6 +38,7 @@ public class SubLine implements ApplesoftConstants
String forVariable = ""; String forVariable = "";
int equalsPosition; // used for aligning the equals sign int equalsPosition; // used for aligning the equals sign
int endPosition; // not sure yet
String functionArgument; String functionArgument;
String functionName; String functionName;
@ -81,7 +82,7 @@ public class SubLine implements ApplesoftConstants
} }
else else
{ {
ptr = startPtr; // ptr = startPtr;
if (isDigit (firstByte)) // split IF xx THEN nnn if (isDigit (firstByte)) // split IF xx THEN nnn
{ {
addXref (getLineNumber (buffer, startPtr), gotoLines); addXref (getLineNumber (buffer, startPtr), gotoLines);
@ -325,6 +326,7 @@ public class SubLine implements ApplesoftConstants
chunk = chunk.trim (); chunk = chunk.trim ();
if (chunk.isEmpty ()) if (chunk.isEmpty ())
continue; continue;
b = (byte) chunk.charAt (0); b = (byte) chunk.charAt (0);
if (isPossibleNumber (b) || b == ASCII_MINUS) if (isPossibleNumber (b) || b == ASCII_MINUS)
{ {
@ -457,15 +459,20 @@ public class SubLine implements ApplesoftConstants
private void setEqualsPosition () private void setEqualsPosition ()
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
int p = startPtr; // int p = startPtr;
int max = startPtr + length; // int max = startPtr + length;
while (++p < max) // while (++p < max)
if (buffer[p] == TOKEN_EQUALS) // if (buffer[p] == TOKEN_EQUALS)
{ // {
equalsPosition = toString ().indexOf ('='); // use expanded line String expandedLine = toString ();
break; equalsPosition = expandedLine.indexOf ('=');
} endPosition = expandedLine.length ();
if (expandedLine.endsWith (":"))
endPosition--;
// break;
// }
assert equalsPosition > 0;
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
@ -500,14 +507,13 @@ public class SubLine implements ApplesoftConstants
boolean has (byte token) boolean has (byte token)
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
int ptr = startPtr + 1; int ptr = startPtr;
int max = startPtr + length; int max = startPtr + length;
while (ptr < max) while (++ptr < max)
{ if (buffer[ptr] == token)
if (buffer[ptr++] == token)
return true; return true;
}
return false; return false;
} }
@ -614,6 +620,7 @@ public class SubLine implements ApplesoftConstants
StringBuilder line = toStringBuilder (); // get line StringBuilder line = toStringBuilder (); // get line
// insert spaces before '=' until it lines up with the other assignment lines // insert spaces before '=' until it lines up with the other assignment lines
if (!is (TOKEN_REM)) if (!is (TOKEN_REM))
while (alignEqualsPos-- > equalsPosition) while (alignEqualsPos-- > equalsPosition)
line.insert (equalsPosition, ' '); line.insert (equalsPosition, ' ');