JalmarazMartn / customDiagnostics

1 stars 0 forks source link

customdiagnostics README

Main features:

Custom diagnostics allows to define your own diagnostics to show them in problems panel. These diagnostics also can be used as bulk replacements rules. A set of rules can be applied with the command "JAM Custom Rules. Pick a ruleset and apply replacements in all workspace documents". Posts about extension. https://community.dynamics.com/business/b/jesusalmarazblog/posts/new-bulk-replacing-vscode-extension

https://community.dynamics.com/business/b/jesusalmarazblog/posts/how-to-set-your-own-vscode-diagnostics-replacing-tool-2

Features

This extension provides a way to define a diagnostic and a replace rule in a file. Then you set the file path in extension settings, in property "JAMDiagnosticsFile".

You can set in this file with replacing rules and rulesets this way:

{
    "rules": [
        {
            "name": "Avoid using transferfields",
            "searchExpresion": "(.+)\\.TransferFields\\((.+)\\)",
            "replaceExpression": "$1 := $2",
            "numberOfRepetitions": 3
        },
        {
            "name": "Remove Scope Internal",
            "searchExpresion": "\\[Scope\\('Internal'\\)]",
            "replaceExpression": ""
        }
    ],
    "rulesets": [
        {
            "name": "Initial replacement rules from al",
            "fileExtension": "al",
            "rules": [
                "Avoid using transferfields",
                "Remove Scope Internal"
            ],
            "saveAfterApply":true,
            "scope":["workspace","document","selection"]
        },
        {
            "name": "Empty rules to test",
            "rules": [
                ""
            ]
        }
    ]

The properties of a replacing rule have the following meaning:

There is another powerful but complex way to set a replacing rule: instead of replaceExpression parameter, you can set a javascript function in a module. You can set the rule this way:

            "name": "Replace parameters in Create Reservation for",
            "searchExpresion": "[A-Za-z]+\\.CreateReservEntryFor\\(([^;]|\r|\n)*;",
            "jsModuleFilePath": "C:\\Users\\Jesus\\Documents\\Proyecto js\\customDiagnostics\\jsReplaceExampleModules\\replaceExample.js",
            "jsFunctionName": "CreateReservEntryFor"

instead of replaceExpression parameter, you can set a javascript function in a module. You can set the rule this way:

The example of the module is in the folder "jsReplaceExampleModules". For further information about how to make a function to replace a regular expression match, you can chech this link: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replace

Replace rule set properties are:

Possible values for scope are (empty or not settled=all):

For helping you to make rules and rulesets, you can use the following snippets:

With command "JAM Custom Rules. Pick a ruleset and apply replacements in all workspace documents" we can pick a ruleset from the list and replace all occurrences in all documents.

Replace image

If you only want to apply replace in current document you can execute command "JAM Custom Rules. Pick a ruleset and apply replacements in current document"

And last: If you only want to apply replace in current document selection you can execute command "JAM Custom Rules. Pick a ruleset and apply replacements in current selection"

To help ruleset editting will raise an error with the rules not defined included in ruleset. To select existing an rule to include it in a ruleset you can use this completion item (with control+Space):

Replace image

Note release 27: We add these helps to diagnostic and fix sets edition, selection an no extisting error.

Fixes

Replace image

Fixes is a subtle way to do replacements. The replacement will be done only if the line has a problem, with an error code. You can set a fix this way:

    "fixes": [
        {
            "name":"Remove unnecesary decimals places",
            "code": "AL0223",
            "searchExpresion": ".*DecimalPlaces.*",
            "replaceExpression": ""
        }
    ],
    "fixsets": [
        {
            "name": "Set of basics fixes of al",
            "fixes": [
                "Remove unnecesary decimals places"
            ]
        }
    ]

The meaning: the replacing will be applied only if the line has a problem with code "AL0223", in problems panel.

You can set in fix code an empty string to apply it in any diagnostic code. But in this case the search expression must also match in the diagnostic message.

Bellow you can configure a set of fixes and aplly them in all workspace documents with the command "JAM Fixes. Pick a fixset and apply fixes in all workspace documents diagnostics". Then you choose a fixset if you are more than one, and will apply all fixes in all documents.

You can apply fixes in a more restricted mode with command:"JAM Fixes. Pick a fixset and apply fixes in current document diagnostics".

Command: Build a fix from current diagnostic and copy it in clipboard

This command features the partial creation of a fix and his store in clipboard to past it in a .json rules file. The steps to use it are:

Diagnostics

In the same file we set the replacing rules we can set a digsnotics to find and show in problems panel.

    "diagnostics": [
            {
                "code": "JAMMIG001",
                "message": "Review SQlDateType Variant",
                "searchExpresion": "SQLDataType.*Variant",
                "severity": "error",
                "language": "al"
            },
            {
                "code": "JAMMIG002",
                "message": "Review layout path",
                "searchExpresion": "RDLCLayout = '",
                "skipFromSearchIfMatch": "/Layout/",
                "severity": "error",
                "language": "al",
                "andFileAlsoMustInclude":
                [
                    {
                        "searchExpresion": "report\\s+\\d*\\s+.+"
                    }
                ],
                "skipIfFileInclude":
                [
                    {
                        "searchExpresion": "ProcessOnly"
                    }
                ]                    
            }
                    ],
    "diagnosticsets": [
        {
            "name": "Cloud Migration Errors",
            "diagnostics": [
                "JAMMIG001",
                "JAMMIG002"
            ]

The properties of a rule have the following meaning:

Note: You only will see custom diagnostics out of the document edition setting the extension parameter JAMDiagnostics.ScanCustomDiagnosticsInAllWS to true.

You also can define a Javascript function to do more complex and customized diagnostic checkings:

    {
        "code": "JAMMIG022",            
        "message": "Campo no declarado en el layout",            
        "searchExpresion": "Fields!.+.Value",
        "severity": "error",
        "language": "xml",
        "jsModuleFilePath": "C:\\Users\\Jesus\\Documents\\Proyecto js\\customDiagnostics\\FileExamples\\ComplexReplaces.js",
        "jsFunctionName": "existsFieldDeclaration"
    }

Js module must include a function with this signature: function existsFieldDeclaration(fieldLine='') and return true if diagnostic matches. You can see example in this repo file: https://github.com/JalmarazMartn/customDiagnostics/blob/main/FileExamples/ComplexReplaces.js

Steps to set the diagnostics

Regex Help

It was hard to write new regular expressions and write them escaped in a JSON value. To ease this work you can edit regular expressions this way, using specific context menu:

Replace image

Requirements

vscode

Extension Settings

This extension contributes the following settings:

Known Issues

Only shows custom diagnostics in problems panel, if you open the text document in your editor. I am thinking to do or not other way due performanece issues. So not sure this will be an issue or not.

Release Notes