linting.ts 723 B

1234567891011121314151617181920212223242526
  1. import { Compartment, EditorState } from '@codemirror/state'
  2. import { setSyntaxValidationEffect } from './language'
  3. import { linter } from '@codemirror/lint'
  4. export const createLinter: typeof linter = (lintSource, config) => {
  5. const linterConfig = new Compartment()
  6. return [
  7. linterConfig.of([]),
  8. // enable/disable the linter to match the syntaxValidation setting
  9. EditorState.transactionExtender.of(tr => {
  10. for (const effect of tr.effects) {
  11. if (effect.is(setSyntaxValidationEffect)) {
  12. return {
  13. effects: linterConfig.reconfigure(
  14. effect.value ? linter(lintSource, config) : []
  15. ),
  16. }
  17. }
  18. }
  19. return null
  20. }),
  21. ]
  22. }