Ejemplo n.º 1
0
        void AddCompletionData(ref List <ICompletionData> resultList, ArrayList completionData, Dom.ExpressionContext context)
        {
            // used to store the method names for grouping overloads
            Dictionary <string, CSCompletionData> nameDictionary = new Dictionary <string, CSCompletionData>();

            // Add the completion data as returned by SharpDevelop.Dom to the
            // list for the text editor
            foreach (object obj in completionData)
            {
                if (!context.ShowEntry(obj))
                {
                    continue;
                }

                if (obj is string)
                {
                    // namespace names are returned as string
                    resultList.Add(new DefaultCompletionData((string)obj, "namespace " + obj, 5));
                }
                else if (obj is Dom.IClass)
                {
                    Dom.IClass c = (Dom.IClass)obj;
                    resultList.Add(new CSCompletionData(c));
                }
                else if (obj is Dom.IMember)
                {
                    Dom.IMember m = (Dom.IMember)obj;
                    if (m is Dom.IMethod && ((m as Dom.IMethod).IsConstructor))
                    {
                        // Skip constructors
                        continue;
                    }
                    // Group results by name and add "(x Overloads)" to the
                    // description if there are multiple results with the same name.

                    CSCompletionData data;
                    if (nameDictionary.TryGetValue(m.Name, out data))
                    {
                        data.AddOverload();
                    }
                    else
                    {
                        nameDictionary[m.Name] = data = new CSCompletionData(m);
                        resultList.Add(data);
                    }
                }
                else
                {
                    // Current ICSharpCode.SharpDevelop.Dom should never return anything else
                    throw new NotSupportedException();
                }
            }
        }
Ejemplo n.º 2
0
        void AddCompletionData(List <ICompletionData> resultList, IEnumerable <Dom.ICompletionEntry> completionData)
        {
            // used to store the method names for grouping overloads
            Dictionary <string, CodeCompletionData> nameDictionary = new Dictionary <string, CodeCompletionData>();

            // Add the completion data as returned by SharpDevelop.Dom to the
            // list for the text editor
            foreach (object obj in completionData)
            {
                if (obj is Dom.NamespaceEntry)
                {
                    resultList.Add(new DefaultCompletionData(obj.ToString(), "namespace " + obj, 5));
                }
                else if (obj is Dom.NRefactoryResolver.KeywordEntry)
                {
                    resultList.Add(new DefaultCompletionData(obj.ToString(), obj.ToString(), 5));
                }
                else if (obj is Dom.IClass)
                {
                    Dom.IClass c = (Dom.IClass)obj;
                    resultList.Add(new CodeCompletionData(c));
                }
                else if (obj is Dom.IMember)
                {
                    Dom.IMember m = (Dom.IMember)obj;
                    if (m is Dom.IMethod && ((m as Dom.IMethod).IsConstructor))
                    {
                        // Skip constructors
                        continue;
                    }
                    // Group results by name and add "(x Overloads)" to the
                    // description if there are multiple results with the same name.

                    CodeCompletionData data;
                    if (nameDictionary.TryGetValue(m.Name, out data))
                    {
                        data.AddOverload();
                    }
                    else
                    {
                        nameDictionary[m.Name] = data = new CodeCompletionData(m);
                        resultList.Add(data);
                    }
                }
                else
                {
                    // Current ICSharpCode.SharpDevelop.Dom should never return anything else
                    throw new NotSupportedException();
                }
            }
        }
Ejemplo n.º 3
0
 void AddCompletionData(List <ICompletionData> resultList, ArrayList completionData)
 {
     // Add the completion data as returned by SharpDevelop.Dom to the
     // list for the text editor
     foreach (object obj in completionData)
     {
         if (obj is string)
         {
             // namespace names are returned as string
             resultList.Add(new DefaultCompletionData((string)obj, "namespace " + obj, 5));
         }
         else if (obj is Dom.IClass)
         {
             Dom.IClass c = (Dom.IClass)obj;
             if (c.ClassType == Dom.ClassType.Enum)
             {
                 resultList.Add(new DefaultCompletionData(c.Name,
                                                          GetDescription(c),
                                                          4));
             }
             else // missing: struct, delegate etc.
             {
                 resultList.Add(new DefaultCompletionData(c.Name,
                                                          GetDescription(c),
                                                          0));
             }
         }
         else if (obj is Dom.IMember)
         {
             Dom.IMember m = (Dom.IMember)obj;
             if (m is Dom.IMethod && ((m as Dom.IMethod).IsConstructor))
             {
                 // Skip constructors
                 continue;
             }
             // TODO: Group results by name and add "(x Overloads)" to the
             // description if there are multiple results with the same name.
             resultList.Add(new DefaultCompletionData(m.Name,
                                                      GetDescription(m),
                                                      GetMemberImageIndex(m)));
         }
         else
         {
             // Current ICSharpCode.SharpDevelop.Dom should never return anything else
             throw new NotSupportedException();
         }
     }
 }
Ejemplo n.º 4
0
        public void InsertAfterCurrentMethod()
        {
            IOutputAstVisitor outputVisitor = this.GetOutputVisitor();

            using (SpecialNodesInserter.Install(this.specialsList, outputVisitor)) {
                string code = "\r\n\r\n" + GenerateCode(this.extractedMethod, true);

                code = code.TrimEnd('\r', '\n', ' ', '\t');

                Dom.IMember p = GetParentMember(this.textEditor, start.Line, start.Column);

                int offset = textEditor.Document.PositionToOffset(p.BodyRegion.EndLine, p.BodyRegion.EndColumn);

                textEditor.Document.Insert(offset, code);
            }
        }
Ejemplo n.º 5
0
        public void InsertAfterCurrentMethod()
        {
            IOutputAstVisitor outputVisitor = this.GetOutputVisitor();

            using (SpecialNodesInserter.Install(this.specialsList, outputVisitor)) {
                string code = "\r\n\r\n" + GenerateCode(this.extractedMethod, true);

                code = code.TrimEnd('\r', '\n', ' ', '\t');

                Dom.IMember p = GetParentMember(this.textEditor, this.currentSelection.StartPosition.Line + 1, this.currentSelection.StartPosition.Column + 1);

                TextLocation loc = new ICSharpCode.TextEditor.TextLocation(
                    p.BodyRegion.EndColumn - 1, p.BodyRegion.EndLine - 1);

                int offset = textEditor.Document.PositionToOffset(loc);

                textEditor.Document.Insert(offset, code);
            }
        }
Ejemplo n.º 6
0
 int GetMemberImageIndex(Dom.IMember member)
 {
     // Missing: different icons for private/public member
     if (member is Dom.IMethod)
     {
         return(1);
     }
     if (member is Dom.IProperty)
     {
         return(2);
     }
     if (member is Dom.IField)
     {
         return(3);
     }
     if (member is Dom.IEvent)
     {
         return(6);
     }
     return(3);
 }
        public override bool Extract()
        {
            using (var parser = ParserFactory.CreateParser(SupportedLanguage.CSharp, new StringReader("class Tmp { void Test() {\n " + this.textEditor.SelectedText + "\n}}"))) {
                parser.Parse();

                if (parser.Errors.Count > 0)
                {
                    MessageService.ShowError("${res:AddIns.SharpRefactoring.ExtractMethod.ParseErrors}");

                    return(false);
                }

                this.specialsList = parser.Lexer.SpecialTracker.RetrieveSpecials();
            }

            this.currentProjectContent = ParserService.GetProjectContent(ProjectService.CurrentProject);

            MethodDeclaration          newMethod            = new MethodDeclaration();
            List <VariableDeclaration> possibleReturnValues = new List <VariableDeclaration>();
            List <VariableDeclaration> otherReturnValues    = new List <VariableDeclaration>();

            // Initialise new method
            newMethod.Body = GetBlock(this.textEditor.SelectedText);
            newMethod.Body.StartLocation = new Location(0, 0);

            this.parentNode = GetParentMember(start, end);

            Dom.IMember member = GetParentMember(textEditor, textEditor.Caret.Line, textEditor.Caret.Column);

            if (parentNode == null || member == null)
            {
                MessageService.ShowError("${res:AddIns.SharpRefactoring.ExtractMethod.InvalidSelection}");
                return(false);
            }

            this.currentClass = member.DeclaringType;

            ErrorKind kind = CheckForJumpInstructions(newMethod);

            if (kind != ErrorKind.None)
            {
                switch (kind)
                {
                case ErrorKind.ContainsBreak:
                    MessageService.ShowError("${res:AddIns.SharpRefactoring.ExtractMethod.ContainsBreakError}");
                    break;

                case ErrorKind.ContainsContinue:
                    MessageService.ShowError("${res:AddIns.SharpRefactoring.ExtractMethod.ContainsContinueError}");
                    break;

                case ErrorKind.ContainsGoto:
                    MessageService.ShowError("${res:AddIns.SharpRefactoring.ExtractMethod.ContainsGotoError}");
                    break;
                }
                return(false);
            }

            newMethod.Modifier = parentNode.Modifier;

            newMethod.Modifier &= ~(Modifiers.Internal | Modifiers.Protected | Modifiers.Private | Modifiers.Public | Modifiers.Override);

            LookupTableVisitor ltv = new LookupTableVisitor(SupportedLanguage.CSharp);

            parentNode.AcceptVisitor(ltv, null);

            var variablesList = (from list in ltv.Variables.Values from item in list select new Variable(item))
                                .Where(v => !(v.StartPos > end || v.EndPos < start) &&
                                       (HasReferencesInSelection(newMethod, v) ||
                                        HasOccurrencesAfter(CSharpNameComparer, this.parentNode, end, v.Name, v.StartPos, v.EndPos)))
                                .Union(FromParameters(newMethod))
                                .Select(va => ResolveVariable(va));

            foreach (var variable in variablesList)
            {
                LoggingService.Debug(variable);

                bool hasOccurrencesAfter = HasOccurrencesAfter(CSharpNameComparer, this.parentNode, end, variable.Name, variable.StartPos, variable.EndPos);
                bool isInitialized       = (variable.Initializer != null) ? !variable.Initializer.IsNull : false;
                bool hasAssignment       = HasAssignment(newMethod, variable);

                if (IsInCurrentSelection(variable.StartPos) && hasOccurrencesAfter)
                {
                    possibleReturnValues.Add(new VariableDeclaration(variable.Name, variable.Initializer, variable.Type));
                    otherReturnValues.Add(new VariableDeclaration(variable.Name, variable.Initializer, variable.Type));
                }

                if (!(IsInCurrentSelection(variable.StartPos) || IsInCurrentSelection(variable.EndPos)))
                {
                    ParameterDeclarationExpression newParam = null;

                    if ((hasOccurrencesAfter && isInitialized) || variable.WasRefParam)
                    {
                        newParam = new ParameterDeclarationExpression(variable.Type, variable.Name, ParameterModifiers.Ref);
                    }
                    else
                    {
                        if ((hasOccurrencesAfter && hasAssignment) || variable.WasOutParam)
                        {
                            newParam = new ParameterDeclarationExpression(variable.Type, variable.Name, ParameterModifiers.Out);
                        }
                        else
                        {
                            if (!hasOccurrencesAfter)
                            {
                                newParam = new ParameterDeclarationExpression(variable.Type, variable.Name, ParameterModifiers.None);
                            }
                            else
                            {
                                if (!hasOccurrencesAfter && !isInitialized)
                                {
                                    newMethod.Body.Children.Insert(0, new LocalVariableDeclaration(new VariableDeclaration(variable.Name, variable.Initializer, variable.Type)));
                                }
                                else
                                {
                                    newParam = new ParameterDeclarationExpression(variable.Type, variable.Name, ParameterModifiers.In);
                                }
                            }
                        }
                    }
                    if (newParam != null)
                    {
                        newMethod.Parameters.Add(newParam);
                    }
                }
            }

            List <VariableDeclaration> paramsAsVarDecls = new List <VariableDeclaration>();

            this.beforeCallDeclarations = new List <LocalVariableDeclaration>();

            for (int i = 0; i < otherReturnValues.Count - 1; i++)
            {
                VariableDeclaration varDecl = otherReturnValues[i];
                paramsAsVarDecls.Add(varDecl);
                ParameterDeclarationExpression p = new ParameterDeclarationExpression(varDecl.TypeReference, varDecl.Name);
                p.ParamModifier = ParameterModifiers.Out;
                if (!newMethod.Parameters.Contains(p))
                {
                    newMethod.Parameters.Add(p);
                }
                else
                {
                    this.beforeCallDeclarations.Add(new LocalVariableDeclaration(varDecl));
                }
            }

            CreateReturnStatement(newMethod, possibleReturnValues);

            newMethod.Name = "NewMethod";

            this.extractedMethod = newMethod;

            return(true);
        }