index.d.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. import * as _codemirror_state from '@codemirror/state';
  2. import { EditorState, TransactionSpec, Extension } from '@codemirror/state';
  3. import { EditorView, Command, KeyBinding } from '@codemirror/view';
  4. /**
  5. Describes a problem or hint for a piece of code.
  6. */
  7. interface Diagnostic {
  8. /**
  9. The start position of the relevant text.
  10. */
  11. from: number;
  12. /**
  13. The end position. May be equal to `from`, though actually
  14. covering text is preferable.
  15. */
  16. to: number;
  17. /**
  18. The severity of the problem. This will influence how it is
  19. displayed.
  20. */
  21. severity: "info" | "warning" | "error";
  22. /**
  23. An optional source string indicating where the diagnostic is
  24. coming from. You can put the name of your linter here, if
  25. applicable.
  26. */
  27. source?: string;
  28. /**
  29. The message associated with this diagnostic.
  30. */
  31. message: string;
  32. /**
  33. An optional custom rendering function that displays the message
  34. as a DOM node.
  35. */
  36. renderMessage?: () => Node;
  37. /**
  38. An optional array of actions that can be taken on this
  39. diagnostic.
  40. */
  41. actions?: readonly Action[];
  42. }
  43. /**
  44. An action associated with a diagnostic.
  45. */
  46. interface Action {
  47. /**
  48. The label to show to the user. Should be relatively short.
  49. */
  50. name: string;
  51. /**
  52. The function to call when the user activates this action. Is
  53. given the diagnostic's _current_ position, which may have
  54. changed since the creation of the diagnostic, due to editing.
  55. */
  56. apply: (view: EditorView, from: number, to: number) => void;
  57. }
  58. declare type DiagnosticFilter = (diagnostics: readonly Diagnostic[]) => Diagnostic[];
  59. interface LintConfig {
  60. /**
  61. Time to wait (in milliseconds) after a change before running
  62. the linter. Defaults to 750ms.
  63. */
  64. delay?: number;
  65. /**
  66. Optional filter to determine which diagnostics produce markers
  67. in the content.
  68. */
  69. markerFilter?: null | DiagnosticFilter;
  70. /**
  71. Filter applied to a set of diagnostics shown in a tooltip. No
  72. tooltip will appear if the empty set is returned.
  73. */
  74. tooltipFilter?: null | DiagnosticFilter;
  75. }
  76. interface LintGutterConfig {
  77. /**
  78. The delay before showing a tooltip when hovering over a lint gutter marker.
  79. */
  80. hoverTime?: number;
  81. /**
  82. Optional filter determining which diagnostics show a marker in
  83. the gutter.
  84. */
  85. markerFilter?: null | DiagnosticFilter;
  86. /**
  87. Optional filter for diagnostics displayed in a tooltip, which
  88. can also be used to prevent a tooltip appearing.
  89. */
  90. tooltipFilter?: null | DiagnosticFilter;
  91. }
  92. /**
  93. Returns a transaction spec which updates the current set of
  94. diagnostics, and enables the lint extension if if wasn't already
  95. active.
  96. */
  97. declare function setDiagnostics(state: EditorState, diagnostics: readonly Diagnostic[]): TransactionSpec;
  98. /**
  99. The state effect that updates the set of active diagnostics. Can
  100. be useful when writing an extension that needs to track these.
  101. */
  102. declare const setDiagnosticsEffect: _codemirror_state.StateEffectType<readonly Diagnostic[]>;
  103. /**
  104. Returns the number of active lint diagnostics in the given state.
  105. */
  106. declare function diagnosticCount(state: EditorState): number;
  107. /**
  108. Command to open and focus the lint panel.
  109. */
  110. declare const openLintPanel: Command;
  111. /**
  112. Command to close the lint panel, when open.
  113. */
  114. declare const closeLintPanel: Command;
  115. /**
  116. Move the selection to the next diagnostic.
  117. */
  118. declare const nextDiagnostic: Command;
  119. /**
  120. A set of default key bindings for the lint functionality.
  121. - Ctrl-Shift-m (Cmd-Shift-m on macOS): [`openLintPanel`](https://codemirror.net/6/docs/ref/#lint.openLintPanel)
  122. - F8: [`nextDiagnostic`](https://codemirror.net/6/docs/ref/#lint.nextDiagnostic)
  123. */
  124. declare const lintKeymap: readonly KeyBinding[];
  125. /**
  126. The type of a function that produces diagnostics.
  127. */
  128. declare type LintSource = (view: EditorView) => readonly Diagnostic[] | Promise<readonly Diagnostic[]>;
  129. /**
  130. Given a diagnostic source, this function returns an extension that
  131. enables linting with that source. It will be called whenever the
  132. editor is idle (after its content changed).
  133. */
  134. declare function linter(source: LintSource, config?: LintConfig): Extension;
  135. /**
  136. Forces any linters [configured](https://codemirror.net/6/docs/ref/#lint.linter) to run when the
  137. editor is idle to run right away.
  138. */
  139. declare function forceLinting(view: EditorView): void;
  140. /**
  141. Returns an extension that installs a gutter showing markers for
  142. each line that has diagnostics, which can be hovered over to see
  143. the diagnostics.
  144. */
  145. declare function lintGutter(config?: LintGutterConfig): Extension;
  146. export { Action, Diagnostic, LintSource, closeLintPanel, diagnosticCount, forceLinting, lintGutter, lintKeymap, linter, nextDiagnostic, openLintPanel, setDiagnostics, setDiagnosticsEffect };