Browse Source

fix #287: Incorrect indentation when inserting newline into XmlDoc

pull/343/merge
Siegfried Pammer 12 years ago
parent
commit
e1ab441f06
  1. 30
      src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs

30
src/AddIns/BackendBindings/CSharpBinding/Project/Src/FormattingStrategy/CSharpFormattingStrategy.cs

@ -21,6 +21,7 @@ using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using ICSharpCode.AvalonEdit.Document;
using ICSharpCode.AvalonEdit.Highlighting; using ICSharpCode.AvalonEdit.Highlighting;
using ICSharpCode.AvalonEdit.Indentation.CSharp; using ICSharpCode.AvalonEdit.Indentation.CSharp;
using ICSharpCode.Core; using ICSharpCode.Core;
@ -62,11 +63,10 @@ namespace CSharpBinding.FormattingStrategy
{ {
engine.Update(line.EndOffset); engine.Update(line.EndOffset);
if (engine.NeedsReindent) { if (engine.NeedsReindent) {
int textOffset = line.Offset; var indentation = TextUtilities.GetWhitespaceAfter(document, line.Offset);
while (textOffset < line.EndOffset && char.IsWhiteSpace(document.GetCharAt(textOffset))) // replacing the indentation in two steps is necessary to make the caret move accordingly.
textOffset++; document.Replace(indentation.Offset, indentation.Length, "");
string newText = document.GetText(textOffset, line.Length + line.Offset - textOffset); document.Replace(indentation.Offset, 0, engine.ThisLineIndent);
document.Replace(line.Offset, line.Length, engine.ThisLineIndent + newText);
engine.ResetEngineToPosition(line.Offset); engine.ResetEngineToPosition(line.Offset);
} }
} }
@ -194,7 +194,7 @@ namespace CSharpBinding.FormattingStrategy
char ch = textArea.Document.GetCharAt(i); char ch = textArea.Document.GetCharAt(i);
if (ch == '"') { if (ch == '"') {
// parsing strings correctly is too complicated (see above), // parsing strings correctly is too complicated (see above),
// but I don't now any case where a doc comment is after a string... // but I don't know any case where a doc comment is after a string...
return false; return false;
} }
if (ch == '/' && i + 2 < cursorOffset && textArea.Document.GetCharAt(i + 1) == '/' && textArea.Document.GetCharAt(i + 2) == '/') { if (ch == '/' && i + 2 < cursorOffset && textArea.Document.GetCharAt(i + 1) == '/' && textArea.Document.GetCharAt(i + 2) == '/') {
@ -274,11 +274,11 @@ namespace CSharpBinding.FormattingStrategy
string terminator = DocumentUtilities.GetLineTerminator(textArea.Document, lineNr); string terminator = DocumentUtilities.GetLineTerminator(textArea.Document, lineNr);
string curLineText; string curLineText;
//// local string for curLine segment // local string for curLine segment
if (ch == '/') { if (ch == '/') {
curLineText = textArea.Document.GetText(curLine); curLineText = textArea.Document.GetText(curLine);
string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove); string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove);
if (curLineText != null && curLineText.EndsWith("///") && (lineAboveText == null || !lineAboveText.Trim().StartsWith("///"))) { if (curLineText != null && curLineText.EndsWith("///", StringComparison.Ordinal) && (lineAboveText == null || !lineAboveText.Trim().StartsWith("///", StringComparison.Ordinal))) {
string indentation = DocumentUtilities.GetWhitespaceAfter(textArea.Document, curLine.Offset); string indentation = DocumentUtilities.GetWhitespaceAfter(textArea.Document, curLine.Offset);
IUnresolvedEntity member = GetMemberAfter(textArea, lineNr); IUnresolvedEntity member = GetMemberAfter(textArea, lineNr);
if (member != null) { if (member != null) {
@ -341,10 +341,10 @@ namespace CSharpBinding.FormattingStrategy
commentBuilder.Append(curLineText[ i]); commentBuilder.Append(curLineText[ i]);
} }
string tag = commentBuilder.ToString().Trim(); string tag = commentBuilder.ToString().Trim();
if (!tag.EndsWith(">")) { if (!tag.EndsWith(">", StringComparison.Ordinal)) {
tag += ">"; tag += ">";
} }
if (!tag.StartsWith("/")) { if (!tag.StartsWith("/", StringComparison.Ordinal)) {
textArea.Document.Insert(cursorOffset, "</" + tag.Substring(1), AnchorMovementType.BeforeInsertion); textArea.Document.Insert(cursorOffset, "</" + tag.Substring(1), AnchorMovementType.BeforeInsertion);
} }
} }
@ -361,10 +361,10 @@ namespace CSharpBinding.FormattingStrategy
break; break;
case '\n': case '\n':
string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove); string lineAboveText = lineAbove == null ? "" : textArea.Document.GetText(lineAbove);
//// curLine might have some text which should be added to indentation // curLine might have some text which should be added to indentation
curLineText = textArea.Document.GetText(curLine); curLineText = textArea.Document.GetText(curLine);
if (lineAboveText != null && lineAboveText.Trim().StartsWith("#region") if (lineAboveText != null && lineAboveText.Trim().StartsWith("#region", StringComparison.Ordinal)
&& NeedEndregion(textArea.Document)) && NeedEndregion(textArea.Document))
{ {
textArea.Document.Insert(cursorOffset, "#endregion"); textArea.Document.Insert(cursorOffset, "#endregion");
@ -398,8 +398,8 @@ namespace CSharpBinding.FormattingStrategy
IDocumentLine nextLine = lineNr + 1 <= textArea.Document.LineCount ? textArea.Document.GetLineByNumber(lineNr + 1) : null; IDocumentLine nextLine = lineNr + 1 <= textArea.Document.LineCount ? textArea.Document.GetLineByNumber(lineNr + 1) : null;
string nextLineText = (nextLine != null) ? textArea.Document.GetText(nextLine) : ""; string nextLineText = (nextLine != null) ? textArea.Document.GetText(nextLine) : "";
int indexAbove = lineAboveText.IndexOf("///"); int indexAbove = lineAboveText.IndexOf("///", StringComparison.Ordinal);
int indexNext = nextLineText.IndexOf("///"); int indexNext = nextLineText.IndexOf("///", StringComparison.Ordinal);
if (indexAbove > 0 && (indexNext != -1 || indexAbove + 4 < lineAbove.Length)) { if (indexAbove > 0 && (indexNext != -1 || indexAbove + 4 < lineAbove.Length)) {
textArea.Document.Insert(cursorOffset, "/// "); textArea.Document.Insert(cursorOffset, "/// ");
return; return;
@ -413,7 +413,7 @@ namespace CSharpBinding.FormattingStrategy
} }
if (textArea.Options.AutoInsertBlockEnd && lineAbove != null && isInNormalCode) { if (textArea.Options.AutoInsertBlockEnd && lineAbove != null && isInNormalCode) {
string oldLineText = textArea.Document.GetText(lineAbove); string oldLineText = textArea.Document.GetText(lineAbove);
if (oldLineText.EndsWith("{")) { if (oldLineText.EndsWith("{", StringComparison.Ordinal)) {
if (NeedCurlyBracket(textArea.Document.Text)) { if (NeedCurlyBracket(textArea.Document.Text)) {
int insertionPoint = curLine.Offset + curLine.Length; int insertionPoint = curLine.Offset + curLine.Length;
textArea.Document.Insert(insertionPoint, terminator + "}"); textArea.Document.Insert(insertionPoint, terminator + "}");

Loading…
Cancel
Save