Parse and rewrite Visual Studio C# source code document character by character - c#

I've been working on learning Roslyn and have made great progress with using the CSharpSyntaxRewriter mechanism to work on code. My goal in the end is to have a customizable coding standards enforcer. As such, I started with Microsoft's code formatter project from https://github.com/dotnet/codeformatter/releases. Right now what I'm working on is formatting white space.
The goal here is to learn Roslyn, so I want to write the code to format all of the whitespace instead of using the Formatter built into Visual Studio and Roslyn.
Formatter.FormatAsync(document, cancellationToken: cancellationToken);
I'm able to parse syntax trees and know I can implement the code necessary to do this using the CSharpSyntaxRewriter, but what I'd like to do is somehow simply get the raw source text for the code, do whatever manipulations are necessary character by character in the source file, and then put the new text back as the document.
However, I cannot figure out if it's even possible to do what I'm trying to do with Roslyn/Visual Studio. I would have thought so, since the source is simply a text file that's already loaded into Visual Studio, and the 'document' object can be acquired for it.
Edit
The optimum solution would be a drop down (or context) menu for C# source files that then ran all modifications on the file. I was unable to link MEF/MPF to any sort of hook that would allow whole-scale modifications of a source file. As such, I went to a console application.
From what I understand, MEF/MPF can provide single entry points to the source file, whereas the Roslyn interface allows simultaneous access to the entire source file at one time. I believe that's why Microsoft used Roslyn in order to implement their coding standards enforcer.
As I said, I've only been looking at Roslyn for about a month, so I may be missing something obvious.

Related

Searching and navigating through code in visual studio

I work on reality big project. And sometimes i get the need to search for some specific keyword inside A single c# file that has many calls of other functions from other c# file.
So i want to know if there is any easy way that could search for give phrase or keyword inside the current file and inside all the functions that my current file calls to. But not in the entire solution or inside the whole project.
For inside the file that's Ctrl+F.
Otherwise Code Search in VS2022 is very fast. Normally it's bound to Ctrl+T.
Introducing a New Way to Search Your Code and Visual Studio Features
Code search in Visual Studio 2022 is about to get much faster).
There also are the Go To options in the context menu that could help:
Finally, at the top of the editor window you can switch between classes and properties/methods.
BTW. You can address the large solution problem by splitting the codbase into multiple smaller solutions that only include parts of the overall code. This has some drawbacks, but overall it works very well in my experience.

How to change formatting options based on solution

My boss set our IDEs up to use non-standard formatting. For example, it adds spaces between the parenthesis and parameters. This is fine for the new stuff that we've started from scratch, but it's super annoying when I go into existing projects and try to format the code with Ctrl-k-d because when I go to compare the file with the latest version before checking in my changes it looks like I've changed almost every single line in the file and it's difficult to pick out the actual changes.
I would like to be able to use his settings in the newer development, but switch to the default settings when working with other projects. Is there a way to accomplish this without having to go into the settings and uncheck/recheck all the appropriate options every time?
EDIT: I understand it might be good to reformat all legacy code to use the new standards. Unfortunately, that's not my decision to make. And even so, I sometimes work from my personal computer which I also use on occasion to do contract work for other companies. I obviously can't reformat their code to fit my bosses standards.
You can easily switch between settings, by creating separate .settings files and using a VSIX package to switch between them by loading them into Visual Studio - I use this for switching between Dual and Single monitor settings via menu items in the Tools menu.
Using an extension is annoyingly complex for such a simple operation, and this was a whole lot easier when Visual Studio supported macros. However I've uploaded my homebrew extension to GitHub in case you want to have a look.
The tricky bit is editing the settings files to only contain the ones you're interested in, which I expect are the ones in Text Editor\C#\Formatting\Spacing.
The Rebracer extension stores code formatting options alongside each solution and when you open a solution, Rebracer will automatically apply that solution's settings.

ANTLR Syntax Highlighting DSL in Visual Studio

I have an ANTLR grammar that defines a DSL (domain specific language). This grammar is relatively simple. It is parsing the language and outputting C code to create a very basic translator.
This language is meant to be used in C# application (typed into some sort of control, whether it be RichTextBox or a custom control) and one requirement is to have syntax highlighting for this language. I have scoured the Internet in hopes of finding some sort of information on how to accomplish this, or find a tool to make this a little easier on myself.
After not finding too much information, my best assumption would be that I need to use the ANTLR generated lexer to look at the tokens and color them accordingly. Is this the correct path of action, or is there some other method/tool to provide syntax highlighting for custom domain specific languages? If this is the correct method, how do I go about recognizing specific tokens?
If I left out any important information, please ask! Thanks!
I successfully used AvalonEdit for a similar project of mine. I just created a small editor with the correct syntax highlighting.
It is very easy and quick to get it up and running in your project. You just have to provide it with a simple XML file to document the syntax of your DSL and you will have a colored output out-of-the-box as a WPF control.
It looks like they added completion facilities since I used it, I don't have experience on that part though, but I suspect it is also very well done if the quality is the same as the colouring.
This language is meant to be used in C# application (typed into some sort of control, whether it be RichTextBox or a custom control) and one requirement is to have syntax highlighting for this language.
Consider using Scintilla for your control. It's a text control for IDE-style text editing. Notepad++ uses it for its text control, as does the SciTE IDE from which it originates. I've used it in a small, custom IDE project written in C# using an unofficial .NET-specific version -- I think it was ScintillaNET.
Scintilla supports custom keyword highlighting and also a variety of programmable features like squiggly-line underlining and things like that.
If you have a control that you'd rather use, I think it's reasonable to use a small ANTLR lexer to produce tokens for you. Each token contains the line number, starting character position, source text, and token type -- everything you'd need to know what to highlight and how. The only hassle would be running the text through the lexer each time the text is changed. There are efficient ways to do that without re-lexing the entire document, but it's still something to keep in mind.

Translating comments and region names in source code

Does anyone know of a batch processor or a VS 2010 plugin/script that would let me translate comments and region names from Chinese into English?
The only ones I've found either process all strings or only one string at a time.
I have two large C# projects that I am trying to read through.
Thanks.
Use PrepTags to prepare your file for translation. It will allow you to select the text to be translated based on regex.
www.preptags.com
You can work file by file for free, or process the files as batch using the pro version (€39)
In your case, it's pretty simple to prepare. You just mark everything as protected, then unprotect the content of the comments & region names.
Disclosure: I develop PrepTags.
As was noted, you can use Google Translate API or alternatively Bing Translator API. You can detect comments and regions in your files using System.CodeDom.
I'm not too sure if this is possible. What you can do to help would be the following:
1) Make sure that both C# projects have the Properties > Build > Xml document file check box checked.
2.1) Write an application that reads in the generated xml file.
2.2) Parse the file, and for each value make a call to Google Translate to get the translated value.
2.3) Place the translated value within another xml file that has the same structure as the one created from building the project.
This wouldn't solve the your desire to translate the region names, but its a start. At least you would have intelligence when using the two projects.
This is actually a good idea for a small open source project. I may decide to pick it up. If I do, I'll let you know.

Idea for extending C# syntax

C# unfortunately does not allow for extra user-defined syntax. But I was wondering whether it was possible to surpass this limitation by tapping into the visual studio onbuild-event.
Suppose I have some syntactic sugar which could be easily translated into actual C# code.
If I were to automatically translate a cs document containing this new syntax into a valid cs document, right before a C#-project is built, then the project could build succesfully. Overall this would function as if I had extended the C# language, because I started with an invalid cs document containing unoffical syntax, but it compiled anyway.
I realize that this has a few problems, such as that this translation is permanent. This could perhaps be circumvented by restoring the original cs(which should be restored after the debugging has ended, otherwise some IDE functionality would be lost). But these are secondary problems.
Please let me know what you think of this idea. Is this possible, and if so, could someone direct me to some useful tutorials so achieve this? In specific the tapping-into-a-onbuild-event.
I've searched MSDN, but the topic(http://msdn.microsoft.com/en-us/library/hthab0h8.aspx) didn't help me.
I won't say whether this is a good idea or not, since I don't know enough about what you're trying to do. I would suggest this, though: What you're proposing is to have some kind of "extended C#" source code file that gets translated into regular cs during the build process.
Personally, I would clarify this by first breaking away from the idea that you are "extending" the C# language; I would instead think of it as defining a new language that happens to be syntactically similar to C# (I assume). Use a different file extension so that Visual Studio does not try to compile your language as C#. (Maybe .csx? People love adding the letter x, right?)
Visual Studio already does this sort of thing in other ways that might not be quite so obvious. If you add a resource file to a project, Visual Studio will typically also include a dynamically generated "designer.cs" with code generated based on the content of your .resx file. If you look at the properties of the .resx file, you'll note that the "Custom Tool" property has a value of "ResXFileCodeGenerator". In theory you should be able to implement your own generator to perform the translation step that you mentioned. In fact, this translation does not have to be a one-time thing as you said. The translation process should generate a new file, but leave the original file intact. Any changes to the original file causes Visual Studio to regenerate the auto-generated file.
I've not tried to implement a custom generator myself, but I think these articles are relevant: Implementing Single File Generators, and Registering Single File Generators
Your .csproj file would contain something such as the following:
<Content Include="Example.csx">
<Generator>ExtendedCSharpCodeGenerator</Generator>
<LastGenOutput>Example.cs</LastGenOutput>
</Content>
<Compile Include="Example.cs">
<AutoGen>True</AutoGen>
<DesignTime>True</DesignTime>
<DependentUpon>Example.csx</DependentUpon>
</Compile>
Where Example.csx is the source code file containing your extended syntax and Example.cs is the resulting output of translating Example.csx into normal C# code.
What you are talking about doing seems like a perfect task for T4 templates in Visual Studio.
http://msdn.microsoft.com/en-us/library/bb126445.aspx
You can define anything you'd like; text files with a certain format, UML models, a database; and your T4 template can transform it into code in what ever way you wish.
I'm not sure it's a good idea, but I just had an idea: maybe you can have a look at Extending Visual Studio, download the SDK and check the doc. Maybe it would be possible to do what you are trying to achieve.

Categories