Inheritance: IServiceProvider
        public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
        {
            var delegateVisitor = new GetDelgateUsagesVisitor (context);
            context.RootNode.AcceptVisitor (delegateVisitor);

            return new GatherVisitor (context, delegateVisitor).GetIssues ();
        }
		public IEnumerable<CodeIssue> GetIssues (BaseRefactoringContext context)
		{
			var unit = context.RootNode as CompilationUnit;
			if (unit == null)
				return Enumerable.Empty<CodeIssue> ();
			return new GatherVisitor (context, unit).GetIssues ();
		}
		public IEnumerable<CodeIssue> GetIssues (BaseRefactoringContext context)
		{
			var unit = context.RootNode as SyntaxTree;
			if (unit == null)
				return Enumerable.Empty<CodeIssue> ();
			return GetGatherVisitor (context, unit).GetIssues ();
		}
Beispiel #4
0
		public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
		{
			var visitor = new GatherVisitor (context, this);
			context.RootNode.AcceptVisitor (visitor);
			visitor.Collect ();
			return visitor.FoundIssues;
		}
			public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
			{
				// use a separate instance for every call, this is necessary
				// for thread-safety
				var provider = (CodeIssueProvider)Activator.CreateInstance(ProviderType);
				return provider.GetIssues(context);
			}
			public GatherVisitor (BaseRefactoringContext context, CompilationUnit unit,
								  AccessToClosureIssue issueProvider)
				: base (context)
			{
				this.title = context.TranslateString (issueProvider.Title);
				this.unit = unit;
				this.issueProvider = issueProvider;
			}
			public GatherVisitor(BaseRefactoringContext context) : base (context)
			{
				this.context = context;
				rules = new Dictionary<string, Func<int, int, bool>>();
				rules [typeof(ArgumentException).FullName] = (left, right) => left > right;
				rules [typeof(ArgumentNullException).FullName] = (left, right) => left < right;
				rules [typeof(ArgumentOutOfRangeException).FullName] = (left, right) => left < right;
				rules [typeof(DuplicateWaitObjectException).FullName] = (left, right) => left < right;
			}
		protected static bool FindUsage (BaseRefactoringContext context, CompilationUnit unit, IVariable variable,
										 AstNode declaration)
		{
			var found = false;
			refFinder.FindLocalReferences (variable, context.ParsedFile, unit, context.Compilation,
				(node, resolveResult) =>
				{
					found = found || node != declaration;
				}, context.CancellationToken);
			return found;
		}
		protected static bool FindUsage (BaseRefactoringContext context, SyntaxTree unit,
										 ITypeParameter typaParameter, AstNode declaration)
		{
			var found = false;
			refFinder.FindTypeParameterReferences (typaParameter, context.UnresolvedFile, unit, context.Compilation,
				(node, resolveResult) =>
				{
					found = found || node != declaration;
				}, context.CancellationToken);
			return found;
		}
		public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
		{
			var sw = new Stopwatch();
			sw.Start();
			var gatherer = new GatherVisitor(context, tryResolve);
			var issues = gatherer.GetIssues();
			sw.Stop();
			Console.WriteLine("Elapsed time in ParameterCanBeDemotedIssue: {0} (Checked types: {3, 4} Qualified for resolution check: {5, 4} Members with issues: {4, 4} Method bodies resolved: {2, 4} File: '{1}')",
			                  sw.Elapsed, context.UnresolvedFile.FileName, gatherer.MethodResolveCount, gatherer.TypesChecked, gatherer.MembersWithIssues, gatherer.TypeResolveCount);
			return issues;
		}
		public override IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context, string subIssue = null)
		{
			var refactoringContext = context as SDRefactoringContext;
			if (refactoringContext == null)
				return Enumerable.Empty<CodeIssue>();
			
			var syntaxTree = context.RootNode as SyntaxTree;
			if (syntaxTree == null)
				return Enumerable.Empty<CodeIssue>();

			return syntaxTree.Errors.Select(error => CreateCodeIssue(error, refactoringContext));
		}
        static IType GetElementType(ResolveResult rr, BaseRefactoringContext context)
        {
            if (rr.IsError || rr.Type.Kind == TypeKind.Unknown)
                return null;
            var type = GetCollectionType(rr.Type);
            if (type == null)
                return null;

            var parameterizedType = type as ParameterizedType;
            if (parameterizedType != null)
                return parameterizedType.TypeArguments.First();
            return context.Compilation.FindType(KnownTypeCode.Object);
        }
		protected static bool HidesMember(BaseRefactoringContext ctx, AstNode node, string variableName)
		{
			var typeDecl = node.GetParent<TypeDeclaration> ();
			if (typeDecl == null)
				return false;
			var typeResolveResult = ctx.Resolve (typeDecl) as TypeResolveResult;
			if (typeResolveResult == null)
				return false;

			var entityDecl = node.GetParent<EntityDeclaration> ();
			var isStatic = (entityDecl.Modifiers & Modifiers.Static) == Modifiers.Static;

			return typeResolveResult.Type.GetMembers (m => m.Name == variableName && m.IsStatic	== isStatic).Any ();
		}
		protected static bool TestOnlyAssigned(BaseRefactoringContext ctx, AstNode rootNode, IVariable variable)
		{
			var assignment = false;
			var nonAssignment = false;
			foreach (var result in ctx.FindReferences(rootNode, variable)) {
				var node = result.Node;
				if (node is ParameterDeclaration)
					continue;

				if (node is VariableInitializer) {
					if (!(node as VariableInitializer).Initializer.IsNull)
						assignment = true;
					continue;
				}

				if (node is IdentifierExpression) {
					var parent = node.Parent;
					if (parent is AssignmentExpression) {
						if (((AssignmentExpression)parent).Left == node) {
							assignment = true;
							continue;
						}
					} else if (parent is UnaryOperatorExpression) {
						var op = ((UnaryOperatorExpression)parent).Operator;
						switch (op) {
							case UnaryOperatorType.Increment:
							case UnaryOperatorType.PostIncrement:
							case UnaryOperatorType.Decrement:
							case UnaryOperatorType.PostDecrement:
								assignment = true;
								if (!(parent.Parent is ExpressionStatement))
									nonAssignment = true;
								continue;
						}
					} else if (parent is DirectionExpression) {
						if (((DirectionExpression)parent).FieldDirection == FieldDirection.Out) {
							assignment = true;
							// Using dummy variables is necessary for ignoring
							// out-arguments, so we don't want to warn for those.
							nonAssignment = true;
							continue;
						}
					}
				}
				nonAssignment = true;
			}
			return assignment && !nonAssignment;
		}
		internal static IField GetBackingField (BaseRefactoringContext context, PropertyDeclaration propertyDeclaration)
		{
			// automatic properties always need getter & setter
			if (propertyDeclaration == null || propertyDeclaration.Getter.IsNull || propertyDeclaration.Setter.IsNull || propertyDeclaration.Getter.Body.IsNull || propertyDeclaration.Setter.Body.IsNull)
				return null;
			if (!context.Supports(csharp3) || propertyDeclaration.HasModifier (ICSharpCode.NRefactory.CSharp.Modifiers.Abstract) || ((TypeDeclaration)propertyDeclaration.Parent).ClassType == ClassType.Interface)
				return null;
			var getterField = ScanGetter (context, propertyDeclaration);
			if (getterField == null)
				return null;
			var setterField = ScanSetter (context, propertyDeclaration);
			if (setterField == null)
				return null;
			if (!getterField.Equals(setterField))
				return null;
			return getterField;
		}
        protected override IEnumerable<CodeAction> GetFixes(BaseRefactoringContext context, Node env,
															 string variableName)
        {
            var containingStatement = env.ContainingStatement;

            // we don't give a fix for these cases since the general fix may not work
            // lambda in while/do-while/for condition
            if (containingStatement is WhileStatement || containingStatement is DoWhileStatement ||
                containingStatement is ForStatement)
                yield break;
            // lambda in for initializer/iterator
            if (containingStatement.Parent is ForStatement &&
                ((ForStatement)containingStatement.Parent).EmbeddedStatement != containingStatement)
                yield break;

            Action<Script> action = script =>
            {
                var newName = LocalVariableNamePicker.PickSafeName (
                    containingStatement.GetParent<EntityDeclaration> (),
                    Enumerable.Range (1, 100).Select (i => variableName + i));

                var variableDecl = new VariableDeclarationStatement (new SimpleType("var"), newName,
                                                                     new IdentifierExpression (variableName));

                if (containingStatement.Parent is BlockStatement || containingStatement.Parent is SwitchSection) {
                    script.InsertBefore (containingStatement, variableDecl);
                } else {
                    var offset = script.GetCurrentOffset (containingStatement.StartLocation);
                    script.InsertBefore (containingStatement, variableDecl);
                    script.InsertText (offset, "{");
                    script.InsertText (script.GetCurrentOffset (containingStatement.EndLocation), "}");
                    script.FormatText (containingStatement.Parent);
                }

                var textNodes = new List<AstNode> ();
                textNodes.Add (variableDecl.Variables.First ().NameToken);

                foreach (var reference in env.GetAllReferences ()) {
                    var identifier = new IdentifierExpression (newName);
                    script.Replace (reference.AstNode, identifier);
                    textNodes.Add (identifier);
                }
                script.Link (textNodes.ToArray ());
            };
            yield return new CodeAction (context.TranslateString ("Copy to local variable"), action, env.AstNode);
        }
        protected static bool HidesMember(BaseRefactoringContext ctx, AstNode node, string variableName)
        {
            var typeDecl = node.GetParent<TypeDeclaration>();
            if (typeDecl == null)
                return false;
            var entityDecl = node.GetParent<EntityDeclaration>();
            var memberResolveResult = ctx.Resolve(entityDecl) as MemberResolveResult;
            if (memberResolveResult == null)
                return false;
            var typeResolveResult = ctx.Resolve(typeDecl) as TypeResolveResult;
            if (typeResolveResult == null)
                return false;

            var sourceMember = memberResolveResult.Member;

            return typeResolveResult.Type.GetMembers(m => m.Name == variableName).Any(m2 => IsAccessible(sourceMember, m2));
        }
		protected static bool TestOnlyAssigned (BaseRefactoringContext ctx, SyntaxTree unit, IVariable variable)
		{
			var assignment = false;
			var nonAssignment = false;
			refFinder.FindLocalReferences (variable, ctx.UnresolvedFile, unit, ctx.Compilation,
				(node, resolveResult) =>
				{
					if (node is ParameterDeclaration)
						return;

					if (node is VariableInitializer) {
						if (!(node as VariableInitializer).Initializer.IsNull)
							assignment = true;
						return;
					}

					if (node is IdentifierExpression) {
						var parent = node.Parent;
						if (parent is AssignmentExpression) {
							if (((AssignmentExpression)parent).Left == node) {
								assignment = true;
								return;
							}
						} else if (parent is UnaryOperatorExpression) {
							var op = ((UnaryOperatorExpression)parent).Operator;
							switch (op) {
							case UnaryOperatorType.Increment:
							case UnaryOperatorType.PostIncrement:
							case UnaryOperatorType.Decrement:
							case UnaryOperatorType.PostDecrement:
								assignment = true;
								return;
							}
						} else if (parent is DirectionExpression) {
							if (((DirectionExpression)parent).FieldDirection == FieldDirection.Out) {
								assignment = true;
								return;
							}
						}
					}
					nonAssignment = true;
				}, ctx.CancellationToken);
			return assignment && !nonAssignment;
		}
		public override IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context, string subIssue = null)
		{
			var refactoringContext = context as SDRefactoringContext;
			if (refactoringContext == null)
				yield break;
			
			var syntaxTree = context.RootNode as SyntaxTree;
			if (syntaxTree == null)
				yield break;
			
			int prevLine = 0;
			foreach (var error in syntaxTree.Errors) {
				if (error.Region.BeginLine == prevLine)
					continue; // show at most one error per line
				prevLine = error.Region.BeginLine;
				var issue = CreateCodeIssue(error, refactoringContext);
				if (issue != null)
					yield return issue;
			}
		}
 public GatherVisitor(BaseRefactoringContext context) : base(context)
 {
     this.context = context;
 }
 public GatherVisitor(BaseRefactoringContext ctx)
     : base(ctx)
 {
     conversion = new CSharpConversions(ctx.Compilation);
 }
 public GatherVisitor(BaseRefactoringContext context)
     : base(context)
 {
 }
			public GatherVisitor (BaseRefactoringContext ctx, SyntaxTree unit)
				: base (ctx)
			{
				this.unit = unit;
			}
Beispiel #24
0
        public static IType GuessType(BaseRefactoringContext context, AstNode expr)
        {
            if (expr is SimpleType && expr.Role == Roles.TypeArgument) {
                if (expr.Parent is MemberReferenceExpression || expr.Parent is IdentifierExpression) {
                    var rr = context.Resolve (expr.Parent);
                    var argumentNumber = expr.Parent.GetChildrenByRole (Roles.TypeArgument).TakeWhile (c => c != expr).Count ();

                    var mgrr = rr as MethodGroupResolveResult;
                    if (mgrr != null && mgrr.Methods.Any () && mgrr.Methods.First ().TypeArguments.Count > argumentNumber)
                        return mgrr.Methods.First ().TypeParameters[argumentNumber];
                } else if (expr.Parent is MemberType || expr.Parent is SimpleType) {
                    var rr = context.Resolve (expr.Parent);
                    var argumentNumber = expr.Parent.GetChildrenByRole (Roles.TypeArgument).TakeWhile (c => c != expr).Count ();
                    var mgrr = rr as TypeResolveResult;
                    if (mgrr != null &&  mgrr.Type.TypeParameterCount > argumentNumber) {
                        return mgrr.Type.GetDefinition ().TypeParameters[argumentNumber];
                    }
                }
            }

            var type = GetValidTypes(context.Resolver, expr).ToArray();
            var typeInference = new TypeInference(context.Compilation);
            typeInference.Algorithm = TypeInferenceAlgorithm.Improved;
            var inferedType = typeInference.FindTypeInBounds(type, emptyTypes);
            return inferedType;
        }
		internal override GatherVisitorBase GetGatherVisitor (BaseRefactoringContext context)
		{
			return new GatherVisitor (context);
		}
Beispiel #26
0
 public GatherVisitor(BaseRefactoringContext context) : base(context)
 {
     threadStaticAttribute = ctx.Compilation.FindType(typeof(ThreadStaticAttribute));
 }
Beispiel #27
0
		public TypeCriteriaCollector(BaseRefactoringContext context)
		{
			this.context = context;
			TypeCriteria = new Dictionary<IVariable, IList<ITypeCriterion>>();
			UsedVariables = new HashSet<IVariable>();
		}
 public override System.Collections.Generic.IEnumerable <CodeIssue> GetIssues(BaseRefactoringContext context)
 {
     return(new GatherVisitor(context).GetIssues());
 }
 public GatherVisitor(BaseRefactoringContext ctx)
     : base(ctx)
 {
 }
Beispiel #30
0
 public GatherVisitor(BaseRefactoringContext context)
     : base(context)
 {
     DeclaredMethods = new List <MethodDeclaration>();
 }
			public GatherVisitor (BaseRefactoringContext ctx, CompilationUnit unit)
				: base (ctx)
			{
				this.unit = unit;
			}
		protected override IGatherVisitor CreateVisitor(BaseRefactoringContext context)
		{
			return new ReplaceWithSingleCallToAverageIssue.GatherVisitor<ReplaceWithSingleCallToMaxIssue>(context, "Max");
		}
		public IEnumerable<CodeIssue> GetIssues (BaseRefactoringContext context)
		{
			return new GatherVisitor (context).GetIssues ();
		}
			public GatherVisitor(BaseRefactoringContext context, ValueParameterUnusedIssue inspector) : base (context)
			{
				this.context = context;
			}
 public GatherVisitor(BaseRefactoringContext ctx, RedundantThisIssue issueProvider) : base(ctx, issueProvider)
 {
 }
 public GatherVisitor(BaseRefactoringContext context, ValueParameterNotUsedIssue inspector) : base(context)
 {
 }
 public IEnumerable <CodeIssue> GetIssues(BaseRefactoringContext context)
 {
     return(new GatherVisitor(context).GetIssues());
 }
Beispiel #38
0
 protected override IGatherVisitor CreateVisitor(BaseRefactoringContext context)
 {
     return(new ReplaceWithSingleCallToAnyIssue.GatherVisitor <ReplaceWithSingleCallToSingleOrDefaultIssue>(context, "SingleOrDefault"));
 }
Beispiel #39
0
 protected override IGatherVisitor CreateVisitor(BaseRefactoringContext context)
 {
     return(new GatherVisitor(context));
 }
		internal abstract GatherVisitorBase GetGatherVisitor (BaseRefactoringContext ctx, SyntaxTree unit);
 protected abstract IEnumerable <CodeAction> GetFixes(BaseRefactoringContext context, Node env,
                                                      string variableName);
			public GatherVisitor (BaseRefactoringContext ctx)
				: base (ctx)
			{
			}
 public GatherVisitor(BaseRefactoringContext ctx, RedundantInternalIssue inspector) : base(ctx)
 {
     this.inspector = inspector;
 }
 public IEnumerable<CodeIssue> GetIssues(BaseRefactoringContext context)
 {
     return new ReplaceWithSingleCallToAnyIssue.GatherVisitor<ReplaceWithSingleCallToSingleOrDefaultIssue>(context, "SingleOrDefault").GetIssues();
 }
Beispiel #45
0
            static Expression CreateDefaultValueExpression(BaseRefactoringContext ctx, AstNode node, IType type, object constantValue)
            {
                var astBuilder = ctx.CreateTypeSystemAstBuilder(node);

                return(astBuilder.ConvertConstantValue(type, constantValue));
            }