From 85a49fecab409475ab91578a8c5f0c089308a9ff Mon Sep 17 00:00:00 2001 From: Siegfried Pammer Date: Mon, 15 Dec 2008 17:50:51 +0000 Subject: [PATCH] fixed insertion of End-Statements in VBNetBinding git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3684 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61 --- .../VBNetFormattingStrategy.cs | 27 +++++---- .../Test/FormattingStrategy/InterfaceTests.cs | 58 +++++++++++++++++++ .../Test/VBNetBinding.Tests.csproj | 1 + 3 files changed, 76 insertions(+), 10 deletions(-) create mode 100644 src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/InterfaceTests.cs diff --git a/src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs b/src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs index 7bf7d33fca..f2898034b7 100644 --- a/src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs +++ b/src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs @@ -7,6 +7,7 @@ using System; using System.Collections.Generic; +using System.Diagnostics; using System.IO; using System.Text; using System.Text.RegularExpressions; @@ -229,7 +230,7 @@ namespace VBNetBinding.FormattingStrategy for (int i = 0; i < statement.IndentPlus; i++) { indentation += Tab.GetIndentationString(textArea.Document); } - + textArea.Document.Replace(curLine.Offset, curLine.Length, indentation + curLineText.Trim()); textArea.Caret.Column = indentation.Length; return; @@ -347,6 +348,11 @@ namespace VBNetBinding.FormattingStrategy return !inString; } + bool IsDeclaration(int type) + { + return (type == Tokens.Class) || (type == Tokens.Module); + } + bool IsEndStatementNeeded(TextArea textArea, ref VBStatement statement, int lineNr) { Stack tokens = new Stack(); @@ -356,24 +362,24 @@ namespace VBNetBinding.FormattingStrategy Token currentToken = null; Token prevToken = null; - + while ((currentToken = lexer.NextToken()).kind != Tokens.EOF) { if (prevToken == null) prevToken = currentToken; if (IsBlockStart(lexer, currentToken, prevToken)) { - tokens.Push(currentToken); + if ((tokens.Count > 0 && tokens.Peek().kind != Tokens.Interface) || IsDeclaration(currentToken.kind)) + tokens.Push(currentToken); } if (IsBlockEnd(currentToken, prevToken)) { while (tokens.Count > 0 && !IsMatchingEnd(tokens.Peek(), currentToken)) { - Token t = null; - missingEnds.Add(t = tokens.Pop()); + missingEnds.Add(tokens.Pop()); } - if (tokens.Count != 0) { - if (IsMatchingEnd(tokens.Peek(), currentToken)) { + + if (tokens.Count > 0) { + if (IsMatchingEnd(tokens.Peek(), currentToken)) tokens.Pop(); - } } } @@ -394,15 +400,16 @@ namespace VBNetBinding.FormattingStrategy foreach (Token t in missingEnds) { if (!IsSingleLine(t.line, textArea)) { if (IsMatchingStatement(t, statement) && ((diff = lineNr - t.line + 1) > -1)) { - if (closest == null) + if (closest == null) { closest = t; - else { + } else { if (diff < lineNr - closest.line + 1) closest = t; } } } } + return closest; } diff --git a/src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/InterfaceTests.cs b/src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/InterfaceTests.cs new file mode 100644 index 0000000000..e3d3327718 --- /dev/null +++ b/src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/InterfaceTests.cs @@ -0,0 +1,58 @@ +// +// +// +// +// $Revision: 3548 $ +// +using System; +using NUnit.Framework; +using ICSharpCode.Core; +using ICSharpCode.SharpDevelop; +using ICSharpCode.TextEditor; +using VBNetBinding; +using VBNetBinding.FormattingStrategy; + +namespace VBNetBinding.Tests +{ + /// + /// Tests the special case of an interface. for ex. no insertion of End-Tags etc. + /// + [TestFixture] + public class InterfaceTests + { + [TestFixtureSetUp] + public void SetUpFixture() + { + if (!PropertyService.Initialized) { + PropertyService.InitializeService(String.Empty, String.Empty, "VBNetBindingTests"); + } + } + + [Test] + public void InterfaceEndSub() + { + string code = "Public Interface 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 Interface"; + + string bar = "Sub Bar"; + int cursorOffset = code.IndexOf(bar) + bar.Length; + int line = 2; + + string expectedCode = "Public Interface Foo\r\n" + + "\tPublic Sub Bar\r\n" + + "\t\t\r\n" + + "End Interface"; + + 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); + } + } + } +} diff --git a/src/AddIns/BackendBindings/VBNetBinding/Test/VBNetBinding.Tests.csproj b/src/AddIns/BackendBindings/VBNetBinding/Test/VBNetBinding.Tests.csproj index 5309ce7e21..4c853ff983 100644 --- a/src/AddIns/BackendBindings/VBNetBinding/Test/VBNetBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/VBNetBinding/Test/VBNetBinding.Tests.csproj @@ -64,6 +64,7 @@ +