How to invoke the Visual Studio Code Markdown renderer - visual-studio-code

The Visual Studio Code Markdown preview uses markdown-it which supports a plugin ecosystem.
Extension of this rendering pipeline is documented, but this does not consider the possibility of clients other than the markdown preview pane.
How can I use the built-in Markdown rendering?
My use case, if it helps, is printing. I wrote a printing extension for Visual Studio Code and a natural enhancement of that was rendering Markdown when it prints.
At the moment I'm effectively recreating the rendering pipeline. Apart from the obvious redundancy, the reason I'd like to use the built-in rendering is to inherit any configured extensions, so that the print out matches the preview in capability.
It seems reasonable to expect the Markdown preview pane to be implemented as a virtual document which is a client of the rendering pipeline. What repository and file(s) contain the implementation of the Markdown preview pane?

It appears there is a MarkdownEngine class that manages the loading of plugins according to configuration, and while there doesn't seem to be a way to reference the MarkdownIt instance, there is a render method defined here
https://github.com/Microsoft/vscode/blob/fa5306d67bb934c42d206fb3c7e028dff00d530f/extensions/markdown-language-features/src/markdownEngine.ts#L96
So on the face of it, all you need to do is import MarkdownEngine and use this method. However, this is not currently supported. I have logged a feature request.
The authors do not want to expose MarkdownEngine, but they proposed providing a render method.
That's the ultimate answer, but it isn't of any help right now. In the interim, it is possible to obtain a reference to Visual Studio Code's markdownIt instance.
Change your extension to masquerade as a Markdown plugin. Notice that in the documentation for adding plugins it says:
Then, in the extension's main activation function, return an object with a function named extendMarkdownIt. This function takes the current MarkdownIt instance and must return a new MarkdownIt instance:
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
return {
extendMarkdownIt(md: any) {
return md.use(require('markdown-it-emoji'));
}
};
}
This is your chance to capture the Markdown renderer. Modify your extension to present as a Markdown plugin:
"contributes": {
"markdown.markdownItPlugins": true,
Give it a private property to hold a reference to the MarkdownIt instance. Don't bother to strong type it. That would require you to bundle the MarkdownIt library.
var md: any;
Then capture a reference by putting this at the end of your activate method.
return { extendMarkdownIt(mdparam: any) { return md = mdparam; } };
When the pipeline initialises, it will invoke the callback you have provided passing a reference to itself. The rest of your code can get it from the property.
This trick depends on the rendering pipeline loading early irrespective of whether you use the Markdown preview pane. Happily it does.

Related

VS Code Conditional Snippets

I am looking to have some snippets listed conditionally in the VS Code IntelliSense for a particular language. I am considering creating an extension, intercepting the list of CompletionItems, and removing any snippets that do not satisfy my conditions based on languageId and settings/configuration I will contribute with the extension. I am looking for the simplest solution to accomplish this, but if I need a Language Server then so be it. If I can dynamically load a snippets file for a particular languageId then that would be even better. I just need a starting point from someone more familiar with the API. I haven't even found how to retrieve CompletionList to start the intercept -- I searched the API doc but found nothing but its object definition.
As of VSCode 1.14, you cannot have conditional user snippets but an extension can contribute snippets conditionally. Try looking into creating a CompletionItemProvider that returns completion items with their kind set to CompletionItemKind.Snippet.
The JsDocCompletionProvider in the VSCode codebase is one example of this pattern. It only returns jsdoc snippets when the area around the cursor matches a regular expression

Configure Visual Studio Code suggestions

What's the best way to identify which Visual Studio Code setting is generating / allowing various suggestions to pop up (so it can be turned off)? In particular I'd like to eliminate these three from ever showing.
Those suggestions are types from the standard library. The TypeScript service that powers VS Code's JavaScript and TypeScript language features loads these types from .d.ts files in order to understand the signatures of standard JavaScript library functions such as parseInt or Promise.
To find out where a type is coming from, try using workspace symbol search (cmdT):
In this case, these types come from the standard lib.d.ts file that TypeScript loads automatically. TypeScript will also automatically load a d.ts file for the DOM api.
To disable these suggestions, create a jsconfig.json at the root of your project with the contents:
{
"compilerOptions": {
"lib": []
}
}
This tells typescript not to include any extra typings files for the core libraries. You can also select which typings you want to include:
{
"compilerOptions": {
"lib": [
"es2015"
]
}
}
See the documentation for a list of valid lib options
If you notice any bugs with this behavior or have any suggestions on how this could be improved, please file an issue against VS Code
Update
To discover where a type suggestion is coming from, you may also be able to write:
/**
* #type {AsyncResultObjectCallback}
*/
var placeholer;
And then run go to type definition on placeholder. Even using "lib": [], you may still be seeing suggestions from #types files or node packages that include d.ts files

How VSCode Handle Conflicting Intellisense extensions on hover

1) How VS Code handles a conflict, in our case we want to add some intellisense with hover in the json file. But in some cases, there could be two separate extensions extending the same type of file or conflict with intellisense provided by VS Code itself. In such cases how does the editor decide which intellisense to show?
2) In Vscode snippets extensions can we give regular expression in prefix to show the auto completion snippet in the specific scope?
example: In the below given JSON file, I want xyz to be shown in auto completion only if parent node is abc. Is there a way to define prefix or scope in snippet file for achieving this?
{
abc :
{
xyz : 123
}
}
For your first question, read the Extension API documentation, particularly the vscode.languages.RegisterXXX() methods. The rules for handling multiple providers is documented for each provider type.

Is there any way of expanding Visual studio Intellisense for .cs files?

I was wondering if it is possible to expand Visual Studio Intellisense for displaying custom statement completition in .cs files. I.e. I have found some examples of using xsd schemas to expand intellisense but they are for custom xml files if I'm not mistaken. What I would like to do is add some additional "options" to intellisense by adding just a string that will appear. Something like "MyCustomIntellisenseOption" and when I start writing I will get it in intellisense. I have found a similar solution here, but although it works exactly like I want it with a slight modification where I changed:
[Export(typeof(IVsTextViewCreationListener))]
[Name("token completion handler")]
[ContentType("plaintext")]
[TextViewRole(PredefinedTextViewRoles.Editable)]
internal class TestCompletionHandlerProvider : IVsTextViewCreationListener
to:
[Export(typeof(IVsTextViewCreationListener))]
[Name("token completion handler")]
[ContentType("CSharp")]
[TextViewRole(PredefinedTextViewRoles.Editable)]
internal class TestCompletionHandlerProvider : IVsTextViewCreationListener
But that gives me another problem.
If I have two strings "AddSomething.SomethingElse.Something1" and "AddSomething.SomethingElse.Something2" whenever I try to type the second string I come to the .(dot) and autocomplete kicks in and selects first option. Is it possible to override the dot? I figured that it has something to do with Token triggers but it seems I'm not able to find a solution for this.
The main idea is that those custom strings work like intellisense for classes but with predefined fixed strings.
Is there any other possible solution for adding custom intellisense and autocomplete option? Or a solution for my .(dot) problem?
Thank you in advance.

Eclipse automatic create new functions/methods

At work we're currently using an IDE called PHPEdit, however we're looking to move to another primary IDE, we've been looking at Aptana Studio 3 based off eclipse.
A very nice feature of PHPedit was you could create new methods by clicking a little tool tip under new methods.
For example you could type
$data = $this->model->getData();
and if the function getData() didn't exist you could click the word "getData" and get a little option to create the method, then it would automatically create it in the relevant model and if you passed any params through it like $var, $var, then it would auto set them up as well.
I was wondering if such feature is available or if anybody knows of one as I'm not overly sure what to be searching for in any documentation as I don't know what this is actually called.
Many thanks!
AFAIK eclipse PDT does not have exactly what you want.
You can have a look at Linux programming editors and meybe even checkout Jetbrains
While you are at it, have a look at this question
Eclipse already does that.
I just typed following code in my open editor.
Intent intent = new Intent()
// some code to init intent
String data = getData(intent);
And of course it cried that getData() does not exist. When hovered with mouse, it gives options to create getData(Intent). And when I choose to create this method, it gives following:
protected String getData(Intent intent) {
// TODO Auto-generated method stub
return null;
}