Skip to content

Commit

Permalink
Pass the editor state to diagnostics filters
Browse files Browse the repository at this point in the history
FEATURE: `markerFilter` and `tooltipFilter` function now get passed the
current editor state.

Issue codemirror/dev#1327
  • Loading branch information
marijnh committed Jan 30, 2024
1 parent 79db0c9 commit fe6bc58
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions src/lint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export interface Action {
apply: (view: EditorView, from: number, to: number) => void
}

type DiagnosticFilter = (diagnostics: readonly Diagnostic[]) => Diagnostic[]
type DiagnosticFilter = (diagnostics: readonly Diagnostic[], state: EditorState) => Diagnostic[]

interface LintConfig {
/// Time to wait (in milliseconds) after a change before running
Expand Down Expand Up @@ -88,7 +88,7 @@ class LintState {
let markedDiagnostics = diagnostics
let diagnosticFilter = state.facet(lintConfig).markerFilter
if (diagnosticFilter)
markedDiagnostics = diagnosticFilter(markedDiagnostics)
markedDiagnostics = diagnosticFilter(markedDiagnostics, state)

let ranges = Decoration.set(markedDiagnostics.map((d: Diagnostic) => {
// For zero-length ranges or ranges covering only a line break, create a widget
Expand Down Expand Up @@ -194,7 +194,7 @@ function lintTooltip(view: EditorView, pos: number, side: -1 | 1) {
})

let diagnosticFilter = view.state.facet(lintConfig).tooltipFilter
if (diagnosticFilter) found = diagnosticFilter(found)
if (diagnosticFilter) found = diagnosticFilter(found, view.state)

if (!found.length) return null

Expand Down Expand Up @@ -704,7 +704,7 @@ class LintGutterMarker extends GutterMarker {

let diagnostics = this.diagnostics
let diagnosticsFilter = view.state.facet(lintGutterConfig).tooltipFilter
if (diagnosticsFilter) diagnostics = diagnosticsFilter(diagnostics)
if (diagnosticsFilter) diagnostics = diagnosticsFilter(diagnostics, view.state)

if (diagnostics.length)
elt.onmouseover = () => gutterMarkerMouseOver(view, elt, diagnostics)
Expand Down Expand Up @@ -797,7 +797,7 @@ const lintGutterMarkers = StateField.define<RangeSet<GutterMarker>>({
if (effect.is(setDiagnosticsEffect)) {
let diagnostics = effect.value
if (diagnosticFilter)
diagnostics = diagnosticFilter(diagnostics || [])
diagnostics = diagnosticFilter(diagnostics || [], tr.state)
markers = markersForDiagnostics(tr.state.doc, diagnostics.slice(0))
}
}
Expand Down

0 comments on commit fe6bc58

Please sign in to comment.