generated from Exabyte-io/template-definitions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update: refactor linter to use codemirror guidance regarding stete up…
…dates
- Loading branch information
Showing
2 changed files
with
47 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,49 @@ | ||
import { Diagnostic } from "@codemirror/lint"; | ||
import { Annotation, StateField } from "@codemirror/state"; | ||
import { EditorView } from "@codemirror/view"; | ||
// eslint-disable-next-line import/no-extraneous-dependencies | ||
import { ConsistencyChecks } from "@exabyte-io/code.js/src/types"; | ||
import _ from "underscore"; | ||
|
||
const linterGenerator = (checks: ConsistencyChecks | undefined) => { | ||
return (view: EditorView): Diagnostic[] => { | ||
const { doc } = view.state; | ||
if (!checks || !doc) return []; | ||
export const ChecksAnnotation = Annotation.define<{ checks: ConsistencyChecks }>(); | ||
|
||
const warnings = checks.messages.map((check) => { | ||
export const checksStateField = StateField.define({ | ||
create() { | ||
return []; | ||
}, // Initializes with an empty array | ||
// @ts-ignore | ||
update(value, tr) { | ||
if (tr.annotation(ChecksAnnotation)) { | ||
// @ts-ignore | ||
return tr.annotation(ChecksAnnotation).checks; | ||
} | ||
return value; | ||
}, | ||
}); | ||
const exaxyzLinter = (view: EditorView): Diagnostic[] => { | ||
const checks: ConsistencyChecks | undefined = view.state.field(checksStateField) as | ||
| ConsistencyChecks | ||
| undefined; | ||
|
||
if (!checks) return []; | ||
if (Object.keys(checks).length === 0) return []; | ||
|
||
return checks.messages | ||
.map((check) => { | ||
const keyFragments = check.key.split("."); | ||
const atomIdStr = _.last(keyFragments); | ||
if (!atomIdStr) return null; | ||
|
||
const atomId = parseInt(atomIdStr, 10); | ||
const lineNumber = atomId + 1; // codemirror counts from 1 | ||
const lineNumber = atomId + 1; | ||
return { | ||
message: check.message, | ||
severity: check.severity, | ||
from: doc.line(lineNumber).from, | ||
to: doc.line(lineNumber).to, | ||
from: view.state.doc.line(lineNumber).from, | ||
to: view.state.doc.line(lineNumber).to, | ||
}; | ||
}); | ||
|
||
return warnings.filter(Boolean) as Diagnostic[]; | ||
}; | ||
}) | ||
.filter(Boolean) as Diagnostic[]; | ||
}; | ||
|
||
export { linterGenerator }; | ||
export { exaxyzLinter }; |