Browse Source

fix wrong indentation when dealing with preprocessor directives (http://community.sharpdevelop.net/forums/p/13149/35570.aspx#35570)

pull/15/head
Siegfried Pammer 14 years ago
parent
commit
ba4afb7792
  1. 5
      src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs
  2. 23
      src/AddIns/BackendBindings/VBNetBinding/Project/Src/LanguageUtils.cs
  3. 26
      src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/IndentationTests.cs
  4. 72
      src/AddIns/BackendBindings/VBNetBinding/Test/LanguageUtilsTests.cs
  5. 1
      src/AddIns/BackendBindings/VBNetBinding/Test/VBNetBinding.Tests.csproj

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

@ -646,7 +646,10 @@ namespace ICSharpCode.VBNetBinding @@ -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

23
src/AddIns/BackendBindings/VBNetBinding/Project/Src/LanguageUtils.cs

@ -26,6 +26,29 @@ namespace ICSharpCode.VBNetBinding @@ -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))

26
src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/IndentationTests.cs

@ -454,6 +454,32 @@ End Class"; @@ -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());

72
src/AddIns/BackendBindings/VBNetBinding/Test/LanguageUtilsTests.cs

@ -0,0 +1,72 @@ @@ -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);
}
}
}

1
src/AddIns/BackendBindings/VBNetBinding/Test/VBNetBinding.Tests.csproj

@ -56,6 +56,7 @@ @@ -56,6 +56,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="LanguageUtilsTests.cs" />
<Compile Include="MockTextEditor.cs" />
<Compile Include="TextEditorBasedTests.cs" />
<ProjectReference Include="..\..\..\..\Libraries\AvalonEdit\ICSharpCode.AvalonEdit\ICSharpCode.AvalonEdit.csproj">

Loading…
Cancel
Save