diff --git a/src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs b/src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs index f1a315456b..ea9f3c51d9 100644 --- a/src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs +++ b/src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs @@ -646,7 +646,10 @@ namespace ICSharpCode.VBNetBinding for (int i = blockStart; i <= blockEnd; i++) { IDocumentLine curLine = editor.Document.GetLine(i); string lineText = curLine.Text.TrimStart(); - string noComments = lineText.TrimComments().TrimEnd(); + // preprocessor directives cannot be multiline (just as comments) + // and they are not included in normal block indentation -> + // treat preprocessor directives as comments -> remove them + string noComments = lineText.TrimComments().TrimPreprocessorDirectives().TrimEnd(); // adjust indentation if the current line is not selected // lines between the selection will be aligned to the selected level diff --git a/src/AddIns/BackendBindings/VBNetBinding/Project/Src/LanguageUtils.cs b/src/AddIns/BackendBindings/VBNetBinding/Project/Src/LanguageUtils.cs index 3677dfd1de..527b9c43ee 100644 --- a/src/AddIns/BackendBindings/VBNetBinding/Project/Src/LanguageUtils.cs +++ b/src/AddIns/BackendBindings/VBNetBinding/Project/Src/LanguageUtils.cs @@ -26,6 +26,29 @@ namespace ICSharpCode.VBNetBinding return line; } + public static string TrimPreprocessorDirectives(this string line) + { + if (string.IsNullOrEmpty(line)) + return string.Empty; + + bool inStr = false; + bool wsOnly = true; + + for (int i = 0; i < line.Length; i++) { + if (line[i] == '#' && wsOnly) { + if (i < line.Length - 1) { + if (char.IsLetter(line[i + 1])) + return line.Substring(0, i); + } else + return line.Substring(0, i); + } + if (!char.IsWhiteSpace(line[i])) + wsOnly = false; + } + + return line; + } + public static string TrimLine(this string line) { if (string.IsNullOrEmpty(line)) diff --git a/src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/IndentationTests.cs b/src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/IndentationTests.cs index 2394108efb..32cd42061f 100644 --- a/src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/IndentationTests.cs +++ b/src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/IndentationTests.cs @@ -454,6 +454,32 @@ End Class"; RunFormatTest(code, expected); } + [Test] + public void Region() + { + string expected = @"Module Program + #Region Test + + #End Region + + Sub asdf() + + End Sub +End Module"; + + string code = @"Module Program +#Region Test + +#End Region + +Sub asdf() + +End Sub +End Module"; + + RunFormatTest(code, expected); + } + void RunFormatTest(string code, string expectedCode) { AvalonEditTextEditorAdapter editor = new AvalonEditTextEditorAdapter(new TextEditor()); diff --git a/src/AddIns/BackendBindings/VBNetBinding/Test/LanguageUtilsTests.cs b/src/AddIns/BackendBindings/VBNetBinding/Test/LanguageUtilsTests.cs new file mode 100644 index 0000000000..407777e83e --- /dev/null +++ b/src/AddIns/BackendBindings/VBNetBinding/Test/LanguageUtilsTests.cs @@ -0,0 +1,72 @@ +// Copyright (c) AlphaSierraPapa for the SharpDevelop Team (for details please see \doc\copyright.txt) +// This code is distributed under the GNU LGPL (for details please see \doc\license.txt) + +using System; +using NUnit.Framework; + +namespace ICSharpCode.VBNetBinding.Tests +{ + [TestFixture] + public class LanguageUtilsTests + { + [Test] + public void TrimPreprocessorDirectives1() + { + string input = " #Region Test"; + string expectedOutput = " "; + + TestTrimPreprocessorDirectives(input, expectedOutput); + } + + [Test] + public void TrimPreprocessorDirectives2() + { + string input = "Dim a# = 12.0"; + string expectedOutput = "Dim a# = 12.0"; + + TestTrimPreprocessorDirectives(input, expectedOutput); + } + + [Test] + public void TrimPreprocessorDirectives3() + { + string input = "Dim a = 12.0#"; + string expectedOutput = "Dim a = 12.0#"; + + TestTrimPreprocessorDirectives(input, expectedOutput); + } + + [Test] + public void TrimPreprocessorDirectives4() + { + string input = " #10/10/2010#"; + string expectedOutput = " #10/10/2010#"; + + TestTrimPreprocessorDirectives(input, expectedOutput); + } + + [Test] + public void TrimPreprocessorDirectives5() + { + string input = "Dim a = 12.0# "; + string expectedOutput = "Dim a = 12.0# "; + + TestTrimPreprocessorDirectives(input, expectedOutput); + } + + [Test] + public void TrimPreprocessorDirectives6() + { + string input = " #10/10/2010# "; + string expectedOutput = " #10/10/2010# "; + + TestTrimPreprocessorDirectives(input, expectedOutput); + } + + void TestTrimPreprocessorDirectives(string input, string expectedOutput) + { + string output = LanguageUtils.TrimPreprocessorDirectives(input); + Assert.AreEqual(expectedOutput, output); + } + } +} diff --git a/src/AddIns/BackendBindings/VBNetBinding/Test/VBNetBinding.Tests.csproj b/src/AddIns/BackendBindings/VBNetBinding/Test/VBNetBinding.Tests.csproj index 37d42030d2..fdedcb7e74 100644 --- a/src/AddIns/BackendBindings/VBNetBinding/Test/VBNetBinding.Tests.csproj +++ b/src/AddIns/BackendBindings/VBNetBinding/Test/VBNetBinding.Tests.csproj @@ -56,6 +56,7 @@ +