1
0
mirror of https://github.com/fadden/6502bench.git synced 2024-11-01 10:06:24 +00:00
6502bench/SourceGen/MultiLineComment.cs
Andy McFadden 83da0d952c Fancy comments, part 1
Added a "fancy" flag to MultiLineComment.  If set, we will use
formatting commands embedded in the text itself.  The max width pop-up
and "boxed" flag will be ignored in favor of width and boxing
directives.  (See issue #111.)

The current "fancy" formatter is just a placeholder that folds lines
every 10 characters.

Removed FormattedMlcCache (added two changes back).  Caching the
rendered string list in the MLC itself is simpler and more efficient
than having a separate cache, and it works for Notes as well.

Added anchors for more comments in the 20090 test.
2024-07-04 17:25:21 -07:00

371 lines
15 KiB
C#

/*
* Copyright 2019 faddenSoft
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Windows.Media;
using System.Text;
namespace SourceGen {
/// <summary>
/// <para>Representation of a multi-line comment, which is a string plus some format options.
/// Used for long comments and notes.</para>
///
/// <para>Instances are effectively immutable, as the text and options can't be modified
/// after the object is created. The object does cache the result of the last FormatText()
/// call, which is determined in part by the Formatter argument, which can change between
/// calls.</para>
/// </summary>
public class MultiLineComment {
/// <summary>
/// If set, sticks a MaxWidth "ruler" at the top, and makes spaces visible.
/// </summary>
public static bool DebugShowRuler { get; set; }
/// <summary>
/// Unformatted text.
/// </summary>
public string Text { get; private set; }
/// <summary>
/// True if this uses "fancy" formatting. If set, the BoxMode and MaxWidth properties
/// are ignored.
/// </summary>
public bool IsFancy { get; private set; }
/// <summary>
/// Set to true to render text surrounded by a box of ASCII characters.
/// </summary>
public bool BoxMode { get; private set; }
/// <summary>
/// Maximum line width. Box mode effectively reduces this by four.
/// </summary>
public int MaxWidth { get; private set; }
/// <summary>
/// Background color for notes.
/// </summary>
public Color BackgroundColor { get; private set; }
/// <summary>
/// Box character to use for "basic" formatting.
/// </summary>
private const char BASIC_BOX_CHAR = '*';
private const int DEFAULT_WIDTH = 80;
private const int MIN_WIDTH = 8;
/// <summary>
/// Constructor. By default, comments use basic formatting, have a basic-mode max
/// width of 80, and aren't boxed.
/// </summary>
/// <remarks>
/// We'd actually prefer to have fancy formatting be the default, but that does the
/// wrong thing when deserializing older projects.
/// </remarks>
/// <param name="text">Unformatted comment text.</param>
public MultiLineComment(string text) {
Debug.Assert(text != null); // empty string is okay
Text = text;
IsFancy = false;
BoxMode = false;
MaxWidth = DEFAULT_WIDTH;
BackgroundColor = CommonWPF.Helper.ZeroColor;
}
/// <summary>
/// Constructor. Used when creating an empty MLC for editing.
/// </summary>
/// <param name="isFancy">True if we want to be in "fancy" mode initially.</param>
public MultiLineComment(bool isFancy) : this(string.Empty) {
IsFancy = isFancy;
}
/// <summary>
/// Constructor. Used for long comments.
/// </summary>
/// <param name="text">Unformatted text.</param>
/// <param name="isFancy">True if we're using fancy format mode.</param>
/// <param name="boxMode">For basic mode, set to true to enable box mode.</param>
/// <param name="maxWidth">For basic mode, maximum line width.</param>
public MultiLineComment(string text, bool isFancy, bool boxMode, int maxWidth)
: this(text) {
if (maxWidth < MIN_WIDTH) {
Debug.Assert(false, "unexpectedly small max width");
maxWidth = MIN_WIDTH;
}
IsFancy = isFancy;
BoxMode = boxMode;
MaxWidth = maxWidth;
}
/// <summary>
/// Constructor. Used for notes.
/// </summary>
/// <param name="text">Unformatted text.</param>
/// <param name="bkgndColor">Background color.</param>
public MultiLineComment(string text, Color bkgndColor) : this(text) {
BackgroundColor = bkgndColor;
}
private List<string> mPreviousRender = null;
private Asm65.Formatter mPreviousFormatter = null;
private string mPreviousPrefix = null;
/// <summary>
/// Generates one or more lines of formatted text.
/// </summary>
/// <param name="formatter">Formatter, with comment delimiters.</param>
/// <param name="textPrefix">String to prepend to text before formatting. If this
/// is non-empty, comment delimiters aren't emitted. (Used for notes.)</param>
/// <returns>List of formatted strings. Do not modify the list.</returns>
public List<string> FormatText(Asm65.Formatter formatter, string textPrefix) {
if (mPreviousRender != null && formatter == mPreviousFormatter &&
textPrefix == mPreviousPrefix) {
// We rendered this with the same formatter before. Return the list. It would
// be safer to clone the list, but I'm not expecting the caller to edit it.
return mPreviousRender;
}
List<string> lines;
if (IsFancy) {
Debug.Assert(string.IsNullOrEmpty(textPrefix));
lines = FormatFancyText(formatter);
} else {
lines = FormatSimpleText(formatter, textPrefix);
}
// Cache result.
mPreviousRender = lines;
mPreviousFormatter = formatter;
mPreviousPrefix = textPrefix;
return lines;
}
/// <summary>
/// Generates one or more lines of formatted text, using the basic formatter.
/// </summary>
/// <param name="formatter">Formatter, with comment delimiters.</param>
/// <param name="textPrefix">String to prepend to text before formatting. If this
/// is non-empty, comment delimiters aren't emitted. (Used for notes.)</param>
/// <returns>List of formatted strings.</returns>
private List<string> FormatSimpleText(Asm65.Formatter formatter, string textPrefix) {
const char spcRep = '\u2219'; // BULLET OPERATOR
string workString = string.IsNullOrEmpty(textPrefix) ? Text : textPrefix + Text;
List<string> lines = new List<string>();
string linePrefix;
if (!string.IsNullOrEmpty(textPrefix)) {
linePrefix = string.Empty;
} else if (BoxMode) {
if (formatter.FullLineCommentDelimiterBase.Length == 1 &&
formatter.FullLineCommentDelimiterBase[0] == BASIC_BOX_CHAR) {
linePrefix = string.Empty;
} else {
linePrefix = formatter.FullLineCommentDelimiterBase;
}
} else {
linePrefix = formatter.FullLineCommentDelimiterPlus;
}
StringBuilder sb = new StringBuilder(MaxWidth);
if (DebugShowRuler) {
for (int i = 0; i < MaxWidth; i++) {
sb.Append((i % 10).ToString());
}
lines.Add(sb.ToString());
sb.Clear();
}
string boxLine, spaces;
if (BoxMode) {
for (int i = 0; i < MaxWidth - linePrefix.Length; i++) {
sb.Append(BASIC_BOX_CHAR);
}
boxLine = sb.ToString();
sb.Clear();
for (int i = 0; i < MaxWidth; i++) {
sb.Append(' ');
}
spaces = sb.ToString();
sb.Clear();
} else {
boxLine = spaces = null;
}
if (BoxMode && workString.Length > 0) {
lines.Add(linePrefix + boxLine);
}
int lineWidth = BoxMode ?
MaxWidth - linePrefix.Length - 4 :
MaxWidth - linePrefix.Length;
int startIndex = 0;
int breakIndex = -1;
for (int i = 0; i < workString.Length; i++) {
// Spaces and hyphens are different. For example, if width is 10,
// "long words<space>more words" becomes:
// 0123456789
// long words
// more words
// However, "long words-more words" becomes:
// long
// words-more
// words
// because the hyphen is retained but the space is discarded.
if (workString[i] == '\r' || workString[i] == '\n') {
// explicit line break, emit line
string str = workString.Substring(startIndex, i - startIndex);
if (DebugShowRuler) { str = str.Replace(' ', spcRep); }
if (BoxMode) {
if (str == "" + BASIC_BOX_CHAR) {
// asterisk on a line by itself means "output row of asterisks"
str = linePrefix + boxLine;
} else {
int padLen = lineWidth - str.Length;
str = linePrefix + BASIC_BOX_CHAR + " " + str +
spaces.Substring(0, padLen + 1) + BASIC_BOX_CHAR;
}
} else {
str = linePrefix + str;
}
lines.Add(str);
// Eat the LF in CRLF. We don't actually work right with just LF,
// because this will consume LFLF, but it's okay to insist that the
// string use CRLF for line breaks.
if (i < workString.Length - 1 && workString[i + 1] == '\n') {
i++;
}
startIndex = i + 1;
breakIndex = -1;
} else if (workString[i] == ' ') {
// can break on a space even if it's one char too far
breakIndex = i;
}
if (i - startIndex >= lineWidth) {
// this character was one too many, break line one back
if (breakIndex <= 0) {
// no break found, just chop it
string str = workString.Substring(startIndex, i - startIndex);
if (DebugShowRuler) { str = str.Replace(' ', spcRep); }
if (BoxMode) {
str = linePrefix + BASIC_BOX_CHAR + " " + str + " " + BASIC_BOX_CHAR;
} else {
str = linePrefix + str;
}
lines.Add(str);
startIndex = i;
} else {
// Copy everything from start to break. If the break was a hyphen,
// we want to keep it.
int adj = 0;
if (workString[breakIndex] == '-') {
adj = 1;
}
string str = workString.Substring(startIndex,
breakIndex + adj - startIndex);
if (DebugShowRuler) { str = str.Replace(' ', spcRep); }
if (BoxMode) {
int padLen = lineWidth - str.Length;
str = linePrefix + BASIC_BOX_CHAR + " " + str +
spaces.Substring(0, padLen + 1) + BASIC_BOX_CHAR;
} else {
str = linePrefix + str;
}
lines.Add(str);
startIndex = breakIndex + 1;
if (adj == 0 && startIndex < workString.Length &&
workString[startIndex] == ' ') {
// We broke on a space, and are now starting a line on a space,
// which looks weird (and happens commonly at the end of a
// sentence). Eat one more space.
startIndex++;
}
breakIndex = -1;
}
}
if (workString[i] == '-') {
// can break on hyphen if it fits in line
breakIndex = i;
}
}
if (startIndex < workString.Length) {
// Output remainder.
string str = workString.Substring(startIndex, workString.Length - startIndex);
if (DebugShowRuler) { str = str.Replace(' ', spcRep); }
if (BoxMode) {
int padLen = lineWidth - str.Length;
str = linePrefix + BASIC_BOX_CHAR + " " + str +
spaces.Substring(0, padLen + 1) + BASIC_BOX_CHAR;
} else {
str = linePrefix + str;
}
lines.Add(str);
}
if (BoxMode && workString.Length > 0) {
lines.Add(linePrefix + boxLine);
}
return lines;
}
/// <summary>
/// Generates one or more lines of formatted text, using the fancy formatter.
/// </summary>
/// <param name="formatter">Formatter, with comment delimiters.</param>
/// <returns>List of formatted strings.</returns>
private List<string> FormatFancyText(Asm65.Formatter formatter) {
List<string> lines = new List<string>();
string mod = Text.Replace("\r\n", "CRLF");
for (int i = 0; i < mod.Length; i += 10) {
lines.Add(formatter.FullLineCommentDelimiterPlus +
mod.Substring(i, Math.Min(10, mod.Length - i)));
}
return lines;
}
public override string ToString() {
return "MLC box=" + BoxMode + " width=" + MaxWidth + " text='" + Text + "'";
}
public static bool operator ==(MultiLineComment a, MultiLineComment b) {
if (ReferenceEquals(a, b)) {
return true; // same object, or both null
}
if (ReferenceEquals(a, null) || ReferenceEquals(b, null)) {
return false; // one is null
}
return a.Text.Equals(b.Text) && a.BoxMode == b.BoxMode && a.MaxWidth == b.MaxWidth
&& a.BackgroundColor == b.BackgroundColor;
}
public static bool operator !=(MultiLineComment a, MultiLineComment b) {
return !(a == b);
}
public override bool Equals(object obj) {
return obj is MultiLineComment && this == (MultiLineComment)obj;
}
public override int GetHashCode() {
return Text.GetHashCode() ^ MaxWidth ^ (BoxMode ? 1 : 0) ^ BackgroundColor.GetHashCode();
}
}
}