Click here to Skip to main content
15,893,722 members
Articles / Programming Languages / C#

Calculating Metrics and Searching with a CodeDOM (Part 8)

Rate me:
Please Sign up or sign in to vote.
5.00/5 (7 votes)
6 Mar 2013CDDL7 min read 22K   684   10  
Calculating metrics on and searching a CodeDOM.
// The Nova Project by Ken Beckett.
// Copyright (C) 2007-2012 Inevitable Software, all rights reserved.
// Released under the Common Development and Distribution License, CDDL-1.0: http://opensource.org/licenses/cddl1.php

using Nova.CodeDOM;

namespace Nova.Analysis
{
    /// <summary>
    /// Represents the result of a Find operation - a <see cref="CodeObject"/> and its location (<see cref="Project"/> and <see cref="CodeUnit"/>).
    /// </summary>
    public class Result
    {
        #region /* FIELDS */

        private readonly CodeObject _codeObject;
        private readonly CodeUnit _codeUnit;

        #endregion

        #region /* CONSTRUCTORS */

        /// <summary>
        /// Create a <see cref="Result"/>.
        /// </summary>
        public Result(CodeObject codeObject, CodeUnit codeUnit)
        {
            _codeObject = codeObject;
            _codeUnit = codeUnit;
        }

        #endregion

        #region /* PROPERTIES */

        /// <summary>
        /// The associated <see cref="CodeObject"/>.
        /// </summary>
        public CodeObject CodeObject
        {
            get { return _codeObject; }
        }

        /// <summary>
        /// The associated <see cref="CodeUnit"/>.
        /// </summary>
        public CodeUnit CodeUnit
        {
            get { return _codeUnit; }
        }

        /// <summary>
        /// The associated <see cref="Project"/>.
        /// </summary>
        public Project Project
        {
            get { return (_codeUnit != null ? _codeUnit.Parent as Project : null); }
        }

        /// <summary>
        /// The associated line number and column in "9999/99" format (if any, otherwise empty string).
        /// </summary>
        public string LineCol
        {
            get
            {
                int lineNumber = _codeObject.LineNumber;
                return (lineNumber != 0 ? lineNumber + "-" + _codeObject.ColumnNumber : null);
            }
        }

        #endregion

        #region /* RENDERING */

        /// <summary>
        /// Format the <see cref="Result"/> as a string.
        /// </summary>
        /// <returns>The text representation of the <see cref="Result"/>.</returns>
        public override string ToString()
        {
            string result = "";
            Project project = Project;
            if (project != null)
                result += "Project '" + project.Name + "': ";
            if (_codeUnit != null)
                result += "File '" + _codeUnit.Name + "': ";
            result += _codeObject.AsString();
            return result;
        }

        public string Render
        {
            get
            {
                // If the object is a SymbolicRef, use the parent instead for better context
                if (_codeObject is SymbolicRef && _codeObject.Parent != null)
                    return _codeObject.Parent.GetDescription();
                return _codeObject.GetDescription();
            }
        }

        #endregion
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Common Development and Distribution License (CDDL)


Written By
Software Developer (Senior)
United States United States
I've been writing software since the late 70's, currently focusing mainly on C#.NET. I also like to travel around the world, and I own a Chocolate Factory (sadly, none of my employees are oompa loompas).

Comments and Discussions