diff --git a/package.json b/package.json index 116b9d4..1806486 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "al-toolbox", "displayName": "AL Toolbox", "description": "AL Language Regions and Snippets", - "version": "2.0.31", + "version": "2.0.32", "publisher": "BartPermentier", "icon": "resources/ALTB.png", "engines": { @@ -21,78 +21,87 @@ "Other", "Snippets" ], - "activationEvents": [ - "onLanguage:al", - "onCommand:al-toolbox.createRelatedTables", - "onCommand:al-toolbox.renumberAll", - "onCommand:al-toolbox.changePrefix", - "onCommand:al-toolbox.initGitignore", - "onCommand:al-toolbox.renumberAllFields" - ], + "activationEvents": [ + "onLanguage:al", + "onCommand:al-toolbox.createRelatedTables", + "onCommand:al-toolbox.renumberAll", + "onCommand:al-toolbox.changePrefix", + "onCommand:al-toolbox.initGitignore", + "onCommand:al-toolbox.renumberAllFields", + "onCommand:al-toolbox.renumberConsistentAllFields" + ], "main": "./src/extension.js", "contributes": { - "commands": [ - { - "command": "al-toolbox.createRelatedTables", - "title": "ALTB: Start Project: Create Related Tables" - }, - { - "command": "al-toolbox.openRelatedTables", - "title": "ALTB: Open Related Tables" - }, - { - "command": "al-toolbox.openRelatedPages", - "title": "ALTB: Open Related Pages" - }, - { - "command": "al-toolbox.openRelatedTablesAndPages", - "title": "ALTB: Open Related Tables and Pages" - }, - { - "command": "al-toolbox.wrapAllFunctions", - "title": "ALTB: Create regions for all AL functions and triggers" - }, - { - "command": "al-toolbox.wrapAllDataItemsAndColumns", - "title": "ALTB: Create regions for all AL dataitems and columns" - }, - { - "command": "al-toolbox.wrapAll", - "title": "ALTB: Create regions for all AL functions, triggers, dataitems, and columns" - }, - { - "command": "al-toolbox.renumberAll", - "title": "ALTB: Renumber AL objects" - }, - { - "command": "al-toolbox.changePrefix", - "title": "ALTB: Change object prefix" - }, - { - "command": "al-toolbox.copyFieldsToRelatedTables", - "title": "ALTB: Copy fields to related tables" - }, - { - "command": "al-toolbox.initGitignore", - "title": "ALTB: init .gitignore" - }, - { - "command": "al-toolbox.addRegion", - "title": "ALTB: Add region" - }, - { - "command": "al-toolbox.generateSetLoadFields", - "title": "ALTB: Generate SetLoadFields" - }, - { - "command": "al-toolbox.renumberFields", - "title": "ALTB: Renumber fields" - }, - { - "command": "al-toolbox.renumberAllFields", - "title": "ALTB: Renumber all fields" - } - ], + "commands": [ + { + "command": "al-toolbox.createRelatedTables", + "title": "ALTB: Start Project: Create Related Tables" + }, + { + "command": "al-toolbox.openRelatedTables", + "title": "ALTB: Open Related Tables" + }, + { + "command": "al-toolbox.openRelatedPages", + "title": "ALTB: Open Related Pages" + }, + { + "command": "al-toolbox.openRelatedTablesAndPages", + "title": "ALTB: Open Related Tables and Pages" + }, + { + "command": "al-toolbox.wrapAllFunctions", + "title": "ALTB: Create regions for all AL functions and triggers" + }, + { + "command": "al-toolbox.wrapAllDataItemsAndColumns", + "title": "ALTB: Create regions for all AL dataitems and columns" + }, + { + "command": "al-toolbox.wrapAll", + "title": "ALTB: Create regions for all AL functions, triggers, dataitems, and columns" + }, + { + "command": "al-toolbox.renumberAll", + "title": "ALTB: Renumber AL objects" + }, + { + "command": "al-toolbox.changePrefix", + "title": "ALTB: Change object prefix" + }, + { + "command": "al-toolbox.copyFieldsToRelatedTables", + "title": "ALTB: Copy fields to related tables" + }, + { + "command": "al-toolbox.initGitignore", + "title": "ALTB: init .gitignore" + }, + { + "command": "al-toolbox.addRegion", + "title": "ALTB: Add region" + }, + { + "command": "al-toolbox.generateSetLoadFields", + "title": "ALTB: Generate SetLoadFields" + }, + { + "command": "al-toolbox.renumberFields", + "title": "ALTB: Renumber fields" + }, + { + "command": "al-toolbox.renumberAllFields", + "title": "ALTB: Renumber all fields" + }, + { + "command": "al-toolbox.renumberConsistentFields", + "title": "ALTB: consistent Renumber fields" + }, + { + "command": "al-toolbox.renumberConsistentAllFields", + "title": "ALTB: consistent Renumber all fields" + } + ], "configuration": [ { "title": "AL Toolbox Configurations", diff --git a/src/extension.js b/src/extension.js index 293c39b..9e0a4bb 100644 --- a/src/extension.js +++ b/src/extension.js @@ -5,6 +5,7 @@ const alFileManagement = require('./fileManagement/alFileManagement'); const regionWrapper = require('./regionWrapper/regionWrapper'); const renumber = require('./renumberObjects/renumber'); const renumberFields = require('./renumberObjects/renumberFields'); +const renumberConsistentFields = require('./renumberObjects/renumberConsistentFields'); const changePrefix = require('./changePrefix/changePrefix'); const uniqueApiEntities = require('./codeAnalyzers/apiPageEntityAnalyzer'); const copyFieldsToRelatedTables = require('./relatedTables/copyFieldsToRelatedTables'); @@ -180,6 +181,31 @@ function activate(context) { } }) })); + + context.subscriptions.push(vscode.commands.registerTextEditorCommand('al-toolbox.renumberConsistentFields', textEditor => { + telemetry.sendRenumberFieldsEvent(); + requestNumber('Start number (1, 80000, ...)').then(info => { + if (info.ok) + renumberConsistentFields.renumberConsistentFields(textEditor.document, info.number) + .then(ok => { + if (ok) textEditor.document.save(); + }); + }) + })); + context.subscriptions.push(vscode.commands.registerCommand('al-toolbox.renumberConsistentAllFields', () => { + telemetry.sendRenumberAllFieldsEvent(); + requestNumber('Start number for tables (1, 80000, ...)').then(async tablesInfo => { + if (tablesInfo.ok) { + const tableExtInfo = await requestNumber('Start number for table extensions (1, 80000, ...)'); + if (tableExtInfo.ok) + renumberConsistentFields.renumberConsistentAllFields(tablesInfo.number, tableExtInfo.number) + .then(count => { + vscode.window.showInformationMessage(`${count} object(s) renumbered`); + SaveAndCloseAll(); + }) + } + }) + })); //#endregion context.subscriptions.push(vscode.commands.registerCommand('al-toolbox.changePrefix', async () => { diff --git a/src/renumberObjects/renumberConsistentFields.js b/src/renumberObjects/renumberConsistentFields.js new file mode 100644 index 0000000..1f9ec14 --- /dev/null +++ b/src/renumberObjects/renumberConsistentFields.js @@ -0,0 +1,94 @@ +const vscode = require('vscode'); +const alFileManagement = require('../fileManagement/alFileManagement'); +const constants = require('../constants'); + +const fieldRegex = /(?<=\bfield\s*\(\s*)\d+(?=\s*;)/g; + +// Mapping f�r konsistente Umnummerierung +const fieldMapping = new Map(); + +/** + * @param {vscode.TextDocument} document + * @param {vscode.WorkspaceEdit} edit + * @param {number} startFrom The new numbers will start from this number, counting up. + */ +function addConsistentRenumberFieldsToEdit(document, edit, startFrom) { + let text = document.getText(); + + let match, fieldNoAndRange = []; + while ((match = fieldRegex.exec(text))) { + const startPos = document.positionAt(match.index); + fieldNoAndRange.push({ + no: Number(match[0]), + range: new vscode.Range(startPos, startPos.translate(0, match[0].length)) + }); + } + + if (fieldNoAndRange.length === 0) return false; + + fieldNoAndRange + .sort((a, b) => a.no - b.no) + .forEach(fieldInfo => { + let newNumber; + if (fieldMapping.has(fieldInfo.no)) { + newNumber = fieldMapping.get(fieldInfo.no); + } else { + newNumber = startFrom++; + fieldMapping.set(fieldInfo.no, newNumber); + } + edit.replace(document.uri, fieldInfo.range, `${newNumber}`); + }); + return true; +} + +/** + * @param {vscode.TextDocument} document + * @param {number} startFrom The new numbers will start from this number, counting up. + */ +async function renumberConsistentFields(document, startFrom) { + const edit = new vscode.WorkspaceEdit(); + if (addConsistentRenumberFieldsToEdit(document, edit, startFrom)) + return await vscode.workspace.applyEdit(edit); + else + return false; +} +exports.renumberConsistentFields = renumberConsistentFields; + +/** + * @param {number} startFromForTable The new numbers for tables will start from this number, counting up. + * @param {number} startFromForTableExtension The new numbers for table extensions will start from this number, counting up. + */ +async function renumberConsistentAllFields(startFromForTable, startFromForTableExtension) { + const uris = await vscode.workspace.findFiles('**/*.al'); + + const increment = 100 / uris.length; + const results = await vscode.window.withProgress({ + location: vscode.ProgressLocation.Notification, + title: "Updating prefixes...", + cancellable: false + }, async progress => { + + return Promise.all(uris.map(uri => vscode.workspace.openTextDocument(uri).then(async document => { + const objInfo = new alFileManagement.AlObjectInfo(document); + let edited = false; + switch (objInfo.type) { + case constants.AlObjectTypes.table: + edited = await renumberConsistentFields(document, startFromForTable); + break; + case constants.AlObjectTypes.tableExtension: + edited = await renumberConsistentFields(document, startFromForTableExtension); + break; + } + progress.report({ increment: increment }); + return edited; + }) + )); + }); + + let docEditedCount = 0; + results.forEach(edited => { + if (edited) ++docEditedCount; + }); + return docEditedCount; +} +exports.renumberConsistentAllFields = renumberConsistentAllFields;