Skip to content

Commit

Permalink
Merge pull request #131 from mghdotdev/feature/mivascript-command-ins…
Browse files Browse the repository at this point in the history
…ert-error-code

added command to insert Miva Script next error code
  • Loading branch information
mghdotdev authored Feb 6, 2025
2 parents 36447bf + 9efa90b commit 513eceb
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 4 deletions.
15 changes: 14 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.

```
<MvCOMMENT>
|
| Prefix : AAA-AAA-AAA-
| Next Error Code: 10
|
</MvCOMMENT>
```

## v1.29.0

* Added variable autocompletion for flex instance templates.

Expand Down
42 changes: 42 additions & 0 deletions client/src/miva-commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(/(?<=<mvcomment>)([^<]*)(\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]*<mvcomment>[^<]*\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 );
Expand Down Expand Up @@ -361,6 +402,7 @@ export default [
chooseFileCommand,
insertFileNameCommand,
insertModuleImportCommand,
insertNextErrorCodeCommand,
convertAndCopyCommand,
convertToEntityCommand,
convertToVariableCommand,
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 513eceb

Please sign in to comment.