Display KCL errors as CodeMirror diagnostics in the editor's gutter (#197)

Also updates the CodeMirror version
This commit is contained in:
Adam Chalmers
2023-08-03 15:56:11 -05:00
committed by GitHub
parent 231371fb16
commit 8c5d7bf648
3 changed files with 25 additions and 3 deletions

View File

@ -5,6 +5,7 @@ import { asyncLexer } from './lang/tokeniser'
import { abstractSyntaxTree } from './lang/abstractSyntaxTree' import { abstractSyntaxTree } from './lang/abstractSyntaxTree'
import { _executor, ExtrudeGroup, SketchGroup } from './lang/executor' import { _executor, ExtrudeGroup, SketchGroup } from './lang/executor'
import CodeMirror from '@uiw/react-codemirror' import CodeMirror from '@uiw/react-codemirror'
import { linter, lintGutter, Diagnostic } from '@codemirror/lint'
import { javascript } from '@codemirror/lang-javascript' import { javascript } from '@codemirror/lang-javascript'
import { ViewUpdate } from '@codemirror/view' import { ViewUpdate } from '@codemirror/view'
import { import {
@ -22,7 +23,7 @@ import { EngineCommandManager } from './lang/std/engineConnection'
import { isOverlap } from './lib/utils' import { isOverlap } from './lib/utils'
import { AppHeader } from './components/AppHeader' import { AppHeader } from './components/AppHeader'
import { isTauri } from './lib/isTauri' import { isTauri } from './lib/isTauri'
import { KCLError } from './lang/errors' import { KCLError, kclErrToDiagnostic } from './lang/errors'
export function App() { export function App() {
const cam = useRef() const cam = useRef()
@ -291,7 +292,14 @@ export function App() {
<CodeMirror <CodeMirror
className="h-full" className="h-full"
value={code} value={code}
extensions={[javascript({ jsx: true }), lineHighlightField]} extensions={[
javascript({ jsx: true }),
lineHighlightField,
lintGutter(),
linter((_view) => {
return kclErrToDiagnostic(useStore.getState().kclErrors)
}),
]}
onChange={onChange} onChange={onChange}
onUpdate={onUpdate} onUpdate={onUpdate}
theme={theme} theme={theme}

View File

@ -1,3 +1,5 @@
import { Diagnostic } from '@codemirror/lint'
export class KCLError { export class KCLError {
kind: string | undefined kind: string | undefined
sourceRanges: [number, number][] sourceRanges: [number, number][]
@ -55,3 +57,15 @@ export class KCLUndefinedValueError extends KCLError {
Object.setPrototypeOf(this, KCLUndefinedValueError.prototype) Object.setPrototypeOf(this, KCLUndefinedValueError.prototype)
} }
} }
/**
* Maps the KCL errors to an array of CodeMirror diagnostics.
* Currently the diagnostics are all errors, but in the future they could include lints.
* */
export function kclErrToDiagnostic(errors: KCLError[]): Diagnostic[] {
return errors.flatMap((err) => {
return err.sourceRanges.map(([from, to]) => {
return { from, to, message: err.msg, severity: 'error' }
})
})
}

View File

@ -20,7 +20,7 @@ import {
SourceRangeMap, SourceRangeMap,
EngineCommandManager, EngineCommandManager,
} from './lang/std/engineConnection' } from './lang/std/engineConnection'
import { KCLError } from './lang/errors' import { KCLError, KCLUndefinedValueError } from './lang/errors'
export type Selection = { export type Selection = {
type: 'default' | 'line-end' | 'line-mid' type: 'default' | 'line-end' | 'line-mid'