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
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:
name of the replacing rule
pattern to search and replace in bulk replacement. This pattern can be a regular expression
target string for substitution
sometimes a rule needs to be applied more then once. Optional setting, for doing more iterations wih the rule
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:
path to the module file
name of the function that implemnets the replacing rule
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:
tDiagnosticsFile
. Template for general scafolding of file.tReplaceRule
. Template to write a replacing rule.tReplaceWithFunctionRule
. Template to write a rule with a replacing function instead an string expression.tReplaceRuleset
. Template to write a replacing ruleset.tDiagnostic
. Template to write a diagnostic rule.tDiagnosticset
. Template to write a diagnostic ruleset.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.
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):
Note release 27: We add these helps to diagnostic and fix sets edition, selection an no extisting error.
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".
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:
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:
code of the diagnostic to show in problems panel
message of the diagnostic to show in problems panel
pattern to search and replace in bulk replacement. This pattern can be a regular expression, and also can raise the diganostic in the problems panel
optional property to set a pattern to exclude from diagnostic the match
severity of the diagnostic in problems panel: error, warning, information, hint
language to apply the rule for replacing and diagnostic
object array of searchExpresion that must be satisfied to raise the diagnostic
object array of searchExpresion that skip diagnostic if match in any place of the file
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
JAMDiagnostics.DefaultDiagnosticRuleset
.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:
vscode
This extension contributes the following settings:
JAMRules.json
: Absolute path of rules json file nameJAMDiagnostics.DefaultDiagnosticRuleset
: Rulesets that will be used as diagnostics in problems PanelJAMDiagnostics.AdditionalFilePaths
: Paths of aditional files to load rules and diagnostics.JAMDiagnostics.ScanCustomDiagnosticsInAllWS
: Custom diagnostics for a doc are not enabled out of doc edition. If you want to keep seeing diagnostics outside the document edition you must set true this property. Is recommended to disable this property in big workspaces to avoid performance issues.JAMDiagnostics.RegexpHelpURL
: URL to open in the context menu action "JAM Custom Rules. Open regex help URL in explorer".JAMDiagnostics.EnableHelpContextMenus
: Option to enable extension context menus.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.