Example #1
0
        IMember InnerRenameMember(IProgressMonitor monitor, IType cls, IMember member, string newName, RefactoryScope scope)
        {
            try {
                MemberReferenceCollection refs = new MemberReferenceCollection();
                Refactor(monitor, cls, scope, new RefactorDelegate(new RefactorFindMemberReferences(cls, member, refs, false).Refactor));
                refs.RenameAll(newName);

                RefactorerContext gctx = GetGeneratorContext(cls);

                IRefactorer gen = GetGeneratorForClass(cls);
                IMember     m   = gen.RenameMember(gctx, cls, member, newName);
                gctx.Save();
                return(m);
            } catch (Exception e) {
                LoggingService.LogError(GettextCatalog.GetString("Error while renaming {0} to {1}: {2}", member, newName, e.ToString()));
                return(null);
            }
        }
        public bool RenameVariable(IProgressMonitor monitor, LocalVariable var, string newName)
        {
            try {
                MemberReferenceCollection refs = new MemberReferenceCollection();
                Refactor(monitor, var, new RefactorDelegate(new RefactorFindVariableReferences(var, refs).Refactor));
                refs.RenameAll(newName);

                RefactorerContext gctx = GetGeneratorContext(var);
                IRefactorer       r    = GetGeneratorForVariable(var);
                bool rv = r.RenameVariable(gctx, var, newName);
                gctx.Save();

                return(rv);
            } catch (Exception e) {
                LoggingService.LogError(GettextCatalog.GetString("Error while renaming {0} to {1}: {2}", var, newName, e.ToString()));
                return(false);
            }
        }
        public bool RenameParameter(IProgressMonitor monitor, IParameter param, string newName)
        {
            try {
                MemberReferenceCollection refs = new MemberReferenceCollection();
                Refactor(monitor, param, new RefactorDelegate(new RefactorFindParameterReferences(param, refs, false).Refactor));
                refs.RenameAll(newName);

                IMember           member = param.DeclaringMember;
                RefactorerContext gctx   = GetGeneratorContext(member.DeclaringType);
                IRefactorer       r      = GetGeneratorForClass(member.DeclaringType);
                bool rv = r.RenameParameter(gctx, param, newName);
                gctx.Save();

                return(rv);
            } catch (Exception e) {
                LoggingService.LogError(GettextCatalog.GetString("Error while renaming {0} to {1}: {2}", param, newName, e.ToString()));
                return(false);
            }
        }
Example #4
0
        void RefactorProject(IProgressMonitor monitor, Project p, RefactorDelegate refactorDelegate)
        {
            RefactorerContext gctx = GetGeneratorContext(p);

            monitor.Log.WriteLine(GettextCatalog.GetString("Refactoring project {0}", p.Name));
            foreach (ProjectFile file in p.Files)
            {
                if (file.BuildAction != BuildAction.Compile || !System.IO.File.Exists(file.FilePath))
                {
                    continue;
                }
                IRefactorer gen = LanguageBindingService.GetRefactorerForFile(file.Name);
                if (gen == null)
                {
                    continue;
                }
                refactorDelegate(monitor, gctx, gen, file.Name);
                gctx.Save();
            }
        }
Example #5
0
        void Refactor(IProgressMonitor monitor, IParameter param, RefactorDelegate refactorDelegate)
        {
            IMember           member = param.DeclaringMember;
            RefactorerContext gctx   = GetGeneratorContext(member.DeclaringType);
            IType             cls    = member.DeclaringType;
            IRefactorer       gen;
            string            file;

            foreach (IType part in cls.Parts)
            {
                file = part.CompilationUnit.FileName;

                if ((gen = LanguageBindingService.GetRefactorerForFile(file)) == null)
                {
                    continue;
                }

                refactorDelegate(monitor, gctx, gen, file);
                gctx.Save();
            }
        }
Example #6
0
		public override void PerformChange (IProgressMonitor monitor, RefactorerContext rctx)
		{
			if (rctx == null)
				throw new InvalidOperationException ("Refactory context not available.");
			
			TextEditorData textEditorData = this.TextEditorData;
			if (textEditorData == null) {
				IEditableTextFile file = rctx.GetFile (FileName);
				if (file != null) {
					if (RemovedChars > 0)
						file.DeleteText (Offset, RemovedChars);
					if (!string.IsNullOrEmpty (InsertedText))
						file.InsertText (Offset, InsertedText);
					rctx.Save ();
				}
			} else if (textEditorData != null) {
				int charsInserted = textEditorData.Replace (Offset, RemovedChars, InsertedText);
				if (MoveCaretToReplace)
					textEditorData.Caret.Offset = Offset + charsInserted;
			}
		}
Example #7
0
		public IType CreateClass (Project project, string language, string directory, string namspace, CodeTypeDeclaration type)
		{
			ProjectDom ctx = ProjectDomService.GetProjectDom (project);
			RefactorerContext gctx = new RefactorerContext (ctx, fileProvider, null);
			IRefactorer gen = LanguageBindingService.GetRefactorerForLanguage (language);
			IType c = gen.CreateClass (gctx, directory, namspace, type);
			gctx.Save ();
			return c;
		}
Example #8
0
        void Refactor(IProgressMonitor monitor, IType cls, RefactoryScope scope, RefactorDelegate refactorDelegate)
        {
            switch (scope)
            {
            case RefactoryScope.DeclaringType:
                ProjectDom ctx = GetParserContext(cls);
                if (cls is InstantiatedType)
                {
                    cls = ((InstantiatedType)cls).UninstantiatedType;
                }
                IType resolvedType = ctx.GetType(cls.FullName, cls.TypeParameters.Count, true, true);
                if (resolvedType == null)
                {
                    goto case RefactoryScope.Solution;
                }
                foreach (IType part in resolvedType.Parts)
                {
                    string            file = part.CompilationUnit.FileName;
                    RefactorerContext gctx = GetGeneratorContext(part);
                    IRefactorer       gen  = LanguageBindingService.GetRefactorerForFile(file);
                    if (gen == null)
                    {
                        return;
                    }
                    refactorDelegate(monitor, gctx, gen, file);
                    gctx.Save();
                }
                break;

            case RefactoryScope.File: {
                string            file = cls.CompilationUnit.FileName;
                RefactorerContext gctx = GetGeneratorContext(cls);
                IRefactorer       gen  = LanguageBindingService.GetRefactorerForFile(file);
                if (gen == null)
                {
                    return;
                }
                refactorDelegate(monitor, gctx, gen, file);
                gctx.Save();
                break;
            }

            case RefactoryScope.Project:
                Project prj = GetProjectForFile(cls.CompilationUnit.FileName);
                if (prj == null)
                {
                    return;
                }
                RefactorProject(monitor, prj, refactorDelegate);
                break;

            case RefactoryScope.Solution:
                if (solution == null)
                {
                    goto case RefactoryScope.File;
                }
                foreach (Project project in solution.GetAllProjects())
                {
                    RefactorProject(monitor, project, refactorDelegate);
                }
                break;
            }
        }
        public IType ImplementInterface(ICompilationUnit pinfo, IType klass, IType iface, bool explicitly, IType declaringClass, IReturnType hintReturnType)
        {
            if (klass == null)
            {
                throw new ArgumentNullException("klass");
            }
            if (iface == null)
            {
                throw new ArgumentNullException("iface");
            }
            RefactorerContext gctx = GetGeneratorContext(klass);

            klass = GetUpdatedClass(gctx, klass);

            bool        alreadyImplemented;
            IReturnType prefix = null;

            List <KeyValuePair <IMember, IReturnType> > toImplement = new List <KeyValuePair <IMember, IReturnType> > ();

            prefix = new DomReturnType(iface);

            // Stub out non-implemented events defined by @iface
            foreach (IEvent ev in iface.Events)
            {
                if (ev.IsSpecialName)
                {
                    continue;
                }
                bool needsExplicitly = explicitly;

                alreadyImplemented = gctx.ParserContext.GetInheritanceTree(klass).Any(x => x.ClassType != ClassType.Interface && x.Events.Any(y => y.Name == ev.Name));

                if (!alreadyImplemented)
                {
                    toImplement.Add(new KeyValuePair <IMember, IReturnType> (ev, needsExplicitly ? prefix : null));
                }
            }

            // Stub out non-implemented methods defined by @iface
            foreach (IMethod method in iface.Methods)
            {
                if (method.IsSpecialName)
                {
                    continue;
                }
                bool needsExplicitly = explicitly;
                alreadyImplemented = false;
                foreach (IType t in gctx.ParserContext.GetInheritanceTree(klass))
                {
                    if (t.ClassType == ClassType.Interface)
                    {
                        continue;
                    }
                    foreach (IMethod cmet in t.Methods)
                    {
                        if (cmet.Name == method.Name && Equals(cmet.Parameters, method.Parameters))
                        {
                            if (!needsExplicitly && !cmet.ReturnType.Equals(method.ReturnType))
                            {
                                needsExplicitly = true;
                            }
                            else
                            {
                                alreadyImplemented |= !needsExplicitly || (iface.FullName == GetExplicitPrefix(cmet.ExplicitInterfaces));
                            }
                        }
                    }
                }

                if (!alreadyImplemented)
                {
                    toImplement.Add(new KeyValuePair <IMember, IReturnType> (method, needsExplicitly ? prefix : null));
                }
            }

            // Stub out non-implemented properties defined by @iface
            foreach (IProperty prop in iface.Properties)
            {
                if (prop.IsSpecialName)
                {
                    continue;
                }
                bool needsExplicitly = explicitly;
                alreadyImplemented = false;
                foreach (IType t in gctx.ParserContext.GetInheritanceTree(klass))
                {
                    if (t.ClassType == ClassType.Interface)
                    {
                        continue;
                    }
                    foreach (IProperty cprop in t.Properties)
                    {
                        if (cprop.Name == prop.Name)
                        {
                            if (!needsExplicitly && !cprop.ReturnType.Equals(prop.ReturnType))
                            {
                                needsExplicitly = true;
                            }
                            else
                            {
                                alreadyImplemented |= !needsExplicitly || (iface.FullName == GetExplicitPrefix(cprop.ExplicitInterfaces));
                            }
                        }
                    }
                }
                if (!alreadyImplemented)
                {
                    toImplement.Add(new KeyValuePair <IMember, IReturnType> (prop, needsExplicitly ? prefix : null));
                }
            }

            Ambience ambience = AmbienceService.GetAmbienceForFile(klass.CompilationUnit.FileName);

            //implement members
            ImplementMembers(klass, toImplement, ambience.GetString(iface, OutputFlags.ClassBrowserEntries | OutputFlags.IncludeGenerics | OutputFlags.IncludeParameters) + " implementation");
            gctx.Save();

            klass = GetUpdatedClass(gctx, klass);
            foreach (IType baseClass in iface.SourceProjectDom.GetInheritanceTree(iface))
            {
                if (baseClass.Equals(iface) || baseClass.FullName == "System.Object")
                {
                    continue;
                }
                klass = ImplementInterface(pinfo, klass, baseClass, explicitly, declaringClass, hintReturnType);
            }


            return(klass);
        }