142 changed files with 11295 additions and 6251 deletions
@ -0,0 +1,132 @@
@@ -0,0 +1,132 @@
|
||||
using System; |
||||
namespace ICSharpCode.NRefactory.CSharp |
||||
{ |
||||
public enum NewLineType { |
||||
Unix, |
||||
Windows, |
||||
Mac |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// A New line node represents a line break in the text.
|
||||
/// </summary>
|
||||
public abstract class NewLineNode : AstNode |
||||
{ |
||||
public override NodeType NodeType { |
||||
get { |
||||
return NodeType.Whitespace; |
||||
} |
||||
} |
||||
|
||||
public abstract NewLineType NewLineType { |
||||
get; |
||||
} |
||||
|
||||
TextLocation startLocation; |
||||
public override TextLocation StartLocation { |
||||
get { |
||||
return startLocation; |
||||
} |
||||
} |
||||
|
||||
public override TextLocation EndLocation { |
||||
get { |
||||
return new TextLocation (startLocation.Line + 1, 1); |
||||
} |
||||
} |
||||
|
||||
public NewLineNode() : this (TextLocation.Empty) |
||||
{ |
||||
} |
||||
|
||||
public NewLineNode(TextLocation startLocation) |
||||
{ |
||||
this.startLocation = startLocation; |
||||
} |
||||
|
||||
public override void AcceptVisitor(IAstVisitor visitor) |
||||
{ |
||||
visitor.VisitNewLine (this); |
||||
} |
||||
|
||||
public override T AcceptVisitor<T>(IAstVisitor<T> visitor) |
||||
{ |
||||
return visitor.VisitNewLine (this); |
||||
} |
||||
|
||||
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data) |
||||
{ |
||||
return visitor.VisitNewLine (this, data); |
||||
} |
||||
} |
||||
|
||||
public class UnixNewLine : NewLineNode |
||||
{ |
||||
public override NewLineType NewLineType { |
||||
get { |
||||
return NewLineType.Unix; |
||||
} |
||||
} |
||||
|
||||
public UnixNewLine() |
||||
{ |
||||
} |
||||
|
||||
public UnixNewLine(TextLocation startLocation) : base (startLocation) |
||||
{ |
||||
} |
||||
|
||||
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
||||
{ |
||||
var o = other as UnixNewLine; |
||||
return o != null; |
||||
} |
||||
} |
||||
|
||||
public class WindowsNewLine : NewLineNode |
||||
{ |
||||
public override NewLineType NewLineType { |
||||
get { |
||||
return NewLineType.Windows; |
||||
} |
||||
} |
||||
|
||||
public WindowsNewLine() |
||||
{ |
||||
} |
||||
|
||||
public WindowsNewLine(TextLocation startLocation) : base (startLocation) |
||||
{ |
||||
} |
||||
|
||||
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
||||
{ |
||||
var o = other as WindowsNewLine; |
||||
return o != null; |
||||
} |
||||
} |
||||
|
||||
public class MacNewLine : NewLineNode |
||||
{ |
||||
public override NewLineType NewLineType { |
||||
get { |
||||
return NewLineType.Mac; |
||||
} |
||||
} |
||||
|
||||
public MacNewLine() |
||||
{ |
||||
} |
||||
|
||||
public MacNewLine(TextLocation startLocation) : base (startLocation) |
||||
{ |
||||
} |
||||
|
||||
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
||||
{ |
||||
var o = other as MacNewLine; |
||||
return o != null; |
||||
} |
||||
} |
||||
} |
||||
|
@ -0,0 +1,94 @@
@@ -0,0 +1,94 @@
|
||||
//
|
||||
// TextNode.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using System; |
||||
namespace ICSharpCode.NRefactory.CSharp |
||||
{ |
||||
/// <summary>
|
||||
/// A text node contains text without syntactic or semantic information.
|
||||
/// (non parseable part of a text)
|
||||
/// </summary>
|
||||
public class TextNode : AstNode |
||||
{ |
||||
public override NodeType NodeType { |
||||
get { |
||||
return NodeType.Whitespace; |
||||
} |
||||
} |
||||
|
||||
public string Text { |
||||
get; |
||||
set; |
||||
} |
||||
|
||||
TextLocation startLocation; |
||||
public override TextLocation StartLocation { |
||||
get { |
||||
return startLocation; |
||||
} |
||||
} |
||||
|
||||
TextLocation endLocation; |
||||
public override TextLocation EndLocation { |
||||
get { |
||||
return endLocation; |
||||
} |
||||
} |
||||
|
||||
public TextNode(string text) : this (text, TextLocation.Empty, TextLocation.Empty) |
||||
{ |
||||
} |
||||
|
||||
public TextNode(string text, TextLocation startLocation, TextLocation endLocation) |
||||
{ |
||||
this.Text = text; |
||||
this.startLocation = startLocation; |
||||
this.endLocation = endLocation; |
||||
} |
||||
|
||||
public override void AcceptVisitor(IAstVisitor visitor) |
||||
{ |
||||
visitor.VisitText (this); |
||||
} |
||||
|
||||
public override T AcceptVisitor<T>(IAstVisitor<T> visitor) |
||||
{ |
||||
return visitor.VisitText (this); |
||||
} |
||||
|
||||
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data) |
||||
{ |
||||
return visitor.VisitText (this, data); |
||||
} |
||||
|
||||
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
||||
{ |
||||
var o = other as TextNode; |
||||
return o != null && o.Text == Text; |
||||
} |
||||
} |
||||
} |
||||
|
@ -0,0 +1,91 @@
@@ -0,0 +1,91 @@
|
||||
//
|
||||
// WhitespaceNode.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using System; |
||||
namespace ICSharpCode.NRefactory.CSharp |
||||
{ |
||||
/// <summary>
|
||||
/// A Whitespace node contains only whitespaces.
|
||||
/// </summary>
|
||||
public class WhitespaceNode : AstNode |
||||
{ |
||||
public override NodeType NodeType { |
||||
get { |
||||
return NodeType.Whitespace; |
||||
} |
||||
} |
||||
|
||||
public string WhiteSpaceText { |
||||
get; |
||||
set; |
||||
} |
||||
|
||||
TextLocation startLocation; |
||||
public override TextLocation StartLocation { |
||||
get { |
||||
return startLocation; |
||||
} |
||||
} |
||||
|
||||
public override TextLocation EndLocation { |
||||
get { |
||||
return new TextLocation (startLocation.Line, startLocation.Column + WhiteSpaceText.Length); |
||||
} |
||||
} |
||||
|
||||
public WhitespaceNode(string whiteSpaceText) : this (whiteSpaceText, TextLocation.Empty) |
||||
{ |
||||
} |
||||
|
||||
public WhitespaceNode(string whiteSpaceText, TextLocation startLocation) |
||||
{ |
||||
this.WhiteSpaceText = WhiteSpaceText; |
||||
this.startLocation = startLocation; |
||||
} |
||||
|
||||
public override void AcceptVisitor(IAstVisitor visitor) |
||||
{ |
||||
visitor.VisitWhitespace (this); |
||||
} |
||||
|
||||
public override T AcceptVisitor<T>(IAstVisitor<T> visitor) |
||||
{ |
||||
return visitor.VisitWhitespace (this); |
||||
} |
||||
|
||||
public override S AcceptVisitor<T, S>(IAstVisitor<T, S> visitor, T data) |
||||
{ |
||||
return visitor.VisitWhitespace (this, data); |
||||
} |
||||
|
||||
protected internal override bool DoMatch(AstNode other, PatternMatching.Match match) |
||||
{ |
||||
var o = other as WhitespaceNode; |
||||
return o != null && o.WhiteSpaceText == WhiteSpaceText; |
||||
} |
||||
} |
||||
} |
||||
|
@ -0,0 +1,339 @@
@@ -0,0 +1,339 @@
|
||||
//
|
||||
// FormattingOptionsFactory.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp |
||||
{ |
||||
/// <summary>
|
||||
/// The formatting options factory creates pre defined formatting option styles.
|
||||
/// </summary>
|
||||
public static class FormattingOptionsFactory |
||||
{ |
||||
/// <summary>
|
||||
/// Creates empty CSharpFormatting options.
|
||||
/// </summary>
|
||||
public static CSharpFormattingOptions CreateEmpty() |
||||
{ |
||||
return new CSharpFormattingOptions(); |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Creates mono indent style CSharpFormatting options.
|
||||
/// </summary>
|
||||
public static CSharpFormattingOptions CreateMono() |
||||
{ |
||||
return new CSharpFormattingOptions() { |
||||
IndentNamespaceBody = true, |
||||
IndentClassBody = true, |
||||
IndentInterfaceBody = true, |
||||
IndentStructBody = true, |
||||
IndentEnumBody = true, |
||||
IndentMethodBody = true, |
||||
IndentPropertyBody = true, |
||||
IndentEventBody = true, |
||||
IndentBlocks = true, |
||||
IndentSwitchBody = false, |
||||
IndentCaseBody = true, |
||||
IndentBreakStatements = true, |
||||
NamespaceBraceStyle = BraceStyle.NextLine, |
||||
ClassBraceStyle = BraceStyle.NextLine, |
||||
InterfaceBraceStyle = BraceStyle.NextLine, |
||||
StructBraceStyle = BraceStyle.NextLine, |
||||
EnumBraceStyle = BraceStyle.NextLine, |
||||
MethodBraceStyle = BraceStyle.NextLine, |
||||
ConstructorBraceStyle = BraceStyle.NextLine, |
||||
DestructorBraceStyle = BraceStyle.NextLine, |
||||
AnonymousMethodBraceStyle = BraceStyle.EndOfLine, |
||||
|
||||
PropertyBraceStyle = BraceStyle.EndOfLine, |
||||
PropertyGetBraceStyle = BraceStyle.EndOfLine, |
||||
PropertySetBraceStyle = BraceStyle.EndOfLine, |
||||
AllowPropertyGetBlockInline = true, |
||||
AllowPropertySetBlockInline = true, |
||||
|
||||
EventBraceStyle = BraceStyle.EndOfLine, |
||||
EventAddBraceStyle = BraceStyle.EndOfLine, |
||||
EventRemoveBraceStyle = BraceStyle.EndOfLine, |
||||
AllowEventAddBlockInline = true, |
||||
AllowEventRemoveBlockInline = true, |
||||
StatementBraceStyle = BraceStyle.EndOfLine, |
||||
|
||||
PlaceElseOnNewLine = false, |
||||
PlaceCatchOnNewLine = false, |
||||
PlaceFinallyOnNewLine = false, |
||||
PlaceWhileOnNewLine = false, |
||||
ArrayInitializerWrapping = Wrapping.WrapIfTooLong, |
||||
ArrayInitializerBraceStyle = BraceStyle.EndOfLine, |
||||
|
||||
SpaceBeforeMethodCallParentheses = true, |
||||
SpaceBeforeMethodDeclarationParentheses = true, |
||||
SpaceBeforeConstructorDeclarationParentheses = true, |
||||
SpaceBeforeDelegateDeclarationParentheses = true, |
||||
SpaceAfterMethodCallParameterComma = true, |
||||
SpaceAfterConstructorDeclarationParameterComma = true, |
||||
|
||||
SpaceBeforeNewParentheses = true, |
||||
SpacesWithinNewParentheses = false, |
||||
SpacesBetweenEmptyNewParentheses = false, |
||||
SpaceBeforeNewParameterComma = false, |
||||
SpaceAfterNewParameterComma = true, |
||||
|
||||
SpaceBeforeIfParentheses = true, |
||||
SpaceBeforeWhileParentheses = true, |
||||
SpaceBeforeForParentheses = true, |
||||
SpaceBeforeForeachParentheses = true, |
||||
SpaceBeforeCatchParentheses = true, |
||||
SpaceBeforeSwitchParentheses = true, |
||||
SpaceBeforeLockParentheses = true, |
||||
SpaceBeforeUsingParentheses = true, |
||||
SpaceAroundAssignment = true, |
||||
SpaceAroundLogicalOperator = true, |
||||
SpaceAroundEqualityOperator = true, |
||||
SpaceAroundRelationalOperator = true, |
||||
SpaceAroundBitwiseOperator = true, |
||||
SpaceAroundAdditiveOperator = true, |
||||
SpaceAroundMultiplicativeOperator = true, |
||||
SpaceAroundShiftOperator = true, |
||||
SpaceAroundNullCoalescingOperator = true, |
||||
SpacesWithinParentheses = false, |
||||
SpaceWithinMethodCallParentheses = false, |
||||
SpaceWithinMethodDeclarationParentheses = false, |
||||
SpacesWithinIfParentheses = false, |
||||
SpacesWithinWhileParentheses = false, |
||||
SpacesWithinForParentheses = false, |
||||
SpacesWithinForeachParentheses = false, |
||||
SpacesWithinCatchParentheses = false, |
||||
SpacesWithinSwitchParentheses = false, |
||||
SpacesWithinLockParentheses = false, |
||||
SpacesWithinUsingParentheses = false, |
||||
SpacesWithinCastParentheses = false, |
||||
SpacesWithinSizeOfParentheses = false, |
||||
SpacesWithinTypeOfParentheses = false, |
||||
SpacesWithinCheckedExpressionParantheses = false, |
||||
SpaceBeforeConditionalOperatorCondition = true, |
||||
SpaceAfterConditionalOperatorCondition = true, |
||||
SpaceBeforeConditionalOperatorSeparator = true, |
||||
SpaceAfterConditionalOperatorSeparator = true, |
||||
|
||||
SpacesWithinBrackets = false, |
||||
SpacesBeforeBrackets = true, |
||||
SpaceBeforeBracketComma = false, |
||||
SpaceAfterBracketComma = true, |
||||
|
||||
SpaceBeforeForSemicolon = false, |
||||
SpaceAfterForSemicolon = true, |
||||
SpaceAfterTypecast = false, |
||||
|
||||
AlignEmbeddedIfStatements = true, |
||||
AlignEmbeddedUsingStatements = true, |
||||
PropertyFormatting = PropertyFormatting.AllowOneLine, |
||||
SpaceBeforeMethodDeclarationParameterComma = false, |
||||
SpaceAfterMethodDeclarationParameterComma = true, |
||||
SpaceBeforeFieldDeclarationComma = false, |
||||
SpaceAfterFieldDeclarationComma = true, |
||||
SpaceBeforeLocalVariableDeclarationComma = false, |
||||
SpaceAfterLocalVariableDeclarationComma = true, |
||||
|
||||
SpaceBeforeIndexerDeclarationBracket = true, |
||||
SpaceWithinIndexerDeclarationBracket = false, |
||||
SpaceBeforeIndexerDeclarationParameterComma = false, |
||||
SpaceInNamedArgumentAfterDoubleColon = true, |
||||
|
||||
SpaceAfterIndexerDeclarationParameterComma = true, |
||||
|
||||
BlankLinesBeforeUsings = 0, |
||||
BlankLinesAfterUsings = 1, |
||||
|
||||
|
||||
BlankLinesBeforeFirstDeclaration = 0, |
||||
BlankLinesBetweenTypes = 1, |
||||
BlankLinesBetweenFields = 0, |
||||
BlankLinesBetweenEventFields = 0, |
||||
BlankLinesBetweenMembers = 1, |
||||
|
||||
KeepCommentsAtFirstColumn = true, |
||||
}; |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Creates sharp develop indent style CSharpFormatting options.
|
||||
/// </summary>
|
||||
public static CSharpFormattingOptions CreateSharpDevelop() |
||||
{ |
||||
return new CSharpFormattingOptions() { |
||||
IndentNamespaceBody = true, |
||||
IndentClassBody = true, |
||||
IndentInterfaceBody = true, |
||||
IndentStructBody = true, |
||||
IndentEnumBody = true, |
||||
IndentMethodBody = true, |
||||
IndentPropertyBody = true, |
||||
IndentEventBody = true, |
||||
IndentBlocks = true, |
||||
IndentSwitchBody = true, |
||||
IndentCaseBody = true, |
||||
IndentBreakStatements = true, |
||||
|
||||
NamespaceBraceStyle = BraceStyle.NextLine, |
||||
ClassBraceStyle = BraceStyle.NextLine, |
||||
InterfaceBraceStyle = BraceStyle.NextLine, |
||||
StructBraceStyle = BraceStyle.NextLine, |
||||
EnumBraceStyle = BraceStyle.NextLine, |
||||
MethodBraceStyle = BraceStyle.NextLine, |
||||
ConstructorBraceStyle = BraceStyle.NextLine, |
||||
DestructorBraceStyle = BraceStyle.NextLine, |
||||
AnonymousMethodBraceStyle = BraceStyle.EndOfLine, |
||||
PropertyBraceStyle = BraceStyle.EndOfLine, |
||||
PropertyGetBraceStyle = BraceStyle.EndOfLine, |
||||
PropertySetBraceStyle = BraceStyle.EndOfLine, |
||||
AllowPropertyGetBlockInline = true, |
||||
AllowPropertySetBlockInline = true, |
||||
|
||||
EventBraceStyle = BraceStyle.EndOfLine, |
||||
EventAddBraceStyle = BraceStyle.EndOfLine, |
||||
EventRemoveBraceStyle = BraceStyle.EndOfLine, |
||||
AllowEventAddBlockInline = true, |
||||
AllowEventRemoveBlockInline = true, |
||||
StatementBraceStyle = BraceStyle.EndOfLine, |
||||
|
||||
PlaceElseOnNewLine = false, |
||||
PlaceCatchOnNewLine = false, |
||||
PlaceFinallyOnNewLine = false, |
||||
PlaceWhileOnNewLine = false, |
||||
ArrayInitializerWrapping = Wrapping.WrapIfTooLong, |
||||
ArrayInitializerBraceStyle = BraceStyle.EndOfLine, |
||||
|
||||
SpaceBeforeMethodCallParentheses = false, |
||||
SpaceBeforeMethodDeclarationParentheses = false, |
||||
SpaceBeforeConstructorDeclarationParentheses = false, |
||||
SpaceBeforeDelegateDeclarationParentheses = false, |
||||
SpaceAfterMethodCallParameterComma = true, |
||||
SpaceAfterConstructorDeclarationParameterComma = true, |
||||
|
||||
SpaceBeforeNewParentheses = false, |
||||
SpacesWithinNewParentheses = false, |
||||
SpacesBetweenEmptyNewParentheses = false, |
||||
SpaceBeforeNewParameterComma = false, |
||||
SpaceAfterNewParameterComma = true, |
||||
|
||||
SpaceBeforeIfParentheses = true, |
||||
SpaceBeforeWhileParentheses = true, |
||||
SpaceBeforeForParentheses = true, |
||||
SpaceBeforeForeachParentheses = true, |
||||
SpaceBeforeCatchParentheses = true, |
||||
SpaceBeforeSwitchParentheses = true, |
||||
SpaceBeforeLockParentheses = true, |
||||
SpaceBeforeUsingParentheses = true, |
||||
|
||||
SpaceAroundAssignment = true, |
||||
SpaceAroundLogicalOperator = true, |
||||
SpaceAroundEqualityOperator = true, |
||||
SpaceAroundRelationalOperator = true, |
||||
SpaceAroundBitwiseOperator = true, |
||||
SpaceAroundAdditiveOperator = true, |
||||
SpaceAroundMultiplicativeOperator = true, |
||||
SpaceAroundShiftOperator = true, |
||||
SpaceAroundNullCoalescingOperator = true, |
||||
SpacesWithinParentheses = false, |
||||
SpaceWithinMethodCallParentheses = false, |
||||
SpaceWithinMethodDeclarationParentheses = false, |
||||
SpacesWithinIfParentheses = false, |
||||
SpacesWithinWhileParentheses = false, |
||||
SpacesWithinForParentheses = false, |
||||
SpacesWithinForeachParentheses = false, |
||||
SpacesWithinCatchParentheses = false, |
||||
SpacesWithinSwitchParentheses = false, |
||||
SpacesWithinLockParentheses = false, |
||||
SpacesWithinUsingParentheses = false, |
||||
SpacesWithinCastParentheses = false, |
||||
SpacesWithinSizeOfParentheses = false, |
||||
SpacesWithinTypeOfParentheses = false, |
||||
SpacesWithinCheckedExpressionParantheses = false, |
||||
SpaceBeforeConditionalOperatorCondition = true, |
||||
SpaceAfterConditionalOperatorCondition = true, |
||||
SpaceBeforeConditionalOperatorSeparator = true, |
||||
SpaceAfterConditionalOperatorSeparator = true, |
||||
|
||||
SpacesWithinBrackets = false, |
||||
SpacesBeforeBrackets = true, |
||||
SpaceBeforeBracketComma = false, |
||||
SpaceAfterBracketComma = true, |
||||
|
||||
SpaceBeforeForSemicolon = false, |
||||
SpaceAfterForSemicolon = true, |
||||
SpaceAfterTypecast = false, |
||||
|
||||
AlignEmbeddedIfStatements = true, |
||||
AlignEmbeddedUsingStatements = true, |
||||
PropertyFormatting = PropertyFormatting.AllowOneLine, |
||||
SpaceBeforeMethodDeclarationParameterComma = false, |
||||
SpaceAfterMethodDeclarationParameterComma = true, |
||||
SpaceBeforeFieldDeclarationComma = false, |
||||
SpaceAfterFieldDeclarationComma = true, |
||||
SpaceBeforeLocalVariableDeclarationComma = false, |
||||
SpaceAfterLocalVariableDeclarationComma = true, |
||||
|
||||
SpaceBeforeIndexerDeclarationBracket = true, |
||||
SpaceWithinIndexerDeclarationBracket = false, |
||||
SpaceBeforeIndexerDeclarationParameterComma = false, |
||||
SpaceInNamedArgumentAfterDoubleColon = true, |
||||
|
||||
SpaceAfterIndexerDeclarationParameterComma = true, |
||||
|
||||
BlankLinesBeforeUsings = 0, |
||||
BlankLinesAfterUsings = 1, |
||||
|
||||
BlankLinesBeforeFirstDeclaration = 0, |
||||
BlankLinesBetweenTypes = 1, |
||||
BlankLinesBetweenFields = 0, |
||||
BlankLinesBetweenEventFields = 0, |
||||
BlankLinesBetweenMembers = 1, |
||||
|
||||
KeepCommentsAtFirstColumn = true, |
||||
}; |
||||
} |
||||
|
||||
/// <summary>
|
||||
/// Creates allman indent style CSharpFormatting options used in Visual Studio.
|
||||
/// </summary>
|
||||
public static CSharpFormattingOptions CreateAllman() |
||||
{ |
||||
var baseOptions = CreateSharpDevelop(); |
||||
baseOptions.AnonymousMethodBraceStyle = BraceStyle.EndOfLine; |
||||
baseOptions.PropertyBraceStyle = BraceStyle.EndOfLine; |
||||
baseOptions.PropertyGetBraceStyle = BraceStyle.EndOfLine; |
||||
baseOptions.PropertySetBraceStyle = BraceStyle.EndOfLine; |
||||
|
||||
baseOptions.EventBraceStyle = BraceStyle.EndOfLine; |
||||
baseOptions.EventAddBraceStyle = BraceStyle.EndOfLine; |
||||
baseOptions.EventRemoveBraceStyle = BraceStyle.EndOfLine; |
||||
baseOptions.StatementBraceStyle = BraceStyle.EndOfLine; |
||||
baseOptions.ArrayInitializerBraceStyle = BraceStyle.EndOfLine; |
||||
return baseOptions; |
||||
} |
||||
} |
||||
} |
||||
|
@ -0,0 +1,216 @@
@@ -0,0 +1,216 @@
|
||||
//
|
||||
// GeneratedCodeSettings.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp |
||||
{ |
||||
public enum GeneratedCodeMember |
||||
{ |
||||
Unknown, |
||||
|
||||
StaticFields, |
||||
InstanceFields, |
||||
StaticProperties, |
||||
InstanceProperties, |
||||
Indexer, |
||||
Constructors, |
||||
StaticMethods, |
||||
InstanceMethods, |
||||
StaticEvents, |
||||
InstanceEvents, |
||||
Operators, |
||||
NestedTypes |
||||
} |
||||
|
||||
public class GeneratedCodeSettings |
||||
{ |
||||
List<GeneratedCodeMember> codeMemberOrder; |
||||
|
||||
public List<GeneratedCodeMember> CodeMemberOrder { |
||||
get { |
||||
return codeMemberOrder; |
||||
} |
||||
set { |
||||
codeMemberOrder = value; |
||||
} |
||||
} |
||||
|
||||
public bool GenerateCategoryComments { |
||||
get; |
||||
set; |
||||
} |
||||
|
||||
public bool SubOrderAlphabetical { |
||||
get; |
||||
set; |
||||
} |
||||
|
||||
public void Apply (AstNode rootNode) |
||||
{ |
||||
if (rootNode == null) |
||||
throw new ArgumentNullException ("rootNode"); |
||||
rootNode.AcceptVisitor (new GenerateCodeVisitior (this)); |
||||
} |
||||
|
||||
public virtual string GetCategoryLabel(GeneratedCodeMember memberCategory) |
||||
{ |
||||
switch (memberCategory) { |
||||
case GeneratedCodeMember.StaticFields: |
||||
return "Static Fields"; |
||||
case GeneratedCodeMember.InstanceFields: |
||||
return "Fields"; |
||||
case GeneratedCodeMember.StaticProperties: |
||||
return "Static Properties"; |
||||
case GeneratedCodeMember.InstanceProperties: |
||||
return "Properties"; |
||||
case GeneratedCodeMember.Indexer: |
||||
return "Indexer"; |
||||
case GeneratedCodeMember.Constructors: |
||||
return "Constructors"; |
||||
case GeneratedCodeMember.StaticMethods: |
||||
return "Static Methods"; |
||||
case GeneratedCodeMember.InstanceMethods: |
||||
return "Methods"; |
||||
case GeneratedCodeMember.StaticEvents: |
||||
return "Static Events"; |
||||
case GeneratedCodeMember.InstanceEvents: |
||||
return "Events"; |
||||
case GeneratedCodeMember.Operators: |
||||
return "Operators"; |
||||
case GeneratedCodeMember.NestedTypes: |
||||
return "Nested Types"; |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
class GenerateCodeVisitior : DepthFirstAstVisitor |
||||
{ |
||||
GeneratedCodeSettings settings; |
||||
|
||||
public GenerateCodeVisitior(GeneratedCodeSettings settings) |
||||
{ |
||||
if (settings == null) |
||||
throw new ArgumentNullException("settings"); |
||||
this.settings = settings; |
||||
} |
||||
|
||||
GeneratedCodeMember GetCodeMemberCategory(EntityDeclaration x) |
||||
{ |
||||
bool isStatic = x.HasModifier(Modifiers.Static) || x.HasModifier(Modifiers.Const); |
||||
if (x is FieldDeclaration) |
||||
return isStatic ? GeneratedCodeMember.StaticFields : GeneratedCodeMember.InstanceFields; |
||||
if (x is IndexerDeclaration) |
||||
return GeneratedCodeMember.Indexer; |
||||
if (x is PropertyDeclaration) |
||||
return isStatic ? GeneratedCodeMember.StaticProperties : GeneratedCodeMember.InstanceProperties; |
||||
if (x is ConstructorDeclaration || x is DestructorDeclaration) |
||||
return GeneratedCodeMember.Constructors; |
||||
if (x is MethodDeclaration) |
||||
return isStatic ? GeneratedCodeMember.StaticMethods : GeneratedCodeMember.InstanceMethods; |
||||
if (x is OperatorDeclaration) |
||||
return GeneratedCodeMember.Operators; |
||||
if (x is EventDeclaration || x is CustomEventDeclaration) |
||||
return isStatic ? GeneratedCodeMember.StaticEvents : GeneratedCodeMember.InstanceEvents; |
||||
|
||||
if (x is TypeDeclaration) |
||||
return GeneratedCodeMember.NestedTypes; |
||||
|
||||
return GeneratedCodeMember.Unknown; |
||||
} |
||||
|
||||
public override void VisitTypeDeclaration (TypeDeclaration typeDeclaration) |
||||
{ |
||||
if (typeDeclaration.ClassType == ClassType.Enum) |
||||
return; |
||||
var entities = new List<EntityDeclaration> (typeDeclaration.Members); |
||||
entities.Sort ((x, y) => { |
||||
int i1 = settings.CodeMemberOrder.IndexOf (GetCodeMemberCategory (x)); |
||||
int i2 = settings.CodeMemberOrder.IndexOf (GetCodeMemberCategory (y)); |
||||
if (i1 != i2) |
||||
return i1.CompareTo (i2); |
||||
if (settings.SubOrderAlphabetical) |
||||
return (x.Name ?? "").CompareTo ((y.Name ?? "")); |
||||
return entities.IndexOf (x).CompareTo (entities.IndexOf (y)); |
||||
}); |
||||
typeDeclaration.Members.Clear (); |
||||
typeDeclaration.Members.AddRange (entities); |
||||
|
||||
if (settings.GenerateCategoryComments) { |
||||
var curCat = GeneratedCodeMember.Unknown; |
||||
foreach (var mem in entities) { |
||||
if (mem.NextSibling is EntityDeclaration) |
||||
mem.Parent.InsertChildAfter (mem, new UnixNewLine (), Roles.NewLine); |
||||
|
||||
var cat = GetCodeMemberCategory (mem); |
||||
if (cat == curCat) |
||||
continue; |
||||
curCat = cat; |
||||
var label = settings.GetCategoryLabel (curCat); |
||||
if (string.IsNullOrEmpty (label)) |
||||
continue; |
||||
|
||||
var cmt = new Comment ("", CommentType.SingleLine); |
||||
var cmt2 = new Comment (" " + label, CommentType.SingleLine); |
||||
var cmt3 = new Comment ("", CommentType.SingleLine); |
||||
mem.Parent.InsertChildsBefore (mem, Roles.Comment, cmt, cmt2, cmt3); |
||||
if (cmt.PrevSibling is EntityDeclaration) |
||||
mem.Parent.InsertChildBefore (cmt, new UnixNewLine (), Roles.NewLine); |
||||
|
||||
mem.Parent.InsertChildAfter (cmt3, new UnixNewLine (), Roles.NewLine); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
static Lazy<GeneratedCodeSettings> defaultSettings = new Lazy<GeneratedCodeSettings>( |
||||
() => new GeneratedCodeSettings() { |
||||
CodeMemberOrder = new List<GeneratedCodeMember>() { |
||||
GeneratedCodeMember.StaticFields, |
||||
GeneratedCodeMember.InstanceFields, |
||||
GeneratedCodeMember.StaticProperties, |
||||
GeneratedCodeMember.InstanceProperties, |
||||
GeneratedCodeMember.Indexer, |
||||
GeneratedCodeMember.Constructors, |
||||
GeneratedCodeMember.StaticMethods, |
||||
GeneratedCodeMember.InstanceMethods, |
||||
GeneratedCodeMember.StaticEvents, |
||||
GeneratedCodeMember.InstanceEvents, |
||||
GeneratedCodeMember.Operators, |
||||
GeneratedCodeMember.NestedTypes |
||||
}, |
||||
GenerateCategoryComments = true, |
||||
SubOrderAlphabetical = true |
||||
}); |
||||
|
||||
public static GeneratedCodeSettings Default { |
||||
get { |
||||
return defaultSettings.Value; |
||||
} |
||||
} |
||||
} |
||||
} |
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,186 @@
@@ -0,0 +1,186 @@
|
||||
//
|
||||
// ExtractMethodAction.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using ICSharpCode.NRefactory.Semantics; |
||||
using System.Linq; |
||||
using ICSharpCode.NRefactory.CSharp.Resolver; |
||||
using ICSharpCode.NRefactory.CSharp.Analysis; |
||||
using System.Threading; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod |
||||
{ |
||||
[ContextAction("Extract method", Description = "Creates a new method out of selected text.")] |
||||
public class ExtractMethodAction : ICodeActionProvider |
||||
{ |
||||
public IEnumerable<CodeAction> GetActions(RefactoringContext context) |
||||
{ |
||||
if (!context.IsSomethingSelected) |
||||
yield break; |
||||
var selected = new List<AstNode>(context.GetSelectedNodes()); |
||||
if (selected.Count == 0) |
||||
yield break; |
||||
|
||||
if (selected.Count == 1 && selected [0] is Expression) { |
||||
var codeAction = CreateFromExpression(context, (Expression)selected [0]); |
||||
if (codeAction == null) |
||||
yield break; |
||||
yield return codeAction; |
||||
} |
||||
|
||||
foreach (var node in selected) { |
||||
if (!(node is Statement)) |
||||
yield break; |
||||
} |
||||
var action = CreateFromStatements (context, new List<Statement> (selected.OfType<Statement> ())); |
||||
if (action != null) |
||||
yield return action; |
||||
} |
||||
|
||||
CodeAction CreateFromExpression(RefactoringContext context, Expression expression) |
||||
{ |
||||
var resolveResult = context.Resolve(expression); |
||||
if (resolveResult.IsError) |
||||
return null; |
||||
|
||||
return new CodeAction(context.TranslateString("Extract method"), script => { |
||||
string methodName = "NewMethod"; |
||||
var method = new MethodDeclaration() { |
||||
ReturnType = context.CreateShortType(resolveResult.Type), |
||||
Name = methodName, |
||||
Body = new BlockStatement() { |
||||
new ReturnStatement(expression.Clone()) |
||||
} |
||||
}; |
||||
if (!StaticVisitor.UsesNotStaticMember(context, expression)) |
||||
method.Modifiers |= Modifiers.Static; |
||||
script.InsertWithCursor(context.TranslateString("Extract method"), method, Script.InsertPosition.Before); |
||||
var target = new IdentifierExpression(methodName); |
||||
script.Replace(expression, new InvocationExpression(target)); |
||||
// script.Link(target, method.NameToken);
|
||||
}); |
||||
} |
||||
|
||||
CodeAction CreateFromStatements(RefactoringContext context, List<Statement> statements) |
||||
{ |
||||
if (!(statements [0].Parent is Statement)) |
||||
return null; |
||||
|
||||
return new CodeAction(context.TranslateString("Extract method"), script => { |
||||
string methodName = "NewMethod"; |
||||
var method = new MethodDeclaration() { |
||||
ReturnType = new PrimitiveType("void"), |
||||
Name = methodName, |
||||
Body = new BlockStatement() |
||||
}; |
||||
bool usesNonStaticMember = false; |
||||
foreach (Statement node in statements) { |
||||
usesNonStaticMember |= StaticVisitor.UsesNotStaticMember(context, node); |
||||
method.Body.Add(node.Clone()); |
||||
} |
||||
if (!usesNonStaticMember) |
||||
method.Modifiers |= Modifiers.Static; |
||||
|
||||
var target = new IdentifierExpression(methodName); |
||||
var invocation = new InvocationExpression(target); |
||||
|
||||
var usedVariables = VariableLookupVisitor.Analyze(context, statements); |
||||
|
||||
var extractedCodeAnalysis = new DefiniteAssignmentAnalysis((Statement)statements [0].Parent, context.Resolver, context.CancellationToken); |
||||
var lastStatement = statements [statements.Count - 1]; |
||||
extractedCodeAnalysis.SetAnalyzedRange(statements [0], lastStatement); |
||||
var statusAfterMethod = new List<Tuple<IVariable, DefiniteAssignmentStatus>>(); |
||||
|
||||
foreach (var variable in usedVariables) { |
||||
extractedCodeAnalysis.Analyze(variable.Name, DefiniteAssignmentStatus.PotentiallyAssigned, context.CancellationToken); |
||||
statusAfterMethod.Add(Tuple.Create(variable, extractedCodeAnalysis.GetStatusAfter(lastStatement))); |
||||
} |
||||
var stmt = statements [0].GetParent<BlockStatement>(); |
||||
while (stmt.GetParent<BlockStatement> () != null) { |
||||
stmt = stmt.GetParent<BlockStatement>(); |
||||
} |
||||
|
||||
var wholeCodeAnalysis = new DefiniteAssignmentAnalysis(stmt, context.Resolver, context.CancellationToken); |
||||
var statusBeforeMethod = new Dictionary<IVariable, DefiniteAssignmentStatus>(); |
||||
foreach (var variable in usedVariables) { |
||||
wholeCodeAnalysis.Analyze(variable.Name, DefiniteAssignmentStatus.PotentiallyAssigned, context.CancellationToken); |
||||
statusBeforeMethod [variable] = extractedCodeAnalysis.GetStatusBefore(statements [0]); |
||||
} |
||||
|
||||
var afterCodeAnalysis = new DefiniteAssignmentAnalysis(stmt, context.Resolver, context.CancellationToken); |
||||
var statusAtEnd = new Dictionary<IVariable, DefiniteAssignmentStatus>(); |
||||
afterCodeAnalysis.SetAnalyzedRange(lastStatement, stmt.Statements.Last(), false, true); |
||||
|
||||
foreach (var variable in usedVariables) { |
||||
afterCodeAnalysis.Analyze(variable.Name, DefiniteAssignmentStatus.PotentiallyAssigned, context.CancellationToken); |
||||
statusBeforeMethod [variable] = extractedCodeAnalysis.GetStatusBefore(statements [0]); |
||||
} |
||||
var beforeVisitor = new VariableLookupVisitor(context); |
||||
beforeVisitor.SetAnalyzedRange(stmt, statements [0], true, false); |
||||
stmt.AcceptVisitor(beforeVisitor); |
||||
var afterVisitor = new VariableLookupVisitor(context); |
||||
afterVisitor.SetAnalyzedRange(lastStatement, stmt, false, true); |
||||
stmt.AcceptVisitor(afterVisitor); |
||||
|
||||
foreach (var status in statusAfterMethod) { |
||||
if (!beforeVisitor.UsedVariables.Contains(status.Item1) && !afterVisitor.UsedVariables.Contains(status.Item1)) |
||||
continue; |
||||
Expression argumentExpression = new IdentifierExpression(status.Item1.Name); |
||||
|
||||
ParameterModifier mod; |
||||
switch (status.Item2) { |
||||
case DefiniteAssignmentStatus.AssignedAfterTrueExpression: |
||||
case DefiniteAssignmentStatus.AssignedAfterFalseExpression: |
||||
case DefiniteAssignmentStatus.PotentiallyAssigned: |
||||
mod = ParameterModifier.Ref; |
||||
argumentExpression = new DirectionExpression(FieldDirection.Ref, argumentExpression); |
||||
break; |
||||
case DefiniteAssignmentStatus.DefinitelyAssigned: |
||||
if (statusBeforeMethod [status.Item1] != DefiniteAssignmentStatus.PotentiallyAssigned) |
||||
goto case DefiniteAssignmentStatus.PotentiallyAssigned; |
||||
mod = ParameterModifier.Out; |
||||
argumentExpression = new DirectionExpression(FieldDirection.Out, argumentExpression); |
||||
break; |
||||
// case DefiniteAssignmentStatus.Unassigned:
|
||||
default: |
||||
mod = ParameterModifier.None; |
||||
break; |
||||
} |
||||
method.Parameters.Add(new ParameterDeclaration(context.CreateShortType(status.Item1.Type), status.Item1.Name, mod)); |
||||
invocation.Arguments.Add(argumentExpression); |
||||
} |
||||
|
||||
foreach (var node in statements.Skip (1)) { |
||||
script.Remove(node); |
||||
} |
||||
script.Replace(statements [0], new ExpressionStatement(invocation)); |
||||
script.InsertWithCursor(context.TranslateString("Extract method"), method, Script.InsertPosition.Before); |
||||
//script.Link(target, method.NameToken);
|
||||
}); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,85 @@
@@ -0,0 +1,85 @@
|
||||
//
|
||||
// StaticVisitor.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using System; |
||||
using ICSharpCode.NRefactory.Semantics; |
||||
using System.Linq; |
||||
using ICSharpCode.NRefactory.CSharp.Resolver; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod |
||||
{ |
||||
class StaticVisitor : DepthFirstAstVisitor |
||||
{ |
||||
readonly RefactoringContext context; |
||||
public bool UsesNonStaticMember = false; |
||||
|
||||
StaticVisitor(RefactoringContext context) |
||||
{ |
||||
this.context = context; |
||||
} |
||||
|
||||
|
||||
public static bool UsesNotStaticMember(RefactoringContext context, AstNode node) |
||||
{ |
||||
var visitor = new StaticVisitor(context); |
||||
node.AcceptVisitor(visitor); |
||||
return visitor.UsesNonStaticMember; |
||||
} |
||||
|
||||
protected override void VisitChildren(AstNode node) |
||||
{ |
||||
if (UsesNonStaticMember) |
||||
return; |
||||
base.VisitChildren(node); |
||||
} |
||||
|
||||
public override void VisitThisReferenceExpression(ThisReferenceExpression thisReferenceExpression) |
||||
{ |
||||
UsesNonStaticMember = true; |
||||
base.VisitThisReferenceExpression(thisReferenceExpression); |
||||
} |
||||
|
||||
public override void VisitBaseReferenceExpression(BaseReferenceExpression baseReferenceExpression) |
||||
{ |
||||
UsesNonStaticMember = true; |
||||
base.VisitBaseReferenceExpression(baseReferenceExpression); |
||||
} |
||||
|
||||
public override void VisitIdentifierExpression(IdentifierExpression identifierExpression) |
||||
{ |
||||
var resolveResult = context.Resolve(identifierExpression); |
||||
if (resolveResult is MemberResolveResult) { |
||||
var memberResult = (MemberResolveResult)resolveResult; |
||||
if (!memberResult.Member.IsStatic) |
||||
UsesNonStaticMember = true; |
||||
} else if (resolveResult is MethodGroupResolveResult) { |
||||
var methodGroupResolveResult = (MethodGroupResolveResult)resolveResult; |
||||
if (methodGroupResolveResult.Methods.Any(m => !m.IsStatic)) |
||||
UsesNonStaticMember = true; |
||||
} |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,97 @@
@@ -0,0 +1,97 @@
|
||||
//
|
||||
// VariableLookupVisitor.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin Inc. (http://xamarin.com)
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using ICSharpCode.NRefactory.Semantics; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring.ExtractMethod |
||||
{ |
||||
class VariableLookupVisitor : DepthFirstAstVisitor |
||||
{ |
||||
readonly RefactoringContext context; |
||||
|
||||
public List<IVariable> UsedVariables = new List<IVariable> (); |
||||
|
||||
TextLocation startLocation = TextLocation.Empty; |
||||
TextLocation endLocation = TextLocation.Empty; |
||||
|
||||
|
||||
public VariableLookupVisitor (RefactoringContext context) |
||||
{ |
||||
this.context = context; |
||||
} |
||||
|
||||
public void SetAnalyzedRange(AstNode start, AstNode end, bool startInclusive = true, bool endInclusive = true) |
||||
{ |
||||
if (start == null) |
||||
throw new ArgumentNullException("start"); |
||||
if (end == null) |
||||
throw new ArgumentNullException("end"); |
||||
startLocation = startInclusive ? start.StartLocation : start.EndLocation; |
||||
endLocation = endInclusive ? end.EndLocation : end.StartLocation; |
||||
} |
||||
|
||||
public override void VisitIdentifierExpression(IdentifierExpression identifierExpression) |
||||
{ |
||||
if (startLocation.IsEmpty || startLocation <= identifierExpression.StartLocation && identifierExpression.EndLocation <= endLocation) { |
||||
var result = context.Resolve(identifierExpression); |
||||
var local = result as LocalResolveResult; |
||||
if (local != null && !UsedVariables.Contains(local.Variable)) |
||||
UsedVariables.Add(local.Variable); |
||||
} |
||||
} |
||||
|
||||
public override void VisitVariableDeclarationStatement(VariableDeclarationStatement variableDeclarationStatement) |
||||
{ |
||||
base.VisitVariableDeclarationStatement(variableDeclarationStatement); |
||||
foreach (var varDecl in variableDeclarationStatement.Variables) { |
||||
if (startLocation.IsEmpty || startLocation <= varDecl.StartLocation && varDecl.EndLocation <= endLocation) { |
||||
var result = context.Resolve(varDecl); |
||||
var local = result as LocalResolveResult; |
||||
if (local != null && !UsedVariables.Contains(local.Variable)) |
||||
UsedVariables.Add(local.Variable); |
||||
} |
||||
} |
||||
} |
||||
|
||||
|
||||
public static List<IVariable> Analyze(RefactoringContext context, Expression expression) |
||||
{ |
||||
var visitor = new VariableLookupVisitor(context); |
||||
expression.AcceptVisitor(visitor); |
||||
return visitor.UsedVariables; |
||||
} |
||||
|
||||
public static List<IVariable> Analyze(RefactoringContext context, List<Statement> statements) |
||||
{ |
||||
var visitor = new VariableLookupVisitor(context); |
||||
statements.ForEach(stmt => stmt.AcceptVisitor(visitor)); |
||||
return visitor.UsedVariables; |
||||
} |
||||
} |
||||
} |
||||
|
@ -0,0 +1,89 @@
@@ -0,0 +1,89 @@
|
||||
//
|
||||
// IntroduceConstantAction.cs
|
||||
//
|
||||
// Author:
|
||||
// Mike Krüger <mkrueger@xamarin.com>
|
||||
//
|
||||
// Copyright (c) 2012 Xamarin <http://xamarin.com>
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
// in the Software without restriction, including without limitation the rights
|
||||
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the Software is
|
||||
// furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in
|
||||
// all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
// THE SOFTWARE.
|
||||
|
||||
using System; |
||||
|
||||
using System.Collections.Generic; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
[ContextAction("Introduce constant", Description = "Creates a constant for a non constant primitive expression.")] |
||||
public class IntroduceConstantAction : ICodeActionProvider |
||||
{ |
||||
public IEnumerable<CodeAction> GetActions(RefactoringContext context) |
||||
{ |
||||
var pexpr = context.GetNode<PrimitiveExpression>(); |
||||
if (pexpr == null) |
||||
yield break; |
||||
var statement = context.GetNode<Statement>(); |
||||
if (statement == null) { |
||||
yield break; |
||||
} |
||||
|
||||
var resolveResult = context.Resolve(pexpr); |
||||
|
||||
yield return new CodeAction(context.TranslateString("Create local constant"), script => { |
||||
string name = CreateMethodDeclarationAction.CreateBaseName(pexpr, resolveResult.Type); |
||||
var service = (NamingConventionService)context.GetService(typeof(NamingConventionService)); |
||||
if (service != null) |
||||
name = service.CheckName(context, name, AffectedEntity.LocalConstant); |
||||
|
||||
var initializer = new VariableInitializer(name, pexpr.Clone()); |
||||
var decl = new VariableDeclarationStatement() { |
||||
Type = context.CreateShortType(resolveResult.Type), |
||||
Modifiers = Modifiers.Const, |
||||
Variables = { initializer } |
||||
}; |
||||
|
||||
script.InsertBefore(statement, decl); |
||||
var variableUsage = new IdentifierExpression(name); |
||||
script.Replace(pexpr, variableUsage); |
||||
script.Link(initializer.NameToken, variableUsage); |
||||
}); |
||||
|
||||
yield return new CodeAction(context.TranslateString("Create constant field"), script => { |
||||
string name = CreateMethodDeclarationAction.CreateBaseName(pexpr, resolveResult.Type); |
||||
var service = (NamingConventionService)context.GetService(typeof(NamingConventionService)); |
||||
if (service != null) |
||||
name = service.CheckName(context, name, AffectedEntity.ConstantField); |
||||
|
||||
var initializer = new VariableInitializer(name, pexpr.Clone()); |
||||
|
||||
var decl = new FieldDeclaration() { |
||||
ReturnType = context.CreateShortType(resolveResult.Type), |
||||
Modifiers = Modifiers.Const, |
||||
Variables = { initializer } |
||||
}; |
||||
|
||||
var variableUsage = new IdentifierExpression(name); |
||||
script.Replace(pexpr, variableUsage); |
||||
// script.Link(initializer.NameToken, variableUsage);
|
||||
script.InsertWithCursor(context.TranslateString("Create constant"), decl, Script.InsertPosition.Before); |
||||
}); |
||||
} |
||||
} |
||||
} |
||||
|
@ -0,0 +1,38 @@
@@ -0,0 +1,38 @@
|
||||
using System; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
/// <summary>
|
||||
/// A specialized code action creates a code action assoziated with one special type of ast nodes.
|
||||
/// </summary>
|
||||
public abstract class SpecializedCodeAction<T> : ICodeActionProvider where T : AstNode |
||||
{ |
||||
/// <summary>
|
||||
/// Gets the action for the specified ast node.
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// The code action. May return <c>null</c>, if no action can be provided.
|
||||
/// </returns>
|
||||
/// <param name='context'>
|
||||
/// The refactoring conext.
|
||||
/// </param>
|
||||
/// <param name='node'>
|
||||
/// The AstNode it's ensured that the node is always != null, if called.
|
||||
/// </param>
|
||||
protected abstract CodeAction GetAction(RefactoringContext context, T node); |
||||
|
||||
#region ICodeActionProvider implementation
|
||||
public System.Collections.Generic.IEnumerable<CodeAction> GetActions(RefactoringContext context) |
||||
{ |
||||
var node = context.GetNode<T>(); |
||||
if (node == null) |
||||
yield break; |
||||
var action = GetAction(context, node); |
||||
if (action == null) |
||||
yield break; |
||||
yield return action; |
||||
} |
||||
#endregion
|
||||
} |
||||
} |
||||
|
@ -0,0 +1,71 @@
@@ -0,0 +1,71 @@
|
||||
// Copyright (c) AlphaSierraPapa for the SharpDevelop Team
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy of this
|
||||
// software and associated documentation files (the "Software"), to deal in the Software
|
||||
// without restriction, including without limitation the rights to use, copy, modify, merge,
|
||||
// publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
|
||||
// to whom the Software is furnished to do so, subject to the following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included in all copies or
|
||||
// substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
|
||||
// PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
|
||||
// FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
using System; |
||||
using System.Collections.Generic; |
||||
using ICSharpCode.NRefactory.CSharp.Resolver; |
||||
using ICSharpCode.NRefactory.Semantics; |
||||
using ICSharpCode.NRefactory.TypeSystem; |
||||
|
||||
namespace ICSharpCode.NRefactory.CSharp.Refactoring |
||||
{ |
||||
[IssueDescription("Incorrect element type in foreach over generic collection", |
||||
Description= "Detects hidden explicit conversions in foreach loops.", |
||||
Category = IssueCategories.CodeQualityIssues, |
||||
Severity = Severity.Warning)] |
||||
public class ExplicitConversionInForEachIssue : ICodeIssueProvider |
||||
{ |
||||
public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context) |
||||
{ |
||||
return new GatherVisitor(context).GetIssues(); |
||||
} |
||||
|
||||
class GatherVisitor : GatherVisitorBase |
||||
{ |
||||
CSharpConversions conversions; |
||||
|
||||
public GatherVisitor (BaseRefactoringContext ctx) : base (ctx) |
||||
{ |
||||
} |
||||
|
||||
public override void VisitForeachStatement(ForeachStatement foreachStatement) |
||||
{ |
||||
base.VisitForeachStatement(foreachStatement); |
||||
var rr = ctx.Resolve(foreachStatement) as ForEachResolveResult; |
||||
if (rr == null) |
||||
return; |
||||
if (rr.ElementType.Kind == TypeKind.Unknown) |
||||
return; |
||||
if (ReflectionHelper.GetTypeCode(rr.ElementType) == TypeCode.Object) |
||||
return; |
||||
if (conversions == null) { |
||||
conversions = CSharpConversions.Get(ctx.Compilation); |
||||
} |
||||
Conversion c = conversions.ImplicitConversion(rr.ElementType, rr.ElementVariable.Type); |
||||
if (c.IsValid) |
||||
return; |
||||
var csResolver = ctx.GetResolverStateBefore(foreachStatement); |
||||
var builder = new TypeSystemAstBuilder(csResolver); |
||||
AstType elementType = builder.ConvertType(rr.ElementType); |
||||
AstType variableType = foreachStatement.VariableType; |
||||
string text = ctx.TranslateString("Collection element type '{0}' is not implicitly convertible to '{1}'"); |
||||
AddIssue(variableType, string.Format(text, elementType.GetText(), variableType.GetText())); |
||||
} |
||||
} |
||||
} |
||||
} |
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in new issue