Browse Source

Fixed SD2-1011: If statement indentation when no bracket used.

TextView now uses integer positions.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/trunk@1926 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Daniel Grunwald 20 years ago
parent
commit
11bde7c539
  1. 15
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/Indentation.cs
  2. 96
      src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextView.cs

15
src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/Indentation.cs

@ -215,10 +215,21 @@ namespace CSharpBinding.FormattingStrategy
block.OneLineBlock = false; block.OneLineBlock = false;
blocks.Push(block); blocks.Push(block);
block.StartLine = doc.LineNumber; block.StartLine = doc.LineNumber;
if (block.LastWord == "switch") if (block.LastWord == "switch") {
block.Indent(set, set.IndentString + set.IndentString); block.Indent(set, set.IndentString + set.IndentString);
else } else if (oldBlock.OneLineBlock) {
// Inside a one-line-block is another statement
// with a full block: indent the inner full block
// by one additional level
block.Indent(set, set.IndentString + set.IndentString);
block.OuterIndent += set.IndentString;
// Indent current line if it starts with the '{' character
if (i == 0) {
oldBlock.InnerIndent += set.IndentString;
}
} else {
block.Indent(set); block.Indent(set);
}
block.Bracket = '{'; block.Bracket = '{';
break; break;
case '}': case '}':

96
src/Libraries/ICSharpCode.TextEditor/Project/Src/Gui/TextView.cs

@ -118,25 +118,25 @@ namespace ICSharpCode.TextEditor
return Math.Max(height1, height2) + 1; return Math.Max(height1, height2) + 1;
} }
float spaceWidth; int spaceWidth;
/// <summary> /// <summary>
/// Gets the width of a space character. /// Gets the width of a space character.
/// This value can be quite small in some fonts - consider using WideSpaceWidth instead. /// This value can be quite small in some fonts - consider using WideSpaceWidth instead.
/// </summary> /// </summary>
public float SpaceWidth { public int SpaceWidth {
get { get {
return spaceWidth; return spaceWidth;
} }
} }
float wideSpaceWidth; int wideSpaceWidth;
/// <summary> /// <summary>
/// Gets the width of a 'wide space' (=one quarter of a tab, if tab is set to 4 spaces). /// Gets the width of a 'wide space' (=one quarter of a tab, if tab is set to 4 spaces).
/// On monospaced fonts, this is the same value as spaceWidth. /// On monospaced fonts, this is the same value as spaceWidth.
/// </summary> /// </summary>
public float WideSpaceWidth { public int WideSpaceWidth {
get { get {
return wideSpaceWidth; return wideSpaceWidth;
} }
@ -211,7 +211,7 @@ namespace ICSharpCode.TextEditor
return; return;
} }
float physicalXPos = lineRectangle.X; int physicalXPos = lineRectangle.X;
// there can't be a folding wich starts in an above line and ends here, because the line is a new one, // there can't be a folding wich starts in an above line and ends here, because the line is a new one,
// there must be a return before this line. // there must be a return before this line.
int column = 0; int column = 0;
@ -295,32 +295,31 @@ namespace ICSharpCode.TextEditor
return BrushRegistry.GetBrush(bgColor); return BrushRegistry.GetBrush(bgColor);
} }
float PaintFoldingText(Graphics g, int lineNumber, float physicalXPos, Rectangle lineRectangle, string text, bool drawSelected) const int additionalFoldTextSize = 1;
int PaintFoldingText(Graphics g, int lineNumber, int physicalXPos, Rectangle lineRectangle, string text, bool drawSelected)
{ {
// TODO: get font and color from the highlighting file // TODO: get font and color from the highlighting file
HighlightColor selectionColor = textArea.Document.HighlightingStrategy.GetColorFor("Selection"); HighlightColor selectionColor = textArea.Document.HighlightingStrategy.GetColorFor("Selection");
Brush bgColorBrush = drawSelected ? BrushRegistry.GetBrush(selectionColor.BackgroundColor) : GetBgColorBrush(lineNumber); Brush bgColorBrush = drawSelected ? BrushRegistry.GetBrush(selectionColor.BackgroundColor) : GetBgColorBrush(lineNumber);
Brush backgroundBrush = textArea.Enabled ? bgColorBrush : SystemBrushes.InactiveBorder; Brush backgroundBrush = textArea.Enabled ? bgColorBrush : SystemBrushes.InactiveBorder;
float wordWidth = MeasureStringWidth(g, text, textArea.Font); Font font = textArea.TextEditorProperties.FontContainer.RegularFont;
RectangleF rect = new RectangleF(physicalXPos, lineRectangle.Y, wordWidth, lineRectangle.Height - 1);
int wordWidth = MeasureStringWidth(g, text, font) + additionalFoldTextSize;
Rectangle rect = new Rectangle(physicalXPos, lineRectangle.Y, wordWidth, lineRectangle.Height - 1);
g.FillRectangle(backgroundBrush, rect); g.FillRectangle(backgroundBrush, rect);
physicalColumn += text.Length; physicalColumn += text.Length;
DrawString(g, DrawString(g,
text, text,
textArea.Font, font,
drawSelected ? selectionColor.Color : Color.Gray, drawSelected ? selectionColor.Color : Color.Gray,
rect.X, rect.Y); rect.X + 1, rect.Y);
g.DrawRectangle(BrushRegistry.GetPen(drawSelected ? Color.DarkGray : Color.Gray), rect.X, rect.Y, rect.Width, rect.Height); g.DrawRectangle(BrushRegistry.GetPen(drawSelected ? Color.DarkGray : Color.Gray), rect.X, rect.Y, rect.Width, rect.Height);
// Bugfix for the problem - of overdrawn right rectangle lines. return physicalXPos + wordWidth + 1;
float ceiling = (float)Math.Ceiling(physicalXPos + wordWidth);
if (ceiling - (physicalXPos + wordWidth) < 0.5) {
++ceiling;
}
return ceiling;
} }
struct MarkerToDraw { struct MarkerToDraw {
@ -390,7 +389,7 @@ namespace ICSharpCode.TextEditor
return null; return null;
} }
float PaintLinePart(Graphics g, int lineNumber, int startColumn, int endColumn, Rectangle lineRectangle, float physicalXPos) int PaintLinePart(Graphics g, int lineNumber, int startColumn, int endColumn, Rectangle lineRectangle, int physicalXPos)
{ {
bool drawLineMarker = DrawLineMarkerAtLine(lineNumber); bool drawLineMarker = DrawLineMarkerAtLine(lineNumber);
Brush backgroundBrush = textArea.Enabled ? GetBgColorBrush(lineNumber) : SystemBrushes.InactiveBorder; Brush backgroundBrush = textArea.Enabled ? GetBgColorBrush(lineNumber) : SystemBrushes.InactiveBorder;
@ -508,7 +507,7 @@ namespace ICSharpCode.TextEditor
if (currentWord.Type == TextWordType.Space) { if (currentWord.Type == TextWordType.Space) {
++physicalColumn; ++physicalColumn;
wordRectangle = new RectangleF(physicalXPos, lineRectangle.Y, (float)Math.Ceiling(SpaceWidth), lineRectangle.Height); wordRectangle = new RectangleF(physicalXPos, lineRectangle.Y, SpaceWidth, lineRectangle.Height);
g.FillRectangle(wordBackBrush, wordRectangle); g.FillRectangle(wordBackBrush, wordRectangle);
if (TextEditorProperties.ShowSpaces) { if (TextEditorProperties.ShowSpaces) {
@ -520,12 +519,12 @@ namespace ICSharpCode.TextEditor
physicalColumn += TextEditorProperties.TabIndent; physicalColumn += TextEditorProperties.TabIndent;
physicalColumn = (physicalColumn / TextEditorProperties.TabIndent) * TextEditorProperties.TabIndent; physicalColumn = (physicalColumn / TextEditorProperties.TabIndent) * TextEditorProperties.TabIndent;
// go to next tabstop // go to next tabstop
float physicalTabEnd = (int)((physicalXPos + MinTabWidth - lineRectangle.X) int physicalTabEnd = ((physicalXPos + MinTabWidth - lineRectangle.X)
/ WideSpaceWidth / TextEditorProperties.TabIndent) / WideSpaceWidth / TextEditorProperties.TabIndent)
* WideSpaceWidth * TextEditorProperties.TabIndent + lineRectangle.X; * WideSpaceWidth * TextEditorProperties.TabIndent + lineRectangle.X;
physicalTabEnd += WideSpaceWidth * TextEditorProperties.TabIndent; physicalTabEnd += WideSpaceWidth * TextEditorProperties.TabIndent;
wordRectangle = new RectangleF(physicalXPos, lineRectangle.Y, (float)Math.Ceiling(physicalTabEnd - physicalXPos), lineRectangle.Height); wordRectangle = new RectangleF(physicalXPos, lineRectangle.Y, physicalTabEnd - physicalXPos, lineRectangle.Height);
g.FillRectangle(wordBackBrush, wordRectangle); g.FillRectangle(wordBackBrush, wordRectangle);
if (TextEditorProperties.ShowTabs) { if (TextEditorProperties.ShowTabs) {
@ -533,12 +532,12 @@ namespace ICSharpCode.TextEditor
} }
physicalXPos = physicalTabEnd; physicalXPos = physicalTabEnd;
} else { } else {
float wordWidth = DrawDocumentWord(g, int wordWidth = DrawDocumentWord(g,
currentWord.Word, currentWord.Word,
new Point((int)physicalXPos, lineRectangle.Y), new Point(physicalXPos, lineRectangle.Y),
currentWord.GetFont(fontContainer), currentWord.GetFont(fontContainer),
wordForeColor, wordForeColor,
wordBackBrush); wordBackBrush);
wordRectangle = new RectangleF(physicalXPos, lineRectangle.Y, wordWidth, lineRectangle.Height); wordRectangle = new RectangleF(physicalXPos, lineRectangle.Y, wordWidth, lineRectangle.Height);
physicalXPos += wordWidth; physicalXPos += wordWidth;
} }
@ -575,17 +574,17 @@ namespace ICSharpCode.TextEditor
return physicalXPos; return physicalXPos;
} }
float DrawDocumentWord(Graphics g, string word, Point position, Font font, Color foreColor, Brush backBrush) int DrawDocumentWord(Graphics g, string word, Point position, Font font, Color foreColor, Brush backBrush)
{ {
if (word == null || word.Length == 0) { if (word == null || word.Length == 0) {
return 0f; return 0;
} }
if (word.Length > MaximumWordLength) { if (word.Length > MaximumWordLength) {
float width = 0; int width = 0;
for (int i = 0; i < word.Length; i += MaximumWordLength) { for (int i = 0; i < word.Length; i += MaximumWordLength) {
Point pos = position; Point pos = position;
pos.X += (int)width; pos.X += width;
if (i + MaximumWordLength < word.Length) if (i + MaximumWordLength < word.Length)
width += DrawDocumentWord(g, word.Substring(i, MaximumWordLength), pos, font, foreColor, backBrush); width += DrawDocumentWord(g, word.Substring(i, MaximumWordLength), pos, font, foreColor, backBrush);
else else
@ -594,11 +593,11 @@ namespace ICSharpCode.TextEditor
return width; return width;
} }
float wordWidth = MeasureStringWidth(g, word, font); int wordWidth = MeasureStringWidth(g, word, font);
//num = ++num % 3; //num = ++num % 3;
g.FillRectangle(backBrush, //num == 0 ? Brushes.LightBlue : num == 1 ? Brushes.LightGreen : Brushes.Yellow, g.FillRectangle(backBrush, //num == 0 ? Brushes.LightBlue : num == 1 ? Brushes.LightGreen : Brushes.Yellow,
new RectangleF(position.X, position.Y, (float)Math.Ceiling(wordWidth + 1), FontHeight)); new RectangleF(position.X, position.Y, wordWidth + 1, FontHeight));
DrawString(g, DrawString(g,
word, word,
@ -676,31 +675,31 @@ namespace ICSharpCode.TextEditor
#endregion #endregion
#region Conversion Functions #region Conversion Functions
Dictionary<Font, Dictionary<char, float>> fontBoundCharWidth = new Dictionary<Font, Dictionary<char, float>>(); Dictionary<Font, Dictionary<char, int>> fontBoundCharWidth = new Dictionary<Font, Dictionary<char, int>>();
public float GetWidth(char ch, Font font) public int GetWidth(char ch, Font font)
{ {
if (!fontBoundCharWidth.ContainsKey(font)) { if (!fontBoundCharWidth.ContainsKey(font)) {
fontBoundCharWidth.Add(font, new Dictionary<char, float>()); fontBoundCharWidth.Add(font, new Dictionary<char, int>());
} }
if (!fontBoundCharWidth[font].ContainsKey(ch)) { if (!fontBoundCharWidth[font].ContainsKey(ch)) {
using (Graphics g = textArea.CreateGraphics()) { using (Graphics g = textArea.CreateGraphics()) {
return GetWidth(g, ch, font); return GetWidth(g, ch, font);
} }
} }
return (float)fontBoundCharWidth[font][ch]; return fontBoundCharWidth[font][ch];
} }
public float GetWidth(Graphics g, char ch, Font font) public int GetWidth(Graphics g, char ch, Font font)
{ {
if (!fontBoundCharWidth.ContainsKey(font)) { if (!fontBoundCharWidth.ContainsKey(font)) {
fontBoundCharWidth.Add(font, new Dictionary<char, float>()); fontBoundCharWidth.Add(font, new Dictionary<char, int>());
} }
if (!fontBoundCharWidth[font].ContainsKey(ch)) { if (!fontBoundCharWidth[font].ContainsKey(ch)) {
//Console.WriteLine("Calculate character width: " + ch); //Console.WriteLine("Calculate character width: " + ch);
fontBoundCharWidth[font].Add(ch, MeasureStringWidth(g, ch.ToString(), font)); fontBoundCharWidth[font].Add(ch, MeasureStringWidth(g, ch.ToString(), font));
} }
return (float)fontBoundCharWidth[font][ch]; return fontBoundCharWidth[font][ch];
} }
public int GetVisualColumn(int logicalLine, int logicalColumn) public int GetVisualColumn(int logicalLine, int logicalColumn)
@ -896,7 +895,7 @@ namespace ICSharpCode.TextEditor
} }
} }
const float MinTabWidth = 4; const int MinTabWidth = 4;
float CountColumns(ref int column, int start, int end, int logicalLine, Graphics g) float CountColumns(ref int column, int start, int end, int logicalLine, Graphics g)
{ {
@ -1029,6 +1028,7 @@ namespace ICSharpCode.TextEditor
drawingPos += CountColumns(ref column, foldEnd, f.StartColumn, f.StartLine, g); drawingPos += CountColumns(ref column, foldEnd, f.StartColumn, f.StartLine, g);
foldEnd = f.EndColumn; foldEnd = f.EndColumn;
column += f.FoldText.Length; column += f.FoldText.Length;
drawingPos += additionalFoldTextSize;
drawingPos += MeasureStringWidth(g, f.FoldText, TextEditorProperties.FontContainer.RegularFont); drawingPos += MeasureStringWidth(g, f.FoldText, TextEditorProperties.FontContainer.RegularFont);
} }
drawingPos += CountColumns(ref column, foldEnd, logicalColumn, logicalLine, g); drawingPos += CountColumns(ref column, foldEnd, logicalColumn, logicalLine, g);
@ -1044,34 +1044,34 @@ namespace ICSharpCode.TextEditor
g.DrawRectangle(Pens.Blue, rect); g.DrawRectangle(Pens.Blue, rect);
} }
void DrawString(Graphics g, string text, Font font, Color color, float x, float y) void DrawString(Graphics g, string text, Font font, Color color, int x, int y)
{ {
TextRenderer.DrawText(g, text, font, new Point((int)x, (int)y), color, textFormatFlags); TextRenderer.DrawText(g, text, font, new Point(x, y), color, textFormatFlags);
} }
void DrawInvalidLineMarker(Graphics g, float x, float y) void DrawInvalidLineMarker(Graphics g, int x, int y)
{ {
HighlightColor invalidLinesColor = textArea.Document.HighlightingStrategy.GetColorFor("InvalidLines"); HighlightColor invalidLinesColor = textArea.Document.HighlightingStrategy.GetColorFor("InvalidLines");
DrawString(g, "~", invalidLinesColor.GetFont(TextEditorProperties.FontContainer), invalidLinesColor.Color, x, y); DrawString(g, "~", invalidLinesColor.GetFont(TextEditorProperties.FontContainer), invalidLinesColor.Color, x, y);
} }
void DrawSpaceMarker(Graphics g, Color color, float x, float y) void DrawSpaceMarker(Graphics g, Color color, int x, int y)
{ {
HighlightColor spaceMarkerColor = textArea.Document.HighlightingStrategy.GetColorFor("SpaceMarkers"); HighlightColor spaceMarkerColor = textArea.Document.HighlightingStrategy.GetColorFor("SpaceMarkers");
DrawString(g, "\u00B7", spaceMarkerColor.GetFont(TextEditorProperties.FontContainer), color, x, y); DrawString(g, "\u00B7", spaceMarkerColor.GetFont(TextEditorProperties.FontContainer), color, x, y);
} }
void DrawTabMarker(Graphics g, Color color, float x, float y) void DrawTabMarker(Graphics g, Color color, int x, int y)
{ {
HighlightColor tabMarkerColor = textArea.Document.HighlightingStrategy.GetColorFor("TabMarkers"); HighlightColor tabMarkerColor = textArea.Document.HighlightingStrategy.GetColorFor("TabMarkers");
DrawString(g, "\u00BB", tabMarkerColor.GetFont(TextEditorProperties.FontContainer), color, x, y); DrawString(g, "\u00BB", tabMarkerColor.GetFont(TextEditorProperties.FontContainer), color, x, y);
} }
float DrawEOLMarker(Graphics g, Color color, Brush backBrush, float x, float y) int DrawEOLMarker(Graphics g, Color color, Brush backBrush, int x, int y)
{ {
HighlightColor eolMarkerColor = textArea.Document.HighlightingStrategy.GetColorFor("EOLMarkers"); HighlightColor eolMarkerColor = textArea.Document.HighlightingStrategy.GetColorFor("EOLMarkers");
float width = GetWidth('\u00B6', eolMarkerColor.GetFont(TextEditorProperties.FontContainer)); int width = GetWidth('\u00B6', eolMarkerColor.GetFont(TextEditorProperties.FontContainer));
g.FillRectangle(backBrush, g.FillRectangle(backBrush,
new RectangleF(x, y, width, fontHeight)); new RectangleF(x, y, width, fontHeight));

Loading…
Cancel
Save