Roslyn - Navigate to SourceText in a Visual Studio extension - roslyn

When using Roslyn in the context of a Visual Studio extension, every SyntaxNode object has a location defined in it (type Microsoft.CodeAnalysis.Location).
Is it somehow possible to let Visual Studio navigate to a specific location in the UI?
The EditorFeatures package has some extension methods to retrieve the TextBuffer and Snapshot from the location, but it's still quite some manual work to open the document when needed, etcetera. I have the feeling there is somewhere a method that can do that for me.
https://github.com/dotnet/roslyn/blob/main/src/EditorFeatures/Text/Extensions.cs

So if you have a symbol in hand and you just want to do the equivalent of go to definition, you could call this method after MEF importing VisualStudioWorkspace. Otherwise, it doesn't look like we have a public helper in Roslyn anywhere. (We've absolutely got the implementation internally, of course...)

Related

Get intelliSense info inside VS extension

I'm trying to write an extension to IntelliSense in Visual Studio 2015.
Right now you can write code something like
constexpr auto res = offset(ClassName, fieldName);
And when you put your mouse over the res variable, VS IntelliSense will show you the result of the method, e.g. offset of the field fieldName inside ClassName class.
Is there a way to:
get information about the offset from the IntelliSense or
evaluate custom code on the selected text from VS extension?
Is there a way to:
get information about the offset from the IntelliSense or
evaluate custom code on the selected text from VS extension?
Unfortunately, the C++ Intellisense feature doesn't allow for something like this. If you were working with the Roslyn based C# or VB.NET compilers, the story is a bit different, as you can access the same pipeline the C# or VB.Net Intellisense features use. But C++ Intellisense is an entirely different beast

Is there a shortcut to automatically include a class in visual studio 2010?

Lastly I've used a lot Java with Eclipse and got used to the ctrl+alt+o shortcut to automatically add imports.
Now that I'm back on C++ coding with Visual Studio 2010, I'm a bit annoyed not having such shortcut (or I may have missed it).
This must be possible as Intellisense detects the class I want to use and shows me all the possibilities when I declare a new variable, it must know where are the headers needed.
The functionality seems to exist in a Visual Studio plugin: Visual Assist X.
EDIT:
Regarding to this post, the feature seems to be missing in VS2010. That was exactly the feature that I was looking for: automatically add the #include <set> when somewhere in the code I write std::set<int> myset;, or with any custom class.
When your caret is on the class name itself you can press ctrl + . (that's a period) This will bring up a list of potential import matches. Select the one you want and press enter.
This only works if the assembly containing the class is referenced in the project itself.
Context menu key -> resolve -> choose correct class.

how do you get c++ Intellisense data in a visual Studio Extention?

I am making a Visual Studio c++ 2010 extension. I was wondering if there was some way to get intellisense data. like, perhapse the scope of where the caret is. class member variable and function names, function parameters ect.
all I've found atm is Microsoft.VisualStudio.Language.Intellisense namespace, but it doesn't seem to give low level direct access to the data? seems more like a bunch of filtering functions?
I basically want to do my own auto complete, but I don't want to have to parse all the code when intellisense is doing a good job of that already...
EDIT:
ok, so the 2010 intellisense data base file is just an sql database file now, I opened it with sql server 2008 express, and had a look through, it seems to have all the types of data I'm looking for, and I should hopefully be able to access it through code.
the only thing is, I would much rather access the in memory database loaded (and presumably, constantly updated) by intellisense, so that I get all the updates in real time, and don't have to load stuff off the disk.
so, anyone know how to access the database loaded by intellisense from an extension?
I don't have the direct answer to access intellisense database. But, I remember Alvin Chardon wrote a nice article about data flow diagram of VC++ intellisense. The link (http://msdn.microsoft.com/en-us/library/ms379615%28v=vs.80%29.aspx) may be useful to this question.

Implementing a custom wizard for Visual Studio for custom C++ classes

in order to make new C++ classes conform to some rather picky coding conventions (upfront: I am not in the position to discuss these...), I was thinking about a way of generating stubs for new C++ classes. Currently, everyone is doing copy-paste, regularly missing some detail. The IDE in use is MS Visual Studio 2005, but I think there has not been much of a change in 2008 and 2010 regarding these topics.
My first idea was to implement a command line script to do this, which would be fairly straight forward to do. Alternatively, I thought about using a default VS extension mechanism for better IDE integration. So, this would be hooking in some custom stuff when selecting Add->New Item... on a filter (Solution Explorer).
After some investigation, I found out there is an easy-to-use templating mechanism, which unfortunately does not work for C++ (http://msdn.microsoft.com/en-us/library/6db0hwky%28v=vs.80%29.aspx). For C++, it seems like you have to implement a custom wizard instead, incorporating html for the layout and javascript for the logic.
Regarding the custom wizard approach, I've come to the conclusion that this would require some effort (at least for me) to get this done. MSDN is not very detailed on this topic. I've found some walkthroughs in the web, which are dealing with custom wizards for projects only (Add->New Project... instead of Add->New Item...).
So, here's the qn: Anyone having done this or something similar? Is it (better IDE integration) worth the effort (coping with the details of custom wizard implementation), or would you suggest the go for the command line tool instead? More than two days of work would not pay off in the current project, I guess.
If you think custom wizards are great, maybe you can give some hints to get started. Also, maybe there are alternatives I did not come up with. VS Add-Ins seemed to be over the top for this, and adapting e.g. VC\vcprojectitems\newc++file.cpp will not do the job.
Thanks in advance and best regards...
Jerb
I have done something similar using a custom wizard.
For my purposes I just wanted to inject some simple macros into each class to insert it into a static factory object.
I didn't need to modify the actual wizard, just copied the default generic class wizard and modified the code generation javascript.
The documentation is rubbish on the topic, but here is a good place to start for the javascript (that is where you are going to get the most power):
http://msdn.microsoft.com/en-us/library/t41260xs(v=VS.71).aspx
The default class wizard javascript is located here:
C:\Program Files\Microsoft Visual Studio
10.0\VC\VCWizards\CodeWiz\Generic\Class\Scripts\1033\default.js
Duping the Project
Duplicating this is trickier than I thought, you need to copy and paste the
Microsoft Visual Studio 10.0\VC\VCWizards\CodeWiz\Generic\
folder, then head to:
C:\Program Files\Microsoft Visual Studio 10.0\VC\VCAddClass\Generic
Dupe this folder and modify the Generic.vsdir inside it to point to a dupe of ..\Simple.vsz (a file in the VCAddClass folder).
The Simple.vsz file points back to the location of the "Generic" folder in VCWizards you duped at the start, so point your new simple.vsz at that.
Code Generation
As for the actual code generation, its not all that difficult to pick up. To get things started faster, here is the General way the default.js works:
Once the wizard is finished, the code gen method kicks off from:
function OnFinish(selProj, selObj)
selProj is (as far as I can tell) an instance of EnvDTE.Project
http://msdn.microsoft.com/en-us/library/envdte.project.aspx
Getting information from the wizard seems to be based around:
wizard.FindSymbol("CLASS_NAME")
The real magic starts to happen on the selProj.CodeModel object
oCM.AddClass(strClassName, strHeader, vsCMAddPositionEnd, "", "", vsCMAccessDefault);
It seems the convention for these methods to add anything to a file simply modifies the file as a single action, as it requires the file path as its paramter.
This returns a CodeClass instance and can be added to by its methods like:
AddAttribute
AddFunction
...
This is quite restricting if you are looking for very strict code formatting (or in my case inserting macros that don't fit normal code syntax.
The simple way around this is to just build a string on your own for the parts that you need full control over using the EditPoint interface.
An EditPoint is a location inside a code file to which you can call methods like:
EditPoint.Insert(string)
Editpoint.InsertFromFile(path)
To get an EditPoint in a location where you would want to insert code, simply use the location of one of the existing items in the code gen file (like class or constructor) and get a TextPoint using .StartPointOf or .EndPointOf and manipulating the parameters.
Once you have a TextPoint you can create an EditPoint like so:
newclass.EndPointOf(vsCMPartBody).CreateEditPoint().Insert("\nprivate:\n REGISTER_TYPE_MEMBER("+strClassName+");\n");
To get a TextPoint inside the .cpp file instead:
oConstructor.StartPointOf(vsCMPartWhole,vsCMWhereDefinition).CreateEditPoint().Insert("REGISTER_TYPE_BODY_ID("+strClassName+",REPLACE_ID);\n\n\n");
This gives you the power to do anything you want via JScript string manipulation as long as you can find the input data you need via the wizard (which I have not yet delved into)

Is there a way to prevent a "keyword" from being syntax highlited in MS Visual Studio

MS Visual Studio editor highlights some non-keyword identifiers as keywords
in C++ files. Particularly "event" and "array" are treated as keywords.
That's very annoying to me, because they are not C++ keywords.
I know how to add my own keywords to the list of syntax-highlighted identifiers,
but how to remove existing built-in ones?
I'm aware that this may require patching some executable files.
So does anyone know how to do this?
Thanks to article mentioned by Steve Guidi, I was able to find executable file that contains Colorizer and IScanner classes. It is named vcpkg.dll and located in /Microsoft Visual Studio 8/VC/vcpackages. (I'm using Visual C++ 2005 Express Edition, things may be different in other versions.)
The vcpkg.dll contains null-terminated UTF-16 encoded strings. I've opened it with hex editor, and searched for "array". There is only one such string in the file, so I've replaced it with "arrry". (It is important to maintain relative alphabetical order with respect to other keywords.) Then I've searched for "event", it shows up in several places, but there is only one that isn't part of some longer string, so I've replaced this one with "evvvt". After starting Visual Studio, it turned out that "array" and "event" weren't any longer highlighted, but "arrry" and "evvvt" were!
Of course this is an ugly hack, and it will void your warranty,
and probably goes against Microsoft EULA, but what a relief for the eyes!
Anyway, if you want to do it, be careful and remember to backup the file.
It doesn't look like a disable-syntax-coloring feature is exposed in a user-friendly way.
The only way I can think of selectively disabling syntax coloring is to create a new syntax coloring plugin for the IDE, and list all of the keywords you want colored. Microsoft gives information in this article on how to accomplish this task.
The drawback to this approach is that your IDE will now have two C++ languages and I'm not sure how it will select which plug-in to choose from once it loads a .h or .cpp file. However, this article suggests that you can override the existing C++ plug-ins by rewriting some registry keys.
I think the only "semi-practical" way to accomplish this to create a Visual Studio package that uses Text Markers to selectively cover up the keywords you don't want colored. Even that is not a little one-day task. Edit: Probably not even a full week task for someone not intricately familiar with the Visual Studio API and all its quirks, especially not getting it bug-free.
In other words, you probably want to just ignore them.