web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Dynamics 365 Community / Blogs / Jesús Almaraz blog / Extending VSCode with API o...

Extending VSCode with API objects: Workspace, Document and WorkspaceEdit

Jalmaraz Profile Picture Jalmaraz 669

VSCode API important objects

With VSCode extensions we can avoid coding repetitive work and most of this work changes source code. I have not seen many other VSCode extensions code but knowing that most of extensions deal with automatic changes in the source code, I think these objects could be widely used in al VSCode extensions: Workspace, Document and WorkspaceEdit. In this post I want to explain with a simple example how to use them.

Yeoman

The first step is already described in many and many posts. Create a new application with YoCode Yeoman option. Yeoman is a scaffolding utility. I mean, Yeoman help us to construct all the application structure. We have Yeoman scaffolders for a lot of application types, node.js, ASP Core and the VSCode scaffolder. We can do it without Yeoman, but Yeoman using allows us to focus in developing, without node.js apps knowledge. This is link to the basic “Hello World”, with Yeoman:
Then you can begin to change the “Hello Words” lines and instead put our contributions in these files:
function activate(context) {
    let RenumSubs = vscode.commands.registerCommand('JAMRenumbering.Renumber'function () {
        const Library = require('./src/Library.js');
 
        Library.RenumberObjects();
    });
    context.subscriptions.push(RenumSubs);
    "activationEvents": [
        "onCommand:JAMRenumbering.Renumber",
And in the same file below:
    "contributes": {
        "commands": [
            {"command""JAMRenumbering.Renumber",
                "title""JAM Renumber Al Objects from CSV File"
As good practice we could write all the code in external files in a src folder. This is a little extension so I only needed a single file, and we will breakdown it in further lines of the post:

Workspace and document

The renumbering utility of the example loops into all .al files of current workspace to change text declaration line, the first line of the document. To get a collection of all the files of the workspace folder we can use the workspace object
    const AllDocs = await vscode.workspace.findFiles('**/*.{al}');
    AllDocs.forEach(ALDocumentURI => {
        vscode.workspace.openTextDocument(ALDocumentURI).then(
            ALDocument => {
                const FirstLine = ALDocument.lineAt(0).text;
Workspace method “findFiles” returns an array of document URIs (the addresses of files). '**/*.{al}' is the filter for getting al .al files of the workspace.
For each URI we open the document with “openTextDocument” method that returns a VSCode document object. Properly we must say that returns a promise and when this promise is resolved we get the document object. If you want a light introduction to promises you can check this link:
When the promise is resolved we can use the document object and access their properties, in the example we take the file´s first line of text to handle the AL object declaration, with the member “lineAt” and its property “text”. You can see all the process in the repo.

Document Edit

For each document, we get its first line and check the object number and change it in first line of text document. To change a text inside a document we use the object “workspaceEdit”:
const WSEdit = new vscode.WorkspaceEdit;
And the change the text with replace and “applyEdit
WSEdit.replace(ALDocumentURInew vscode.Range(PositionOpenPostionEnd),
                 LineReplaced);
vscode.workspace.applyEdit(WSEdit);
Replace method change a document text (URI) in a range with a new text). Below we use applyEdit to commit the change.

Comments

*This post is locked for comments