/// <summary> /// Create a new <see cref="MSBuildLocator"/>. /// </summary> /// <param name="project"> /// The MSBuild project. /// </param> /// <param name="projectXmlLocator"> /// The <see cref="XmlLocator"/> for the project XML. /// </param> /// <param name="xmlPositions"> /// The position-lookup for the project XML. /// </param> public MSBuildLocator(Project project, XmlLocator projectXmlLocator, TextPositions xmlPositions) { if (project == null) { throw new ArgumentNullException(nameof(project)); } if (projectXmlLocator == null) { throw new ArgumentNullException(nameof(projectXmlLocator)); } if (xmlPositions == null) { throw new ArgumentNullException(nameof(xmlPositions)); } _project = project; _projectFile = _project.FullPath ?? String.Empty; _projectXmlLocator = projectXmlLocator; _xmlPositions = xmlPositions; AddTargets(); AddProperties(); AddItems(); AddImports(); _objectRanges.Sort(); }
/// <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()); }
/// <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)); }