C
C#•2mo ago
Humoroussix2799

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
No description
1 Reply
Humoroussix2799
Humoroussix2799OP•2mo ago
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();
this is the relevant code Huzzah! We Triumph!🎺 🎺 🎺 it was because the buffer was being split by \n before I passed it into the parse tree, Instead i just made the document contents a SourceText dict Before:
private readonly IDictionary<Uri, string[]> _documentContents;
private readonly IDictionary<Uri, string[]> _documentContents;
After:
private readonly IDictionary<Uri, SourceText> _documentContents;
private readonly IDictionary<Uri, SourceText> _documentContents;
and in my change/update doc handlers it was splitting by new lines to get it into one string
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.");
}
Want results from more Discord servers?
Add your server