Development of simple C# application for understanding Resharper Open APIs - c#

I wanted to start developing plugins for Resharper using its Open API.
As there is very less documentation available, for starters I wanted to develop a simple C# application to extract the class details (eg. class name, number of methods in a class etc), so that I can understand the working of Resharper library and the in-built functions it provides.
It would be ideal if I can extract these details and print it on the console, for ease of testing.
Thanks.

in terms of documentation, there is some info available on the Developer Wiki, with more to come. You might also want to check out the ReSharper Extensibility Guide, which is a bit oudated but still relevant.
As for your question of how to examine the file structure - guess what - you don't even need the API! Here's what you need to do:
Start Visual Studio using the /ReSharper.Internal key. This will enhance the ReSharper top-level menu with an Internal section.
Pick ReSharper-->Internal-->PSI Viewer. This will pop up a form similar to the following:
Now, click the Create button, which will let you create a file, statement, expression or member declaration.
The bottom pane will then illustrate the parsed structure and let you navigate it.
Good luck!

If you want to parse the code yourself, that's a bit more complicated. The following is a rough outline of how you could go about parsing a file.
var buffer = new StringBuffer(sourceCode);
var lexer = LexerFactory.CreateLexer(buffer);
var defines = EmptyArray<PreProcessingDirective>.Instance; // assume there are none
var parser = new CSharpParser(lexer, defines);
var file = parser.ParseFile() as CSharpFile;
Now you can use file to navigate the code structure.
BTW, I strongly recommend using F#. It's made for this sort of thing.

Related

Access IVsFontAndColorDefaults to read complete existing VS theme information

I am new at working on a Visual Studio extension. I would like to access the complete font and color information for the current theme using C# I want to access this as a AllColorableItemInfo structure as this structure includes more information (e.g. flags and the localized name) for the each item. I believe that I need to use IVsFontAndColorDefaults interface to do this but I don't know how to get this. This is probably easy but I cannot figure out how to do it. I very much appreciate any help.
I think that you are probably on the wrong track IVsFontAndColorDefaults. The documentation seems to indicate, that this is an interface a Visual Studio Package would implement, rather than use itself.
I have some code which accesses the colors used in Visual Studio. It is based almost exactly on this article by Carlos Quintero. The code is in VB.NET, but it would be easy to port to C#.
Alternatively, you might find what you are looking for in the interface IVsFontAndColorStorage.
You can get that interface with code like
IVsFontAndColorStorage fontAndColorStorage =
GetService(typeof(SVsFontAndColorStorage)) as IVsFontAndColorStorage;
which I took from this posting this posting on Stack Overflow

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

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.

How to programmatically create custom comment tokens

I am writing a small Visual Studio addin to let the user use more features of the comment tokens (TODO, HACK, etc.). For this I want to extend Visual Studio with new commands (such as TODISCUSS, TODELETE, FIXME, TESTME, etc.). I'd like to do this programmatically when my addin starts.
I already found out how to do this manually: http://msdn.microsoft.com/en-us/library/ekwz6akh%28v=vs.80%29.aspx
Does anyone have an idea how this could be done via the API?
Any ideas or suggestions would be appreciated!
Edit :
See this MSDN Link perhaps digging around in TaskList will get you somewhere.
Original Post :
It's not very clear (to me :) what exactly you want to do. Is it: find, edit, or refactor the tokens and then do something else with the sources? I can suggest this article on Code Project (NRefactory).
Using NRefactory you can do anything and everything with the source files including locating the tokens you want and doing stuff with them. You will also know the files you found them in, line numbers etc.
Perhaps you will be able to use Nrefactory to do all the heavy lifting as far as sources are concerned.

building text editor with code completion feature

Presently I'm trying to build up a text editor for the PHP language which should have the feature of code completion, i.e. if i start to type a word a dynamically created drop down list will display all available keywords starts with my typed letters. Can any body suggest me how it can be done. Idea will be enough for me. If possible please provide me a link to such simple application build in C#.
Considering that in general it's not an easy task, so there is no some "easy" application.
Even if in general idea is not a rocket science. You need to define a dictionary of words, corresponding to some key. When you type "." (in C#) you have to pick from the dictionary all words corresponding to the key equal to the word found on left side from the "."
To do it real working applicaiton is not so easy. By the way I can recommend to have a look on MonoDevlop look on their editor.
I worked with it years ago to make a simple editor for DSL company needed, and spend not small amount of time to correctly understand intenals, integrate well "my new language", "detach" control from Mono and inject into our applicaitons, like a dockable window.
If I where you I would really try to avoid the wheel... code completion is something that most IDE's now come with so what you are after is already available...
That being said, what I would try would be to go over the PHP API and construct a Suffix tree. This type of tree usually allow for a fast way to look for a given word. Once you index the API, you would also add in any other variable the user adds while he/she is performing the actual programming.
You could kick the search automatically in your suffix tree after the user has entered the 3rd letter, or maybe provide means for the user to activate it manually, like the Ctrl-Space keyboard most IDE's (Visual Studio, Netbeans, Eclipse, etc) have.
Note that this could get tricky, since you might want to select the appropriate variable type.

VS2010 Extensibility: Custom document formatting

Good afternoon,
I've created a visual studio package that registers the Verilog language as a valid content type.
I've got syntax highlighting, outlining, smart indenting, etc all working.
However, I would like to be able to get Visual Studio to automatically format the entire document via Edit->Advanced->Format Document/Selection. Currently these options are invisible, and I expect that I have to let VS2010 know (somehow) that these methods can be called, and provide the correct methods to do this formatting.
I can't seem to find any reference to formatting in the VS2010 SDK and documentation. I was hoping that ISmartIndent would be the solution I was looking for, but it seems that this code only runs on an empty line, or when the enter key is pressed.
Does anyone have any tips or ideas on how I can solve this problem?
Thanks,
Giawa
Edit: I'm using the managed extension framework introduced with VS2010 to accomplish this. I'm writing in C# (and just added the c# tag to my question). Thanks
MEF is not the right way to accomplish the task of creating a language service. Instead, the Managed Package Framework (MPF) should be used to register the language service and perform tasks such as syntax highlighting, outlining, parsing, formatting, parsing, etc.
Since my question was about formatting, I'll cover a little bit of it in my answer. You must override the ReformatSpan method in the custom Source object that you've creating for your language service. There's a good example on the MSDN webpages for VS2005 (applicable to VS2010 as well).
You can also force formatting at any time by calling the ReformatSpan method directly. Here's a working example from my code:
Region region = service.GetRegionContainingLine((line > 0 ? line - 1 : 0));
if (region != null)
{
using (EditArray mgr = new EditArray(this, service.LastActiveTextView, true, "Reformat Brace"))
this.ReformatSpan(mgr, region.ToSpan());
}
Thanks to #James McNellis for pointing me in the correct direction.
Here you go: http://msdn.microsoft.com/en-us/library/ee197665.aspx

Categories