VSCode extension for quick semantic info - visual-studio-code

I am used to point the mouse and get information about certain references in Visual studio code.
Here one example, using Javascript, I point the mouse to a function reference and I get information about the function signature.
I would like to have something similar to other files.
e.g.
Take the following example, in a less popular language
module top #(
parameter NB=4
);
logic [NB /*I would like info here */ -1:0] b;
endmodule
How can I write an extension that when I point the mouse to the parameter it shows me the the declaration in a box, preferably with the same syntax highlight as it is shown in the editor.

Now there is a pull request with a sample to vscode-extension-samples.
Basically you have to write something like this
import * as vscode from 'vscode';
class SimpleHoverProvider implements vscode.HoverProvider {
public provideHover(
document: vscode.TextDocument,
position: vscode.Position,
token: vscode.CancellationToken
): vscode.Hover | null {
return new vscode.Hover(`${location.line}: ${location.character}`);
// return null; if there is no information to show
}
}
export function activate(context: vscode.ExtensionContext) {
// Use the console to output diagnostic information (console.log) and errors (console.error)
// This line of code will only be executed once when your extension is activated
console.log('Congratulations, hover-provider-sample extension is active!');
const hoverProvider = new SimpleHoverProvider();
vscode.languages.registerHoverProvider('text', hoverProvider);
}
And define languages and activation events on package.json
{
"activationEvents": [
"onLanguage:text",
"onLanguage:report"
],
"contributes": {
"languages": [
{
"id": "text",
"extensions": [
".txt"
]
},
{
"id": "report",
"extensions": [
".rpt"
]
}
]
},
}

Related

How to pass command line arguments to web debug session in VS Code

I need to pass parameter to debug session in VS Code while programming in flutter/dart. I added the following data into launch.json as described in documentation.
{
"configurations": {
..
// Any custom environment variables to set when running the app with this
// launch config.
"env": {
"DEBUG_MODE": true
}
// Arguments to be passed to the Dart or Flutter app.
"args": [
"--dart-define",
"DEBUG_VALUE=true",
],
}
and tried to read the value so:
void main(List<String> args) {
final debugMode = String.fromEnvironment('DEBUG_MODE');
final debugValue = String.fromEnvironment('DEBUG_VALUE');
...
}
but variables are empty, and args list is also empty. So please give me advise what I did wrong?
Are you definitely using const when reading the value? For reasons I don't entirely understand, this seems to only work if the value is const:
With this in my launch.json:
"toolArgs": [
"--dart-define",
"FOO=bar",
]
And code like:
final foo1 = String.fromEnvironment('FOO');
final foo2 = const String.fromEnvironment('FOO');
print('FOO: "$foo1" "$foo2"');
The output is
flutter: FOO: "" "bar"
So if String.fromEnvironment is called in a non-const context, it doesn't seem to return the value.
Edit: Actually, in web, not using const results in an exception. But with const, it still works as expected.

Adding jar to vscode debug extension

We are having a runnable jar for the DAP(Debugger Adapater Protocol) of ABL language and we are planning to add a vscode extension for that ABL DAP.
Could you provide us some documentation or give us an idea about how to do it?
There are basically two approaches for making VS Code run your *.jar:
Declarative:
In the package.json of your extension, you can register your executable as part of the debuggers extension point. Here is the doc: https://code.visualstudio.com/api/extension-guides/debugger-extension#anatomy-of-the-package.json-of-a-debugger-extension.
And here is an example from the node.js debugger.
Basically the following attributes are available:
"runtime": "node",
"runtimeArgs": [ "--arguments_passed_to_runtime" ],
"program": "./dist/dap.js",
"args": [ "--arguments_passed_to_program" ],
The resulting command that VS Code will call is this:
node --arguments_passed_to_runtime ./dist/dap.js --arguments_passed_to_program
You do not have to make a distinction between runtime and program. You can use either "runtime" or "program" and leave out the other.
Non-declarative:
Alternatively you can use extension code to "calculate" the command that VS Code should execute when launching your Debug Adapter. See this API: https://github.com/microsoft/vscode/blob/4c3769071459718f89bd48fa3b6e806c83cf3336/src/vs/vscode.d.ts#L8797-L8816
Based on this API the following snippet shows how you could create a DebugAdapterDescriptor for your abl debugger:
// this method is called when your extension is activated
// your extension is activated the very first time the command is executed
export function activate(context: ExtensionContext) {
vscode.debug.registerDebugAdapterDescriptorFactory('abl', {
createDebugAdapterDescriptor(session: DebugSession, executable: DebugAdapterExecutable | undefined): ProviderResult<DebugAdapterDescriptor> {
return new vscode.DebugAdapterExecutable(
"/usr/bin/java",
[
join(context.extensionPath, "bin", "abl.jar")
],
{
"cwd": "some current working directory path",
"env": {
"key": "value"
}
}
);
}
});
}

VS-Code command inputs using an (example) extension

In the VSCode documentation for variable substitution, it says there is now a way of using an extension to generate a list of options. Specifically, it gives an example and then says "It is assumed that some extension provides an extension.mochaSupport.testPicker command...".
I accept there is help provided in how to get started with extensions of various kinds, but I was wondering if anyone had indeed already written something equivalent to extension.mochaSupport.testPicker?
I appreciate it's probably trivial for someone not starting from scratch, but the graphic simulation on the first page implies someone has indeed already written such an extension (to generate the graphic), so my question is "does anyone know of an extension that is documented anywhere that complements this example to select an input from a list?"
I was faced with the same issue, so I wrote a small example extension. You can find the sample code on https://github.com/GianUlli/vscode-custom-command-input-example.
The basic idea is to register a command that returns a string. Following the official VS Code guide, you start by registering the command when the extension is activated in extension.ts in the activate function:
let disposable = vscode.commands.registerCommand(
'vscode-custom-command-input-example.pickTestCase',
async () => {
return pickTestCase().catch(console.error);
}
);
context.subscriptions.push(disposable);
pickTestCase is the custom function that extracts all test cases from the workspace using a glob search.
export async function pickTestCase() {
let workingFolder = getWorkspaceFolder();
return new Promise<string[]>((resolve, reject) => {
glob('*.json', { cwd: workingFolder }, (err, files) => {
if (err) {
return reject(err);
}
resolve(files);
});
})
.then((files) => {
if (files.length > 0) {
return window.showQuickPick(files);
} else {
window.showErrorMessage('Could not find any test cases.');
return undefined;
}
})
.then((testCase) => {
return testCase;
});
}
Do not forget to register the command in package.json:
"contributes": {
"commands": [
{
"command": "vscode-custom-command-input-example.pickTestCase",
"title": "Pick a test case"
}
]
}
With those pieces, the user is now able to put ${command:vscode-custom-command-input-example.pickTestCase} in his task definitions (.vscode/tasks.json) and launch configurations (.vscode/launch.json), for example in a build task:
{
"label": "Echo example",
"command": "echo",
"type": "shell",
"args": ["${command:vscode-custom-command-input-example.pickTestCase}"],
"presentation": {
"reveal": "always"
},
"group": "build"
}

Extension API - Task Provider - Build Task example

I've built an extension for a programming language that I use and I've created hotkey shortcuts for calling the compiler executable with the currently open document's URI. I want to convert that to a build task in my extension. I have made a tasks.json file with a build task that works and catches errors and such, but it only works if I put it in the current workspace.
There are absolutely no examples of adding a build task anywhere and the API documentation for task providers is specifically for Ruby Rakefiles or something. I'm just wanting to make a shell executable build task with a problem matcher. Can anyone give me an example of that?
Here's a minimal TaskProvider implementation that simply runs echo "Hello World" in the shell:
'use strict';
import * as vscode from 'vscode';
export function activate(context: vscode.ExtensionContext) {
var type = "exampleProvider";
vscode.tasks.registerTaskProvider(type, {
provideTasks(token?: vscode.CancellationToken) {
var execution = new vscode.ShellExecution("echo \"Hello World\"");
var problemMatchers = ["$myProblemMatcher"];
return [
new vscode.Task({type: type}, vscode.TaskScope.Workspace,
"Build", "myExtension", execution, problemMatchers)
];
},
resolveTask(task: vscode.Task, token?: vscode.CancellationToken) {
return task;
}
});
}
The task definition (first argument for new Task()) needs to be contributed via package.json and can have additional properties if needed:
"contributes": {
"taskDefinitions": [
{
"type": "exampleProvider"
}
]
}
Extensions with a task provider should activate when the Tasks: Run Task command is executed:
"activationEvents": [
"onCommand:workbench.action.tasks.runTask"
]
And finally, the problem matcher(s) you want to reference need to be contributed in the package.json's contributes.problemMatchers section.

Corrupt file when using Azure Functions External File binding

I'm running a very simple ExternalFileTrigger scenario in Azure Functions were I copy one created image file from one onedrive directory to another.
function.json
{
"bindings": [
{
"type": "apiHubFileTrigger",
"name": "input",
"direction": "in",
"path": "Bilder/Org/{name}",
"connection": "onedrive_ONEDRIVE"
},
{
"type": "apiHubFile",
"name": "$return",
"direction": "out",
"path": "Bilder/Minimized/{name}",
"connection": "onedrive_ONEDRIVE"
}
],
"disabled": false
}
run.csx
using System;
public static string Run(string input, string name, TraceWriter log)
{
log.Info($"C# File trigger function processed: {name}");
return input;
}
Every things seems to work well BUT the new output image file i corrupt. The size is almost twice as big.
When looking at the encoding the original file is in ANSI but the new generated file from Azure Functions is in UTF-8.
It's working fine when I'm using a text file when source encoding is UTF-8.
Is it possible to force Azure binding ExternalFileTrigger to use ANSI? Or how to solve this?
UPDATE 2019: The external file binding seems to be deprecated from the current version of Azure Functions.
If you want to copy the file as-is, or do more fine-grained binary operations on the file contents, I recommend using Stream type instead of string for your input and output bindings:
public static async Task Run(Stream input, Stream output, string name, TraceWriter log)
{
using (MemoryStream ms = new MemoryStream())
{
input.CopyTo(ms);
var byteArray = ms.ToArray();
await output.WriteAsync(byteArray, 0, byteArray.Length);
}
log.Info($"C# File trigger function processed: {name}");
}
Change the ouput binding in function.json:
"name": "output",
This function will do exact binary copy of the file, without conversion.
You can see which other types you can use for bindings in External File bindings (see "usage" sections).