Incorrect Position for syntax tree nod
As you can see by the debug log provided, the absolute position given by the request is so far off from its actual position? the position I'm looking for is the object creation node, and basically all i want for now is to detect when the cursor is in the braces (scope) of that object creation. and of course it is, and the token at the cursor should be a blank line inside (or what ever I type next) inside the cursor, and yet its ActorTrait??? The absolute position is far off from the actual position of the cursor in my editor
1 Reply
var documentLines = _documentContents[(Uri)request.TextDocument.Uri];
if (documentLines == null) FileLogger.Log("Doc is null???");
var documentText = string.Join(Environment.NewLine, documentLines);
FileLogger.Log("Docuemnt text: " + documentText);
var syntaxTree = CSharpSyntaxTree.ParseText(documentText);
var root = syntaxTree.GetRoot();
var text = syntaxTree.GetText();
FileLogger.Log("SyntaxTree text: " + text);
var nodesWithPositions = new List<Tuple<SyntaxNode, int, int>>();
FileLogger.Log("Document is same as syntax text: " + (documentText.Equals(text)));
FileLogger.Log($"Document is same as syntax text count {documentText.Length} == {text.Length}: " + (documentText.Length == text.Length));
// Traverse the syntax tree and collect all nodes
TraverseSyntaxTree(root, nodesWithPositions);
// Output the nodes and their absolute positions
foreach (var tuple in nodesWithPositions)
{
var node = tuple.Item1;
var start = tuple.Item2;
var end = tuple.Item3;
FileLogger.Log($"{node.Kind()} - Start: {start}, End: {end}");
}
FileLogger.Log("(Not absolute) Pos: " + request.Position);
var absolutePosition = text.Lines.GetPosition(new Microsoft.CodeAnalysis.Text.LinePosition(request.Position.Line, request.Position.Character));
FileLogger.Log("Absolute Position: " + absolutePosition.ToString());
var tokenAtCursor = root.FindToken(absolutePosition);
FileLogger.Log("Token At Cursor: " + tokenAtCursor.ToString());
// Trace up the syntax tree to find an ObjectCreationExpressionSyntax
var objectCreation = tokenAtCursor.Parent.AncestorsAndSelf()
.OfType<ObjectCreationExpressionSyntax>()
.FirstOrDefault();
var documentLines = _documentContents[(Uri)request.TextDocument.Uri];
if (documentLines == null) FileLogger.Log("Doc is null???");
var documentText = string.Join(Environment.NewLine, documentLines);
FileLogger.Log("Docuemnt text: " + documentText);
var syntaxTree = CSharpSyntaxTree.ParseText(documentText);
var root = syntaxTree.GetRoot();
var text = syntaxTree.GetText();
FileLogger.Log("SyntaxTree text: " + text);
var nodesWithPositions = new List<Tuple<SyntaxNode, int, int>>();
FileLogger.Log("Document is same as syntax text: " + (documentText.Equals(text)));
FileLogger.Log($"Document is same as syntax text count {documentText.Length} == {text.Length}: " + (documentText.Length == text.Length));
// Traverse the syntax tree and collect all nodes
TraverseSyntaxTree(root, nodesWithPositions);
// Output the nodes and their absolute positions
foreach (var tuple in nodesWithPositions)
{
var node = tuple.Item1;
var start = tuple.Item2;
var end = tuple.Item3;
FileLogger.Log($"{node.Kind()} - Start: {start}, End: {end}");
}
FileLogger.Log("(Not absolute) Pos: " + request.Position);
var absolutePosition = text.Lines.GetPosition(new Microsoft.CodeAnalysis.Text.LinePosition(request.Position.Line, request.Position.Character));
FileLogger.Log("Absolute Position: " + absolutePosition.ToString());
var tokenAtCursor = root.FindToken(absolutePosition);
FileLogger.Log("Token At Cursor: " + tokenAtCursor.ToString());
// Trace up the syntax tree to find an ObjectCreationExpressionSyntax
var objectCreation = tokenAtCursor.Parent.AncestorsAndSelf()
.OfType<ObjectCreationExpressionSyntax>()
.FirstOrDefault();
private readonly IDictionary<Uri, string[]> _documentContents;
private readonly IDictionary<Uri, string[]> _documentContents;
private readonly IDictionary<Uri, SourceText> _documentContents;
private readonly IDictionary<Uri, SourceText> _documentContents;
var documentLines = _documentContents[(Uri)request.TextDocument.Uri];
if (documentLines == null) FileLogger.Log("Doc is null???");
var documentText = string.Join(Environment.NewLine, documentLines);
FileLogger.Log("Docuemnt text: " + documentText);
var syntaxTree = CSharpSyntaxTree.ParseText(documentText);
var root = syntaxTree.GetRoot();
var text = syntaxTree.GetText();
var absolutePosition = text.Lines.GetPosition(new Microsoft.CodeAnalysis.Text.LinePosition(request.Position.Line, request.Position.Character));
FileLogger.Log("Absolute Position: " + absolutePosition);
var tokenAtCursor = root.FindToken(absolutePosition);
// Trace up the syntax tree to find an ObjectCreationExpressionSyntax
var objectCreation = tokenAtCursor.Parent.AncestorsAndSelf()
.OfType<ObjectCreationExpressionSyntax>()
.FirstOrDefault();
if (objectCreation != null)
{
// Check if the cursor is within the initializer span
if (objectCreation.Initializer != null &&
absolutePosition >= objectCreation.Initializer.SpanStart &&
absolutePosition <= objectCreation.Initializer.Span.End)
{
FileLogger.Log("Cursor inside ObjectCreationExpressionSyntax initializer.");
}
else
{
FileLogger.Log("Found ObjectCreationExpressionSyntax for object creation.");
}
}
else
{
FileLogger.Log("No ObjectCreationExpressionSyntax found at cursor position.");
}
var documentLines = _documentContents[(Uri)request.TextDocument.Uri];
if (documentLines == null) FileLogger.Log("Doc is null???");
var documentText = string.Join(Environment.NewLine, documentLines);
FileLogger.Log("Docuemnt text: " + documentText);
var syntaxTree = CSharpSyntaxTree.ParseText(documentText);
var root = syntaxTree.GetRoot();
var text = syntaxTree.GetText();
var absolutePosition = text.Lines.GetPosition(new Microsoft.CodeAnalysis.Text.LinePosition(request.Position.Line, request.Position.Character));
FileLogger.Log("Absolute Position: " + absolutePosition);
var tokenAtCursor = root.FindToken(absolutePosition);
// Trace up the syntax tree to find an ObjectCreationExpressionSyntax
var objectCreation = tokenAtCursor.Parent.AncestorsAndSelf()
.OfType<ObjectCreationExpressionSyntax>()
.FirstOrDefault();
if (objectCreation != null)
{
// Check if the cursor is within the initializer span
if (objectCreation.Initializer != null &&
absolutePosition >= objectCreation.Initializer.SpanStart &&
absolutePosition <= objectCreation.Initializer.Span.End)
{
FileLogger.Log("Cursor inside ObjectCreationExpressionSyntax initializer.");
}
else
{
FileLogger.Log("Found ObjectCreationExpressionSyntax for object creation.");
}
}
else
{
FileLogger.Log("No ObjectCreationExpressionSyntax found at cursor position.");
}