Skip to content

Commit

Permalink
feat: add casbin linter to editor component
Browse files Browse the repository at this point in the history
  • Loading branch information
HashCookie committed Aug 31, 2024
1 parent 02b7efc commit a7aebab
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 0 deletions.
3 changes: 3 additions & 0 deletions app/components/editor/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}
Expand Down
24 changes: 24 additions & 0 deletions app/utils/casbinLinter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Diagnostic } from '@codemirror/lint';
import { EditorView } from '@codemirror/view';
import { Config } from 'casbin';

export const casbinLinter = (view: EditorView): Diagnostic[] => {
const diagnostics: Diagnostic[] = [];
try {
Config.newConfigFromText(view.state.doc.toString());
} catch (e) {
const error = e as Error;
const lineMatch = error.message.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: error.message,
});
}
}
return diagnostics;
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 9 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit a7aebab

Please sign in to comment.