private static FileSourceRangeCollection GetAllPropertyReferences(LanguageElement startElement) { FileSourceRangeCollection allReferences = new FileSourceRangeCollection(); Property property; PrimitiveExpression primitiveExpression; if (!GetPrimitiveExpressionAndProperty(startElement, out primitiveExpression, out property)) { return(allReferences); } // Add references found... IElementCollection allPropertyReferences = property.FindAllReferences(); foreach (IElement element in allPropertyReferences) { allReferences.Add(new FileSourceRange(element.FirstFile as SourceFile, element.FirstNameRange)); } // Add the contents of the string primitive... SourceRange primitiveRange = primitiveExpression.NameRange; int nameStart = primitiveExpression.Name.IndexOf(property.Name); int nameEndMargin = primitiveExpression.Name.Length - property.Name.Length - nameStart; primitiveRange.Start.Offset += nameStart; primitiveRange.End.Offset -= nameEndMargin; allReferences.Add(new FileSourceRange(primitiveExpression.FileNode, primitiveRange)); // Add the NameRange of the property declaration itself.... allReferences.Add(new FileSourceRange(property.FileNode, property.NameRange)); return(allReferences); }
private static FileSourceRangeCollection GetAllPropertyReferences(LanguageElement startElement) { FileSourceRangeCollection allReferences = new FileSourceRangeCollection(); Property property; PrimitiveExpression primitiveExpression; if (!GetPrimitiveExpressionAndProperty(startElement, out primitiveExpression, out property)) return allReferences; // Add references found... IElementCollection allPropertyReferences = property.FindAllReferences(); foreach (IElement element in allPropertyReferences) allReferences.Add(new FileSourceRange(element.FirstFile as SourceFile, element.FirstNameRange)); // Add the contents of the string primitive... SourceRange primitiveRange = primitiveExpression.NameRange; int nameStart = primitiveExpression.Name.IndexOf(property.Name); int nameEndMargin = primitiveExpression.Name.Length - property.Name.Length - nameStart; primitiveRange.Start.Offset += nameStart; primitiveRange.End.Offset -= nameEndMargin; allReferences.Add(new FileSourceRange(primitiveExpression.FileNode, primitiveRange)); // Add the NameRange of the property declaration itself.... allReferences.Add(new FileSourceRange(property.FileNode, property.NameRange)); return allReferences; }
/// <summary> /// Main method to link the source ranges and start refactoring /// </summary> /// <param name="context"></param> /// <param name="ranges"></param> public static void ApplyRename(RefactoringContext context, FileSourceRangeCollection ranges) { if ((ranges != null) && (ranges.Count != 0)) { LinkedTextHelper.breakLinks(context, ranges); LinkedTextHelper.setUpLinks(context, ranges); } }
/// <summary> /// Main method to link the source ranges and start refactoring /// </summary> /// <param name="context"></param> /// <param name="ranges"></param> public static void ApplyRename(RefactoringContext context, FileSourceRangeCollection ranges) { if ((ranges != null) && (ranges.Count != 0)) { LinkedTextHelper.breakLinks(context, ranges); LinkedTextHelper.setUpLinks(context, ranges); } }
private FileSourceRangeCollection extractHqlNamedQuery(LanguageElement hqlQueryElement) { string hqlQuery = (hqlQueryElement as IPrimitiveExpression).Value.ToString(); SourceFile namedQueriesXmlFile = getNamedQueriesXmlFile(hqlQueryElement.Project as ProjectElement); if (namedQueriesXmlFile != null) { string tempQueryName = getTempQueryName(); if (!namedQueriesXmlFile.IsOpened) { CodeRush.File.Activate(namedQueriesXmlFile.FilePath); } TextDocument namedQueriesXmlDocument = namedQueriesXmlFile.Document as TextDocument; TextDocument currentDocument = hqlQueryElement.Document as TextDocument; string currentFilePath = hqlQueryElement.FileNode.FilePath; SourceFile currentSourceFile = hqlQueryElement.FileNode; string xmlContent = getXmlContent(hqlQuery, tempQueryName); insertXmlContentInNamedQueriesFile(namedQueriesXmlFile, xmlContent); string quotedTempQueryName = String.Format("\"{0}\"", tempQueryName); currentDocument.Replace(hqlQueryElement.Range, quotedTempQueryName, string.Empty, true); currentDocument.ParseIfTextChanged(); FileSourceRangeCollection rangesToLink = new FileSourceRangeCollection(); SourceRange queryNameElementRange = findElementByName(currentDocument.FileNode, quotedTempQueryName).Range; SourceRange queryNameElementRangeWithoutQuotes = new SourceRange( queryNameElementRange.Start.Line, queryNameElementRange.Start.Offset + 1, queryNameElementRange.End.Line, queryNameElementRange.End.Offset - 1); rangesToLink.Add(new FileSourceRange(currentSourceFile, queryNameElementRangeWithoutQuotes)); SourcePoint queryNameLocationInNamedQueriesXmlDocument = namedQueriesXmlDocument.FindText(tempQueryName); SourceRange queryNameElementRangeInNamedQueriesXmlDocument = new SourceRange( queryNameLocationInNamedQueriesXmlDocument, new SourcePoint(queryNameLocationInNamedQueriesXmlDocument.Line, queryNameLocationInNamedQueriesXmlDocument.Offset + tempQueryName.Length)); rangesToLink.Add(new FileSourceRange(namedQueriesXmlFile, queryNameElementRangeInNamedQueriesXmlDocument)); CodeRush.File.Activate(currentFilePath); TextDocument.Active.SelectText(queryNameElementRangeWithoutQuotes); return(rangesToLink); } else { MessageBox.Show("No file for hql named queries found", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } return(null); }
private void spINotifyPropertyChangedRename_SearchPreviewReferences(object sender, SearchForPreviewEventArgs ea) { FileSourceRangeCollection allReferences = GetAllPropertyReferences(ea.Element); foreach (FileSourceRange fileSourceRange in allReferences) { ea.AddRange(fileSourceRange.Range); } }
/// <summary> /// Creates links between all the ranges passed as parameter /// </summary> /// <param name="context"></param> /// <param name="ranges"></param> private static void setUpLinks(RefactoringContext context, FileSourceRangeCollection ranges) { if (LinkedTextHelper.isOneDocumentLidList(context.Document, ranges)) { LinkedTextHelper.setUpLinksForDocument(context.Document, ranges); } else { CodeRush.LinkedIdentifiers.NewMultiDocumentContainer().AddRange(ranges); } CodeRush.LinkedIdentifiers.Invalidate(context.Document); }
/// <summary> /// Returns a collection of source ranges that belongs to the document passed as parameter /// </summary> /// <param name="document"></param> /// <param name="ranges"></param> /// <returns></returns> private static SourceRangeCollection getDocumentRanges(TextDocument document, FileSourceRangeCollection ranges) { SourceRangeCollection ranges2 = new SourceRangeCollection(document); if ((document != null) && (ranges != null)) { foreach (FileSourceRange range in ranges) { TextDocument textDocument = getTextDocument(range.File); if ((textDocument == null) || (textDocument != document)) { continue; } ranges2.Add(range.Range); } } return ranges2; }
/// <summary> /// Links ranges in the document passed as parameter /// </summary> /// <param name="document"></param> /// <param name="ranges"></param> private static void setUpLinksForDocument(TextDocument document, FileSourceRangeCollection ranges) { if ((ranges != null) && (document != null)) { ILinkedIdentifierList list = CodeRush.LinkedIdentifiers.GetStorage(document).NewList(); int count = ranges.Count; for (int i = 0; i < count; i++) { FileSourceRange range = ranges[i]; ILinkedIdentifier identifier = list.Add(range.Range); if (identifier != null) { identifier.Affix = range.Data as LinkedIdentifierAffix; } } } }
/// <summary> /// Breaks preexisting links between the source ranges passed as parameters /// </summary> /// <param name="context"></param> /// <param name="ranges"></param> private static void breakLinks(RefactoringContext context, FileSourceRangeCollection ranges) { if ((ranges != null) && (ranges.Count != 0)) { TextDocument document = context.Document; foreach (SourceRange range in getDocumentRanges(document, ranges)) { foreach (ILinkedIdentifier identifier in CodeRush.LinkedIdentifiers.Find(document, range)) { ILinkedIdentifierList list = identifier.List; if (list.MultiDocumentContainer != null) { list.MultiDocumentContainer.BreakAllLinks(); } } CodeRush.LinkedIdentifiers.BreakAllLinksInRange(document, range); } } }
/// <summary> /// Breaks preexisting links between the source ranges passed as parameters /// </summary> /// <param name="context"></param> /// <param name="ranges"></param> private static void breakLinks(RefactoringContext context, FileSourceRangeCollection ranges) { if ((ranges != null) && (ranges.Count != 0)) { TextDocument document = context.Document; foreach (SourceRange range in getDocumentRanges(document, ranges)) { foreach (ILinkedIdentifier identifier in CodeRush.LinkedIdentifiers.Find(document, range)) { ILinkedIdentifierList list = identifier.List; if (list.MultiDocumentContainer != null) { list.MultiDocumentContainer.BreakAllLinks(); } } CodeRush.LinkedIdentifiers.BreakAllLinksInRange(document, range); } } }
private void executeRefactoring(ApplyContentEventArgs ea) { try { if (actionIsAvailable(ea.Element)) { FileSourceRangeCollection collection = extractHqlNamedQuery(ea.Element); if (collection != null) { RefactoringContext context = new RefactoringContext(ea); LinkedTextHelper.ApplyRename(context, collection); } } } catch (Exception ex) { ShowException(ex); } }
/// <summary> /// Indicates if all the ranges passed as parameter belongs to the same document /// </summary> /// <param name="document"></param> /// <param name="ranges"></param> /// <returns></returns> private static bool isOneDocumentLidList(TextDocument document, FileSourceRangeCollection ranges) { if ((ranges == null) || (document == null)) { return(false); } string strA = document.FullName; int count = ranges.Count; for (int i = 0; i < count; i++) { SourceFile file = ranges[i].File; if (strA == null) { strA = file.Name; } else if (string.Compare(strA, file.Name, true) != 0) { return(false); } } return(true); }
/// <summary> /// Indicates if all the ranges passed as parameter belongs to the same document /// </summary> /// <param name="document"></param> /// <param name="ranges"></param> /// <returns></returns> private static bool isOneDocumentLidList(TextDocument document, FileSourceRangeCollection ranges) { if ((ranges == null) || (document == null)) { return false; } string strA = document.FullName; int count = ranges.Count; for (int i = 0; i < count; i++) { SourceFile file = ranges[i].File; if (strA == null) { strA = file.Name; } else if (string.Compare(strA, file.Name, true) != 0) { return false; } } return true; }
/// <summary> /// Creates links between all the ranges passed as parameter /// </summary> /// <param name="context"></param> /// <param name="ranges"></param> private static void setUpLinks(RefactoringContext context, FileSourceRangeCollection ranges) { if (LinkedTextHelper.isOneDocumentLidList(context.Document, ranges)) { LinkedTextHelper.setUpLinksForDocument(context.Document, ranges); } else { CodeRush.LinkedIdentifiers.NewMultiDocumentContainer().AddRange(ranges); } CodeRush.LinkedIdentifiers.Invalidate(context.Document); }
private void spINotifyPropertyChangedRename_SearchReferences(object sender, SearchEventArgs ea) { FileSourceRangeCollection allReferences = GetAllPropertyReferences(ea.Element); ea.AddRanges(allReferences); }
private void spFindPrimitives_SearchReferences(object sender, SearchEventArgs ea) { // Rory says: Exhaust the events args first, before looking elsewhere (e.g., the CodeRush.Xxxx properties). PrimitiveExpression primitiveExpression = ea.Element as PrimitiveExpression; if (primitiveExpression == null) return; // [email protected] -- before 11.2 -- ask to get in on the beta. NDA is currently required. FileSourceRangeCollection ranges = new FileSourceRangeCollection(); LanguageElement scope = CodeRush.Source.ActiveSourceFile; foreach (PrimitiveExpression primitive in FindMatching(scope, primitiveExpression)) ranges.Add(new FileSourceRange(primitive.FileNode, primitive.Range)); ea.AddRanges(ranges); }
/// <summary> /// Links ranges in the document passed as parameter /// </summary> /// <param name="document"></param> /// <param name="ranges"></param> private static void setUpLinksForDocument(TextDocument document, FileSourceRangeCollection ranges) { if ((ranges != null) && (document != null)) { ILinkedIdentifierList list = CodeRush.LinkedIdentifiers.GetStorage(document).NewList(); int count = ranges.Count; for (int i = 0; i < count; i++) { FileSourceRange range = ranges[i]; ILinkedIdentifier identifier = list.Add(range.Range); if (identifier != null) { identifier.Affix = range.Data as LinkedIdentifierAffix; } } } }
/// <summary> /// Returns a collection of source ranges that belongs to the document passed as parameter /// </summary> /// <param name="document"></param> /// <param name="ranges"></param> /// <returns></returns> private static SourceRangeCollection getDocumentRanges(TextDocument document, FileSourceRangeCollection ranges) { SourceRangeCollection ranges2 = new SourceRangeCollection(document); if ((document != null) && (ranges != null)) { foreach (FileSourceRange range in ranges) { TextDocument textDocument = getTextDocument(range.File); if ((textDocument == null) || (textDocument != document)) { continue; } ranges2.Add(range.Range); } } return(ranges2); }
private FileSourceRangeCollection extractHqlNamedQuery(LanguageElement hqlQueryElement) { string hqlQuery = (hqlQueryElement as IPrimitiveExpression).Value.ToString(); SourceFile namedQueriesXmlFile = getNamedQueriesXmlFile(hqlQueryElement.Project as ProjectElement); if (namedQueriesXmlFile != null) { string tempQueryName = getTempQueryName(); if (!namedQueriesXmlFile.IsOpened) CodeRush.File.Activate(namedQueriesXmlFile.FilePath); TextDocument namedQueriesXmlDocument = namedQueriesXmlFile.Document as TextDocument; TextDocument currentDocument = hqlQueryElement.Document as TextDocument; string currentFilePath = hqlQueryElement.FileNode.FilePath; SourceFile currentSourceFile = hqlQueryElement.FileNode; string xmlContent = getXmlContent(hqlQuery, tempQueryName); insertXmlContentInNamedQueriesFile(namedQueriesXmlFile, xmlContent); string quotedTempQueryName = String.Format("\"{0}\"", tempQueryName); currentDocument.Replace(hqlQueryElement.Range, quotedTempQueryName, string.Empty, true); currentDocument.ParseIfTextChanged(); FileSourceRangeCollection rangesToLink = new FileSourceRangeCollection(); SourceRange queryNameElementRange = findElementByName(currentDocument.FileNode, quotedTempQueryName).Range; SourceRange queryNameElementRangeWithoutQuotes = new SourceRange( queryNameElementRange.Start.Line, queryNameElementRange.Start.Offset + 1, queryNameElementRange.End.Line, queryNameElementRange.End.Offset - 1); rangesToLink.Add(new FileSourceRange(currentSourceFile, queryNameElementRangeWithoutQuotes)); SourcePoint queryNameLocationInNamedQueriesXmlDocument = namedQueriesXmlDocument.FindText(tempQueryName); SourceRange queryNameElementRangeInNamedQueriesXmlDocument = new SourceRange( queryNameLocationInNamedQueriesXmlDocument, new SourcePoint(queryNameLocationInNamedQueriesXmlDocument.Line, queryNameLocationInNamedQueriesXmlDocument.Offset + tempQueryName.Length)); rangesToLink.Add(new FileSourceRange(namedQueriesXmlFile, queryNameElementRangeInNamedQueriesXmlDocument)); CodeRush.File.Activate(currentFilePath); TextDocument.Active.SelectText(queryNameElementRangeWithoutQuotes); return rangesToLink; } else { MessageBox.Show("No file for hql named queries found", "Information", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } return null; }