From 9efa90b644d4952d8154115320e3804ef764e676 Mon Sep 17 00:00:00 2001 From: Max Hegler Date: Thu, 6 Feb 2025 11:50:05 -0800 Subject: [PATCH] added command to insert Miva Script next error code --- CHANGELOG.md | 15 ++++++++++++- client/src/miva-commands.ts | 42 +++++++++++++++++++++++++++++++++++++ package-lock.json | 4 ++-- package.json | 8 ++++++- 4 files changed, 65 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index aee893f..c4dafd8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,19 @@ # Miva IDE CHANGELOG -## v1.29.0 (latest) +## v1.30.0 (latest) + +* Added Miva Script command to insert next error code based off of "Prefix" and "Next Error Code" comment block. + + ``` + + | + | Prefix : AAA-AAA-AAA- + | Next Error Code: 10 + | + + ``` + +## v1.29.0 * Added variable autocompletion for flex instance templates. diff --git a/client/src/miva-commands.ts b/client/src/miva-commands.ts index 70847ff..0770851 100644 --- a/client/src/miva-commands.ts +++ b/client/src/miva-commands.ts @@ -155,6 +155,47 @@ const insertModuleImportCommand = commands.registerTextEditorCommand( 'mivaIde.i } }); +const insertNextErrorCodeCommand = commands.registerTextEditorCommand('mivaIde.MV.insertNextErrorCode', (textEditor: TextEditor, edit: TextEditorEdit) => { + // exit if not Miva Script + if (textEditor.document.languageId !== 'mv') { + return; + } + + const documentText = textEditor.document.getText(); + + // Get error code "Prefix" + const [,, prefixBlock] = documentText?.match(/(?<=)([^<]*)(\s*\|\s*prefix\s*:.*)/i) || []; + const [, rawPrefix] = prefixBlock?.split(':') || []; + if (!rawPrefix) { + window.showWarningMessage( 'Unable to find "Prefix" comment definition.' ); + + return; + } + const prefix = rawPrefix.trim(); + + // Get "Next Error Code" + const [, match, rawErrorCode] = /([\s\S]*[^<]*\s*\|\s*next error code\s*:\s*)([0-9]+)/i.exec(documentText) || []; + if (!rawErrorCode) { + window.showWarningMessage( 'Unable to find "Next Error Code" comment definition.' ); + + return; + } + let errorCode = parseInt(rawErrorCode.trim()) || 1; + + textEditor.selections.forEach(( selection ) => { + const range = new Range(selection.start, selection.end); + + edit.replace(range, `${prefix}${String(errorCode).padStart(5, '0')}`); + + errorCode++; + }); + + // Update next error code comment block + const startPosition = textEditor.document.positionAt(match.length); + const nextErrorCodeRange = new Range(startPosition, startPosition.translate(0, String(errorCode).length)); + edit.replace(nextErrorCodeRange, String(errorCode)); +}); + function convertEntityToVariable( entity: string, showMessage: boolean = true ) { const globalMatch = patterns.MVT.ENTITY_GLOBAL.exec( entity ); @@ -361,6 +402,7 @@ export default [ chooseFileCommand, insertFileNameCommand, insertModuleImportCommand, + insertNextErrorCodeCommand, convertAndCopyCommand, convertToEntityCommand, convertToVariableCommand, diff --git a/package-lock.json b/package-lock.json index ec873d1..20b2470 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "vscode-miva-ide", - "version": "1.29.0", + "version": "1.30.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "vscode-miva-ide", - "version": "1.29.0", + "version": "1.30.0", "workspaces": [ "client", "server" diff --git a/package.json b/package.json index 63f94c8..bfe4bcc 100644 --- a/package.json +++ b/package.json @@ -2,7 +2,7 @@ "name": "vscode-miva-ide", "displayName": "Miva IDE", "description": "Syntax highlighting, snippets and tools for building websites with Miva.", - "version": "1.29.0", + "version": "1.30.0", "engines": { "vscode": "^1.77.0", "node": ">=16" @@ -178,6 +178,12 @@ "category": "MVT", "enablement": "(editorHasSelection || editorHasMultipleSelections) && (editorLangId == mv || editorLangId == mvt || editorLangId == mvtcss || editorLangId == mvtjs)" }, + { + "command": "mivaIde.MV.insertNextErrorCode", + "title": "Insert Next Error Code", + "category": "Miva Script", + "enablement": "editorLangId == mv" + }, { "command": "mivaIde.MVT.convertToEntity", "title": "Convert Variable → Entity",