@codemirror/lint 6.2.1 → 6.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,3 +1,15 @@
1
+ ## 6.3.0 (2023-06-23)
2
+
3
+ ### New features
4
+
5
+ A new `previousDiagnostic` command can be used to move back through the active diagnostics.
6
+
7
+ ## 6.2.2 (2023-06-05)
8
+
9
+ ### Bug fixes
10
+
11
+ Make sure lint gutter tooltips are properly closed when the content of their line changes.
12
+
1
13
  ## 6.2.1 (2023-04-13)
2
14
 
3
15
  ### Bug fixes
package/dist/index.cjs CHANGED
@@ -55,7 +55,8 @@ function findDiagnostic(diagnostics, diagnostic = null, after = 0) {
55
55
  return found;
56
56
  }
57
57
  function hideTooltip(tr, tooltip) {
58
- return !!(tr.effects.some(e => e.is(setDiagnosticsEffect)) || tr.changes.touchesRange(tooltip.pos));
58
+ let line = tr.startState.doc.lineAt(tooltip.pos);
59
+ return !!(tr.effects.some(e => e.is(setDiagnosticsEffect)) || tr.changes.touchesRange(line.from, line.to));
59
60
  }
60
61
  function maybeEnableLint(state$1, effects) {
61
62
  return state$1.field(lintState, false) ? effects : effects.concat(state.StateEffect.appendConfig.of(lintExtensions));
@@ -181,6 +182,30 @@ const nextDiagnostic = (view) => {
181
182
  return true;
182
183
  };
183
184
  /**
185
+ Move the selection to the previous diagnostic.
186
+ */
187
+ const previousDiagnostic = (view) => {
188
+ let { state } = view, field = state.field(lintState, false);
189
+ if (!field)
190
+ return false;
191
+ let sel = state.selection.main;
192
+ let prevFrom, prevTo, lastFrom, lastTo;
193
+ field.diagnostics.between(0, state.doc.length, (from, to) => {
194
+ if (to < sel.to && (prevFrom == null || prevFrom < from)) {
195
+ prevFrom = from;
196
+ prevTo = to;
197
+ }
198
+ if (lastFrom == null || from > lastFrom) {
199
+ lastFrom = from;
200
+ lastTo = to;
201
+ }
202
+ });
203
+ if (lastFrom == null || prevFrom == null && lastFrom == sel.from)
204
+ return false;
205
+ view.dispatch({ selection: { anchor: prevFrom !== null && prevFrom !== void 0 ? prevFrom : lastFrom, head: prevTo !== null && prevTo !== void 0 ? prevTo : lastTo }, scrollIntoView: true });
206
+ return true;
207
+ };
208
+ /**
184
209
  A set of default key bindings for the lint functionality.
185
210
 
186
211
  - Ctrl-Shift-m (Cmd-Shift-m on macOS): [`openLintPanel`](https://codemirror.net/6/docs/ref/#lint.openLintPanel)
@@ -612,8 +637,8 @@ class LintGutterMarker extends view.GutterMarker {
612
637
  function trackHoverOn(view, marker) {
613
638
  let mousemove = (event) => {
614
639
  let rect = marker.getBoundingClientRect();
615
- if (event.clientX > rect.left - 10 /* Hover.Margin */ && event.clientX < rect.right + 10 /* Hover.Margin */ &&
616
- event.clientY > rect.top - 10 /* Hover.Margin */ && event.clientY < rect.bottom + 10 /* Hover.Margin */)
640
+ if (event.clientX > rect.left - 10 /* Margin */ && event.clientX < rect.right + 10 /* Margin */ &&
641
+ event.clientY > rect.top - 10 /* Margin */ && event.clientY < rect.bottom + 10 /* Margin */)
617
642
  return;
618
643
  for (let target = event.target; target; target = target.parentNode) {
619
644
  if (target.nodeType == 1 && target.classList.contains("cm-tooltip-lint"))
@@ -734,7 +759,7 @@ const lintExtensions = [
734
759
  const lintGutterConfig = state.Facet.define({
735
760
  combine(configs) {
736
761
  return state.combineConfig(configs, {
737
- hoverTime: 300 /* Hover.Time */,
762
+ hoverTime: 300 /* Time */,
738
763
  markerFilter: null,
739
764
  tooltipFilter: null
740
765
  });
@@ -771,5 +796,6 @@ exports.lintKeymap = lintKeymap;
771
796
  exports.linter = linter;
772
797
  exports.nextDiagnostic = nextDiagnostic;
773
798
  exports.openLintPanel = openLintPanel;
799
+ exports.previousDiagnostic = previousDiagnostic;
774
800
  exports.setDiagnostics = setDiagnostics;
775
801
  exports.setDiagnosticsEffect = setDiagnosticsEffect;
@@ -0,0 +1,167 @@
1
+ import * as _codemirror_state from '@codemirror/state';
2
+ import { EditorState, TransactionSpec, Extension } from '@codemirror/state';
3
+ import { EditorView, Command, KeyBinding, ViewUpdate } from '@codemirror/view';
4
+
5
+ /**
6
+ Describes a problem or hint for a piece of code.
7
+ */
8
+ interface Diagnostic {
9
+ /**
10
+ The start position of the relevant text.
11
+ */
12
+ from: number;
13
+ /**
14
+ The end position. May be equal to `from`, though actually
15
+ covering text is preferable.
16
+ */
17
+ to: number;
18
+ /**
19
+ The severity of the problem. This will influence how it is
20
+ displayed.
21
+ */
22
+ severity: "info" | "warning" | "error";
23
+ /**
24
+ An optional source string indicating where the diagnostic is
25
+ coming from. You can put the name of your linter here, if
26
+ applicable.
27
+ */
28
+ source?: string;
29
+ /**
30
+ The message associated with this diagnostic.
31
+ */
32
+ message: string;
33
+ /**
34
+ An optional custom rendering function that displays the message
35
+ as a DOM node.
36
+ */
37
+ renderMessage?: () => Node;
38
+ /**
39
+ An optional array of actions that can be taken on this
40
+ diagnostic.
41
+ */
42
+ actions?: readonly Action[];
43
+ }
44
+ /**
45
+ An action associated with a diagnostic.
46
+ */
47
+ interface Action {
48
+ /**
49
+ The label to show to the user. Should be relatively short.
50
+ */
51
+ name: string;
52
+ /**
53
+ The function to call when the user activates this action. Is
54
+ given the diagnostic's _current_ position, which may have
55
+ changed since the creation of the diagnostic, due to editing.
56
+ */
57
+ apply: (view: EditorView, from: number, to: number) => void;
58
+ }
59
+ declare type DiagnosticFilter = (diagnostics: readonly Diagnostic[]) => Diagnostic[];
60
+ interface LintConfig {
61
+ /**
62
+ Time to wait (in milliseconds) after a change before running
63
+ the linter. Defaults to 750ms.
64
+ */
65
+ delay?: number;
66
+ /**
67
+ Optional predicate that can be used to indicate when diagnostics
68
+ need to be recomputed. Linting is always re-done on document
69
+ changes.
70
+ */
71
+ needsRefresh?: null | ((update: ViewUpdate) => boolean);
72
+ /**
73
+ Optional filter to determine which diagnostics produce markers
74
+ in the content.
75
+ */
76
+ markerFilter?: null | DiagnosticFilter;
77
+ /**
78
+ Filter applied to a set of diagnostics shown in a tooltip. No
79
+ tooltip will appear if the empty set is returned.
80
+ */
81
+ tooltipFilter?: null | DiagnosticFilter;
82
+ }
83
+ interface LintGutterConfig {
84
+ /**
85
+ The delay before showing a tooltip when hovering over a lint gutter marker.
86
+ */
87
+ hoverTime?: number;
88
+ /**
89
+ Optional filter determining which diagnostics show a marker in
90
+ the gutter.
91
+ */
92
+ markerFilter?: null | DiagnosticFilter;
93
+ /**
94
+ Optional filter for diagnostics displayed in a tooltip, which
95
+ can also be used to prevent a tooltip appearing.
96
+ */
97
+ tooltipFilter?: null | DiagnosticFilter;
98
+ }
99
+ /**
100
+ Returns a transaction spec which updates the current set of
101
+ diagnostics, and enables the lint extension if if wasn't already
102
+ active.
103
+ */
104
+ declare function setDiagnostics(state: EditorState, diagnostics: readonly Diagnostic[]): TransactionSpec;
105
+ /**
106
+ The state effect that updates the set of active diagnostics. Can
107
+ be useful when writing an extension that needs to track these.
108
+ */
109
+ declare const setDiagnosticsEffect: _codemirror_state.StateEffectType<readonly Diagnostic[]>;
110
+ /**
111
+ Returns the number of active lint diagnostics in the given state.
112
+ */
113
+ declare function diagnosticCount(state: EditorState): number;
114
+ /**
115
+ Command to open and focus the lint panel.
116
+ */
117
+ declare const openLintPanel: Command;
118
+ /**
119
+ Command to close the lint panel, when open.
120
+ */
121
+ declare const closeLintPanel: Command;
122
+ /**
123
+ Move the selection to the next diagnostic.
124
+ */
125
+ declare const nextDiagnostic: Command;
126
+ /**
127
+ Move the selection to the previous diagnostic.
128
+ */
129
+ declare const previousDiagnostic: Command;
130
+ /**
131
+ A set of default key bindings for the lint functionality.
132
+
133
+ - Ctrl-Shift-m (Cmd-Shift-m on macOS): [`openLintPanel`](https://codemirror.net/6/docs/ref/#lint.openLintPanel)
134
+ - F8: [`nextDiagnostic`](https://codemirror.net/6/docs/ref/#lint.nextDiagnostic)
135
+ */
136
+ declare const lintKeymap: readonly KeyBinding[];
137
+ /**
138
+ The type of a function that produces diagnostics.
139
+ */
140
+ declare type LintSource = (view: EditorView) => readonly Diagnostic[] | Promise<readonly Diagnostic[]>;
141
+ /**
142
+ Given a diagnostic source, this function returns an extension that
143
+ enables linting with that source. It will be called whenever the
144
+ editor is idle (after its content changed).
145
+ */
146
+ declare function linter(source: LintSource, config?: LintConfig): Extension;
147
+ /**
148
+ Forces any linters [configured](https://codemirror.net/6/docs/ref/#lint.linter) to run when the
149
+ editor is idle to run right away.
150
+ */
151
+ declare function forceLinting(view: EditorView): void;
152
+ /**
153
+ Returns an extension that installs a gutter showing markers for
154
+ each line that has diagnostics, which can be hovered over to see
155
+ the diagnostics.
156
+ */
157
+ declare function lintGutter(config?: LintGutterConfig): Extension;
158
+ /**
159
+ Iterate over the marked diagnostics for the given editor state,
160
+ calling `f` for each of them. Note that, if the document changed
161
+ since the diagnostics were created, the `Diagnostic` object will
162
+ hold the original outdated position, whereas the `to` and `from`
163
+ arguments hold the diagnostic's current position.
164
+ */
165
+ declare function forEachDiagnostic(state: EditorState, f: (d: Diagnostic, from: number, to: number) => void): void;
166
+
167
+ export { Action, Diagnostic, LintSource, closeLintPanel, diagnosticCount, forEachDiagnostic, forceLinting, lintGutter, lintKeymap, linter, nextDiagnostic, openLintPanel, previousDiagnostic, setDiagnostics, setDiagnosticsEffect };
package/dist/index.d.ts CHANGED
@@ -124,6 +124,10 @@ Move the selection to the next diagnostic.
124
124
  */
125
125
  declare const nextDiagnostic: Command;
126
126
  /**
127
+ Move the selection to the previous diagnostic.
128
+ */
129
+ declare const previousDiagnostic: Command;
130
+ /**
127
131
  A set of default key bindings for the lint functionality.
128
132
 
129
133
  - Ctrl-Shift-m (Cmd-Shift-m on macOS): [`openLintPanel`](https://codemirror.net/6/docs/ref/#lint.openLintPanel)
@@ -160,4 +164,4 @@ arguments hold the diagnostic's current position.
160
164
  */
161
165
  declare function forEachDiagnostic(state: EditorState, f: (d: Diagnostic, from: number, to: number) => void): void;
162
166
 
163
- export { Action, Diagnostic, LintSource, closeLintPanel, diagnosticCount, forEachDiagnostic, forceLinting, lintGutter, lintKeymap, linter, nextDiagnostic, openLintPanel, setDiagnostics, setDiagnosticsEffect };
167
+ export { Action, Diagnostic, LintSource, closeLintPanel, diagnosticCount, forEachDiagnostic, forceLinting, lintGutter, lintKeymap, linter, nextDiagnostic, openLintPanel, previousDiagnostic, setDiagnostics, setDiagnosticsEffect };
package/dist/index.js CHANGED
@@ -47,7 +47,8 @@ function findDiagnostic(diagnostics, diagnostic = null, after = 0) {
47
47
  return found;
48
48
  }
49
49
  function hideTooltip(tr, tooltip) {
50
- return !!(tr.effects.some(e => e.is(setDiagnosticsEffect)) || tr.changes.touchesRange(tooltip.pos));
50
+ let line = tr.startState.doc.lineAt(tooltip.pos);
51
+ return !!(tr.effects.some(e => e.is(setDiagnosticsEffect)) || tr.changes.touchesRange(line.from, line.to));
51
52
  }
52
53
  function maybeEnableLint(state, effects) {
53
54
  return state.field(lintState, false) ? effects : effects.concat(StateEffect.appendConfig.of(lintExtensions));
@@ -173,6 +174,30 @@ const nextDiagnostic = (view) => {
173
174
  return true;
174
175
  };
175
176
  /**
177
+ Move the selection to the previous diagnostic.
178
+ */
179
+ const previousDiagnostic = (view) => {
180
+ let { state } = view, field = state.field(lintState, false);
181
+ if (!field)
182
+ return false;
183
+ let sel = state.selection.main;
184
+ let prevFrom, prevTo, lastFrom, lastTo;
185
+ field.diagnostics.between(0, state.doc.length, (from, to) => {
186
+ if (to < sel.to && (prevFrom == null || prevFrom < from)) {
187
+ prevFrom = from;
188
+ prevTo = to;
189
+ }
190
+ if (lastFrom == null || from > lastFrom) {
191
+ lastFrom = from;
192
+ lastTo = to;
193
+ }
194
+ });
195
+ if (lastFrom == null || prevFrom == null && lastFrom == sel.from)
196
+ return false;
197
+ view.dispatch({ selection: { anchor: prevFrom !== null && prevFrom !== void 0 ? prevFrom : lastFrom, head: prevTo !== null && prevTo !== void 0 ? prevTo : lastTo }, scrollIntoView: true });
198
+ return true;
199
+ };
200
+ /**
176
201
  A set of default key bindings for the lint functionality.
177
202
 
178
203
  - Ctrl-Shift-m (Cmd-Shift-m on macOS): [`openLintPanel`](https://codemirror.net/6/docs/ref/#lint.openLintPanel)
@@ -604,8 +629,8 @@ class LintGutterMarker extends GutterMarker {
604
629
  function trackHoverOn(view, marker) {
605
630
  let mousemove = (event) => {
606
631
  let rect = marker.getBoundingClientRect();
607
- if (event.clientX > rect.left - 10 /* Hover.Margin */ && event.clientX < rect.right + 10 /* Hover.Margin */ &&
608
- event.clientY > rect.top - 10 /* Hover.Margin */ && event.clientY < rect.bottom + 10 /* Hover.Margin */)
632
+ if (event.clientX > rect.left - 10 /* Margin */ && event.clientX < rect.right + 10 /* Margin */ &&
633
+ event.clientY > rect.top - 10 /* Margin */ && event.clientY < rect.bottom + 10 /* Margin */)
609
634
  return;
610
635
  for (let target = event.target; target; target = target.parentNode) {
611
636
  if (target.nodeType == 1 && target.classList.contains("cm-tooltip-lint"))
@@ -726,7 +751,7 @@ const lintExtensions = [
726
751
  const lintGutterConfig = /*@__PURE__*/Facet.define({
727
752
  combine(configs) {
728
753
  return combineConfig(configs, {
729
- hoverTime: 300 /* Hover.Time */,
754
+ hoverTime: 300 /* Time */,
730
755
  markerFilter: null,
731
756
  tooltipFilter: null
732
757
  });
@@ -754,4 +779,4 @@ function forEachDiagnostic(state, f) {
754
779
  f(iter.value.spec.diagnostic, iter.from, iter.to);
755
780
  }
756
781
 
757
- export { closeLintPanel, diagnosticCount, forEachDiagnostic, forceLinting, lintGutter, lintKeymap, linter, nextDiagnostic, openLintPanel, setDiagnostics, setDiagnosticsEffect };
782
+ export { closeLintPanel, diagnosticCount, forEachDiagnostic, forceLinting, lintGutter, lintKeymap, linter, nextDiagnostic, openLintPanel, previousDiagnostic, setDiagnostics, setDiagnosticsEffect };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@codemirror/lint",
3
- "version": "6.2.1",
3
+ "version": "6.3.0",
4
4
  "description": "Linting support for the CodeMirror code editor",
5
5
  "scripts": {
6
6
  "test": "cm-runtests",
@@ -31,7 +31,7 @@
31
31
  "crelt": "^1.0.5"
32
32
  },
33
33
  "devDependencies": {
34
- "@codemirror/buildhelper": "^0.1.0"
34
+ "@codemirror/buildhelper": "^1.0.0"
35
35
  },
36
36
  "repository": {
37
37
  "type": "git",