Browse Source

* NRefactory.csproj:

* Src/PrettyPrinter/CSharp/OutputFormatter.cs:
* Src/PrettyPrinter/CSharp/PrettyPrintOptions.cs:
* Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs: Added some options.

git-svn-id: svn://svn.sharpdevelop.net/sharpdevelop/branches/3.0@3849 1ccf3a8d-04fe-1044-b7c0-cef0b8235c61
shortcuts
Mike Krüger 17 years ago
parent
commit
c16eaaff79
  1. 43
      src/Libraries/NRefactory/Project/NRefactory.csproj
  2. 71
      src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs
  3. 25
      src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/OutputFormatter.cs
  4. 129
      src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/PrettyPrintOptions.cs

43
src/Libraries/NRefactory/Project/NRefactory.csproj

@ -1,4 +1,5 @@
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5"> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="3.5">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@ -26,18 +27,19 @@
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion> <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<Optimize>False</Optimize> <Optimize>false</Optimize>
<CheckForOverflowUnderflow>True</CheckForOverflowUnderflow> <CheckForOverflowUnderflow>true</CheckForOverflowUnderflow>
<DefineConstants>TEST;DEBUG;NET35</DefineConstants> <DefineConstants>TEST;DEBUG;NET35</DefineConstants>
<OutputPath>..\..\..\..\bin\</OutputPath> <OutputPath>..\..\..\..\bin\</OutputPath>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors> <DebugType>none</DebugType>
<WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>True</Optimize> <Optimize>true</Optimize>
<CheckForOverflowUnderflow>False</CheckForOverflowUnderflow>
<DefineConstants>TEST;NET35</DefineConstants> <DefineConstants>TEST;NET35</DefineConstants>
<OutputPath>..\..\..\..\bin\</OutputPath> <OutputPath>..\..\..\..\bin\</OutputPath>
<TreatWarningsAsErrors>false</TreatWarningsAsErrors> <DebugType>none</DebugType>
<WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug' "> <PropertyGroup Condition=" '$(Configuration)' == 'Debug' ">
<DebugType>Full</DebugType> <DebugType>Full</DebugType>
@ -77,9 +79,6 @@
<Compile Include="Src\Lexer\VBNet\Lexer.cs" /> <Compile Include="Src\Lexer\VBNet\Lexer.cs" />
<Compile Include="Src\Lexer\VBNet\Tokens.cs" /> <Compile Include="Src\Lexer\VBNet\Tokens.cs" />
<Compile Include="Src\Lexer\Special\ISpecial.cs" /> <Compile Include="Src\Lexer\Special\ISpecial.cs" />
<Compile Include="..\..\..\Main\GlobalAssemblyInfo.cs">
<Link>Configuration\GlobalAssemblyInfo.cs</Link>
</Compile>
<Compile Include="Src\OperatorPrecedence.cs" /> <Compile Include="Src\OperatorPrecedence.cs" />
<Compile Include="Src\Parser\CSharp\CSharpParser.cs" /> <Compile Include="Src\Parser\CSharp\CSharpParser.cs" />
<Compile Include="Src\Parser\CSharp\Parser.cs" /> <Compile Include="Src\Parser\CSharp\Parser.cs" />
@ -137,26 +136,24 @@
<ItemGroup> <ItemGroup>
<Content Include="Src\Lexer\CSharp\KeywordList.txt" /> <Content Include="Src\Lexer\CSharp\KeywordList.txt" />
<Content Include="Src\Lexer\VBNet\KeywordList.txt" /> <Content Include="Src\Lexer\VBNet\KeywordList.txt" />
<Folder Include="Src\Parser" /> <Folder Include="Src\" />
<Folder Include="Src\Parser\CSharp" /> <Folder Include="Src\Parser\" />
<Content Include="Src\Parser\CSharp\cs.ATG" /> <Content Include="Src\Parser\CSharp\cs.ATG" />
<Folder Include="Src\Parser\Frames" /> <Folder Include="Src\Parser\" />
<Content Include="Src\Parser\Frames\Parser.frame" /> <Content Include="Src\Parser\Frames\Parser.frame" />
<Content Include="Src\Parser\Frames\Scanner.frame" /> <Content Include="Src\Parser\Frames\Scanner.frame" />
<Content Include="Src\Parser\Frames\SharpCoco.exe" /> <Content Include="Src\Parser\Frames\SharpCoco.exe" />
<Content Include="Src\Parser\Frames\trace.txt" /> <Content Include="Src\Parser\Frames\trace.txt" />
<Folder Include="Src\Parser\VBNet" /> <Folder Include="Src\Parser\" />
<Content Include="Src\Parser\VBNet\VBNET.ATG" /> <Content Include="Src\Parser\VBNet\VBNET.ATG" />
<Content Include="Src\Parser\gen.bat" /> <Content Include="Src\Parser\gen.bat" />
<Folder Include="Src\PrettyPrinter" /> <Folder Include="Src\" />
<Folder Include="Src\PrettyPrinter\CSharp" /> <Folder Include="Src\PrettyPrinter\" />
<Folder Include="Src\PrettyPrinter\VBNet" /> <Folder Include="Src\PrettyPrinter\" />
<Folder Include="Src\Visitors" /> <Folder Include="Src\" />
<Folder Include="Src\Ast" /> <Folder Include="Src\" />
<Folder Include="Src\Ast\General" /> <Folder Include="Src\Ast\" />
</ItemGroup> <Folder Include="Src\" />
<ItemGroup>
<Folder Include="Src\AstBuilder" />
</ItemGroup> </ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup> <PropertyGroup>

71
src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/CSharpOutputVisitor.cs

@ -315,11 +315,11 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.Space(); outputFormatter.Space();
outputFormatter.PrintIdentifier(namespaceDeclaration.Name); outputFormatter.PrintIdentifier(namespaceDeclaration.Name);
outputFormatter.BeginBrace(this.prettyPrintOptions.NamespaceBraceStyle); outputFormatter.BeginBrace (this.prettyPrintOptions.NamespaceBraceStyle, this.prettyPrintOptions.IndentNamespaceBody);
namespaceDeclaration.AcceptChildren(this, data); namespaceDeclaration.AcceptChildren(this, data);
outputFormatter.EndBrace(); outputFormatter.EndBrace(this.prettyPrintOptions.IndentNamespaceBody);
return null; return null;
} }
@ -392,16 +392,16 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
switch (typeDeclaration.Type) { switch (typeDeclaration.Type) {
case ClassType.Enum: case ClassType.Enum:
outputFormatter.BeginBrace(this.prettyPrintOptions.EnumBraceStyle); outputFormatter.BeginBrace(this.prettyPrintOptions.EnumBraceStyle, this.prettyPrintOptions.IndentEnumBody);
break; break;
case ClassType.Interface: case ClassType.Interface:
outputFormatter.BeginBrace(this.prettyPrintOptions.InterfaceBraceStyle); outputFormatter.BeginBrace(this.prettyPrintOptions.InterfaceBraceStyle, this.prettyPrintOptions.IndentInterfaceBody);
break; break;
case ClassType.Struct: case ClassType.Struct:
outputFormatter.BeginBrace(this.prettyPrintOptions.StructBraceStyle); outputFormatter.BeginBrace(this.prettyPrintOptions.StructBraceStyle, this.prettyPrintOptions.IndentStructBody);
break; break;
default: default:
outputFormatter.BeginBrace(this.prettyPrintOptions.ClassBraceStyle); outputFormatter.BeginBrace(this.prettyPrintOptions.ClassBraceStyle, this.prettyPrintOptions.IndentClassBody);
break; break;
} }
@ -413,7 +413,20 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
typeDeclaration.AcceptChildren(this, data); typeDeclaration.AcceptChildren(this, data);
} }
currentType = oldType; currentType = oldType;
outputFormatter.EndBrace(); switch (typeDeclaration.Type) {
case ClassType.Enum:
outputFormatter.EndBrace(this.prettyPrintOptions.IndentEnumBody);
break;
case ClassType.Interface:
outputFormatter.EndBrace(this.prettyPrintOptions.IndentInterfaceBody);
break;
case ClassType.Struct:
outputFormatter.EndBrace(this.prettyPrintOptions.IndentStructBody);
break;
default:
outputFormatter.EndBrace(this.prettyPrintOptions.IndentCaseBody);
break;
}
return null; return null;
} }
@ -536,12 +549,12 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
} }
outputFormatter.PrintIdentifier(propertyDeclaration.Name); outputFormatter.PrintIdentifier(propertyDeclaration.Name);
outputFormatter.BeginBrace(this.prettyPrintOptions.PropertyBraceStyle); outputFormatter.BeginBrace(this.prettyPrintOptions.PropertyBraceStyle, this.prettyPrintOptions.IndentPropertyBody);
TrackVisit(propertyDeclaration.GetRegion, data); TrackVisit(propertyDeclaration.GetRegion, data);
TrackVisit(propertyDeclaration.SetRegion, data); TrackVisit(propertyDeclaration.SetRegion, data);
outputFormatter.EndBrace(); outputFormatter.EndBrace(this.prettyPrintOptions.IndentPropertyBody);
return null; return null;
} }
@ -601,10 +614,10 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.PrintToken(Tokens.Semicolon); outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.NewLine(); outputFormatter.NewLine();
} else { } else {
outputFormatter.BeginBrace(this.prettyPrintOptions.PropertyBraceStyle); outputFormatter.BeginBrace(this.prettyPrintOptions.PropertyBraceStyle, this.prettyPrintOptions.IndentEventBody);
TrackVisit(eventDeclaration.AddRegion, data); TrackVisit(eventDeclaration.AddRegion, data);
TrackVisit(eventDeclaration.RemoveRegion, data); TrackVisit(eventDeclaration.RemoveRegion, data);
outputFormatter.EndBrace(); outputFormatter.EndBrace(this.prettyPrintOptions.IndentEventBody);
} }
return null; return null;
} }
@ -924,7 +937,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.PrintToken(Tokens.Semicolon); outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.NewLine(); outputFormatter.NewLine();
} else { } else {
outputFormatter.BeginBrace(braceStyle); outputFormatter.BeginBrace(braceStyle, this.prettyPrintOptions.IndentBlocks);
foreach (Statement stmt in blockStatement.Children) { foreach (Statement stmt in blockStatement.Children) {
outputFormatter.Indent(); outputFormatter.Indent();
if (stmt is BlockStatement) { if (stmt is BlockStatement) {
@ -935,7 +948,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
if (!outputFormatter.LastCharacterIsNewLine) if (!outputFormatter.LastCharacterIsNewLine)
outputFormatter.NewLine(); outputFormatter.NewLine();
} }
outputFormatter.EndBrace(); outputFormatter.EndBrace(this.prettyPrintOptions.IndentBlocks);
} }
EndVisit(blockStatement); EndVisit(blockStatement);
} }
@ -1035,7 +1048,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.PrintToken(Tokens.Null); outputFormatter.PrintToken(Tokens.Null);
outputFormatter.PrintToken(Tokens.CloseParenthesis); outputFormatter.PrintToken(Tokens.CloseParenthesis);
outputFormatter.BeginBrace(BraceStyle.EndOfLine); outputFormatter.BeginBrace(BraceStyle.EndOfLine, this.prettyPrintOptions.IndentBlocks);
outputFormatter.Indent(); outputFormatter.Indent();
outputFormatter.PrintIdentifier(raiseEventStatement.EventName); outputFormatter.PrintIdentifier(raiseEventStatement.EventName);
@ -1045,7 +1058,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.PrintToken(Tokens.Semicolon); outputFormatter.PrintToken(Tokens.Semicolon);
outputFormatter.NewLine(); outputFormatter.NewLine();
outputFormatter.EndBrace(); outputFormatter.EndBrace(this.prettyPrintOptions.IndentBlocks);
return null; return null;
} }
@ -1321,11 +1334,15 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
outputFormatter.Space(); outputFormatter.Space();
outputFormatter.PrintToken(Tokens.OpenCurlyBrace); outputFormatter.PrintToken(Tokens.OpenCurlyBrace);
outputFormatter.NewLine(); outputFormatter.NewLine();
++outputFormatter.IndentationLevel; if (prettyPrintOptions.IndentSwitchBody)
++outputFormatter.IndentationLevel;
foreach (SwitchSection section in switchStatement.SwitchSections) { foreach (SwitchSection section in switchStatement.SwitchSections) {
TrackVisit(section, data); TrackVisit(section, data);
} }
--outputFormatter.IndentationLevel;
if (prettyPrintOptions.IndentSwitchBody)
--outputFormatter.IndentationLevel;
outputFormatter.Indent(); outputFormatter.Indent();
outputFormatter.PrintToken(Tokens.CloseCurlyBrace); outputFormatter.PrintToken(Tokens.CloseCurlyBrace);
return null; return null;
@ -1336,14 +1353,26 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
foreach (CaseLabel label in switchSection.SwitchLabels) { foreach (CaseLabel label in switchSection.SwitchLabels) {
TrackVisit(label, data); TrackVisit(label, data);
} }
int standardIndentLevel = outputFormatter.IndentationLevel;
++outputFormatter.IndentationLevel; if (prettyPrintOptions.IndentCaseBody)
foreach (Statement stmt in switchSection.Children) { ++outputFormatter.IndentationLevel;
for (int i = 0; i < switchSection.Children.Count; i++) {
Statement stmt = switchSection.Children[i] as Statement;
int oldIndent = outputFormatter.IndentationLevel;
if (i == switchSection.Children.Count - 1) {
if (prettyPrintOptions.IndentBreakStatements)
outputFormatter.IndentationLevel = standardIndentLevel + 1;
else
outputFormatter.IndentationLevel = standardIndentLevel;
}
outputFormatter.Indent(); outputFormatter.Indent();
TrackVisit(stmt, data); TrackVisit(stmt, data);
outputFormatter.NewLine(); outputFormatter.NewLine();
outputFormatter.IndentationLevel = oldIndent;
} }
--outputFormatter.IndentationLevel;
if (prettyPrintOptions.IndentCaseBody)
--outputFormatter.IndentationLevel;
return null; return null;
} }

25
src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/OutputFormatter.cs

@ -40,7 +40,7 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
Stack braceStack = new Stack(); Stack braceStack = new Stack();
public void BeginBrace(BraceStyle style) public void BeginBrace(BraceStyle style, bool indent)
{ {
switch (style) { switch (style) {
case BraceStyle.EndOfLine: case BraceStyle.EndOfLine:
@ -49,25 +49,29 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
} }
PrintToken(Tokens.OpenCurlyBrace); PrintToken(Tokens.OpenCurlyBrace);
NewLine(); NewLine();
++IndentationLevel; if (indent)
++IndentationLevel;
break; break;
case BraceStyle.NextLine: case BraceStyle.NextLine:
NewLine(); NewLine();
Indent(); Indent();
PrintToken(Tokens.OpenCurlyBrace); PrintToken(Tokens.OpenCurlyBrace);
NewLine(); NewLine();
++IndentationLevel; if (indent)
++IndentationLevel;
break; break;
case BraceStyle.NextLineShifted: case BraceStyle.NextLineShifted:
NewLine(); NewLine();
++IndentationLevel; if (indent)
++IndentationLevel;
Indent(); Indent();
PrintToken(Tokens.OpenCurlyBrace); PrintToken(Tokens.OpenCurlyBrace);
NewLine(); NewLine();
break; break;
case BraceStyle.NextLineShifted2: case BraceStyle.NextLineShifted2:
NewLine(); NewLine();
++IndentationLevel; if (indent)
++IndentationLevel;
Indent(); Indent();
PrintToken(Tokens.OpenCurlyBrace); PrintToken(Tokens.OpenCurlyBrace);
NewLine(); NewLine();
@ -77,13 +81,14 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
braceStack.Push(style); braceStack.Push(style);
} }
public void EndBrace() public void EndBrace(bool indent)
{ {
BraceStyle style = (BraceStyle)braceStack.Pop(); BraceStyle style = (BraceStyle)braceStack.Pop();
switch (style) { switch (style) {
case BraceStyle.EndOfLine: case BraceStyle.EndOfLine:
case BraceStyle.NextLine: case BraceStyle.NextLine:
--IndentationLevel; if (indent)
--IndentationLevel;
Indent(); Indent();
PrintToken(Tokens.CloseCurlyBrace); PrintToken(Tokens.CloseCurlyBrace);
NewLine(); NewLine();
@ -92,10 +97,12 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
Indent(); Indent();
PrintToken(Tokens.CloseCurlyBrace); PrintToken(Tokens.CloseCurlyBrace);
NewLine(); NewLine();
--IndentationLevel; if (indent)
--IndentationLevel;
break; break;
case BraceStyle.NextLineShifted2: case BraceStyle.NextLineShifted2:
--IndentationLevel; if (indent)
--IndentationLevel;
Indent(); Indent();
PrintToken(Tokens.CloseCurlyBrace); PrintToken(Tokens.CloseCurlyBrace);
NewLine(); NewLine();

129
src/Libraries/NRefactory/Project/Src/PrettyPrinter/CSharp/PrettyPrintOptions.cs

@ -213,6 +213,132 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
} }
#endregion #endregion
#region Indentation
bool indentNamespaceBody = true;
bool indentClassBody = true;
bool indentInterfaceBody = true;
bool indentStructBody = true;
bool indentEnumBody = true;
bool indentMethodBody = true;
bool indentPropertyBody = true;
bool indentEventBody = true;
bool indentBlocks = true;
bool indentSwitchBody = true;
bool indentCaseBody = true;
bool indentBreakStatements = true;
public bool IndentBlocks {
get {
return indentBlocks;
}
set {
indentBlocks = value;
}
}
public bool IndentClassBody {
get {
return indentClassBody;
}
set {
indentClassBody = value;
}
}
public bool IndentStructBody {
get {
return indentStructBody;
}
set {
indentStructBody = value;
}
}
public bool IndentPropertyBody {
get {
return indentPropertyBody;
}
set {
indentPropertyBody = value;
}
}
public bool IndentNamespaceBody {
get {
return indentNamespaceBody;
}
set {
indentNamespaceBody = value;
}
}
public bool IndentMethodBody {
get {
return indentMethodBody;
}
set {
indentMethodBody = value;
}
}
public bool IndentInterfaceBody {
get {
return indentInterfaceBody;
}
set {
indentInterfaceBody = value;
}
}
public bool IndentEventBody {
get {
return indentEventBody;
}
set {
indentEventBody = value;
}
}
public bool IndentEnumBody {
get {
return indentEnumBody;
}
set {
indentEnumBody = value;
}
}
public bool IndentBreakStatements {
get {
return indentBreakStatements;
}
set {
indentBreakStatements = value;
}
}
public bool IndentSwitchBody {
get {
return indentSwitchBody;
}
set {
indentSwitchBody = value;
}
}
public bool IndentCaseBody {
get {
return indentCaseBody;
}
set {
indentCaseBody = value;
}
}
#endregion
#region Before Parentheses #region Before Parentheses
bool beforeMethodCallParentheses = false; bool beforeMethodCallParentheses = false;
bool beforeDelegateDeclarationParentheses = false; bool beforeDelegateDeclarationParentheses = false;
@ -553,5 +679,6 @@ namespace ICSharpCode.NRefactory.PrettyPrinter
} }
} }
#endregion #endregion
} }
} }
Loading…
Cancel
Save