Skip to main content

Notifications

Dynamics 365 Community / Blogs / Jesús Almaraz blog / New bulk replacing VSCode e...

New bulk replacing VSCode extension

Jalmaraz Profile Picture Jalmaraz 667

The why

This summer I found my self making a upgrade from 140 to 20 with a thousand of objects from customization. In this situation you discover yourself making the same replacing over and over.

. Remove Scope Internal.

. BuildInvLineBuffer for BuildInvLineBuffer2 in pre-payment.

. Item Cross Reference for Item Reference

. Cross-Reference to Reference.

. Language record to codeunit.

. FilterReservFor in codeunits for SetReservationFilters in records.

 

And lots of them than raise daily!!!

 

The logic step for me, repetitive problem=new VSCode extension. And I already published a bunch of them, all focused in increase software production in VSCode: Publisher Jesús Almaraz Martin - Visual Studio Marketplace

 

I love to make new tools, but actually first step was asking in the web “Anyone has done this before?” And the answer in Market Place use to be….yes.

 

The how

 

And yes, there is a previous extension to make replacing in VSCode in Market place. In this app you can set replacements once and apply them, but we had a problem: we have to do the apply operations in every single file, I mean, there is no bulk replacement in this previous extension. I left a message in Git to the author three months ago asking for a solution, and still waiting. But for do the tool I waited very few time(my lack of patience is legendary). So, I forked the repository and my first idea was to do one only customization: bulk replacing in every workspace file. But when I began I had a lot of problems with TypeScript and the way the code is structured: it wasn´t a conscious choice, but I did all the entire extension again, adding two areas, the possibility to add new self-configured diagnostics and fixes for every diagnostics. I kept the way to set the replacing rules from original, but soon I changed it.

The result: Custom diagnostics, bulk replacements and configurable fixes - Visual Studio Marketplace

 

JavaScript replace: do you remember the regular expressions?

 

The basic idea is:

. Set replacements in a JSON file.

. Group these replacements in sets.

. With a command choose a settled replace rule set and apply all the containing rules in this set to all files of workspace.

 

The replacing rules are settled this way:

        {
            "name": "Replace Codeunits FilterReservFor",
            "searchExpresion": ".*(.FilterReservFor)\\((.*),(.*)\\);",
            "replaceExpression": "$3.SetReservationFilters($2);"
        },

I begin for a complex substitution to explain that the extension makes the substitution using JavaScript regular expressions, and this has been very powerful. Here you a brief guide about this subject: Regular expressions - JavaScript | MDN (mozilla.org)

The most important concept here are groups, groups for searching and groups for replacing. Is easier to explain this with an example. With the previous expressions replaces the string

SalesLineReserve.FilterReservFor(ReservationEntry , SalesLine);

for

SalesLine.SetReservationFilters(ReservationEntry);

 

You can set search groups with () and use them ins replacement with $ with group order number. We also can set a JavaScript substitution function in a module, but I´d rather to explain this feature in further posts.

 

Of course, you can set a simple string replacement like these:

Remove internal scope statement

        {
            "name": "Remove Scope Internal",
            "searchExpresion": "\\[Scope\\('Internal'\\)]",
            "replaceExpression": ""
        },

Replace BuildInvLineBuffer2 by BuildInvLineBuffer.

        {
            "name": "BuildInvLineBuffer2 en prepmt",
            "searchExpresion": "BuildInvLineBuffer2",
            "replaceExpression": "BuildInvLineBuffer"
        },

 

Configuring these examples in the tool.

You can find more info in the extension README initial screen. Custom diagnostics, bulk replacements and configurable fixes - Visual Studio Marketplace

 

Step 1. Create JSON setup file.

All the definition of replacements and their grouping in sets must be settled in a file. This file definition could be eased with extension snippets “tDiagnosticsFile”, “tReplaceRule” and “tReplaceRuleset” to reach this file:

{
    "rules": [
        {
            "name": "Remove Scope Internal",
            "searchExpresion": "\\[Scope\\('Internal'\\)]",
            "replaceExpression": ""
        },
        {
            "name": "BuildInvLineBuffer2 en prepmt",
            "searchExpresion": "BuildInvLineBuffer2",
            "replaceExpression": "BuildInvLineBuffer"
        },
        {
            "name": "Replace Codeunits FilterReservFor",
            "searchExpresion": ".*(.FilterReservFor)\\((.*),(.*)\\);",
            "replaceExpression": "$3.SetReservationFilters($2);"
        }
    ],
    "rulesets": [
        {
            "name": "Initial replacement rules from Txt2al",
            "fileExtension": "al",
            "rules": [
                "Remove Scope Internal",
                "BuildInvLineBuffer2 en prepmt",
                "Replace Codeunits FilterReservFor"
            ]
        }
    ]
}

Step 2. Set file in extension settings.

In VSCode, go to File\Preference\Settings, and set in Extensions the file path you create as shown image below:


7418.SetFile.png

Step 3. Execute  the rule.

Push F1 and execute command “JAM. Custom Rules. Change all rules in all documents”, select a rule set, and all replacements will be performed. When it finishes you can save the result with File/Save All and close all the editors.

 0410.bulkReplace.gif

Next and sponsorship

 

In further posts I will talk about other extension features as custom diagnostics and fixes. Also, I will explain the plans for further improvements and features.

I am launching a sponsorship campaign to receive donations from companies that feel my extension work is useful form them. If your companies want to make a contribution to my software projects will be welcome.

 

 

Comments

*This post is locked for comments