Reputation: 185
We have an addon/extension (VB.NET forms exe application) for Eclipse, but now also want to use it for Visual Studio Code. Simply said the exe makes up some code lines (text) to insert at the cursor.
So I have to :
Does anyone know how to do this ? ... and possibly have an up-and-running example of this ?
EDIT: this is what I have, and it works ... only problem is to startup the VB.NET executable :
// Get the active text editor
let editor = vscode.window.activeTextEditor;
if (editor) {
let selection = editor.selection;
// start VB.NET exe here ... but how ?
// insert a text at current position
editor.edit(editBuilder => {
editBuilder.insert(selection.start, 'text inserted');
});
}
Upvotes: 4
Views: 2333
Reputation: 352
It's been a while, but since I was struggling with the same problem and now I've figured it out, thought it would be nice to leave an answer. I'm also assuming your problem is not the texteditor itself, but how to get the text from the exe.
The way I've done it was by creating a console app that serializes the result as JSON and then outputs it to the console, and from the vsc extension side I read the buffer from the process and parse it back to an object I can use.
My app:
class Program
{
public class TMSCheckResult
{
public bool ValidationSucceeded { get; set; }
public string ResultMessage { get; set; }
public int ErrorColumn { get; set; }
public int ErrorRow { get; set; }
}
static void Main(string[] args)
{
var result = new TMSChecker().Validate(args[0], args[1]);
var serializedResult = JsonConvert.SerializeObject(result);
Console.WriteLine(serializedResult);
}
}
My extension:
import * as child from 'child_process';
interface TMSCheckResult {
ValidationSucceeded: boolean;
ResultMessage: string;
ErrorColumn: number;
ErrorRow: number;
}
export class TMSChecker {
runValidation() {
const res = child.execFileSync('myapppath.exe', ['arg0', 'arg1']);
let result: TMSCheckResult = JSON.parse(res.toString());
if (!result.ValidationSucceeded) {
vscode.window.showErrorMessage(result.ResultMessage);
//... work on the texteditor to show where the error is
}
else {
vscode.window.showInformationMessage('Syntax is correct.');
}
}
}
PS 1: your exe must always exit with 0 - just leave Main
as void
- otherwise you'll get an exception on the extension side.
PS 2: if your resulting text is too large, you may want to write the result to a file and then read this file on your extension or you may end up having to deal with the buffer size limitation.
Upvotes: 3