Esempio n. 1
0
        public virtual void Complete(CompletionContext context)
        {
            MarkAsUsed();

            string insertedText  = this.Text;
            IClass selectedClass = GetClassOrExtensionMethodClass(this.Entity);

            if (selectedClass != null)
            {
                // Class or Extension method is being inserted
                var editor   = context.Editor;
                var document = context.Editor.Document;
                //  Resolve should return AmbiguousResolveResult or something like that when we resolve a name that exists in more imported namespaces
                //   - so that we would know that we always want to insert fully qualified name
                var nameResult = ResolveAtCurrentOffset(selectedClass.Name, context);

                bool addUsing = false;

                if (this.Entity is IClass)
                {
                    if (!IsUserTypingFullyQualifiedName(context))
                    {
                        nameResult = ResolveAtCurrentOffset(insertedText, context);
                        addUsing   = (!IsKnownName(nameResult));
                    }
                    // Special case for Attributes
                    if (insertedText.EndsWith("Attribute") && IsInAttributeContext(editor, context.StartOffset))
                    {
                        insertedText = insertedText.RemoveFromEnd("Attribute");
                    }
                }
                else if (this.Entity is IMethod)
                {
                    addUsing = !IsKnownName(nameResult);
                }

                context.Editor.Document.Replace(context.StartOffset, context.Length, insertedText);
                context.EndOffset = context.StartOffset + insertedText.Length;

                if (addUsing && nameResult != null && nameResult.CallingClass != null)
                {
                    var cu = nameResult.CallingClass.CompilationUnit;
                    NamespaceRefactoringService.AddUsingDeclaration(cu, document, selectedClass.Namespace, false);
                    ParserService.BeginParse(editor.FileName, document);
                }
            }
            else
            {
                // Something else than a class or Extension method is being inserted - just insert text
                context.Editor.Document.Replace(context.StartOffset, context.Length, insertedText);
                context.EndOffset = context.StartOffset + insertedText.Length;
            }
        }
Esempio n. 2
0
        public MenuItem Create(RefactoringMenuContext context)
        {
            if (context.ExpressionResult.Context == ExpressionContext.Attribute)
            {
                return(null);
            }
            if (!(context.ResolveResult is UnknownMethodResolveResult))
            {
                return(null);
            }
            if (context.ProjectContent == null)
            {
                return(null);
            }

            UnknownMethodResolveResult rr = context.ResolveResult as UnknownMethodResolveResult;

            MenuItem item = new MenuItem()
            {
                Header = string.Format(StringParser.Parse("${res:AddIns.SharpRefactoring.ResolveExtensionMethod}"), rr.CallName),
                Icon   = ClassBrowserIconService.GotoArrow.CreateImage()
            };

            List <IClass> results = new List <IClass>();

            SearchAllExtensionMethodsWithName(results, context.ProjectContent, rr.CallName);

            foreach (IProjectContent content in context.ProjectContent.ThreadSafeGetReferencedContents())
            {
                SearchAllExtensionMethodsWithName(results, content, rr.CallName);
            }

            if (!results.Any())
            {
                return(null);
            }

            foreach (IClass c in results)
            {
                string   newNamespace = c.Namespace;
                MenuItem subItem      = new MenuItem();
                subItem.Header = "using " + newNamespace;
                subItem.Icon   = ClassBrowserIconService.Namespace.CreateImage();
                item.Items.Add(subItem);
                subItem.Click += delegate {
                    NamespaceRefactoringService.AddUsingDeclaration(context.CompilationUnit, context.Editor.Document, newNamespace, true);
                    ParserService.BeginParse(context.Editor.FileName, context.Editor.Document);
                };
            }

            return(item);
        }
Esempio n. 3
0
 public void AddNamespace(ICompilationUnit compilationUnit, string newNamespace)
 {
     if (WorkbenchSingleton.InvokeRequired)
     {
         WorkbenchSingleton.SafeThreadCall(() => AddNamespace(compilationUnit, newNamespace));
     }
     else
     {
         IViewContent view       = FileService.OpenFile(compilationUnit.FileName);
         var          textEditor = view as ITextEditorProvider;
         IDocument    document   = textEditor.TextEditor.Document;
         NamespaceRefactoringService.AddUsingDeclaration(compilationUnit, document, newNamespace, false);
     }
 }
Esempio n. 4
0
        public MenuItem Create(RefactoringMenuContext context)
        {
            // TODO : [Test] above method is in Default context?
//			if (context.ExpressionResult.Context != ExpressionContext.Attribute)
//				return null;
            if (!(context.ResolveResult is UnknownIdentifierResolveResult || context.ResolveResult is UnknownMethodResolveResult))
            {
                return(null);
            }

            List <IClass> results = new List <IClass>();

            ParseInformation info = ParserService.GetParseInformation(context.Editor.FileName);

            if (info == null || info.CompilationUnit == null || info.CompilationUnit.ProjectContent == null)
            {
                return(null);
            }

            ICompilationUnit unit = info.CompilationUnit;
            IProjectContent  pc   = info.CompilationUnit.ProjectContent;

            string name = null;

            if (context.ResolveResult is UnknownMethodResolveResult)
            {
                var rr = context.ResolveResult as UnknownMethodResolveResult;
                SearchAttributesWithName(results, pc, rr.CallName);

                foreach (IProjectContent content in pc.ReferencedContents)
                {
                    SearchAttributesWithName(results, content, rr.CallName);
                }

                name = rr.CallName;
            }

            if (context.ResolveResult is UnknownIdentifierResolveResult)
            {
                var rr = context.ResolveResult as UnknownIdentifierResolveResult;
                SearchAttributesWithName(results, pc, rr.Identifier);

                foreach (IProjectContent content in pc.ReferencedContents)
                {
                    SearchAttributesWithName(results, content, rr.Identifier);
                }

                name = rr.Identifier;
            }

            if (!results.Any())
            {
                return(null);
            }

            MenuItem item = new MenuItem()
            {
                Header = string.Format(StringParser.Parse("${res:AddIns.SharpRefactoring.ResolveAttribute}"), name),
                Icon   = ClassBrowserIconService.GotoArrow.CreateImage()
            };

            foreach (IClass c in results)
            {
                string   newNamespace = c.Namespace;
                MenuItem subItem      = new MenuItem();
                subItem.Header = "using " + newNamespace;
                subItem.Icon   = ClassBrowserIconService.Namespace.CreateImage();
                item.Items.Add(subItem);
                subItem.Click += delegate {
                    NamespaceRefactoringService.AddUsingDeclaration(unit, context.Editor.Document, newNamespace, true);
                    ParserService.BeginParse(context.Editor.FileName, context.Editor.Document);
                };
            }

            return(item);
        }