diff --git a/app/components/editor/hooks/useRunTest.tsx b/app/components/editor/hooks/useRunTest.tsx index 71b592e..60e2542 100755 --- a/app/components/editor/hooks/useRunTest.tsx +++ b/app/components/editor/hooks/useRunTest.tsx @@ -15,6 +15,7 @@ import React from 'react'; import { DefaultRoleManager, newEnforcer, newModel, StringAdapter, Util } from 'casbin'; import { newEnforceContext } from '@/app/components/editor/hooks/useSetupEnforceContext'; +import { setError } from '@/app/utils/errorManager'; interface RunTestProps { model: string; @@ -178,11 +179,15 @@ async function enforcer(props: RunTestProps) { const stopTime = performance.now(); + setError(null); + props.onResponse(
{'Done in ' + (stopTime - startTime).toFixed(2) + 'ms'}
); props.onResponse(result); } catch (e) { - props.onResponse(
{(e as any).message}
); + const errorMessage = (e as any).message; + props.onResponse(
{errorMessage}
); props.onResponse([]); + setError(errorMessage); } } diff --git a/app/components/editor/index.tsx b/app/components/editor/index.tsx index 1c91ba4..e5abb97 100755 --- a/app/components/editor/index.tsx +++ b/app/components/editor/index.tsx @@ -23,6 +23,8 @@ import { extractPageContent } from '../../utils/contentExtractor'; import { buttonPlugin } from './ButtonPlugin'; import { useLang } from '@/app/context/LangContext'; import LanguageMenu from '@/app/components/LanguageMenu'; +import { linter } from '@codemirror/lint'; +import { casbinLinter } from '@/app/utils/casbinLinter'; export const EditorScreen = () => { const { @@ -238,6 +240,7 @@ export const EditorScreen = () => { indentUnit.of(' '), EditorView.lineWrapping, buttonPlugin(openDrawerWithMessage, extractContent, 'model'), + linter(casbinLinter), ]} className={'function flex-grow h-[300px]'} value={modelText} diff --git a/app/utils/casbinLinter.ts b/app/utils/casbinLinter.ts new file mode 100644 index 0000000..ae36057 --- /dev/null +++ b/app/utils/casbinLinter.ts @@ -0,0 +1,31 @@ +import { Diagnostic } from '@codemirror/lint'; +import { EditorView } from '@codemirror/view'; +import { getError } from './errorManager'; + +export const casbinLinter = (view: EditorView): Diagnostic[] => { + const diagnostics: Diagnostic[] = []; + + const runTestError = getError(); + if (runTestError) { + const lineMatch = runTestError.match(/line (\d+)/); + if (lineMatch) { + const errorLine = parseInt(lineMatch[1], 10); + const line = view.state.doc.line(errorLine); + diagnostics.push({ + from: line.from, + to: line.to, + severity: 'error', + message: runTestError, + }); + } else { + diagnostics.push({ + from: 0, + to: view.state.doc.length, + severity: 'error', + message: runTestError, + }); + } + } + + return diagnostics; +}; diff --git a/app/utils/errorManager.ts b/app/utils/errorManager.ts new file mode 100644 index 0000000..857cb5c --- /dev/null +++ b/app/utils/errorManager.ts @@ -0,0 +1,7 @@ +let currentError: string | null = null; + +export const setError = (error: string | null) => { + currentError = error; +}; + +export const getError = () => {return currentError}; \ No newline at end of file diff --git a/package.json b/package.json index e2fe628..81d763f 100644 --- a/package.json +++ b/package.json @@ -79,6 +79,7 @@ "@codemirror/lang-javascript": "^6.2.1", "@codemirror/language": "^6.10.1", "@codemirror/legacy-modes": "^6.3.3", + "@codemirror/lint": "^6.8.1", "@codemirror/state": "^6.4.1", "@codemirror/view": "^6.24.1", "@lezer/highlight": "^1.2.0", diff --git a/yarn.lock b/yarn.lock index bcad127..3418a76 100644 --- a/yarn.lock +++ b/yarn.lock @@ -80,6 +80,15 @@ "@codemirror/view" "^6.0.0" crelt "^1.0.5" +"@codemirror/lint@^6.8.1": + version "6.8.1" + resolved "https://registry.yarnpkg.com/@codemirror/lint/-/lint-6.8.1.tgz#6427848815baaf68c08e98c7673b804d3d8c0e7f" + integrity sha512-IZ0Y7S4/bpaunwggW2jYqwLuHj0QtESf5xcROewY6+lDNwZ/NzvR4t+vpYgg9m7V8UXLPYqG+lu3DF470E5Oxg== + dependencies: + "@codemirror/state" "^6.0.0" + "@codemirror/view" "^6.0.0" + crelt "^1.0.5" + "@codemirror/search@^6.0.0": version "6.5.6" resolved "https://registry.yarnpkg.com/@codemirror/search/-/search-6.5.6.tgz#8f858b9e678d675869112e475f082d1e8488db93"