leviathan
leviathan
CC#
Created by leviathan on 8/14/2024 in #help
Adding a CommaToken in a code fix
I'm trying to write a code fix that just adds a comma to the end of an InitializerExpressionSyntax (which was previously flagged by an analyzer). I've followed this guide and tried adapting it to my use case, but for some reason it's not working. This is my first time working with Roslyn, and I don't know what I could be doing wrong. Here's the relevant method:
private async Task<Document> AddTrailingCommaAsync(
Document document,
InitializerExpressionSyntax initializerExpression,
CancellationToken cancellationToken
)
{
// Get the last expression
var lastExpression = initializerExpression.Expressions.Last();
// Get the last token
var lastToken = lastExpression.GetLastToken();
// Get any trailing trivia from that token
var trailingTrivia = lastToken.TrailingTrivia;
// Remove the trivia from the expression
var trimmedInitializerExpression = initializerExpression.ReplaceToken(
lastToken,
lastToken.WithTrailingTrivia(SyntaxTriviaList.Empty)
);
// Create the comma token
var commaToken = SyntaxFactory.Token(
SyntaxFactory.TriviaList(SyntaxFactory.ElasticMarker),
SyntaxKind.CommaToken,
trailingTrivia
);
// Insert the new token
var newInitializerExpression = trimmedInitializerExpression.InsertTokensAfter(
lastToken,
SyntaxFactory.TokenList(commaToken)
);
// Add a Formatter annotation
var formattedInitializerExpression = newInitializerExpression.WithAdditionalAnnotations(
Formatter.Annotation
);

var oldRoot = await document.GetSyntaxRootAsync(cancellationToken);
var newRoot = oldRoot.ReplaceNode(
initializerExpression,
formattedInitializerExpression
);

return document.WithSyntaxRoot(newRoot);
}
private async Task<Document> AddTrailingCommaAsync(
Document document,
InitializerExpressionSyntax initializerExpression,
CancellationToken cancellationToken
)
{
// Get the last expression
var lastExpression = initializerExpression.Expressions.Last();
// Get the last token
var lastToken = lastExpression.GetLastToken();
// Get any trailing trivia from that token
var trailingTrivia = lastToken.TrailingTrivia;
// Remove the trivia from the expression
var trimmedInitializerExpression = initializerExpression.ReplaceToken(
lastToken,
lastToken.WithTrailingTrivia(SyntaxTriviaList.Empty)
);
// Create the comma token
var commaToken = SyntaxFactory.Token(
SyntaxFactory.TriviaList(SyntaxFactory.ElasticMarker),
SyntaxKind.CommaToken,
trailingTrivia
);
// Insert the new token
var newInitializerExpression = trimmedInitializerExpression.InsertTokensAfter(
lastToken,
SyntaxFactory.TokenList(commaToken)
);
// Add a Formatter annotation
var formattedInitializerExpression = newInitializerExpression.WithAdditionalAnnotations(
Formatter.Annotation
);

var oldRoot = await document.GetSyntaxRootAsync(cancellationToken);
var newRoot = oldRoot.ReplaceNode(
initializerExpression,
formattedInitializerExpression
);

return document.WithSyntaxRoot(newRoot);
}
And I've also attached the whole file in case it's helpful.
4 replies
CC#
Created by leviathan on 3/18/2023 in #help
Simple 2D graphics in C#
I want to make a 2D physics engine (mostly for learning), and I need a way to visualise the simulations I run. I want something that is simple to use, lightweight and (ideally) cross-platform. I need it to provide a direct way to open a window and draw basic shapes onto a canvas (circles/ellipses, polygons), providing me with full control over aspects like colour and line width. In brief, I'm looking for something like Processing but for C#.
7 replies
CC#
Created by leviathan on 10/22/2022 in #help
Rendering and updating a colormap
I'm developing a library related to real-time image generation. To test it, I'm trying to make an app that can convert a Color[,] into an image and display it, updating at 60 fps (Color is a class with 3 float fields). Ideally, I'd want my code to be like this:
DynamicImage dynImg = new DynamicImage();
every 1/60 seconds do{
Color[,] colormap = dynImg.getImgAtTime(time);
imageDisplay.setImage(colormap.convertToImage());
}
DynamicImage dynImg = new DynamicImage();
every 1/60 seconds do{
Color[,] colormap = dynImg.getImgAtTime(time);
imageDisplay.setImage(colormap.convertToImage());
}
where imageDisplay would probably be a PictureBox in a WPF app (with which I have no experience at all). (This is of course pseudocode) Any help is greatly appreciated.
113 replies