コード例 #1
0
        ObjectCreateExpression GetCreateExpression(RefactoringOptions options)
        {
            TextEditorData data = options.GetTextEditorData();

            if (data == null)
            {
                return(null);
            }
            string expression = options.ResolveResult.ResolvedExpression.Expression;

            if (!expression.Contains("("))
            {
                int startPos = data.Document.LocationToOffset(options.ResolveResult.ResolvedExpression.Region.Start.Line, options.ResolveResult.ResolvedExpression.Region.Start.Column);
                if (startPos < 0)
                {
                    return(null);
                }
                for (int pos = startPos; pos < data.Document.Length; pos++)
                {
                    char ch = data.Document.GetCharAt(pos);
                    if (ch == '(')
                    {
                        int offset = data.Document.GetMatchingBracketOffset(pos);
                        if (offset < startPos)
                        {
                            return(null);
                        }
                        expression = data.Document.GetTextAt(startPos, offset - startPos + 1);
                        break;
                    }
                }
            }
            if (!expression.StartsWith("new "))
            {
                int startPos = data.Document.LocationToOffset(options.ResolveResult.ResolvedExpression.Region.Start.Line, options.ResolveResult.ResolvedExpression.Region.Start.Column);
                if (startPos < 0)
                {
                    return(null);
                }
                for (int pos = startPos; pos >= 0; pos--)
                {
                    char ch = data.Document.GetCharAt(pos);
                    if (Char.IsWhiteSpace(ch) && !Char.IsLetterOrDigit(ch) && ch != '_')
                    {
                        return(null);
                    }
                    if (data.Document.GetTextAt(pos, 4) == "new ")
                    {
                        expression = "new " + expression;
                        break;
                    }
                }
            }

            INRefactoryASTProvider provider = options.GetASTProvider();

            return(provider != null?provider.ParseText(expression) as ObjectCreateExpression : null);
        }
コード例 #2
0
        InvocationExpression GetInvocationExpression(RefactoringOptions options)
        {
            TextEditorData data = options.GetTextEditorData();

            if (data == null || options.ResolveResult == null || options.ResolveResult.ResolvedExpression == null)
            {
                return(null);
            }
            string expression = options.ResolveResult.ResolvedExpression.Expression;

            if (!expression.Contains("("))
            {
                int startPos = data.Document.LocationToOffset(options.ResolveResult.ResolvedExpression.Region.Start.Line - 1, options.ResolveResult.ResolvedExpression.Region.Start.Column - 1);
                if (startPos < 0)
                {
                    return(null);
                }
                bool gotWs = false;
                for (int pos = startPos; pos > 2; pos--)
                {
                    char ch = data.Document.GetCharAt(pos);
                    if (char.IsWhiteSpace(ch))
                    {
                        if (gotWs)
                        {
                            break;
                        }
                        gotWs = true;
                        continue;
                    }
                    if (gotWs && ch == 'w' && data.Document.GetCharAt(pos - 1) == 'e' && data.Document.GetCharAt(pos - 2) == 'n')
                    {
                        return(null);
                    }
                }
                for (int pos = startPos; pos < data.Document.Length; pos++)
                {
                    char ch = data.Document.GetCharAt(pos);
                    if (ch == '(')
                    {
                        int offset = data.Document.GetMatchingBracketOffset(pos);
                        if (offset < startPos)
                        {
                            return(null);
                        }
                        expression = data.Document.GetTextAt(startPos, offset - startPos + 1);
                        break;
                    }
                }
            }
            INRefactoryASTProvider provider = options.GetASTProvider();

            return(provider != null?provider.ParseText(expression) as InvocationExpression : null);
        }
コード例 #3
0
        public override bool IsValid(RefactoringOptions options)
        {
            IResolver resolver = options.GetResolver();
            INRefactoryASTProvider provider = options.GetASTProvider();

            if (resolver == null || provider == null)
            {
                return(false);
            }
            TextEditorData data = options.GetTextEditorData();

            if (data == null)
            {
                return(false);
            }
            if (data.IsSomethingSelected)
            {
                ExpressionResult expressionResult = new ExpressionResult(data.SelectedText.Trim());
                if (expressionResult.Expression.Contains(" ") || expressionResult.Expression.Contains("\t"))
                {
                    expressionResult.Expression = "(" + expressionResult.Expression + ")";
                }
                var endPoint = data.MainSelection.Anchor < data.MainSelection.Lead ? data.MainSelection.Lead : data.MainSelection.Anchor;
                options.ResolveResult = resolver.Resolve(expressionResult, new DomLocation(endPoint.Line, endPoint.Column));
                if (options.ResolveResult == null)
                {
                    return(false);
                }
                if (options.ResolveResult.CallingMember == null || !options.ResolveResult.CallingMember.BodyRegion.Contains(endPoint.Line, endPoint.Column))
                {
                    return(false);
                }
                return(true);
            }
            LineSegment    lineSegment = data.Document.GetLine(data.Caret.Line);
            string         line        = data.Document.GetTextAt(lineSegment);
            Expression     expression  = provider.ParseExpression(line);
            BlockStatement block       = provider.ParseText(line) as BlockStatement;

            if (expression == null || (block != null && block.Children [0] is LocalVariableDeclaration))
            {
                return(false);
            }

            options.ResolveResult = resolver.Resolve(new ExpressionResult(line), new DomLocation(options.Document.Editor.Caret.Line, options.Document.Editor.Caret.Column));
            return(options.ResolveResult.ResolvedType != null && !string.IsNullOrEmpty(options.ResolveResult.ResolvedType.FullName) && options.ResolveResult.ResolvedType.FullName != DomReturnType.Void.FullName);
        }
コード例 #4
0
        public ICSharpCode.NRefactory.Ast.INode ParseMember(IMember member)
        {
            if (member == null || member.BodyRegion.IsEmpty)
            {
                return(null);
            }
            INRefactoryASTProvider provider = GetASTProvider();

            if (provider == null)
            {
                return(null);
            }

            int    start      = Document.Editor.Document.LocationToOffset(member.BodyRegion.Start.Line, member.BodyRegion.Start.Column);
            int    end        = Document.Editor.Document.LocationToOffset(member.BodyRegion.End.Line, member.BodyRegion.End.Column);
            string memberBody = Document.Editor.GetTextBetween(start, end);

            return(provider.ParseText(memberBody));
        }
コード例 #5
0
        ICSharpCode.NRefactory.Ast.INode GetMemberBodyNode(MonoDevelop.Refactoring.RefactoringOptions options)
        {
            IMember member = ((LocalVariable)options.SelectedItem).DeclaringMember;

            if (member == null)
            {
                return(null);
            }
            int    start      = options.Document.Editor.Document.LocationToOffset(member.BodyRegion.Start.Line, member.BodyRegion.Start.Column);
            int    end        = options.Document.Editor.Document.LocationToOffset(member.BodyRegion.End.Line, member.BodyRegion.End.Column);
            string memberBody = options.Document.Editor.GetTextBetween(start, end);
            INRefactoryASTProvider provider = options.GetASTProvider();

            if (provider == null)
            {
//				Console.WriteLine("!!!Provider not found!");
                return(null);
            }
            return(provider.ParseText(memberBody));
        }
コード例 #6
0
        ICSharpCode.NRefactory.Ast.INode Analyze(RefactoringOptions options, ExtractMethodParameters param, bool fillParameter)
        {
            IResolver resolver = options.GetResolver();
            INRefactoryASTProvider provider = options.GetASTProvider();

            if (resolver == null || provider == null)
            {
                return(null);
            }

            string text = options.Document.TextEditor.GetText(options.Document.TextEditor.SelectionStartPosition, options.Document.TextEditor.SelectionEndPosition);

            TextEditorData data = options.GetTextEditorData();
            var            cu   = provider.ParseFile(data.Document.GetTextAt(0, data.SelectionRange.Offset) + "MethodCall ();" + data.Document.GetTextAt(data.SelectionRange.EndOffset, data.Document.Length - data.SelectionRange.EndOffset));

            if (cu == null || provider.LastErrors.Count > 0)
            {
                cu = provider.ParseFile(data.Document.GetTextAt(0, data.SelectionRange.Offset) + "MethodCall ()" + data.Document.GetTextAt(data.SelectionRange.EndOffset, data.Document.Length - data.SelectionRange.EndOffset));
            }

            if (cu == null || provider.LastErrors.Count > 0)
            {
                return(null);
            }

            param.Text = RemoveIndent(text, GetIndent(text)).TrimEnd('\n', '\r');

            ICSharpCode.NRefactory.Ast.INode result = provider.ParseText(text);
            if (cu == null || provider.LastErrors.Count > 0)
            {
                return(null);
            }

            VariableLookupVisitor visitor = new VariableLookupVisitor(resolver, param.Location);

            visitor.MemberLocation = new Location(param.DeclaringMember.Location.Column, param.DeclaringMember.Location.Line);
            if (fillParameter)
            {
                if (result != null)
                {
                    result.AcceptVisitor(visitor, null);
                }
                if (result is Expression)
                {
                    ResolveResult resolveResult = resolver.Resolve(new ExpressionResult(text), param.Location);
                    if (resolveResult != null)
                    {
                        param.ExpressionType = resolveResult.ResolvedType;
                    }
                }

                var startLocation = data.Document.OffsetToLocation(data.SelectionRange.Offset);
                var endLocation   = data.Document.OffsetToLocation(data.SelectionRange.EndOffset);
//				Console.WriteLine ("startLocation={0}, endLocation={1}", startLocation, endLocation);

                foreach (VariableDescriptor varDescr in visitor.VariableList.Where(v => !v.IsDefined && v.InitialValueUsed))
                {
                    if (startLocation <= varDescr.Location && varDescr.Location < endLocation)
                    {
                        continue;
                    }
//					Console.WriteLine (varDescr.Location);
//					Console.WriteLine (startLocation <= varDescr.Location);
//					Console.WriteLine (varDescr.Location < endLocation);
                    param.Parameters.Add(varDescr);
                }
                param.Variables = new List <VariableDescriptor> (visitor.Variables.Values);
                foreach (VariableDescriptor varDescr in visitor.VariableList.Where(v => !v.IsDefined && param.Variables.Contains(v)))
                {
                    if (param.Parameters.Contains(varDescr))
                    {
                        continue;
                    }
                    if (startLocation <= varDescr.Location && varDescr.Location < endLocation)
                    {
                        continue;
                    }
                    param.Parameters.Add(varDescr);
                }

                param.ReferencesMember = visitor.ReferencesMember;
                param.ChangedVariables = new HashSet <string> (visitor.Variables.Values.Where(v => v.GetsChanged).Select(v => v.Name));

                // analyze the variables outside of the selected text
                IMember member = param.DeclaringMember;

                int startOffset = data.Document.LocationToOffset(member.BodyRegion.Start.Line, member.BodyRegion.Start.Column);
                int endOffset   = data.Document.LocationToOffset(member.BodyRegion.End.Line, member.BodyRegion.End.Column);
                if (data.SelectionRange.Offset < startOffset || endOffset < data.SelectionRange.EndOffset)
                {
                    return(null);
                }
                text = data.Document.GetTextBetween(startOffset, data.SelectionRange.Offset) + data.Document.GetTextBetween(data.SelectionRange.EndOffset, endOffset);
                ICSharpCode.NRefactory.Ast.INode parsedNode = provider.ParseText(text);
                visitor                = new VariableLookupVisitor(resolver, param.Location);
                visitor.CutRegion      = new DomRegion(data.MainSelection.MinLine, data.MainSelection.MaxLine);
                visitor.MemberLocation = new Location(param.DeclaringMember.Location.Column, param.DeclaringMember.Location.Line);
                if (parsedNode != null)
                {
                    parsedNode.AcceptVisitor(visitor, null);
                }


                param.VariablesOutside = new Dictionary <string, VariableDescriptor> ();
                foreach (var pair in visitor.Variables)
                {
                    if (startLocation < pair.Value.Location || endLocation >= pair.Value.Location)
                    {
                        param.VariablesOutside.Add(pair.Key, pair.Value);
                    }
                }
                param.OutsideVariableList = new List <VariableDescriptor> ();
                foreach (var v in visitor.VariableList)
                {
                    if (startLocation < v.Location || endLocation >= v.Location)
                    {
                        param.OutsideVariableList.Add(v);
                    }
                }



                param.ChangedVariablesUsedOutside = new List <VariableDescriptor> (param.Variables.Where(v => v.GetsChanged && param.VariablesOutside.ContainsKey(v.Name)));
                param.OneChangedVariable          = result is BlockStatement;
                if (param.OneChangedVariable)
                {
                    param.OneChangedVariable = param.ChangedVariablesUsedOutside.Count == 1;
                }

                param.VariablesToGenerate = new List <VariableDescriptor> (param.ChangedVariablesUsedOutside.Where(v => v.IsDefined));
                foreach (VariableDescriptor var in param.VariablesToGenerate)
                {
                    param.Parameters.Add(var);
                }
                if (param.OneChangedVariable)
                {
                    param.VariablesToDefine = new List <VariableDescriptor> (param.Parameters.Where(var => !var.InitialValueUsed));
                    param.VariablesToDefine.ForEach(var => param.Parameters.Remove(var));
                }
                else
                {
                    param.VariablesToDefine = new List <VariableDescriptor> ();
                }
            }
            return(result);
        }