π₯ π₯ π₯ A Free & Self-hostable Airtable Alternative
TypeScript airtableairtable-alternativeautomatic-apihacktoberfestlow-codeno-codeno-code-databaseno-code-platformpostgresqlrest-apirestful-apispreadsheetsqliteswagger
markers on formula editor
Fendy Heryanto1273ce83bb5a
diff --git a/packages/nc-gui/components/smartsheet/column/FormulaInputHelper.vue b/packages/nc-gui/components/smartsheet/column/FormulaInputHelper.vueindex 548dd55496..e2ee1b92a9 100644--- a/packages/nc-gui/components/smartsheet/column/FormulaInputHelper.vue+++ b/packages/nc-gui/components/smartsheet/column/FormulaInputHelper.vue@@ -2,13 +2,22 @@import { UITypes, isHiddenCol, jsepCurlyHook } from 'nocodb-sdk'import type { Ref } from 'vue'import type { ListItem as AntListItem } from 'ant-design-vue/lib/list'-import { KeyCode, type editor as MonacoEditor, Position, Range, languages, editor as monacoEditor } from 'monaco-editor'+import {+ KeyCode,+ MarkerSeverity,+ type editor as MonacoEditor,+ Position,+ Range,+ languages,+ editor as monacoEditor,+} from 'monaco-editor'import jsep from 'jsep'import formulaLanguage from '../../monaco/formula'import { isCursorInsideString } from '../../../utils/formulaUtils'interface Props {error?: boolean+ editorError: { isError: boolean; message: string; position: { column: number; row: number; length: number } }value: stringlabel?: stringeditorHeight?: string@@ -148,6 +157,7 @@ const variableList = computed(() => {const monacoRoot = ref<HTMLDivElement>()let editor: MonacoEditor.IStandaloneCodeEditor+let model: MonacoEditor.ITextModelfunction getCurrentKeyword() {const model = editor.getModel()@@ -167,7 +177,7 @@ const handleInputDeb = useDebounceFn(function () {onMounted(async () => {if (monacoRoot.value) {- const model = monacoEditor.createModel(value.value, 'formula')+ model = monacoEditor.createModel(value.value, 'formula')languages.register({id: formulaLanguage.name,@@ -669,6 +679,27 @@ const enableAI = async () => {aiMode.value = AI_MODE.PROMPT}}++// set monaco module markers every editor error change+watch(+ () => props.editorError,+ (value, _oldValue) => {+ if (value?.isError) {+ monacoEditor.setModelMarkers(editor.getModel()!, 'owner', [+ {+ startLineNumber: value.position.row + 1,+ startColumn: value.position.column + 1,+ endLineNumber: value.position.row + 1,+ endColumn: value.position.column + 1 + value.position.length,+ message: value.message,+ severity: MarkerSeverity.Error,+ },+ ])+ } else {+ monacoEditor.setModelMarkers(editor.getModel()!, 'owner', [])+ }+ },+)</script><template>diff --git a/packages/nc-gui/components/smartsheet/column/FormulaOptions.vue b/packages/nc-gui/components/smartsheet/column/FormulaOptions.vueindex 539ccac07c..fe0ccb93ce 100644--- a/packages/nc-gui/components/smartsheet/column/FormulaOptions.vue+++ b/packages/nc-gui/components/smartsheet/column/FormulaOptions.vue@@ -34,6 +34,16 @@ const { t } = useI18n()const meta = inject(MetaInj, ref())+const defaultEditorError = {+ isError: false,+ message: '',+ position: {+ column: -1,+ row: -1,+ },+}+const editorError = ref(defaultEditorError)+const { base: activeBase } = storeToRefs(useBase())const supportedColumns = computed(@@ -59,7 +69,6 @@ const validators = {validator: (_: any, formula: any) => {return (async () => {if (!formula?.trim()) throw new Error('Required')-try {await validateFormulaAndExtractTreeWithType({column: column.value,@@ -68,12 +77,20 @@ const validators = {clientOrSqlUi: sqlUi.value,getMeta,})+ editorError.value = { ...defaultEditorError }} catch (e: any) {- if (e instanceof FormulaError && e.extra?.key) {- throw new Error(t(e.extra.key, e.extra))+ const errorMessage = e instanceof FormulaError && e.extra?.key ? t(e.extra.key, e.extra) : e.message+ if (e instanceof FormulaError && e.extra?.position) {+ editorError.value = {+ isError: true,+ message: errorMessage,+ position: e.extra.position,+ }+ } else {+ editorError.value = { ...defaultEditorError }}-- throw new Error(e.message)+ console.log('editorError.value', editorError.value)+ throw new Error(errorMessage)}})()},@@ -221,6 +238,7 @@ watch(<SmartsheetColumnFormulaInputHelperv-model:value="vModel.formula_raw":error="validateInfos.formula_raw?.validateStatus === 'error'"+ :editor-error="editorError"/></div></a-tab-pane>diff --git a/packages/nocodb-sdk/src/lib/formulaHelpers.spec.ts b/packages/nocodb-sdk/src/lib/formulaHelpers.spec.tsindex 00f1d73896..ed43b9e8fe 100644--- a/packages/nocodb-sdk/src/lib/formulaHelpers.spec.ts+++ b/packages/nocodb-sdk/src/lib/formulaHelpers.spec.ts@@ -163,6 +163,7 @@ describe('Formula parsing and type validation', () => {expect(ex.extra.position).toEqual({column: 3,row: 0,+ length: 1,});}try {@@ -176,13 +177,14 @@ describe('Formula parsing and type validation', () => {expect(ex.extra.position).toEqual({column: 6,row: 0,+ length: 1,});}});it(`will provide position for column not found`, async () => {try {await validateFormulaAndExtractTreeWithType({- formula: '1 + _',+ formula: '1 + __a_',columns: [],clientOrSqlUi: 'mysql2',getMeta: async () => ({}),@@ -191,6 +193,21 @@ describe('Formula parsing and type validation', () => {expect(ex.extra.position).toEqual({column: 4,row: 0,+ length: 4,+ });+ }+ try {+ await validateFormulaAndExtractTreeWithType({+ formula: '__a_',+ columns: [],+ clientOrSqlUi: 'mysql2',+ getMeta: async () => ({}),+ });+ } catch (ex) {+ expect(ex.extra.position).toEqual({+ column: 0,+ row: 0,+ length: 4,});}});diff --git a/packages/nocodb-sdk/src/lib/formulaHelpers.ts b/packages/nocodb-sdk/src/lib/formulaHelpers.tsindex a5e8e208bc..a841552b53 100644--- a/packages/nocodb-sdk/src/lib/formulaHelpers.ts+++ b/packages/nocodb-sdk/src/lib/formulaHelpers.ts@@ -1808,12 +1808,14 @@ export function handleFormulaError({error;}) {let position: number;+ let identifierLength: number;if (error.extra?.columnName) {const identifierMatch = formula.match(new RegExp(`\\b${error.extra?.columnName}\\b`));if (typeof identifierMatch?.index === 'number') {position = identifierMatch.index;+ identifierLength = error.extra.columnName.length;}} else {const message: string = error.message;@@ -1822,9 +1824,10 @@ export function handleFormulaError({const errorAtRegex = message.match(REGEX_ERROR_AT_CHARACTER);if (errorAtRegex?.[0]) {position = Number(errorAtRegex[1]);+ identifierLength = 1;}}- if (position) {+ if (typeof position === 'number') {const colRowPosition = getRowColPositionFromIndex({stack: formula,position,@@ -1834,9 +1837,9 @@ export function handleFormulaError({error.type ?? FormulaErrorType.INVALID_SYNTAX,{...(error.extra ?? {}),- position: colRowPosition,+ position: { ...colRowPosition, length: identifierLength },},- 'Required arguments missing'+ error.message);} else {throw error;