From 0cb2888c111ad145f76563ad880ba2c0c9854e99 Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Sun, 21 Sep 2008 06:42:39 +0000 Subject: [PATCH] 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 --- .../VBNetFormattingStrategy.cs | 42 +++++++--- .../Test/FormattingStrategy/EndSubTests.cs | 81 ++++++++++++++++--- .../Gui/Pads/ClassBrowser/Nodes/ClassNode.cs | 14 ++-- .../Pads/ClassBrowser/Nodes/ProjectNode.cs | 4 +- 4 files changed, 112 insertions(+), 29 deletions(-) diff --git a/src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs b/src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs index 8502eac48b..7bf7d33fca 100644 --- a/src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs +++ b/src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs @@ -239,7 +239,7 @@ namespace VBNetBinding.FormattingStrategy // fix for SD2-1284 string prevLineText = textArea.Document.GetText(lineAbove); 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); textArea.Document.Replace(curLine.Offset, curLine.Length, indentation + curLineText.Trim()); SmartIndentInternal(textArea, lineNr - 1, lineNr); @@ -381,29 +381,49 @@ namespace VBNetBinding.FormattingStrategy } if (missingEnds.Count > 0) { - return GetClosestMissing(missingEnds, statement, lineNr) != null; + return GetClosestMissing(missingEnds, statement, textArea, lineNr) != null; } else return false; } - Token GetClosestMissing(List missingEnds, VBStatement statement, int lineNr) + Token GetClosestMissing(List missingEnds, VBStatement statement, TextArea textArea, int lineNr) { Token closest = null; int diff = 0; foreach (Token t in missingEnds) { - if (IsMatchingStatement(t, statement) && ((diff = lineNr - t.line + 1) > -1)) { - if (closest == null) - closest = t; - else { - if (diff < lineNr - closest.line + 1) + if (!IsSingleLine(t.line, textArea)) { + if (IsMatchingStatement(t, statement) && ((diff = lineNr - t.line + 1) > -1)) { + if (closest == null) closest = t; + else { + if (diff < lineNr - closest.line + 1) + closest = t; + } } } } 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) { if (begin.kind == end.kind) @@ -672,7 +692,7 @@ namespace VBNetBinding.FormattingStrategy if (begin >= end) { 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; @@ -682,9 +702,9 @@ namespace VBNetBinding.FormattingStrategy for (int i = begin; i < end; 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 == "}") { diff --git a/src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/EndSubTests.cs b/src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/EndSubTests.cs index 242c12bcb3..024bd60e61 100644 --- a/src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/EndSubTests.cs +++ b/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. /// [TestFixture] - public class EndSubTests + public class EndInsertionTests { [TestFixtureSetUp] public void SetUpFixture() @@ -33,19 +33,82 @@ namespace VBNetBinding.Tests public void EndSub() { string code = "Public Class Foo\r\n" + - "\tPublic Sub Bar\r\n" + - "\r\n" + // This extra new line is required. This is the new line just entered by the user. - "End Class"; - + "\tPublic Sub Bar\r\n" + + "\r\n" + // This extra new line is required. This is the new line just entered by the user. + "End Class"; + string bar = "Bar"; int cursorOffset = code.IndexOf(bar) + bar.Length; int line = 2; string expectedCode = "Public Class Foo\r\n" + - "\tPublic Sub Bar\r\n" + - "\t\t\r\n" + - "\tEnd Sub\r\n" + - "End Class"; + "\tPublic Sub Bar\r\n" + + "\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); + } + } + + [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; diff --git a/src/Main/Base/Project/Src/Gui/Pads/ClassBrowser/Nodes/ClassNode.cs b/src/Main/Base/Project/Src/Gui/Pads/ClassBrowser/Nodes/ClassNode.cs index a7262dd665..2df3e3c692 100644 --- a/src/Main/Base/Project/Src/Gui/Pads/ClassBrowser/Nodes/ClassNode.cs +++ b/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) { - new BaseTypesNode(project, c).AddTo(this); + new BaseTypesNode(project, c).InsertSorted(this); } 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) { - new ClassNode(project, innerClass).AddTo(this); + new ClassNode(project, innerClass).InsertSorted(this); } foreach (IMethod method in c.Methods) { - new MemberNode(method).AddTo(this); + new MemberNode(method).InsertSorted(this); } foreach (IProperty property in c.Properties) { - new MemberNode(property).AddTo(this); + new MemberNode(property).InsertSorted(this); } foreach (IField field in c.Fields) { - new MemberNode(field).AddTo(this); + new MemberNode(field).InsertSorted(this); } foreach (IEvent e in c.Events) { - new MemberNode(e).AddTo(this); + new MemberNode(e).InsertSorted(this); } UpdateVisibility(); } diff --git a/src/Main/Base/Project/Src/Gui/Pads/ClassBrowser/Nodes/ProjectNode.cs b/src/Main/Base/Project/Src/Gui/Pads/ClassBrowser/Nodes/ProjectNode.cs index cf50918099..3177f5ab2e 100644 --- a/src/Main/Base/Project/Src/Gui/Pads/ClassBrowser/Nodes/ProjectNode.cs +++ b/src/Main/Base/Project/Src/Gui/Pads/ClassBrowser/Nodes/ProjectNode.cs @@ -52,7 +52,7 @@ namespace ICSharpCode.SharpDevelop.Gui.ClassBrowser if (node != null) { node.Class = c.GetCompoundClass(); } else { - new ClassNode(Project, c.GetCompoundClass()).AddTo(path); + new ClassNode(Project, c.GetCompoundClass()).InsertSorted(path); } wasUpdatedDictionary[c.DotNetName] = true; } @@ -111,7 +111,7 @@ namespace ICSharpCode.SharpDevelop.Gui.ClassBrowser TreeNode path = GetNodeByPath(c.Namespace, true); TreeNode node = GetNodeByClass(path.Nodes, c); if (node == null) { - new ClassNode(Project, c.GetCompoundClass()).AddTo(path); + new ClassNode(Project, c.GetCompoundClass()).InsertSorted(path); } } }