예제 #1
0
        /// <summary>
        ///     Get the <see cref="Range"/> represented by the <see cref="InvalidProjectFileException"/>.
        /// </summary>
        /// <param name="invalidProjectFileException">
        ///     The <see cref="InvalidProjectFileException"/>.
        /// </param>
        /// <param name="xmlLocator">
        ///     The XML locator API (if available).
        /// </param>
        /// <returns>
        ///     The <see cref="Range"/>.
        /// </returns>
        public static Range GetRange(this InvalidProjectFileException invalidProjectFileException, XmlLocator xmlLocator)
        {
            if (invalidProjectFileException == null)
            {
                throw new ArgumentNullException(nameof(invalidProjectFileException));
            }

            Position startPosition = new Position(
                invalidProjectFileException.LineNumber,
                invalidProjectFileException.ColumnNumber
                );

            // Attempt to use the range of the actual XML that the exception refers to.
            XmlLocation location = xmlLocator?.Inspect(startPosition);

            if (location != null)
            {
                return(location.Node.Range);
            }

            // Otherwise, fall back to using the exception's declared end position...
            Position endPosition = new Position(
                invalidProjectFileException.EndLineNumber,
                invalidProjectFileException.EndColumnNumber
                );

            // ...although it's sometimes less reliable.
            if (endPosition == Position.Zero)
            {
                endPosition = startPosition;
            }

            return(new Range(startPosition, endPosition));
        }
예제 #2
0
        /// <summary>
        ///     Add a target.
        /// </summary>
        /// <param name="target">
        ///     The target's declaring <see cref="ProjectTargetElement"/>.
        /// </param>
        void AddTarget(ProjectTargetElement target)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            XmlLocation targetLocation = _projectXmlLocator.Inspect(
                target.Location.ToNative()
                );

            if (targetLocation == null)
            {
                return;
            }

            XSElement targetElement;

            if (!targetLocation.IsElement(out targetElement))
            {
                return;
            }

            Add(
                new MSBuildTarget(target, targetElement)
                );
        }
        /// <summary>
        ///     Get the <see cref="Range"/> represented by the <see cref="XmlException"/>.
        /// </summary>
        /// <param name="invalidXml">
        ///     The <see cref="XmlException"/>.
        /// </param>
        /// <param name="xmlLocator">
        ///     The XML locator API (if available).
        /// </param>
        /// <returns>
        ///     The <see cref="Range"/>.
        /// </returns>
        public static Range GetRange(this XmlException invalidXml, XmlLocator xmlLocator)
        {
            if (invalidXml == null)
            {
                throw new ArgumentNullException(nameof(invalidXml));
            }

            Position startPosition = new Position(
                invalidXml.LineNumber,
                invalidXml.LinePosition
                );

            // Attempt to use the range of the actual XML that the exception refers to.
            XmlLocation location = xmlLocator?.Inspect(startPosition);

            if (location != null)
            {
                return(location.Node.Range);
            }

            // Otherwise, just use the start position.
            return(startPosition.ToEmptyRange());
        }