Browse Source

Added tests for VBNetFormattingStrategy; fixed inserting of end statements after single line statements; fixed SD2-1319

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3548 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Siegfried Pammer 18 years ago
parent
commit
0cb2888c11
  1. 32
      src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs
  2. 65
      src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/EndSubTests.cs
  3. 14
      src/Main/Base/Project/Src/Gui/Pads/ClassBrowser/Nodes/ClassNode.cs
  4. 4
      src/Main/Base/Project/Src/Gui/Pads/ClassBrowser/Nodes/ProjectNode.cs

32
src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs

@ -239,7 +239,7 @@ namespace VBNetBinding.FormattingStrategy
// fix for SD2-1284 // fix for SD2-1284
string prevLineText = textArea.Document.GetText(lineAbove); string prevLineText = textArea.Document.GetText(lineAbove);
string prevLineText2 = (lineNr > 1) ? textArea.Document.GetText(textArea.Document.GetLineSegment(lineNr - 2)) : ""; string prevLineText2 = (lineNr > 1) ? textArea.Document.GetText(textArea.Document.GetLineSegment(lineNr - 2)) : "";
if (StripComment(prevLineText.ToLowerInvariant()).Trim(' ', '\t', '\n').StartsWith("case")) { if (StripComment(prevLineText.ToLowerInvariant()).Trim(' ', '\t', '\r', '\n').StartsWith("case")) {
string indentation = GetIndentation(textArea, lineNr - 1) + Tab.GetIndentationString(textArea.Document); string indentation = GetIndentation(textArea, lineNr - 1) + Tab.GetIndentationString(textArea.Document);
textArea.Document.Replace(curLine.Offset, curLine.Length, indentation + curLineText.Trim()); textArea.Document.Replace(curLine.Offset, curLine.Length, indentation + curLineText.Trim());
SmartIndentInternal(textArea, lineNr - 1, lineNr); SmartIndentInternal(textArea, lineNr - 1, lineNr);
@ -381,17 +381,18 @@ namespace VBNetBinding.FormattingStrategy
} }
if (missingEnds.Count > 0) { if (missingEnds.Count > 0) {
return GetClosestMissing(missingEnds, statement, lineNr) != null; return GetClosestMissing(missingEnds, statement, textArea, lineNr) != null;
} else } else
return false; return false;
} }
Token GetClosestMissing(List<Token> missingEnds, VBStatement statement, int lineNr) Token GetClosestMissing(List<Token> missingEnds, VBStatement statement, TextArea textArea, int lineNr)
{ {
Token closest = null; Token closest = null;
int diff = 0; int diff = 0;
foreach (Token t in missingEnds) { foreach (Token t in missingEnds) {
if (!IsSingleLine(t.line, textArea)) {
if (IsMatchingStatement(t, statement) && ((diff = lineNr - t.line + 1) > -1)) { if (IsMatchingStatement(t, statement) && ((diff = lineNr - t.line + 1) > -1)) {
if (closest == null) if (closest == null)
closest = t; closest = t;
@ -401,9 +402,28 @@ namespace VBNetBinding.FormattingStrategy
} }
} }
} }
}
return closest; return closest;
} }
bool IsSingleLine(int line, TextArea textArea)
{
if (line < 1)
return false;
LineSegment lineSeg = textArea.Document.GetLineSegment(line - 1);
if (lineSeg == null)
return false;
string text = textArea.Document.GetText(lineSeg);
if (StripComment(text).Trim(' ', '\t', '\r', '\n').EndsWith("_"))
return true;
else
return false;
}
bool IsMatchingEnd(Token begin, Token end) bool IsMatchingEnd(Token begin, Token end)
{ {
if (begin.kind == end.kind) if (begin.kind == end.kind)
@ -672,7 +692,7 @@ namespace VBNetBinding.FormattingStrategy
if (begin >= end) { if (begin >= end) {
LineSegment curLine = textArea.Document.GetLineSegment(begin); LineSegment curLine = textArea.Document.GetLineSegment(begin);
string lineText = textArea.Document.GetText(curLine).Trim(' ', '\t', '\n'); string lineText = textArea.Document.GetText(curLine).Trim(' ', '\t', '\r', '\n');
string newLine = new string('\t', tabs) + new string(' ', spaces) + lineText; string newLine = new string('\t', tabs) + new string(' ', spaces) + lineText;
@ -682,9 +702,9 @@ namespace VBNetBinding.FormattingStrategy
for (int i = begin; i < end; i++) { for (int i = begin; i < end; i++) {
LineSegment curLine = textArea.Document.GetLineSegment(i); LineSegment curLine = textArea.Document.GetLineSegment(i);
string lineText = textArea.Document.GetText(curLine).Trim(' ', '\t', '\n'); string lineText = textArea.Document.GetText(curLine).Trim(' ', '\t', '\r', '\n');
string noComments = StripComment(lineText).TrimEnd(' ', '\t', '\n'); string noComments = StripComment(lineText).TrimEnd(' ', '\t', '\r', '\n');
if (otherMultiLine && noComments == "}") { if (otherMultiLine && noComments == "}") {

65
src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/EndSubTests.cs

@ -19,7 +19,7 @@ namespace VBNetBinding.Tests
/// Tests that Operator overrides have "End Operator" added after the user presses the return key. /// Tests that Operator overrides have "End Operator" added after the user presses the return key.
/// </summary> /// </summary>
[TestFixture] [TestFixture]
public class EndSubTests public class EndInsertionTests
{ {
[TestFixtureSetUp] [TestFixtureSetUp]
public void SetUpFixture() public void SetUpFixture()
@ -56,5 +56,68 @@ namespace VBNetBinding.Tests
Assert.AreEqual(expectedCode, editor.Document.TextContent); Assert.AreEqual(expectedCode, editor.Document.TextContent);
} }
} }
[Test]
public void EndIf()
{
string code = "Public Class Foo\r\n" +
"\tPublic Sub Bar\r\n" +
"\t\tIf True Then\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"\tEnd Sub\r\n" +
"End Class";
string bar = "Then";
int cursorOffset = code.IndexOf(bar) + bar.Length;
int line = 3;
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Sub Bar\r\n" +
"\t\tIf True Then\r\n" +
"\t\t\t\r\n" +
"\t\tEnd If\r\n" +
"\tEnd Sub\r\n" +
"End Class";
using (TextEditorControl editor = new TextEditorControl()) {
editor.Document.TextContent = code;
editor.ActiveTextAreaControl.Caret.Position = editor.Document.OffsetToPosition(cursorOffset);
VBFormattingStrategy formattingStrategy = new VBFormattingStrategy();
formattingStrategy.FormatLine(editor.ActiveTextAreaControl.TextArea, line, cursorOffset, '\n');
Assert.AreEqual(expectedCode, editor.Document.TextContent);
}
}
[Test]
public void SingleLineIf()
{
string code = "Public Class Foo\r\n" +
"\tPublic Sub Bar\r\n" +
"\t\tIf True Then _\r\n" +
"\r\n" + // This extra new line is required. This is the new line just entered by the user.
"\tEnd Sub\r\n" +
"End Class";
string bar = "Then _";
int cursorOffset = code.IndexOf(bar) + bar.Length;
int line = 3;
string expectedCode = "Public Class Foo\r\n" +
"\tPublic Sub Bar\r\n" +
"\t\tIf True Then _\r\n" +
"\t\t\t\r\n" +
"\tEnd Sub\r\n" +
"End Class";
using (TextEditorControl editor = new TextEditorControl()) {
editor.Document.TextContent = code;
editor.ActiveTextAreaControl.Caret.Position = editor.Document.OffsetToPosition(cursorOffset);
VBFormattingStrategy formattingStrategy = new VBFormattingStrategy();
formattingStrategy.FormatLine(editor.ActiveTextAreaControl.TextArea, line, cursorOffset, '\n');
Assert.AreEqual(expectedCode, editor.Document.TextContent);
}
}
} }
} }

14
src/Main/Base/Project/Src/Gui/Pads/ClassBrowser/Nodes/ClassNode.cs

@ -75,30 +75,30 @@ namespace ICSharpCode.SharpDevelop.Gui.ClassBrowser
} }
if (c.BaseTypes.Count > 0) { if (c.BaseTypes.Count > 0) {
new BaseTypesNode(project, c).AddTo(this); new BaseTypesNode(project, c).InsertSorted(this);
} }
if ((c.Modifiers & ModifierEnum.Sealed) != ModifierEnum.Sealed) { if ((c.Modifiers & ModifierEnum.Sealed) != ModifierEnum.Sealed) {
new DerivedTypesNode(project, c).AddTo(this); new DerivedTypesNode(project, c).InsertSorted(this);
} }
foreach (IClass innerClass in c.InnerClasses) { foreach (IClass innerClass in c.InnerClasses) {
new ClassNode(project, innerClass).AddTo(this); new ClassNode(project, innerClass).InsertSorted(this);
} }
foreach (IMethod method in c.Methods) { foreach (IMethod method in c.Methods) {
new MemberNode(method).AddTo(this); new MemberNode(method).InsertSorted(this);
} }
foreach (IProperty property in c.Properties) { foreach (IProperty property in c.Properties) {
new MemberNode(property).AddTo(this); new MemberNode(property).InsertSorted(this);
} }
foreach (IField field in c.Fields) { foreach (IField field in c.Fields) {
new MemberNode(field).AddTo(this); new MemberNode(field).InsertSorted(this);
} }
foreach (IEvent e in c.Events) { foreach (IEvent e in c.Events) {
new MemberNode(e).AddTo(this); new MemberNode(e).InsertSorted(this);
} }
UpdateVisibility(); UpdateVisibility();
} }

4
src/Main/Base/Project/Src/Gui/Pads/ClassBrowser/Nodes/ProjectNode.cs

@ -52,7 +52,7 @@ namespace ICSharpCode.SharpDevelop.Gui.ClassBrowser
if (node != null) { if (node != null) {
node.Class = c.GetCompoundClass(); node.Class = c.GetCompoundClass();
} else { } else {
new ClassNode(Project, c.GetCompoundClass()).AddTo(path); new ClassNode(Project, c.GetCompoundClass()).InsertSorted(path);
} }
wasUpdatedDictionary[c.DotNetName] = true; wasUpdatedDictionary[c.DotNetName] = true;
} }
@ -111,7 +111,7 @@ namespace ICSharpCode.SharpDevelop.Gui.ClassBrowser
TreeNode path = GetNodeByPath(c.Namespace, true); TreeNode path = GetNodeByPath(c.Namespace, true);
TreeNode node = GetNodeByClass(path.Nodes, c); TreeNode node = GetNodeByClass(path.Nodes, c);
if (node == null) { if (node == null) {
new ClassNode(Project, c.GetCompoundClass()).AddTo(path); new ClassNode(Project, c.GetCompoundClass()).InsertSorted(path);
} }
} }
} }

Loading…
Cancel
Save