/// <summary>
        /// Rename function in scope of parentElement.
        /// </summary>
        /// <param name="element">Element to rename.</param>
        /// <param name="parentElement">Containing element.</param>
        /// <param name="elementType">Type of element.</param>
        /// <param name="oldName">Old name of element.</param>
        /// <param name="newName">New name of element.</param>
        public override IRenameResult RenameSymbols(CodeElement element, LuaCodeClass parentElement,
                                                    vsCMElement elementType, string oldName, string newName)
        {
            renameResult        = new RenameResult(oldName, newName);
            changedCodeElements = new List <CodeElement>();

            //Function without parent element could not be renamed
            if (element is LuaCodeFunction && parentElement == null)
            {
                var ex = new InvalidCodeElementException(Resources.InvalidFunctionParentMessage, parentElement);
                Trace.WriteLine(ex);
                throw ex;
            }
            //Rename function, function calls or null element by its name
            if (element is LuaCodeFunction || element is LuaCodeElement <FunctionCall> || element == null)
            {
                renameResult = Rename(element, parentElement, oldName, newName);
            }
            else
            {
                throw new InvalidCodeElementException(
                          Resources.InvalidFunctionElementMessage, parentElement);
            }

            //Set RenameReferences flag to indicates that rename is local or not
            renameResult.RenameReferences = !IsLocalDeclaration;
            renameResult.ChangedElements  = changedCodeElements;

            renameResult.Success = true;

            return(renameResult);
        }
        /// <summary>
        /// Rename elements in all referenced lua files.
        /// </summary>
        /// <param name="elementType">Type of element.</param>
        /// <param name="oldName">Old name of element.</param>
        /// <param name="newName">New name of element.</param>
        /// <param name="result">Rename result.</param>
        private IRenameResult RenameAllReferences(vsCMElement elementType, string oldName, string newName,
                                                  IRenameResult result)
        {
            Trace.WriteLine("RenameAllReferences started...");
            try
            {
                var                multiResult           = new MultiRenameResult(oldName, newName);
                Project            currentProject        = DTE.ActiveDocument.ProjectItem.ContainingProject;
                ProjectItem        activeProjectItem     = DTE.ActiveDocument.ProjectItem;
                string             activeProjectFileName = activeProjectItem.get_FileNames(1);
                List <ProjectItem> projectItems          = GetLuaProjectItems(currentProject);

                foreach (ProjectItem projectItem in projectItems)
                {
                    string fileName = projectItem.get_FileNames(1);
                    if (!string.IsNullOrEmpty(fileName))
                    {
                        //If projectItem is the active then merge changes into MultiRenameResult
                        if (activeProjectFileName == fileName)
                        {
                            multiResult.MergeChanges(projectItem, result);
                        }
                        else
                        {
                            if (IsLuaFile(fileName))
                            {
                                LuaFileCodeModel fileCodeModel = GetFileCodeModel(projectItem);
                                if (fileCodeModel != null)
                                {
                                    CodeElement   root = GetRootElement(fileCodeModel);
                                    IRenameResult renameResult;
                                    //Rename references in Lua file.
                                    if (root != null)
                                    {
                                        renameResult = refactoringService.Rename(null, root, elementType, oldName, newName);
                                        renameResult.Parents.Add(fileCodeModel);
                                    }
                                    else
                                    {
                                        string message = String.Format(Resources.RootElementCannotBeFoundMessage, fileName);
                                        renameResult = new RenameResult(false, message);
                                        Trace.WriteLine(message);
                                    }
                                    multiResult.MergeChanges(projectItem, renameResult);
                                }
                            }
                        }
                    }
                }
                return(multiResult);
            }
            catch (Exception e)
            {
                Trace.WriteLine(e);
                throw;
            }
        }
Exemple #3
0
 /// <summary>
 /// Merges changes from other RenameResult.
 /// </summary>
 /// <param name="success">Rename operation status.</param>
 /// <param name="changedElements">Changed CodeElements in rename operations.</param>
 /// <param name="message">Information about rename operation.</param>
 /// <param name="projectItem">ProjectItem associated with CodeElements</param>
 public void MergeChanges(bool success, string message, ProjectItem projectItem,
                          IEnumerable <CodeElement> changedElements)
 {
     Success = success;
     if (!string.IsNullOrEmpty(message))
     {
         messageBuilder.Append(message);
     }
     if (projectItem != null && changedElements != null)
     {
         if (changedProjectElements.ContainsKey(projectItem))
         {
             if (changedProjectElements[projectItem] == null)
             {
                 changedProjectElements[projectItem] = new RenameResult(success, message, changedElements,
                                                                        OldName, NewName)
                 {
                     Parents = Parents
                 };
             }
             else
             {
                 var elements = new List <CodeElement>(changedProjectElements[projectItem].ChangedElements);
                 elements.AddRange(changedElements);
                 changedProjectElements[projectItem] = new RenameResult(success, message, elements, OldName,
                                                                        NewName)
                 {
                     Parents = Parents
                 };
             }
         }
         else
         {
             changedProjectElements.Add(projectItem,
                                        new RenameResult(success, message, changedElements, OldName, NewName)
             {
                 Parents = Parents
             });
         }
     }
 }
Exemple #4
0
        /// <summary>
        /// Rename element in scope of parentElement.
        /// </summary>
        /// <param name="element">Element to rename.</param>
        /// <param name="parentElement">Containing element.</param>
        /// <param name="elementType">Type of element.</param>
        /// <param name="oldName">Old name of element.</param>
        /// <param name="newName">New name of element.</param>
        public override IRenameResult RenameSymbols(CodeElement element, LuaCodeClass parentElement,
                                                    vsCMElement elementType, string oldName, string newName)
        {
            CodeElement declaration;

            renameResult        = new RenameResult(oldName, newName);
            changedCodeElements = new List <CodeElement>();
            isFunctionParameter = false;

            if (element == null) //Declaration is in other lua file or not recognized by caller.
            {
                //Get declaration of the Variable.
                declaration = GetDeclaration(oldName, parentElement);
                if (declaration != null && !IsLocalDeclaration)
                {
                    RenameVariableDeclaration(declaration, oldName, newName);
                }
                //If declaration is global then rename elements in all referenced files
                if (!IsLocalDeclaration)
                {
                    //Rename all references in scope of class
                    renameResult.Success = RenameMemberVariableReferences(parentElement, elementType, oldName, newName);
                }
                renameResult.Success = true;
            }
            else
            {
                //Get declaration of the Variable.
                declaration = GetDeclaration(element, parentElement);

                //Get parent of the Variable declaration.
                if (declaration != null)
                {
                    variableParent = ((ICodeDomElement)declaration).ParentElement;
                    if (!(variableParent is LuaCodeFunction) || (variableParent is LuaCodeClass))
                    {
                        variableParent = LuaCodeDomNavigator.GetParentElement((ICodeDomElement)declaration);
                    }
                }
                else
                {
                    variableParent = ((ICodeDomElement)element).ParentElement;
                }

                //Rename CodeElements and all references.
                if (variableParent is LuaCodeClass) //CodeElement is global declared variable.
                {
                    //Rename member variable
                    if (RenameVariableDeclaration(declaration, oldName, newName))
                    {
                        //Rename all references in scope of current class.
                        renameResult.Success = RenameMemberVariableReferences(parentElement, oldName, newName);
                    }
                }
                else if (variableParent is LuaCodeFunction)//CodeElement is local declared variable.
                {
                    //Rename local variable.
                    if (RenameVariableDeclaration(declaration, oldName, newName))
                    {
                        if (IsLocalDeclaration)
                        {
                            //Rename all references in scope of Function
                            renameResult.Success = RenameMemberVariableReferencesInScope(oldName, newName);
                        }
                        else
                        {
                            //Rename all references in scope of Class.
                            renameResult.Success = RenameMemberVariableReferences(parentElement, oldName, newName);
                        }
                    }
                }
                else if (variableParent == null)
                {
                    throw new InvalidCodeElementException(
                              Resources.InvalidElementParentMessage, parentElement);
                }
                else
                {
                    Trace.WriteLine("Trace:Unrecognized variable...");
                    RenameSymbols(null, parentElement, elementType, oldName, newName);
                }
            }
            renameResult.ChangedElements  = changedCodeElements;
            renameResult.RenameReferences = !IsLocalDeclaration;

            renameResult.Success = true;

            return(renameResult);
        }