Macros 2.0 (v0.7.3) - Variable Shorthand: Comparison Operators & Autocomplete Improvements (#5050)

* feat: Add numeric comparison operators (>, >=, <, <=) to variable shorthand syntax

- Added four new comparison operators for local and global variables
- Implemented greaterThan, greaterThanOrEqual, lessThan, lessThanOrEqual operations in MacroCstWalker
- Updated lexer to recognize >=, >, <=, < operators (longer patterns before shorter to avoid conflicts)
- Simplified parser by consolidating operator alternatives into single OR block
- Enhanced autocomplete with operator definitions, examples, and usage

* Improve autocomplete for variable shorthand operators with cursor-aware filtering

- Track `variableNameEnd` and `variableOperatorEnd` positions in parsed macro context for accurate cursor position checks
- Add `isShortOperatorPrefix()` helper to detect operators that could be prefixes of longer ones (e.g., `>` → `>=`)
- Fix operator autocomplete to show longer variants when typing short operators (typing `>` now shows both `>` and `>=`)
- Show operator suggestions immediately when variable
This commit is contained in:
Wolfsblvt
2026-01-23 00:05:00 +01:00
committed by GitHub
parent 6f5032f20e
commit 42155ecebf
6 changed files with 323 additions and 97 deletions
+96
View File
@@ -2699,6 +2699,102 @@ test.describe('MacroEngine', () => {
expect(output).toBe('true');
});
// {{.myvar > value}} - greater than comparison (numeric)
test('should return true when variable is greater than value with >', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar > 5}}', { local: { myvar: '10' } });
expect(output).toBe('true');
});
test('should return false when variable is not greater than value with >', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar > 10}}', { local: { myvar: '5' } });
expect(output).toBe('false');
});
test('should return false when variable equals value with >', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar > 10}}', { local: { myvar: '10' } });
expect(output).toBe('false');
});
test('should return false for non-numeric values with >', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar > 5}}', { local: { myvar: 'abc' } });
expect(output).toBe('false');
});
// {{.myvar >= value}} - greater than or equal comparison (numeric)
test('should return true when variable is greater than value with >=', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar >= 5}}', { local: { myvar: '10' } });
expect(output).toBe('true');
});
test('should return true when variable equals value with >=', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar >= 10}}', { local: { myvar: '10' } });
expect(output).toBe('true');
});
test('should return false when variable is less than value with >=', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar >= 10}}', { local: { myvar: '5' } });
expect(output).toBe('false');
});
// {{.myvar < value}} - less than comparison (numeric)
test('should return true when variable is less than value with <', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar < 10}}', { local: { myvar: '5' } });
expect(output).toBe('true');
});
test('should return false when variable is not less than value with <', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar < 5}}', { local: { myvar: '10' } });
expect(output).toBe('false');
});
test('should return false when variable equals value with <', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar < 10}}', { local: { myvar: '10' } });
expect(output).toBe('false');
});
test('should return false for non-numeric values with <', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar < 5}}', { local: { myvar: 'abc' } });
expect(output).toBe('false');
});
// {{.myvar <= value}} - less than or equal comparison (numeric)
test('should return true when variable is less than value with <=', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar <= 10}}', { local: { myvar: '5' } });
expect(output).toBe('true');
});
test('should return true when variable equals value with <=', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar <= 10}}', { local: { myvar: '10' } });
expect(output).toBe('true');
});
test('should return false when variable is greater than value with <=', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar <= 5}}', { local: { myvar: '10' } });
expect(output).toBe('false');
});
// Negative numbers with comparison operators
test('should handle negative numbers with > operator', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar > -5}}', { local: { myvar: '0' } });
expect(output).toBe('true');
});
test('should handle negative numbers with < operator', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar < 0}}', { local: { myvar: '-5' } });
expect(output).toBe('true');
});
// Decimal numbers with comparison operators
test('should handle decimal numbers with >= operator', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar >= 3.14}}', { local: { myvar: '3.14' } });
expect(output).toBe('true');
});
test('should handle decimal numbers with <= operator', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{.myvar <= 2.5}}', { local: { myvar: '2.49' } });
expect(output).toBe('true');
});
// Global variable versions of new operators
test('should use || with global variable', async ({ page }) => {
const output = await evaluateWithEngineAndVariables(page, '{{$myvar || globaldefault}}', { global: { myvar: '' } });