Example #1
0
        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;
		}
        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);
        }
Example #4
0
        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);
        }
        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;
        }