Browse Source

properly indent declarations with attributes (http://community.sharpdevelop.net/forums/t/13157.aspx)

pull/15/head
Siegfried Pammer 15 years ago
parent
commit
be773345b9
  1. 27
      src/AddIns/BackendBindings/VBNetBinding/Project/Src/FormattingStrategy/VBNetFormattingStrategy.cs
  2. 1
      src/AddIns/BackendBindings/VBNetBinding/Project/Src/LanguageUtils.cs
  3. 44
      src/AddIns/BackendBindings/VBNetBinding/Test/FormattingStrategy/IndentationTests.cs
  4. 9
      src/Libraries/NRefactory/Project/Src/Lexer/VBNet/ExpressionFinder.atg
  5. 16
      src/Libraries/NRefactory/Project/Src/Lexer/VBNet/ExpressionFinder.cs
  6. 2572
      src/Libraries/NRefactory/Project/Src/Lexer/VBNet/Parser.cs

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

@ -83,7 +83,7 @@ namespace ICSharpCode.VBNetBinding @@ -83,7 +83,7 @@ namespace ICSharpCode.VBNetBinding
new int[] {
Tokens.Class, Tokens.Module, Tokens.Namespace, Tokens.Interface, Tokens.Structure,
Tokens.Sub, Tokens.Function, Tokens.Operator, Tokens.Enum,
Tokens.If, Tokens.For, Tokens.Do, Tokens.While, Tokens.With, Tokens.Select, Tokens.Try, Tokens.Using, Tokens.SyncLock,
Tokens.If, Tokens.For, Tokens.Do, Tokens.While, Tokens.With, Tokens.Select, Tokens.Try, Tokens.Using, Tokens.SyncLock,
Tokens.Property, Tokens.Get, Tokens.Set
});
#endregion
@ -514,6 +514,8 @@ namespace ICSharpCode.VBNetBinding @@ -514,6 +514,8 @@ namespace ICSharpCode.VBNetBinding
{
ILexer lexer = ParserFactory.CreateLexer(SupportedLanguage.VBNet, new StringReader(editor.Document.Text));
ExpressionFinder context = new ExpressionFinder();
Stack<string> indentation = new Stack<string>();
indentation.Push(string.Empty);
@ -530,7 +532,14 @@ namespace ICSharpCode.VBNetBinding @@ -530,7 +532,14 @@ namespace ICSharpCode.VBNetBinding
int blockStart = 1;
int lambdaNesting = 0;
bool sawAttribute = false;
while ((currentToken = lexer.NextToken()).Kind != Tokens.EOF) {
if (context.InContext(Context.Attribute) && currentToken.Kind == Tokens.GreaterThan)
sawAttribute = true;
context.InformToken(currentToken);
if (prevToken == null)
prevToken = currentToken;
@ -544,7 +553,7 @@ namespace ICSharpCode.VBNetBinding @@ -544,7 +553,7 @@ namespace ICSharpCode.VBNetBinding
isDeclare = true;
if (currentToken.Kind == Tokens.EOL) {
isDelegate = isDeclare = isMustOverride = false;
isDelegate = isDeclare = isMustOverride = sawAttribute = false;
eols.Add(currentToken.Location.Line);
}
@ -559,12 +568,12 @@ namespace ICSharpCode.VBNetBinding @@ -559,12 +568,12 @@ namespace ICSharpCode.VBNetBinding
// blockEnd++;
// }
ApplyToRange(editor, indentation, eols, blockStart, blockEnd, begin, end);
ApplyToRange(editor, indentation, eols, blockStart, blockEnd, begin, end, sawAttribute);
if (lambdaNesting > 0 && (currentToken.Kind == Tokens.Function || currentToken.Kind == Tokens.Sub)) {
Unindent(indentation);
ApplyToRange(editor, indentation, eols, currentToken.Location.Line, currentToken.Location.Line, begin, end);
ApplyToRange(editor, indentation, eols, currentToken.Location.Line, currentToken.Location.Line, begin, end, sawAttribute);
}
if (currentToken.Kind == Tokens.Interface)
@ -593,7 +602,7 @@ namespace ICSharpCode.VBNetBinding @@ -593,7 +602,7 @@ namespace ICSharpCode.VBNetBinding
// hence we indent from blockStart to the this line
int lastVisualLine = FindNextEol(lexer);
eols.Add(lastVisualLine);
ApplyToRange(editor, indentation, eols, blockStart, lastVisualLine, begin, end);
ApplyToRange(editor, indentation, eols, blockStart, lastVisualLine, begin, end, sawAttribute);
if (isMultiLineLambda && (currentToken.Kind == Tokens.Function || currentToken.Kind == Tokens.Sub)) {
lambdaNesting++;
@ -620,7 +629,7 @@ namespace ICSharpCode.VBNetBinding @@ -620,7 +629,7 @@ namespace ICSharpCode.VBNetBinding
prevToken = currentToken;
}
ApplyToRange(editor, indentation, eols, blockStart, editor.Document.TotalNumberOfLines, begin, end);
ApplyToRange(editor, indentation, eols, blockStart, editor.Document.TotalNumberOfLines, begin, end, sawAttribute);
return (indentation.PeekOrDefault() ?? string.Empty).Length;
}
@ -637,7 +646,7 @@ namespace ICSharpCode.VBNetBinding @@ -637,7 +646,7 @@ namespace ICSharpCode.VBNetBinding
return t.Location.Line;
}
static void ApplyToRange(ITextEditor editor, Stack<string> indentation, List<int> eols, int blockStart, int blockEnd, int selectionStart, int selectionEnd) {
static void ApplyToRange(ITextEditor editor, Stack<string> indentation, List<int> eols, int blockStart, int blockEnd, int selectionStart, int selectionEnd, bool sawAttribute) {
LoggingService.InfoFormatted("indenting line {0} to {1} with {2}", blockStart, blockEnd, (indentation.PeekOrDefault() ?? "").Length);
int nextEol = -1;
@ -649,7 +658,7 @@ namespace ICSharpCode.VBNetBinding @@ -649,7 +658,7 @@ namespace ICSharpCode.VBNetBinding
// 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();
string noComments = lineText.TrimComments().TrimPreprocessorDirectives().TrimEnd().TrimEnd('_').TrimEnd();
// adjust indentation if the current line is not selected
// lines between the selection will be aligned to the selected level
@ -668,7 +677,7 @@ namespace ICSharpCode.VBNetBinding @@ -668,7 +677,7 @@ namespace ICSharpCode.VBNetBinding
}
// remove indentation in last line of multiline array(, collection, object) initializers
if (i == nextEol && wasMultiLine && noComments == "}") {
if (i == nextEol && wasMultiLine && (noComments == "}" || sawAttribute)) {
wasMultiLine = false;
Unindent(indentation);
}

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

@ -31,7 +31,6 @@ namespace ICSharpCode.VBNetBinding @@ -31,7 +31,6 @@ namespace ICSharpCode.VBNetBinding
if (string.IsNullOrEmpty(line))
return string.Empty;
bool inStr = false;
bool wsOnly = true;
for (int i = 0; i < line.Length; i++) {

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

@ -474,6 +474,50 @@ End Module"; @@ -474,6 +474,50 @@ End Module";
Sub asdf()
End Sub
End Module";
RunFormatTest(code, expected);
}
[Test]
public void Attribute()
{
string expected = @"Module Core
<STAThreadAttribute> _
Sub Main
End Sub
End Module";
string code = @"Module Core
<STAThreadAttribute> _
Sub Main
End Sub
End Module";
RunFormatTest(code, expected);
}
[Test]
public void Attribute2()
{
string expected = @"Module Core
<AttributeUsage(
bla.bla
)> _
Sub Main
End Sub
End Module";
string code = @"Module Core
<AttributeUsage(
bla.bla
)> _
Sub Main
End Sub
End Module";

9
src/Libraries/NRefactory/Project/Src/Lexer/VBNet/ExpressionFinder.atg

@ -287,7 +287,14 @@ ImportsStatement = @@ -287,7 +287,14 @@ ImportsStatement =
.
AttributeBlock =
"<" (. wasNormalAttribute = true; PushContext(Context.Attribute, la, t); .) [ ( "Assembly" | "Module" ) ":" (. wasNormalAttribute = false; .) ] { ANY } ">" (. PopContext(); .) [ EOL ]
"<" (. wasNormalAttribute = true; PushContext(Context.Attribute, la, t); .)
Attribute { "," Attribute }
">" (. PopContext(); .) [ EOL ]
.
Attribute =
[ EXPECTEDCONFLICT("Assembly") ( "Assembly" | "Module" ) ":" (. wasNormalAttribute = false; .) ]
Identifier { "." Identifier } [ "(" { ANY } ")" ]
.
NamespaceMemberDeclaration =

16
src/Libraries/NRefactory/Project/Src/Lexer/VBNet/ExpressionFinder.cs

@ -25,14 +25,14 @@ namespace ICSharpCode.NRefactory.Parser.VB @@ -25,14 +25,14 @@ namespace ICSharpCode.NRefactory.Parser.VB
}
}
void PushContext(Context context, Token la, Token t)
{
string indent = new string('\t', stack.Count);
Location l = la == null ? (t == null ? Location.Empty : t.EndLocation) : la.Location;
stack.Push(new Block() { context = context, lastExpressionStart = l });
Print(indent + "enter " + context);
}
void PushContext(Context context, Token la, Token t)
{
string indent = new string('\t', stack.Count);
Location l = la == null ? (t == null ? Location.Empty : t.EndLocation) : la.Location;
stack.Push(new Block() { context = context, lastExpressionStart = l });
Print(indent + "enter " + context);
}
public ExpressionFinder(ExpressionFinderState state)
{

2572
src/Libraries/NRefactory/Project/Src/Lexer/VBNet/Parser.cs

File diff suppressed because it is too large Load Diff
Loading…
Cancel
Save