private static CodeElement FindCodeElementByFullName( ProjectItem projectItem, string targetFullName, vsCMElement elementKind) { foreach (CodeElement element in new DteHelperEx.CodeElementsIterator(projectItem)) { if (element.Kind == vsCMElement.vsCMElementNamespace) { foreach (CodeElement type in ((CodeNamespace)element).Members) { CodeElement result = InspectCodeElement(type, targetFullName, elementKind); if (result != null) { return(result); } } } else { CodeElement result = InspectCodeElement(element, targetFullName, elementKind); if (result != null) { return(result); } } } return(null); }
public static CodeElement GetCodeElementFromSelection(Document document, vsCMElement elementType) { if (document == null) { throw new ArgumentNullException("document"); } ProjectItem projectItem = document.ProjectItem; if (projectItem != null) { FileCodeModel codeModel = projectItem.FileCodeModel; if (codeModel != null) { try { TextSelection textSelection = (TextSelection)document.Selection; return(codeModel.CodeElementFromPoint(textSelection.TopPoint, elementType)); } catch (Exception) { //Swallow this exception ... } } } return(null); }
private CodeElement GetMethodFromTextPoint(TextPoint textPoint) { // Discover every code element containing the insertion point. string elems = ""; const vsCMElement scopes = 0; foreach (vsCMElement scope in Enum.GetValues(scopes.GetType())) { CodeElement elem = textPoint.CodeElement[scope]; if (elem != null) { elems += elem.Name + " (" + scope + ")\n"; } } foreach (vsCMElement scope in Enum.GetValues(scopes.GetType())) { CodeElement elem = textPoint.CodeElement[vsCMElement.vsCMElementFunction]; if (elem != null) { return(elem); } } return(null); }
/// <summary> /// Gets the specified type list of selected elements or null if there is no elements of that type selected. /// </summary> public static IList <T> GetList <T>(CodeElements members, vsCMElement type, TextSelection selection) where T : class { int minLine = selection.TopLine; int maxLine = (selection.BottomPoint.LineCharOffset > 1 ? selection.BottomPoint.Line : selection.BottomPoint.Line - 1); return(GetList <T>(members, type, minLine, maxLine)); }
public static CodeElement ReduceResultSet(DTE2 dte, List <CodeElement> elements, string targetPath, string targetName) { List <CodeElement> codeElements = new List <CodeElement>(); List <string> activeNamespaces = new List <string>(); vsCMElement[] whiteList = new vsCMElement[] { vsCMElement.vsCMElementImportStmt, vsCMElement.vsCMElementUsingStmt, vsCMElement.vsCMElementIncludeStmt }; string target = targetPath.ToLower() + targetName.ToLower(); foreach (CodeElement ele in elements) { if (ele.FullName.ToLower() == targetPath.ToLower() || ele.FullName.ToLower() == target) { codeElements.Add(ele); } } if (codeElements != null && codeElements.Count > 0) { if (codeElements.Count == 1) { return(codeElements[0]); } activeNamespaces = ConvertToElementArray <CodeElement>(dte.ActiveDocument.ProjectItem.FileCodeModel.CodeElements) .Where(e => whiteList.Contains(e.Kind)).Select(e => ((CodeImport)e).Namespace).ToList(); return(HandleFunctionResultSet(codeElements.Where(e => activeNamespaces.Any(a => e.FullName.Contains(a))))); } return(null); }
/// <summary> /// Performs the style task. /// </summary> /// <param name="projectItem">The project Item</param> /// <param name="ideWindow">The IDE window.</param> protected override void DoWork(ProjectItem projectItem, EnvDTE.Window ideWindow) { if (projectItem.Name.EndsWith(".cs") && !IsTemporarilyDisabled) { Debug.WriteLine("Sorting Functions: " + projectItem.Name); FileCodeModel fileCodeModel = projectItem.FileCodeModel; if (CheckForPreprocessorDirectives(ideWindow)) { if (MessageBox.Show("There appear to be preprocessor directives in this file. Sorting elements within a class may break scope of these. Do you want to continue the action on this file?", "Warning: Preprocessor Directives Detected", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2) == DialogResult.No) { return; } } for (int i = 1; i <= fileCodeModel.CodeElements.Count; i++) { if (fileCodeModel.CodeElements.Item(i).Kind == vsCMElement.vsCMElementNamespace) { CodeElement element = fileCodeModel.CodeElements.Item(i); for (int j = 1; j <= element.Children.Count; j++) { vsCMElement kind = element.Children.Item(j).Kind; if (kind == vsCMElement.vsCMElementClass || kind == vsCMElement.vsCMElementInterface || kind == vsCMElement.vsCMElementStruct) { if (!EvaluateElementsWithinClassSorted(element.Children.Item(j))) { SortFunctionsWithinClass(element.Children.Item(j)); } } } } } } }
public static CodeElement GetCodeElementAtCursor(this DTE dte, vsCMElement elementType) { try { CodeElement objCodeElement = null; var objCursorTextPoint = GetCursorTextPoint(dte); if ((objCursorTextPoint != null)) { // Get the class at the cursor objCodeElement = GetCodeElementAtTextPoint(elementType, dte.ActiveDocument.ProjectItem.FileCodeModel.CodeElements, objCursorTextPoint); } //if (objCodeElement == null) { // MessageBox.Show("No matching elementType found at the cursor!"); //} //else { // MessageBox.Show("Class at the cursor: " + objCodeElement.FullName); //} return objCodeElement; } catch (Exception ex) { Debug.DebugHere(ex); } return null; }
/// <summary> /// 获得一个class元素下所有指定的元素 /// </summary> /// <param name="classElement"></param> /// <param name="kind"></param> /// <returns></returns> public List <CodeElement> GetSpecifiedKindInClass(CodeElement classElement, vsCMElement kind) { ////缓存操作 //string key = classElement.GetHashCode() + "_GetSpecifiedKindInClass_" + kind.ToString(); //if (!IsCurrenFileDirty() && dict.ContainsKey(key)) //{ // return dict[key]; //} List <CodeElement> resLists = new List <CodeElement>(); IEnumerator iterator = classElement.Children.GetEnumerator(); while (iterator.MoveNext()) { CodeElement e = iterator.Current as CodeElement; resLists.AddRange(GetSpecifiedKindUnderElement(e, kind)); } ////重复添加会报错 //if (dict.ContainsKey(key)) //{ // dict.Remove(key); //} //dict.Add(key, resLists); return(resLists); }
/// <summary> /// Gets the specified type list selected from given elements or null if there is no elements of that type. /// </summary> public static IList <T> GetList <T>(CodeElements members, vsCMElement type) where T : class { if (members == null) { return(null); } try { IList <T> result = new List <T>(); foreach (CodeElement e in members) { if (e.Kind == type) { result.Add(e as T); } } if (result.Count == 0) { return(null); } return(result); } catch { return(null); } }
private static string ExamineCodeElement(CodeElement codeElement, vsCMElement type) { return codeElement.Kind == type ? codeElement.Name : (from CodeElement childElement in codeElement.Children select ExamineCodeElement(childElement, type)) .FirstOrDefault(result => !string.IsNullOrEmpty(result)); }
public static List <CodeElement> GetAllCodeElementsOfType (CodeElements elements, vsCMElement elementType, bool includeExternalTypes) { var ret = new List <CodeElement>(); foreach (CodeElement elem in elements) { if (elem.Kind == EnvDTE.vsCMElement.vsCMElementNamespace) { ret.AddRange(GetAllCodeElementsOfType(((EnvDTE.CodeNamespace)elem).Members, elementType, includeExternalTypes)); } else if (elem.InfoLocation == EnvDTE.vsCMInfoLocation.vsCMInfoLocationExternal && !includeExternalTypes) { continue; } else if (elem.IsCodeType) { ret.AddRange(GetAllCodeElementsOfType(((EnvDTE.CodeType)elem).Members, elementType, includeExternalTypes)); } if (elem.Kind == elementType) { ret.Add(elem); } } return(ret); }
///------------------------------------------------------------------------------------------------------------- /// <summary> /// Recursively searches the CodeElements for the specified class /// </summary> ///------------------------------------------------------------------------------------------------------------- protected virtual CodeClass FindClass(CodeElements codeElements, string className) { if (codeElements != null && !string.IsNullOrEmpty(className)) { foreach (CodeElement codeElement in codeElements) { vsCMElement kind = codeElement.Kind; if (kind == vsCMElement.vsCMElementClass) { CodeClass codeClass = codeElement as CodeClass; if (codeClass != null && string.Compare(codeClass.FullName, className, StringComparison.Ordinal) == 0) { return codeClass; } } else if (kind == vsCMElement.vsCMElementNamespace) { EnvDTE.CodeNamespace codeNamespace = codeElement as EnvDTE.CodeNamespace; if (codeNamespace != null) { CodeClass codeClass = FindClass(codeNamespace.Children, className); if (codeClass != null) { return codeClass; } } } } } return null; }
private static string ExamineCodeElement(CodeElement codeElement, vsCMElement type) { return(codeElement.Kind == type ? codeElement.Name : (from CodeElement childElement in codeElement.Children select ExamineCodeElement(childElement, type)) .FirstOrDefault(result => !string.IsNullOrEmpty(result))); }
private CodeElement CodeElementFromPoint(vsCMElement elementType, CodeElements codeElements, TextPoint point) { CodeElement element = null; if (codeElements != null) { foreach (CodeElement ce in codeElements) { if (ce.StartPoint.LessThan(point) && ce.EndPoint.GreaterThan(point)) { if (ce.Kind == elementType) { element = ce; } // Check for inner ellements of a class or namespace. if (ce.Kind == vsCMElement.vsCMElementNamespace || ce.Kind == vsCMElement.vsCMElementClass) { CodeElement innerElement = CodeElementFromPoint(elementType, ce.Children, point); if (innerElement != null) { element = innerElement; } } break; } } } return(element); }
private static bool IsNamespaceable(vsCMElement kind) { return(kind == vsCMElement.vsCMElementClass || kind == vsCMElement.vsCMElementEnum || kind == vsCMElement.vsCMElementInterface || kind == vsCMElement.vsCMElementStruct); }
static public CodeElement GetCodeElementAtCursor(DTE dte, vsCMElement elementType) { try { CodeElement objCodeElement = null; var objCursorTextPoint = GetCursorTextPoint(dte); if ((objCursorTextPoint != null)) { // Get the class at the cursor objCodeElement = GetCodeElementAtTextPoint(elementType, dte.ActiveDocument.ProjectItem.FileCodeModel.CodeElements, objCursorTextPoint); } //if (objCodeElement == null) { // MessageBox.Show("No matching elementType found at the cursor!"); //} //else { // MessageBox.Show("Class at the cursor: " + objCodeElement.FullName); //} return(objCodeElement); } catch (Exception ex) { Debug.DebugHere(ex); } return(null); }
/// <summary> /// Gets the specified type list of selected from given elements that are between particular code lines or null /// if there is no elements of that type. /// </summary> public static IList <T> GetList <T>(CodeElements members, vsCMElement type, int minLine, int maxLine) where T : class { if (members == null) { return(null); } try { IList <T> result = new List <T>(); foreach (CodeElement e in members) { if (e.Kind == type && (Fits(e.StartPoint.Line, minLine, maxLine) || Fits(e.EndPoint.Line, minLine, maxLine))) { result.Add(e as T); } } if (result.Count == 0) { return(null); } return(result); } catch { return(null); } }
/// <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> /// 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); }
private CodeElement GetCodeElementAtTextPoint(vsCMElement eRequestedCodeElementKind, CodeElements colCodeElements, TextPoint objTextPoint) { // CodeElement objCodeElement = default(CodeElement); CodeElement objResultCodeElement = default(CodeElement); CodeElements colCodeElementMembers = default(CodeElements); CodeElement objMemberCodeElement = default(CodeElement); if ((colCodeElements != null)) { foreach (CodeElement objCodeElement in colCodeElements) { if (objCodeElement.StartPoint.GreaterThan(objTextPoint)) { // The code element starts beyond the point } else if (objCodeElement.EndPoint.LessThan(objTextPoint)) { // The code element ends before the point // The code element contains the point } else { if (objCodeElement.Kind == eRequestedCodeElementKind) { // Found objResultCodeElement = objCodeElement; } // We enter in recursion, just in case there is an inner code element that also // satisfies the conditions, for example, if we are searching a namespace or a class colCodeElementMembers = GetCodeElementMembers(objCodeElement); objMemberCodeElement = GetCodeElementAtTextPoint(eRequestedCodeElementKind, colCodeElementMembers, objTextPoint); if ((objMemberCodeElement != null)) { // A nested code element also satisfies the conditions objResultCodeElement = objMemberCodeElement; } break; // TODO: might not be correct. Was : Exit For } } } return objResultCodeElement; }
static protected CodeElement SmartGetActiveCodeElement(EditPoint selPoint) { var list = new vsCMElement[] { vsCMElement.vsCMElementFunction, vsCMElement.vsCMElementClass }; CodeElement element = null; foreach (vsCMElement elem in list) { element = GetActiveCodeElement(selPoint, elem); if (element != null) { break; } } // if (element == null) // { // //if( GetSelectedText( selPoint ).IndexOf(";") >= 0 ) // { // while (GetActiveCodeElement(selPoint, 0) == null) // { // selPoint.LineDown(1); // if (selPoint.AtEndOfDocument) // break; // } // element = GetActiveCodeElement(selPoint, 0); // } // } return(element); }
/// <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; }
public void Kind_PublicStruct_ReturnsStruct() { CreatePublicStruct("MyStruct"); vsCMElement kind = codeStruct.Kind; Assert.AreEqual(vsCMElement.vsCMElementStruct, kind); }
public void Kind_Parameter_ReturnsParameter() { CreateParameter(); vsCMElement kind = parameter.Kind; Assert.AreEqual(vsCMElement.vsCMElementParameter, kind); }
public void Kind_PublicDelegate_ReturnsClass() { CreatePublicDelegate("MyDelegate"); vsCMElement kind = codeDelegate.Kind; Assert.AreEqual(vsCMElement.vsCMElementDelegate, kind); }
public void Kind_SystemXmlNamespace_ReturnsImport() { CreateCodeImport("System.Xml"); vsCMElement kind = codeImport.Kind; Assert.AreEqual(vsCMElement.vsCMElementImportStmt, kind); }
public void Kind_Interface_ReturnsInterface() { CreateInterface(); vsCMElement kind = codeInterface.Kind; Assert.AreEqual(vsCMElement.vsCMElementInterface, kind); }
public void Kind_PublicVariable_ReturnsVariable() { CreatePublicVariable("MyVariable"); vsCMElement kind = codeVariable.Kind; Assert.AreEqual(vsCMElement.vsCMElementVariable, kind); }
public void Kind_PublicFunction_ReturnsFunction() { CreatePublicFunction("MyFunction"); vsCMElement kind = codeFunction.Kind; Assert.AreEqual(vsCMElement.vsCMElementFunction, kind); }
/// <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 IRenameResult RenameSymbols(CodeElement element, LuaCodeClass parentElement, vsCMElement elementType, string oldName, string newName) { if (CodeElementRename == null) { throw new InvalidStrategyException(CodeElementRename); } return CodeElementRename.RenameSymbols(element, parentElement, elementType, oldName, newName); }
public void Kind_PublicProperty_ReturnsProperty() { helper.CreatePublicProperty("MyProperty"); vsCMElement kind = property.Kind; Assert.AreEqual(vsCMElement.vsCMElementProperty, kind); }
public void Kind_EmptyStringNamespace_ReturnsNamespace() { CreateProjectContent(); CreateCodeNamespace(String.Empty); vsCMElement kind = codeNamespace.Kind; Assert.AreEqual(vsCMElement.vsCMElementNamespace, kind); }
public void Kind_AttributeIsDataAnnotationsDisplayColumnAttribute_ReturnsAttribute() { CreateMSBuildAttribute("System.ComponentModel.DataAnnotations.DisplayColumnAttribute"); CreateAttribute(); vsCMElement kind = codeAttribute.Kind; Assert.AreEqual(vsCMElement.vsCMElementAttribute, kind); }
public void Kind_PublicClass_ReturnsClass() { CreateProjectContent(); CreatePublicClass("MyClass"); vsCMElement kind = codeClass.Kind; Assert.AreEqual(vsCMElement.vsCMElementClass, kind); }
/// <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; } }
public static CodeElement GetCodeElement(DTE dte, vsCMElement[] searchScopes) { if (dte.ActiveDocument == null) return null; if (dte.ActiveDocument.ProjectItem == null) return null; if (dte.ActiveDocument.ProjectItem.FileCodeModel == null) return null; TextSelection selection = (TextSelection)dte.ActiveWindow.Selection; if (selection == null || selection.ActivePoint == null) return null; EditPoint selPoint = selection.ActivePoint.CreateEditPoint(); CodeLanguage currentLang = CodeLanguage.CSharp; selPoint.StartOfLine(); while (true) { string BlockText = selPoint.GetText(selPoint.LineLength).Trim(); // *** Skip over any XML Doc comments and Attributes if (currentLang == CodeLanguage.CSharp && BlockText.StartsWith("/// ") || currentLang == CodeLanguage.CSharp && BlockText.StartsWith("[") || currentLang == CodeLanguage.VB && BlockText.StartsWith("''' ") || currentLang == CodeLanguage.VB && BlockText.StartsWith("<")) { selPoint.LineDown(1); selPoint.StartOfLine(); } else break; } // *** Make sure the cursor is placed inside of the definition always // *** Especially required for single line methods/fields/events etc. selPoint.EndOfLine(); selPoint.CharLeft(1); // Force into the text string xBlockText = selPoint.GetText(selPoint.LineLength).Trim(); // get the element under the cursor CodeElement element = null; FileCodeModel2 CodeModel = dte.ActiveDocument.ProjectItem.FileCodeModel as FileCodeModel2; // *** Supported scopes - set up here in the right parsing order // *** from lowest level to highest level // *** NOTE: Must be adjusted to match any CodeElements supported foreach (vsCMElement scope in searchScopes) { try { element = CodeModel.CodeElementFromPoint(selPoint, scope); if (element != null) break; // if no exception - break } catch { ; } } if (element == null) return null; return element; }
private CodeElement GetCodeElement(DTE dte, SnapshotPoint point, CodeElements codeElements, vsCMElement elementType) { foreach (CodeElement ce in codeElements) { if (ce.Kind.ToString() == "vsCMElementImportStmt") { continue; } try { _log.Debug("Searching " + ce.Name); Debug.WriteLine(string.Format("Searching {0}:{1}", ce.Kind.ToString(), ce.Name)); } catch (Exception e) { Debug.WriteLine("Exception getting object name: " + e.Message ); } var sp = getStartPoint(ce); var pointLine = point.GetContainingLine().LineNumber; if (sp != null) { if (sp.Line > pointLine ) { // code element starts after the droplocation, ignore it } else if (ce.EndPoint.Line < pointLine ) { // code element finishes before the droplocation, ignore it } else { if (elementType == ce.Kind) { return ce; } var childElements = getCodeElements(ce); if (childElements != null) { var ret = GetCodeElement(dte, point, childElements, elementType); if (ret != null) return ret; } } } } return null; }
/// <summary> /// Creates IConflictResolver by elementType. /// </summary> /// <param name="elementType">Type of CodeElement.</param> /// <param name="fileCodeModel">LuaFileCodeModel instance.</param> /// <returns>IConflictResolver implementation.</returns> public static IConflictResolver CreateConflictResolver(vsCMElement elementType, LuaFileCodeModel fileCodeModel) { IConflictResolver result = null; switch (elementType) { case vsCMElement.vsCMElementFunctionInvokeStmt: case vsCMElement.vsCMElementFunction: { result = new FunctionConflictResolver(fileCodeModel); break; } } return result; }
/// <summary> /// Creates IRenameStrategy by elementType. /// </summary> /// <param name="elementType">Type of CodeElement.</param> /// <returns>IRenameStrategy implementation.</returns> public static IRenameStrategy Create(vsCMElement elementType) { IRenameStrategy result = null; switch (elementType) { case vsCMElement.vsCMElementFunctionInvokeStmt: case vsCMElement.vsCMElementFunction: { result = new FunctionRenameStrategy(); break; } case vsCMElement.vsCMElementVariable: case vsCMElement.vsCMElementMap: case vsCMElement.vsCMElementLocalDeclStmt: case vsCMElement.vsCMElementDefineStmt: case vsCMElement.vsCMElementDeclareDecl: { result = new VariableRenameStrategy(); break; } } return result; }
private static CodeElement GetCodeElementAtTextPoint(vsCMElement eRequestedCodeElementKind, CodeElements colCodeElements, EnvDTE.TextPoint objTextPoint) { CodeElement objResultCodeElement = null; if (colCodeElements == null) return null; foreach (var objCodeElement in colCodeElements.Cast<CodeElement>()) { if (objCodeElement.StartPoint.GreaterThan(objTextPoint)) { // The code element starts beyond the point } else if (objCodeElement.EndPoint.LessThan(objTextPoint)) { // The code element ends before the point // The code element contains the point } else { if (objCodeElement.Kind == eRequestedCodeElementKind) { // Found objResultCodeElement = objCodeElement; } // We enter in recursion, just in case there is an inner code element that also // satisfies the conditions, for example, if we are searching a namespace or a class var colCodeElementMembers = GetCodeElementMembers(objCodeElement); var objMemberCodeElement = GetCodeElementAtTextPoint(eRequestedCodeElementKind, colCodeElementMembers, objTextPoint); if ((objMemberCodeElement != null)) { // A nested code element also satisfies the conditions objResultCodeElement = objMemberCodeElement; } break; } } return objResultCodeElement; }
private CodeElement getCodeElementAtTextPoint(vsCMElement requestedElementKind, CodeElements codeElements, TextPoint cursor) { if (codeElements != null) { var c = cursor.CodeElement[vsCMElement.vsCMElementClass]; if (c != null) { Debug.WriteLine("Cursor is in " + c.FullName ); } foreach (CodeElement e in codeElements) { Debug.WriteLine("Searching..." + getEleentName(e) ); if (e.StartPoint.GreaterThan(cursor) || e.EndPoint.LessThan(cursor)) { // ignore } else { var children = getMembers(e); if (children != null) { var memberElement = getCodeElementAtTextPoint(requestedElementKind, children, cursor); if (memberElement != null) { return memberElement; } } if (e.Kind.Equals(requestedElementKind)) { return e; } } } } Debug.WriteLine("Element not found"); return null; }
private static CodeElement InspectBaseCodeElement( CodeElement element, string targetFullName, vsCMElement elementKind) { if (element.Kind == vsCMElement.vsCMElementClass) { CodeClass target = (CodeClass)element; foreach (CodeElement interfaceType in target.ImplementedInterfaces) { if (interfaceType.Kind == elementKind && interfaceType.FullName.Equals(targetFullName, StringComparison.InvariantCultureIgnoreCase)) { return interfaceType; } if (interfaceType.InfoLocation == vsCMInfoLocation.vsCMInfoLocationExternal) { // look in other projects ProjectItem item = DteHelperEx.FindContainingProjectItem(element.ProjectItem.DTE, (CodeType)interfaceType); if (item != null) { return FindCodeElementByFullName(item, targetFullName, elementKind); } return null; } // look in childs CodeElement child = InspectChildren(interfaceType.Children, targetFullName, elementKind); if(child != null) { return child; } } return null; } if (element.Kind == vsCMElement.vsCMElementInterface) { return InspectChildren(element.Children, targetFullName, elementKind); } return null; }
public static CodeElementNodeFactory CreateNodeFactoryFromCurrentSelection(DTE2 dte, vsCMElement codeElementType) { if (null == dte.ActiveDocument || null == dte.ActiveDocument.Selection) { return null; } TextSelection selection = dte.ActiveDocument.Selection as TextSelection; if (null == selection) { return null; } TextPoint textPoint = selection.ActivePoint; if (null == textPoint) { return null; } return CreateNodeFactoryFromTextPoint(textPoint, codeElementType); }
internal static CodeElementNodeFactory CreateNodeFactoryFromTextPoint(TextPoint textPoint, vsCMElement codeElementType) { var codeElement = textPoint.get_CodeElement(codeElementType); if (null == codeElement) { return null; } var factory = CreateNodeFactoryFromCodeElement(codeElement); return factory; }
/// <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; }
/// <summary> /// Finds the type of the code element from the specified CodeType. /// </summary> /// <param name="element">The element.</param> /// <param name="typeName">Name of the type.</param> /// <param name="elementKind">Kind of the element.</param> /// <returns></returns> public static CodeElement FindCodeElementFromType(CodeElement element, string typeName, vsCMElement elementKind) { // check if we got it in the specified CodeType if (element.Kind == elementKind && element.FullName.Equals(typeName, StringComparison.InvariantCultureIgnoreCase)) { return element; } // look into base types or interfaces return InspectBaseCodeElement(element, typeName, elementKind); }
/// <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; } }
private static CodeElement InspectCodeElement( CodeElement element, string targetFullName, vsCMElement elementKind) { if (element.IsCodeType) { if (element.Kind == elementKind && element.FullName.Equals(targetFullName, StringComparison.InvariantCultureIgnoreCase)) { return element; } // look into base types or interfaces CodeElement result = InspectBaseCodeElement(element, targetFullName, elementKind); if (result != null) { return result; } } return null; }
public CodeElement get_CodeElement(vsCMElement Scope) { throw new NotImplementedException(); }
public SelectedCodeElementVariable(DTE2 dte, string name, vsCMElement codeElementType ) : base(dte, name) { _codeElementType = codeElementType; }
protected static CodeElement GetActiveCodeElement(EditPoint selPoint, vsCMElement scope) { // get the element under the cursor CodeElement element = null; try { if (m_applicationObject.ActiveDocument.ProjectItem.FileCodeModel != null) { element = m_applicationObject.ActiveDocument.ProjectItem.FileCodeModel.CodeElementFromPoint(selPoint, scope); } } catch (System.Runtime.InteropServices.COMException) { element = null; } return element; }
protected static CodeElement SmartGetActiveCodeElement(EditPoint selPoint) { var list = new vsCMElement[] { vsCMElement.vsCMElementFunction, vsCMElement.vsCMElementClass }; CodeElement element = null; foreach (vsCMElement elem in list) { element = GetActiveCodeElement(selPoint, elem); if (element != null) break; } return element; }
/// <summary> /// Finds the type of the code element from the specified project. /// </summary> /// <param name="project">The project.</param> /// <param name="typeName">Name of the type.</param> /// <param name="elementKind">Kind of the element.</param> /// <returns> /// The <see cref="CodeElement"/> found or null if no matches. /// </returns> public static CodeElement FindCodeElementFromType(Project project, string typeName, vsCMElement elementKind) { if (project == null || string.IsNullOrEmpty(typeName)) { return null; } // try to find it in the selected project CodeElement result = FindCodeElementByFullName(project, typeName, elementKind); if (result == null) { // navigate through the project references and look into each project if (!DteHelper.IsWebProject(project)) { VSProject vsProject = project.Object as VSProject; foreach (Reference reference in vsProject.References) { result = FindCodeElementFromType(reference.SourceProject, typeName, elementKind); if (result != null && result.InfoLocation == vsCMInfoLocation.vsCMInfoLocationProject) { break; } } } else { VSWebSite webProject = project.Object as VSWebSite; if (webProject != null) { foreach (AssemblyReference reference in webProject.References) { Project sourceProject = GetSourceProject(reference); result = FindCodeElementFromType(sourceProject, typeName, elementKind); if (result != null && result.InfoLocation == vsCMInfoLocation.vsCMInfoLocationProject) { break; } } } } } return result; }
/// <summary> /// Given a point and an element type to search for returns the element of that type at that point /// or null if no element is found. /// </summary> /// <param name="point"></param> /// <param name="scope"></param> /// <returns></returns> public CodeElement CodeElementFromPoint(TextPoint point, vsCMElement scope) { codeElementFromPointObject = null; currentEditingPoint = point; WalkMembers(RootElement); //Get FunctionCall if result is Identifier if (codeElementFromPointObject is LuaCodeElement<Identifier> && ((ICodeDomElement) codeElementFromPointObject).ParentElement is LuaCodeElement<FunctionCall>) { var funcCall = ((ICodeDomElement) codeElementFromPointObject).ParentElement as LuaCodeElement<FunctionCall>; if (funcCall != null) if (funcCall.Children != null && codeElementFromPointObject == funcCall.Children.Item(1)) { codeElementFromPointObject = funcCall; } } return codeElementFromPointObject; }
private static CodeElement WalkStatements(TextPoint Point, vsCMElement Scope, CodeStatementCollection statements) { foreach (CodeStatement cs in statements) { if (Scope == vsCMElement.vsCMElementAssignmentStmt && cs is CodeAssignStatement) { if (IsInRange(cs, Point)) return (CodeElement)cs.UserData[CodeKey]; } if (Scope == vsCMElement.vsCMElementLocalDeclStmt && cs is CodeVariableDeclarationStatement) { if (IsInRange(cs, Point)) return (CodeElement)cs.UserData[CodeKey]; } CodeExpressionStatement ces = cs as CodeExpressionStatement; if (ces != null) { if (Scope == vsCMElement.vsCMElementFunctionInvokeStmt && ces.Expression is CodeMethodInvokeExpression) { if (IsInRange(cs, Point)) return (CodeElement)cs.UserData[CodeKey]; } if (Scope == vsCMElement.vsCMElementPropertySetStmt && ces.Expression is CodePropertySetValueReferenceExpression) { if (IsInRange(cs, Point)) return (CodeElement)cs.UserData[CodeKey]; } } if (Scope == vsCMElement.vsCMElementOther && IsInRange(cs, Point)) return (CodeElement)cs.UserData[CodeKey]; } return null; }
private static CodeElement FindCodeElementByFullName( ProjectItem projectItem, string targetFullName, vsCMElement elementKind) { foreach (CodeElement element in new DteHelperEx.CodeElementsIterator(projectItem)) { if (element.Kind == vsCMElement.vsCMElementNamespace) { foreach (CodeElement type in ((CodeNamespace)element).Members) { CodeElement result = InspectCodeElement(type, targetFullName, elementKind); if (result != null) { return result; } } } else { CodeElement result = InspectCodeElement(element, targetFullName, elementKind); if (result != null) { return result; } } } return null; }
public SelectedCodeModelItemNodeFactory(DTE2 dte, vsCMElement codeElementType, string name) { _dte = dte; _codeElementType = codeElementType; _name = name; }
/// <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; }
private static CodeElement FindCodeElementByFullName( Project project, string targetFullName, vsCMElement elementKind) { if (project == null) { return null; } foreach (ProjectItem projectItem in new DteHelperEx.ProjectItemIterator(project)) { CodeElement element = FindCodeElementByFullName(projectItem, targetFullName, elementKind); if (element != null) { return element; } } return null; }
private static CodeElement InspectChildren( CodeElements elements, string targetFullName, vsCMElement elementKind) { foreach (CodeElement children in elements) { if (children.Kind == elementKind && children.FullName.Equals(targetFullName, StringComparison.InvariantCultureIgnoreCase)) { return children; } } return null; }