-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #166 from EXXETA/feature/160-variable-syntax-highl…
…ighting feat(160): Variable Syntax Highlighting and Completion
- Loading branch information
Showing
26 changed files
with
869 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
import { editor, IPosition, IRange } from 'monaco-editor'; | ||
|
||
class MockModel implements Partial<editor.ITextModel> { | ||
constructor(private readonly lines: string[]) {} | ||
|
||
getValueInRange(range: IRange) { | ||
if (range.startLineNumber !== range.endLineNumber) | ||
throw new Error('Range must be in the same line'); | ||
return this.lines[range.startLineNumber - 1].slice(range.startColumn - 1, range.endColumn - 1); | ||
} | ||
|
||
getLineMaxColumn(lineNumber: number) { | ||
return this.lines[lineNumber - 1].length + 1; | ||
} | ||
|
||
getLineCount() { | ||
return this.lines.length; | ||
} | ||
|
||
getLineContent(lineNumber: number) { | ||
return this.lines[lineNumber - 1]; | ||
} | ||
|
||
getWordAtPosition(position: IPosition) { | ||
const prefixLength = /\w*$/.exec( | ||
this.getValueInRange({ | ||
startLineNumber: position.lineNumber, | ||
startColumn: 1, | ||
endLineNumber: position.lineNumber, | ||
endColumn: position.column, | ||
}) | ||
)[0].length; | ||
const postfixLength = /^\w*/.exec( | ||
this.getValueInRange({ | ||
startLineNumber: position.lineNumber, | ||
startColumn: position.column, | ||
endLineNumber: position.lineNumber, | ||
endColumn: this.getLineMaxColumn(position.lineNumber), | ||
}) | ||
)[0].length; | ||
|
||
const startColumn = position.column - prefixLength; | ||
const endColumn = position.column + postfixLength; | ||
if (startColumn === endColumn) { | ||
return null; | ||
} | ||
|
||
return { | ||
word: this.getValueInRange({ | ||
startLineNumber: position.lineNumber, | ||
startColumn, | ||
endLineNumber: position.lineNumber, | ||
endColumn, | ||
}), | ||
startColumn, | ||
endColumn, | ||
}; | ||
} | ||
} | ||
|
||
/** | ||
* Mock a monaco editor model. | ||
* @param json The JSON content of the model as object. | ||
*/ | ||
export function mockModel(json: object) { | ||
const lines = JSON.stringify(json, null, 2).split('\n'); | ||
return new MockModel(lines) as Partial<editor.ITextModel> as editor.ITextModel; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import { getSystemVariable, getSystemVariableKeys, getSystemVariables } from './system-variable'; | ||
|
||
describe('getSystemVariable()', () => { | ||
it('should return the the system variable object', () => { | ||
// Arrange | ||
const key = '$randomUuid'; | ||
|
||
// Act | ||
const result = getSystemVariable(key); | ||
|
||
// Assert | ||
expect(result.key).toBe(key); | ||
expect(result.isActive).toBe(true); | ||
expect(result.value).toBeDefined(); | ||
expect(result.description).toBeDefined(); | ||
}); | ||
|
||
it('should return a random UUID for $randomUuid', () => { | ||
// Arrange | ||
const key = '$randomUuid'; | ||
|
||
// Act | ||
const firstValue = getSystemVariable(key).value; | ||
const secondValue = getSystemVariable(key).value; | ||
|
||
// Assert | ||
expect(firstValue).not.toBe(secondValue); | ||
}); | ||
}); | ||
|
||
describe('getSystemVariableKeys()', () => { | ||
it('should return all system variable keys', () => { | ||
// Act | ||
const result = getSystemVariableKeys(); | ||
|
||
// Assert | ||
expect(result).toContain('$randomUuid'); | ||
expect(result).toHaveLength(6); | ||
}); | ||
}); | ||
|
||
describe('getSystemVariables()', () => { | ||
it('should return all system variables', () => { | ||
// Act | ||
const result = getSystemVariables(); | ||
|
||
// Assert | ||
expect(result).toContainEqual({ | ||
key: '$randomUuid', | ||
isActive: true, | ||
value: expect.any(String), | ||
description: expect.any(String), | ||
}); | ||
expect(result).toHaveLength(6); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { VariableObject } from 'shim/variables'; | ||
import { randomInt, randomUUID } from 'node:crypto'; | ||
|
||
const systemVariables = new Map<VariableObject['key'], VariableObject>( | ||
[ | ||
{ | ||
key: '$timestampIso', | ||
get value() { | ||
return new Date().toISOString(); | ||
}, | ||
description: 'The current timestamp in ISO format.', | ||
}, | ||
{ | ||
key: '$timestampUnix', | ||
get value() { | ||
return Math.floor(Date.now() / 1e3).toString(); | ||
}, | ||
description: 'The current timestamp in Unix format.', | ||
}, | ||
{ | ||
key: '$time', | ||
get value() { | ||
return new Date().toTimeString(); | ||
}, | ||
description: 'The current time.', | ||
}, | ||
{ | ||
key: '$date', | ||
get value() { | ||
return new Date().toDateString(); | ||
}, | ||
description: 'The current date.', | ||
}, | ||
{ | ||
key: '$randomInt', | ||
get value() { | ||
return randomInt(2 ** 48 - 1).toString(); | ||
}, | ||
description: 'A positive random integer less than 2⁴⁸.', | ||
}, | ||
{ | ||
key: '$randomUuid', | ||
get value() { | ||
return randomUUID(); | ||
}, | ||
description: 'A random UUID.', | ||
}, | ||
].map((variable) => [variable.key, Object.assign(variable, { isActive: true })]) | ||
); | ||
|
||
/** | ||
* Returns the value of a dynamic, predefined system variable. | ||
* @param key The key of the system variable. | ||
* @returns The value of the system variable if it exists, otherwise undefined. | ||
*/ | ||
export function getSystemVariable(key: string) { | ||
return systemVariables.get(key); | ||
} | ||
|
||
/** | ||
* Returns all keys of the system variables. | ||
*/ | ||
export function getSystemVariableKeys() { | ||
return Array.from(systemVariables.keys()); | ||
} | ||
|
||
/** | ||
* Returns all system variables. | ||
*/ | ||
export function getSystemVariables() { | ||
return Array.from(systemVariables.values()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.