How to get Csharp syntax tree from VS Code Extension? - visual-studio-code

Is it possible to interact with the CSharp syntax tree from my own VS code extension? I have an extension which I made in Visual Studio proper which uses the Roslyn Syntax Tree to do a variety of code editing and sorting tasks and I want to migrate it to VS Code.
My naive assumption is that if I can get access to the OmnisharpServer object by reaching into the Omnisharp VS Code Extension I might be able to pull the relevant information out of there. I can get access to the extension itself by calling:
let csextension = vscode.extensions.getExtension('ms-vscode.csharp')
But the extension itself doesn't expose much so I'm wondering if this is the correct methodology? If so, how do I get access to the running server from my own extension?
The only other strategy I have is to run a .net core exe which uses Roslyn to do the analysis, but this seems to be duplicating a problem which must be solved somewhere in the stack.

I don't think there's any way to do this without going through the Omnisharp VSCode Extension / its extension API (unless perhaps the server implements some custom interface). If an API doesn't already exist, you could consider creating a feature request on the issue tracker?

Related

Robot Framework Language Server Extension automatically opening CANoe application

I use Visual Studio Code with Robot Framework and Python for test purposes. I also use CANoe application as a simulator.
I installed the Robot Framework Language Server and for some reason when I open VSCode or even just click on it CANoe automatically opens, I know that the extension is causing this because if I disable the extension the application stops to automatically open.
Do you already had any similar behavior with Robot Framework Language Server and any other process? If yes, how did you solve it?
There are 2 different LSP implementations for robot framework. This answer relates mainly to the one Robocorp maintains.
That particular implementation of lsp works by executing each LIBRARY settings section in python interpreter and executing the actual library's constructor to get the data it needs for auto completion / typing / whatnot. If the python library makes any connection to com API thats associated with CANoe during execution of the constructor, it will trigger actual CANoe application to start - even if that's not mandatory at this point but that's how COM works.
I'm quite there are ways to hack around the issue but essentially, the easiest way to fix this is to;
Fix the library that provides your keywords that interact with CANoe and/or ecutest or similar that requires CANoe to be running.
Try switching to https://github.com/d-biehl/robotcode within vsc to provide you autocomplete and the rest ..
If you where using robotcode lsp, see first suggestion.

Is it possible to start a React application from within a Visual Studio Code extension?

See question. I am working within a Visual Studio Code extension that uses the Webview API. I've been searching for any method but can't seem to find one.
Yes, that should be possible. After generating the production build you can load the main page into a Webview and it should work just like in a browser. You have to ship the production build with your extension, of course.

Code analysis using visual studio extension

Is it possible to write an extension which will do code analysis? I looked at the api and I didnt see how I can get all variables that are declared in the document/xref on specific variable and get it flow in the code. If someone know api in visual studio code which actually helps with it, it will be great.
Creating such an extension is certainly possible, however you have to do your own language processing. AFAIK there's no way to access the internal structures (syntax tree, symbol table etc.) of the existing JS/TS (or other language for that matter) extensions.

Can I create VS Code extensions in Python/C++?

I am totally new to creating extensions in VS Code, and all the official examples of extensions are written in Typescript/Javascript, which I have no experience with. Is it possible to create VS Code extensions in other languages, such as Python or C++?
If so, could anyone point me to any resources to get me started?
It is possible by creating a C++ module for Node.js, which can then be loaded like any other node module. Of course, some glue code written in JS or TS is necessary to register the extension and translate calls to/from vscode.
I've gone this way in my ANTLR4 extension, but gave up eventually, because of the troubles I had due to incompatible dependencies (you have to make sure the extension uses the exact same V8 version, which was used to build the underlying Node.js used by vscode, on all supported platforms).
This situation might have change, I don't know, but with that in the background I don't recommend it.
If you want to add support for a new language in vscode you can also write a separate language server, as is mentioned in the linked SO answer. For other type of work, I'm afraid, you have no alternative to use.
No, as #rioV8 said, since VSCode is an electron app and runs on Javascript.

Evaluating Environment Variables in VS Code Extension

I'm developing an extension in VS Code to add language support for OpenSCAD (Script-based 3D modeling program). Currently, I have been working on a way to open / preview a file in OpenSCAD from VS Code, which I have been able to do successfully using my own preview manager.
My issue is that I want to add configurable naming formats when exporting an OpenSCAD file that use environment variables similar to those used in the tasks.json file. More info can be found here: https://code.visualstudio.com/docs/editor/variables-reference. As an example, taking the file test.scad and the export configuration ${fileBasenameNoExtension}.stl would export to the file test.stl.
Additionally, I want to add a custom variable, ${#} that would evaluate after all other variables as a unique version number to avoid duplicate exported files. Using the example file: test.scad and the export configuration, ${fileBasenameNoExtension}_${#}.stl, the extension would export to the file test_1.stl for the first time. Then, seeing that test_1.stl exists, it would export to test_2.stl, and so on. I implement similar functionality in all of my exporting utilities, so it is important I can implement it here.
Now that the intro is done, on to the actual question: To anyone who knows more about the VS Code API than I do, in order to best get the functionality described above, should I implement environment variable evaluation into my custom preview manager or reimplement the preview manager I have using tasks? Because I have already implemented my own preview manager that I am happy with, I would prefer to do the former. However, I have been unable to find any functions in the VS Code API that will evaluate the environment variables in a string. Is there a typescript function to evaluate environment variables in the API that I have missed?
If re-implementing this functionality using tasks is a better way to achieve my goal, would I have to sacrifice the control I have in my preview manager, such as being able to selectively kill open previews and dispalying active exports?
Or, is there a compromise that could use all of the power from tasks without losing any functionality I've already developed?
Link to branch of my extension's repository: https://github.com/Antyos/vscode-openscad/tree/PreviewModel