Files
nocodb/packages/nc-gui/components/monaco/formula.ts
Anbarasu a5fc9be175 feat: Formula colouring and improved suggestions (#9072)
* feat: formula language

* feat: formula coloring and ux improvements

* fix: suggestions generation

* fix: handle undefined editor

* fix: handle formula errors

* fix: update imports

* fix: minor corrections

* fix: test corrections

* fix: increase timeout

* fix: clear existing formulas before pasting

* fix: ux improve

* fix: ux improve

* fix: coloring issue

* fix: remove styles

* fix: handle wrapping

* fix: bug fixes

* fix: strict suggestion handling

* fix: update indent strategy

* fix: handle formula in nested state and unbalanced parens

* fix: formula fix

* chore: sync dependencies
2024-07-30 13:47:07 +05:30

107 lines
2.9 KiB
TypeScript

import type { Thenable, editor, languages } from 'monaco-editor'
import { formulas } from 'nocodb-sdk'
const formulaKeyWords = Object.keys(formulas)
const theme: editor.IStandaloneThemeData = {
base: 'vs',
inherit: true,
rules: [
{ token: 'string', foreground: '#007b77', fontStyle: 'bold' },
{ token: 'keyword', foreground: '#00921d', fontStyle: 'bold' },
{ token: 'number', foreground: '#9c6200', fontStyle: 'bold' },
{ token: 'operator', foreground: '#000000' },
{ token: 'identifier', foreground: '#8541f9', fontStyle: 'bold' },
{ token: 'delimiter.parenthesis', foreground: '#333333', fontStyle: 'bold' },
{ token: 'delimiter.brace', foreground: '#8541f9', fontStyle: 'bold' },
{ token: 'invalid', foreground: '#000000' },
],
colors: {
'editor.foreground': '#000000',
'editor.background': '#FFFFFF',
'editorCursor.foreground': '#3366FF',
'editor.selectionBackground': '#3366FF50',
'focusBorder': '#ffffff',
},
}
const generateLanguageDefinition = (identifiers: string[]) => {
identifiers = identifiers.map((identifier) => `{${identifier}}`)
const languageDefinition: languages.IMonarchLanguage | Thenable<languages.IMonarchLanguage> = {
defaultToken: 'invalid',
keywords: formulaKeyWords,
identifiers,
brackets: [
{ open: '(', close: ')', token: 'delimiter.parenthesis' },
{ open: '{', close: '}', token: 'delimiter.brace' },
],
tokenizer: {
root: [
[/"/, { token: 'string.quote', bracket: '@open', next: '@dblString' }],
[/'/, { token: 'string.quote', bracket: '@open', next: '@sglString' }],
[
new RegExp(`\\{(${identifiers.join('|').replace(/[{}]/g, '')})\\}`),
{
cases: {
'@identifiers': 'identifier',
'@default': 'invalid',
},
},
],
[
/[a-zA-Z_]\w*/,
{
cases: {
'@keywords': 'keyword',
'@default': 'invalid',
},
},
],
[/\d+/, 'number'],
[/[-+/*=<>!]+/, 'operator'],
[/[{}()\[\]]/, '@brackets'],
[/[ \t\r\n]+/, 'white'],
],
dblString: [
[/[^\\"]+/, 'string'],
[/\\./, 'string.escape'],
[/"/, { token: 'string.quote', bracket: '@close', next: '@pop' }],
],
sglString: [
[/[^\\']+/, 'string'],
[/\\./, 'string.escape'],
[/'/, { token: 'string.quote', bracket: '@close', next: '@pop' }],
],
},
}
return languageDefinition
}
const languageConfiguration: languages.LanguageConfiguration = {
brackets: [
['{', '}'],
['[', ']'],
['(', ')'],
],
autoClosingPairs: [
{ open: '{', close: '}' },
{ open: '[', close: ']' },
{ open: '(', close: ')' },
{ open: '"', close: '"' },
{ open: "'", close: "'" },
],
}
export default {
name: 'formula',
theme,
generateLanguageDefinition,
languageConfiguration,
}