/// <summary> /// Renames all references of member variable in FileCodeModel. /// </summary> /// <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> /// <returns></returns> private bool RenameMemberVariableReferences(CodeElement parentElement, vsCMElement elementType, string oldName, string newName) { var navigator = new LuaCodeDomNavigator(parentElement); codeElements = new List <SimpleCodeElement>( navigator.WalkMembers <LuaCodeElement <Identifier>, LuaCodeVariable>()); codeElements.ForEach(identifier => { if (identifier.Name == oldName) { CodeElement parent = LuaCodeDomNavigator.GetParentElement(identifier as ICodeDomElement); if (parent != null && (parent is LuaCodeFunction || parent is LuaCodeClass)) { if (!parent.Children.OfType <LuaCodeVariable>().Any( variable => variable.Name == oldName)) { identifier.RenameSymbol(newName); changedCodeElements.Add(identifier); } } } }); return(true); }
/// <summary> /// Get global/local declaration of element, if exists. /// </summary> /// <param name="element">CodeElement.</param> /// <param name="parentElement">Parent of CodeElement.</param> /// <returns>Return null if global declaration not found else the specified Variable.</returns> private CodeElement GetDeclaration(CodeElement element, CodeElement parentElement) { var codeDomElement = element as ICodeDomElement; if (codeDomElement != null && codeDomElement.ParentElement is LuaCodeClass) { return(element); } var parent = LuaCodeDomNavigator.GetParentElement(element as ICodeDomElement); if (parent is LuaCodeFunction)//Check for local declaration { var navigator = new LuaCodeDomNavigator(parent); codeElements = new List <SimpleCodeElement>( navigator.WalkMembers <LuaCodeVariable>()); CodeElement declaration; if (codeElements.Count > 0) { declaration = codeElements.FirstOrDefault(child => child.Name == element.Name); if (declaration != null) { CheckLocalDeclaration(declaration as LuaCodeVariable); return(declaration); } } //Check for parameter declaration declaration = ((LuaCodeFunction)parent).Parameters.OfType <LuaCodeElement <Identifier> >() .FirstOrDefault(parameter => parameter.Name == element.Name); if (declaration != null) { IsLocalDeclaration = true; isFunctionParameter = true; return(declaration); } parent = LuaCodeDomNavigator.GetParentElement(parent as ICodeDomElement); } if (parent is LuaCodeClass)//Check for global declaration { var navigator = new LuaCodeDomNavigator(parentElement); codeElements = new List <SimpleCodeElement>(navigator.WalkTopLevelMembers <LuaCodeVariable>()); if (codeElements.Count > 0) { var declaration = codeElements.FirstOrDefault(child => child.Name == element.Name); CheckLocalDeclaration(declaration as LuaCodeVariable); return(declaration); } } return(null); }
/// <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); }