Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Add C# Library to Monaco Editor (code completion) and limit possible libraries?

How can I add my custom C# assemblies to Monaco Editor, so the editor would recognize/complete my assembly types?

And how could I choose and limit the .net libraries that Monaco Editor can use?

Thanks in advance.

like image 842
Conan.Net Avatar asked Oct 21 '18 01:10

Conan.Net


1 Answers

No simple way. .Net is a framework that could not be easily analyzed with JS, especially if you need to read all types in binary dependencies (dlls).

So you will also need some sort of a backend engine (probably WebApi). Loading a project in Monaco could ask the Backend to read all exported types from the binary references (see Reflection) and send them to the frontend.

If you expect those things to change real time (someone creates a new class, for example), you may get into significantly more complex scenario where you will need to have both - the code in Monaco Editor and the project in the backend constantly synced (see Roslyn).

If you only need to support read + types, you can do something like:

  1. You use the https://microsoft.github.io/monaco-editor/playground.html#extending-language-services-completion-provider-example to register completion provider.
  2. You also load the project in the backend and read all references with C#.
  3. In provideCompletionItems - you get the caret position
  4. You send the file and the coordinates to the backend.
  5. The backend reads the symbol at that position, infers its type (probably with Roslyn)
  6. Tracks down and reflects the type using reflection.
  7. Sends back the members of this type.

If your Monaco editor IS NOT read only - you are also expected to sync the backend and frontend, so positions always match.

like image 178
Pavel Donchev Avatar answered Oct 28 '22 12:10

Pavel Donchev