public void CanCompleteAttribute(string testFileName, int line, int column, string expectedElementName, PaddingType expectedPadding) { Position testPosition = new Position(line, column); string testXml = LoadTestFile("TestFiles", testFileName + ".xml"); TextPositions positions = new TextPositions(testXml); XmlDocumentSyntax document = Parser.ParseText(testXml); XmlLocator locator = new XmlLocator(document, positions); XmlLocation location = locator.Inspect(testPosition); Assert.NotNull(location); XSPath elementPath = XSPath.Parse(expectedElementName); XSElement element; XSAttribute replaceAttribute; PaddingType needsPadding; Assert.True( location.CanCompleteAttribute(out element, out replaceAttribute, out needsPadding, onElementWithPath: elementPath), "CanCompleteAttribute" ); Assert.NotNull(element); Assert.Null(replaceAttribute); Assert.Equal(expectedPadding, needsPadding); }
/// <summary> /// Perform post-parse processing on the node to ensure that <see cref="ExpressionNode.Range"/>s are populated and children are connected via the usual relationships (<see cref="ExpressionNode.Parent"/>, <see cref="ExpressionNode.PreviousSibling"/>, and <see cref="ExpressionNode.NextSibling"/>). /// </summary> /// <typeparam name="TNode"> /// The root node type. /// </typeparam> /// <param name="root"> /// The root node. /// </param> /// <param name="textPositions"> /// A <see cref="TextPositions"/> used to map absolute node positions to line / column. /// </param> /// <returns> /// The root node (enables inline use). /// </returns> public static TNode PostParse <TNode>(this TNode root, TextPositions textPositions) where TNode : ExpressionNode { if (root == null) { throw new System.ArgumentNullException(nameof(root)); } if (textPositions == null) { throw new System.ArgumentNullException(nameof(textPositions)); } Dictionary <int, Position> positionCache = new Dictionary <int, Position>(); void SetRange(ExpressionNode node) { Position start; if (!positionCache.TryGetValue(node.AbsoluteStart, out start)) { start = textPositions.GetPosition(node.AbsoluteStart); positionCache.Add(node.AbsoluteStart, start); } Position end; if (!positionCache.TryGetValue(node.AbsoluteEnd, out end)) { end = textPositions.GetPosition(node.AbsoluteEnd); positionCache.Add(node.AbsoluteEnd, end); } node.Range = new Range(start, end); } SetRange(root); foreach (ExpressionContainerNode parent in root.DescendantNodes().OfType <ExpressionContainerNode>()) { SetRange(root); ExpressionNode previousSibling = null; foreach (ExpressionNode nextSibling in parent.Children) { SetRange(nextSibling); nextSibling.Parent = parent; nextSibling.PreviousSibling = previousSibling; if (previousSibling != null) { previousSibling.NextSibling = nextSibling; } previousSibling = nextSibling; } } return(root); }
/// <summary> /// Load and parse the project. /// </summary> /// <param name="cancellationToken"> /// An optional <see cref="CancellationToken"/> that can be used to cancel the operation. /// </param> /// <returns> /// A task representing the load operation. /// </returns> public virtual async Task Load(CancellationToken cancellationToken = default(CancellationToken)) { ClearDiagnostics(); Xml = null; XmlPositions = null; XmlLocator = null; string xml; using (StreamReader reader = ProjectFile.OpenText()) { xml = await reader.ReadToEndAsync(); } Xml = Parser.ParseText(xml); XmlPositions = new TextPositions(xml); XmlLocator = new XmlLocator(Xml, XmlPositions); IsDirty = false; await ConfigurePackageSources(cancellationToken); bool loaded = TryLoadMSBuildProject(); if (loaded) { MSBuildLocator = new MSBuildLocator(MSBuildProject, XmlLocator, XmlPositions); } else { MSBuildLocator = null; } IsMSBuildProjectCached = !loaded; }
/// <summary> /// Update the project in-memory state. /// </summary> /// <param name="xml"> /// The project XML. /// </param> /// <param name="cancellationToken"> /// An optional <see cref="CancellationToken"/> that can be used to cancel the operation. /// </param> /// <returns> /// A task representing the update operation. /// </returns> public virtual async Task Update(string xml, CancellationToken cancellationToken = default) { if (xml == null) { throw new ArgumentNullException(nameof(xml)); } ClearDiagnostics(); Xml = Parser.ParseText(xml); XmlPositions = new TextPositions(xml); XmlLocator = new XmlLocator(Xml, XmlPositions); IsDirty = true; bool loaded = TryLoadMSBuildProject(); if (loaded) { MSBuildLocator = new MSBuildObjectLocator(MSBuildProject, XmlLocator, XmlPositions); } else { MSBuildLocator = null; } IsMSBuildProjectCached = !loaded; await UpdatePackageReferences(cancellationToken); }
public void InEmptyElementName(string testFileName, int line, int column, string expectedElementName) { Position testPosition = new Position(line, column); string testXml = LoadTestFile("TestFiles", testFileName + ".xml"); TextPositions positions = new TextPositions(testXml); XmlDocumentSyntax document = Parser.ParseText(testXml); XmlLocator locator = new XmlLocator(document, positions); XmlLocation result = locator.Inspect(testPosition); Assert.NotNull(result); Assert.Equal(XSNodeKind.Element, result.Node.Kind); Assert.True(result.IsElement(), "IsElement"); XSElement element = (XSElement)result.Node; Assert.Equal(expectedElementName, element.Name); Assert.True(result.IsEmptyElement(), "IsEmptyElement"); Assert.True(result.IsName(), "IsName"); Assert.False(result.IsElementContent(), "IsElementContent"); // TODO: Verify Parent, PreviousSibling, and NextSibling. }
/// <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> /// Update the project in-memory state. /// </summary> /// <param name="xml"> /// The project XML. /// </param> public virtual void Update(string xml) { if (xml == null) { throw new ArgumentNullException(nameof(xml)); } ClearDiagnostics(); Xml = Parser.ParseText(xml); XmlPositions = new TextPositions(xml); XmlLocator = new XmlLocator(Xml, XmlPositions); IsDirty = true; bool loaded = TryLoadMSBuildProject(); if (loaded) { MSBuildLocator = new MSBuildLocator(MSBuildProject, XmlLocator, XmlPositions); } else { MSBuildLocator = null; } IsMSBuildProjectCached = !loaded; }
public void InvalidElementRange(string testFileName, int nodeIndex, string elementName, int startLine, int startColumn, int endLine, int endColumn) { string testXml = LoadTestFile("TestFiles", testFileName + ".xml"); TextPositions xmlPositions = new TextPositions(testXml); XmlDocumentSyntax xmlDocument = Parser.ParseText(testXml); List <XSNode> nodes = xmlDocument.GetSemanticModel(xmlPositions); Assert.NotNull(nodes); XSNode targetNode = nodes[nodeIndex]; Assert.NotNull(targetNode); Assert.IsAssignableFrom <XSElement>(targetNode); XSElement targetElement = (XSElement)targetNode; Assert.Equal(elementName, targetElement.Name); Assert.False(targetElement.IsValid, "IsValid"); Range expectedRange = new Range( start: new Position(startLine, startColumn), end: new Position(endLine, endColumn) ); Assert.Equal(expectedRange, targetElement.Range); }
void NodeRange(string testFileName, int index, int startLine, int startColumn, int endLine, int endColumn) { string testXml = LoadTestFile("TestFiles", testFileName + ".xml"); TextPositions xmlPositions = new TextPositions(testXml); XmlDocumentSyntax xmlDocument = Parser.ParseText(testXml); List <XSNode> nodes = xmlDocument.GetSemanticModel(xmlPositions); Assert.NotNull(nodes); Assert.InRange(index, 0, nodes.Count - 1); XSNode node = nodes[index]; Assert.NotNull(node); TestOutput.WriteLine("Node {0} at {1} is {2}.", index, node.Range, node.Kind ); Range expectedRange = new Range( start: new Position(startLine, startColumn), end: new Position(endLine, endColumn) ); Assert.Equal(expectedRange, node.Range); }
public void setText(TextPositions pos, string _value) { timers[(int)pos] = Time.time; text[(int)pos].SetMessageText(_value, false); currentPos = pos; }
public virtual void MeasureWidths(SplitText st, int position, int len, TextPositions positions) { byte[] s = new byte[len * 3]; int lenUTF8 = st.RetrieveUTF8(position, s, len); MeasureWidths(s, lenUTF8, positions, st.GetEncoding()); }
public void SetText(int xMap, int yMap, char[] text, TextPositions textPosition = TextPositions.LeftToRight) { var index = xMap + yMap * MapWidth; var length = text.Length; if (Tiles.Length < text.Length) { length = Tiles.Length; } if (textPosition == TextPositions.LeftToRight) { for (int i = 0; i < length; i++) { var character = text[i]; var number = this.Font.GetTileNumber(character); this.Tiles[i + index] = new MapTileDescriptor(number); } } else { for (int i = 0; i < length; i++) { var character = text[text.Length - 1 - i]; var number = this.Font.GetTileNumber(character); this.Tiles[index - i] = new MapTileDescriptor(number); } } }
public void InsideElement1(string testFileName, int line, int column) { // TODO: Change this test to use XmlLocator. Position testPosition = new Position(line, column); string testXml = LoadTestFile("TestFiles", testFileName + ".xml"); TextPositions positions = new TextPositions(testXml); XmlDocumentSyntax xmlDocument = Parser.ParseText(testXml); int absolutePosition = positions.GetAbsolutePosition(testPosition) - 1; // To find out if we can insert an element, make sure we find the node at the position ONE BEFORE the insertion point! SyntaxNode foundNode = xmlDocument.FindNode(absolutePosition, descendIntoChildren: node => true ); Assert.NotNull(foundNode); Assert.IsAssignableFrom <SyntaxList>(foundNode); SyntaxList list = (SyntaxList)foundNode; Range listSpan = list.Span.ToNative(positions); Assert.True( listSpan.Contains(testPosition), "List's span must contain the test position." ); }
/// <summary> /// Create a new <see cref="XSParserVisitor"/>. /// </summary> /// <param name="textPositions"> /// Lookup for document text positions. /// </param> public XSParserVisitor(TextPositions textPositions) { if (textPositions == null) { throw new ArgumentNullException(nameof(textPositions)); } _textPositions = textPositions; }
public virtual void MeasureWidths(byte[] s, int len, TextPositions positions, int enc) { // A bug in GDI+ means that the string has to be broken up into blocks // of 32 or less. int maxMeasurePerCall = 32; try { String sAll = System.Text.Encoding.UTF8.GetString(s, 0, len); int sAllLength = sAll.Length; for (int start = 0; start < sAllLength; start += maxMeasurePerCall) { int sLength = sAllLength - start; if (sLength > maxMeasurePerCall) { sLength = maxMeasurePerCall; } CharacterRange [] characterRanges = new CharacterRange[sLength]; for (int i = 0; i < sLength; i++) { // Another problem with measuring trailing spaces means // that this doesn't work with a range containing a single space. if (sAll[start + i] == ' ') { characterRanges[i] = new CharacterRange(start, i + 1); } else { characterRanges[i] = new CharacterRange(start + i, 1); } } sf.SetMeasurableCharacterRanges(characterRanges); RectangleF layoutRect = new RectangleF(0.0f, 0.0f, float.MaxValue, float.MaxValue); Region[] regions = new Region[sLength]; regions = g.MeasureCharacterRanges(sAll, fontHandle, layoutRect, sf); for (int i = 0; i < sLength; i++) { char ch = sAll[start + i]; float xOfCharRight = (regions[i].GetBounds(g).Right); int xPixel = (int)(xOfCharRight); positions.Add(xPixel); if (ch >= 0x80) { positions.Add(xPixel); } if (ch >= 0x800) { positions.Add(xPixel); } } } } catch (System.IO.IOException) { System.Console.Out.WriteLine("Failed to convert"); } }
void NodeCount(string testFileName, int expectedNodeCount) { string testXml = LoadTestFile("TestFiles", testFileName + ".xml"); TextPositions xmlPositions = new TextPositions(testXml); XmlDocumentSyntax xmlDocument = Parser.ParseText(testXml); List <XSNode> nodes = xmlDocument.GetSemanticModel(xmlPositions); Assert.NotNull(nodes); Assert.Equal(expectedNodeCount, nodes.Count); }
/// <summary> /// Affiche un integer /// </summary> /// <param name="xMap"></param> /// <param name="yMap"></param> /// <param name="integer"></param> public void SetText(int xMap, int yMap, int integer, TextPositions textPosition = TextPositions.LeftToRight) { int index = 0; var font = this.Font; int sign = Math.Sign(integer); integer = Math.Abs(integer); while (true) { var mask = ((integer / 10) * 10); int digit = integer - mask; integerString[index] = (char)('0' + digit); index++; integer = integer / 10; if (integer == 0) { break; } } if (sign == -1) { integerString[index] = '-'; index++; } int x = 0; if (textPosition == TextPositions.LeftToRight) { for (int i = index - 1; i >= 0; i--) { var tileNumber = font.GetTileNumber(integerString[i]); this.SetTile(xMap + x, yMap, new MapTileDescriptor(tileNumber)); x++; } } else { for (int i = 0; i < index; i++) { var tileNumber = font.GetTileNumber(integerString[i]); this.SetTile(xMap + x, yMap, new MapTileDescriptor(tileNumber)); x--; } } }
public void GetAbsolutePosition_UnixLineEndings(int line, int column, char expectedChar) { const string text = TestData.TextWithUnixLineEndings.Text; TextPositions textPositions = new TextPositions(text); int absolutePosition = textPositions.GetAbsolutePosition(line, column); Assert.Equal(expectedChar, text[absolutePosition]); Position roundTripped = textPositions.GetPosition(absolutePosition).ToZeroBased(); Assert.Equal(roundTripped.LineNumber, line); Assert.Equal(roundTripped.ColumnNumber, column); }
/// <summary> /// Update the project in-memory state. /// </summary> /// <param name="xml"> /// The project XML. /// </param> public virtual void Update(string xml) { if (xml == null) { throw new ArgumentNullException(nameof(xml)); } ClearDiagnostics(); SourcePositions = new TextPositions(xml); SourceLocator = new SourceLocator(SourcePositions); IsDirty = true; bool loaded = TryLoadProject(); }
/// <summary> /// Get a <see cref="Range"/> representing the attribute's span (without quotes) in the XML. /// </summary> /// <param name="attribute"> /// The attribute. /// </param> /// <param name="xmlPositions"> /// The XML position lookup. /// </param> /// <returns> /// The <see cref="Range"/>. /// </returns> public static Range GetValueRange(this XmlAttributeSyntax attribute, TextPositions xmlPositions) { if (attribute == null) { throw new ArgumentNullException(nameof(attribute)); } if (xmlPositions == null) { throw new ArgumentNullException(nameof(xmlPositions)); } Range valueRange = attribute.ValueNode.Span.ToNative(xmlPositions); // Trim off leading and trailing quotes. return(valueRange.Transform(moveStartColumns: 1, moveEndColumns: -1)); }
void NodeKind(string testFileName, int index, XSNodeKind nodeKind) { string testXml = LoadTestFile("TestFiles", testFileName + ".xml"); TextPositions xmlPositions = new TextPositions(testXml); XmlDocumentSyntax xmlDocument = Parser.ParseText(testXml); List <XSNode> nodes = xmlDocument.GetSemanticModel(xmlPositions); Assert.NotNull(nodes); Assert.InRange(index, 0, nodes.Count - 1); XSNode node = nodes[index]; Assert.NotNull(node); Assert.Equal(nodeKind, node.Kind); }
/// <summary> /// Convert the <see cref="TextSpan"/> to its native equivalent. /// </summary> /// <param name="span"> /// The <see cref="TextSpan"/> to convert. /// </param> /// <param name="textPositions"> /// The textual position lookup used to map absolute positions to lines and columns. /// </param> /// <returns> /// The equivalent <see cref="Range"/>. /// </returns> public static Range ToNative(this TextSpan span, TextPositions textPositions) { if (textPositions == null) { throw new ArgumentNullException(nameof(textPositions)); } Position startPosition = textPositions.GetPosition(span.Start); Position endPosition = textPositions.GetPosition(span.End); if (endPosition.ColumnNumber == 0) { throw new InvalidOperationException("Should not happen anymore"); } return(new Range(startPosition, endPosition)); }
public void CanCompleteElement(string testFileName, int line, int column) { Position testPosition = new Position(line, column); string testXml = LoadTestFile("TestFiles", testFileName + ".xml"); TextPositions positions = new TextPositions(testXml); XmlDocumentSyntax document = Parser.ParseText(testXml); XmlLocator locator = new XmlLocator(document, positions); XmlLocation location = locator.Inspect(testPosition); Assert.NotNull(location); XSElement replacingElement; Assert.True(location.CanCompleteElement(out replacingElement), "CanCompleteElement"); Assert.NotNull(replacingElement); }
/// <summary> /// Parse the syntax model to derive a semantic model. /// </summary> /// <param name="node"> /// The <see cref="SyntaxNode"/> to parse. /// </param> /// <param name="xmlPositions"> /// The lookup for document positions. /// </param> /// <returns> /// A list of <see cref="XSNode"/>s, sorted by <see cref="XSNode.Range"/>. /// </returns> public static List <XSNode> GetSemanticModel(this XmlNodeSyntax node, TextPositions xmlPositions) { if (node == null) { throw new ArgumentNullException(nameof(node)); } if (xmlPositions == null) { throw new ArgumentNullException(nameof(xmlPositions)); } XSParserVisitor parserVisitor = new XSParserVisitor(xmlPositions); parserVisitor.Visit(node); parserVisitor.FinaliseModel(); return(parserVisitor.DiscoveredNodes); }
public void GetPosition_WindowsLineEndings(char forChar, int expectedLine, int expectedColumn) { const string text = TestData.TextWithWindowsLineEndings.Text; int absolutePosition = text.IndexOf(forChar); Assert.InRange(absolutePosition, 0, text.Length - 1); TextPositions textPositions = new TextPositions(text); Position position = textPositions.GetPosition(absolutePosition); Assert.True(position.IsOneBased); Assert.Equal(expectedLine, position.LineNumber); Assert.Equal(expectedColumn, position.ColumnNumber); int absolutePositionRoundTripped = textPositions.GetAbsolutePosition(position); Assert.Equal(absolutePosition, absolutePositionRoundTripped); }
/// <summary> /// Create a new <see cref="SourceLocator"/>. /// </summary> /// <param name="documentPositions"> /// The position-lookup for the underlying XML document text. /// </param> public SourceLocator(TextPositions documentPositions) { if (documentPositions == null) { throw new ArgumentNullException(nameof(documentPositions)); } _documentPositions = documentPositions; // TODO List <SourceNode> allNodes = null; foreach (SourceNode node in allNodes) { _nodeRanges.Add(node.Range); _nodesByStartPosition.Add(node.Range.Start, node); } _nodeRanges.Sort(); }
/// <summary> /// Load and parse the project. /// </summary> /// <param name="cancellationToken"> /// An optional <see cref="CancellationToken"/> that can be used to cancel the operation. /// </param> /// <returns> /// A task representing the load operation. /// </returns> public virtual async Task Load(CancellationToken cancellationToken = default(CancellationToken)) { ClearDiagnostics(); SourcePositions = null; SourceLocator = null; string xml; using (StreamReader reader = ProjectFile.OpenText()) { xml = await reader.ReadToEndAsync(); } SourcePositions = new TextPositions(xml); SourceLocator = new SourceLocator(SourcePositions); IsDirty = false; bool loaded = TryLoadProject(); }
public void InAttributeValue(string testFileName, int line, int column, string expectedAttributeName) { Position testPosition = new Position(line, column); string testXml = LoadTestFile("TestFiles", testFileName + ".xml"); TextPositions positions = new TextPositions(testXml); XmlDocumentSyntax document = Parser.ParseText(testXml); XmlLocator locator = new XmlLocator(document, positions); XmlLocation result = locator.Inspect(testPosition); Assert.NotNull(result); XSAttribute attribute; Assert.True(result.IsAttribute(out attribute), "IsAttribute"); Assert.True(result.IsAttributeValue(), "IsAttributeValue"); Assert.Equal(expectedAttributeName, attribute.Name); // TODO: Verify Parent, PreviousSibling, and NextSibling. }
void ElementAttributesRange(string testFileName, string elementName, int startLine, int startColumn, int endLine, int endColumn) { string testXml = LoadTestFile("TestFiles", testFileName + ".xml"); TextPositions xmlPositions = new TextPositions(testXml); XmlDocumentSyntax xmlDocument = Parser.ParseText(testXml); List <XSNode> nodes = xmlDocument.GetSemanticModel(xmlPositions); Assert.NotNull(nodes); XSNode targetNode = nodes.Find(node => node.Name == elementName); Assert.NotNull(targetNode); Assert.IsAssignableFrom <XSElement>(targetNode); XSElement targetElement = (XSElement)targetNode; Range expectedRange = new Range( start: new Position(startLine, startColumn), end: new Position(endLine, endColumn) ); Assert.Equal(expectedRange, targetElement.AttributesRange); }
public void IsExpression_Success(string testFileName, int line, int column, ExpressionKind expectedExpressionKind) { Position testPosition = new Position(line, column); string testXml = LoadTestFile("TestFiles", testFileName + ".xml"); TextPositions positions = new TextPositions(testXml); XmlDocumentSyntax document = Parser.ParseText(testXml); XmlLocator locator = new XmlLocator(document, positions); XmlLocation location = locator.Inspect(testPosition); Assert.NotNull(location); ExpressionNode actualExpression; Range actualExpressionRange; Assert.True( location.IsExpression(out actualExpression, out actualExpressionRange), "IsExpression" ); Assert.NotNull(actualExpression); Assert.Equal(expectedExpressionKind, actualExpression.Kind); }