diff options
Diffstat (limited to 'subprojects/frontend/src/editor/EditorStore.ts')
-rw-r--r-- | subprojects/frontend/src/editor/EditorStore.ts | 53 |
1 files changed, 30 insertions, 23 deletions
diff --git a/subprojects/frontend/src/editor/EditorStore.ts b/subprojects/frontend/src/editor/EditorStore.ts index 4ee24779..acad3d09 100644 --- a/subprojects/frontend/src/editor/EditorStore.ts +++ b/subprojects/frontend/src/editor/EditorStore.ts | |||
@@ -11,6 +11,7 @@ import { | |||
11 | type Transaction, | 11 | type Transaction, |
12 | type TransactionSpec, | 12 | type TransactionSpec, |
13 | type EditorState, | 13 | type EditorState, |
14 | RangeSet, | ||
14 | } from '@codemirror/state'; | 15 | } from '@codemirror/state'; |
15 | import { type Command, EditorView } from '@codemirror/view'; | 16 | import { type Command, EditorView } from '@codemirror/view'; |
16 | import { makeAutoObservable, observable } from 'mobx'; | 17 | import { makeAutoObservable, observable } from 'mobx'; |
@@ -20,6 +21,7 @@ import type PWAStore from '../PWAStore'; | |||
20 | import getLogger from '../utils/getLogger'; | 21 | import getLogger from '../utils/getLogger'; |
21 | import XtextClient from '../xtext/XtextClient'; | 22 | import XtextClient from '../xtext/XtextClient'; |
22 | 23 | ||
24 | import DiagnosticValue from './DiagnosticValue'; | ||
23 | import LintPanelStore from './LintPanelStore'; | 25 | import LintPanelStore from './LintPanelStore'; |
24 | import SearchPanelStore from './SearchPanelStore'; | 26 | import SearchPanelStore from './SearchPanelStore'; |
25 | import createEditorState from './createEditorState'; | 27 | import createEditorState from './createEditorState'; |
@@ -46,11 +48,7 @@ export default class EditorStore { | |||
46 | 48 | ||
47 | showLineNumbers = false; | 49 | showLineNumbers = false; |
48 | 50 | ||
49 | errorCount = 0; | 51 | diagnostics: RangeSet<DiagnosticValue> = RangeSet.of([]); |
50 | |||
51 | warningCount = 0; | ||
52 | |||
53 | infoCount = 0; | ||
54 | 52 | ||
55 | constructor(initialValue: string, pwaStore: PWAStore) { | 53 | constructor(initialValue: string, pwaStore: PWAStore) { |
56 | this.id = nanoid(); | 54 | this.id = nanoid(); |
@@ -161,6 +159,9 @@ export default class EditorStore { | |||
161 | log.trace('Editor transaction', tr); | 159 | log.trace('Editor transaction', tr); |
162 | this.state = tr.state; | 160 | this.state = tr.state; |
163 | this.client.onTransaction(tr); | 161 | this.client.onTransaction(tr); |
162 | if (tr.docChanged) { | ||
163 | this.diagnostics = this.diagnostics.map(tr.changes); | ||
164 | } | ||
164 | } | 165 | } |
165 | 166 | ||
166 | doCommand(command: Command): boolean { | 167 | doCommand(command: Command): boolean { |
@@ -178,25 +179,31 @@ export default class EditorStore { | |||
178 | } | 179 | } |
179 | 180 | ||
180 | updateDiagnostics(diagnostics: Diagnostic[]): void { | 181 | updateDiagnostics(diagnostics: Diagnostic[]): void { |
182 | diagnostics.sort((a, b) => a.from - b.from); | ||
181 | this.dispatch(setDiagnostics(this.state, diagnostics)); | 183 | this.dispatch(setDiagnostics(this.state, diagnostics)); |
182 | this.errorCount = 0; | 184 | this.diagnostics = RangeSet.of( |
183 | this.warningCount = 0; | 185 | diagnostics.map(({ severity, from, to }) => |
184 | this.infoCount = 0; | 186 | DiagnosticValue.VALUES[severity].range(from, to), |
185 | diagnostics.forEach(({ severity }) => { | 187 | ), |
186 | switch (severity) { | 188 | ); |
187 | case 'error': | 189 | } |
188 | this.errorCount += 1; | 190 | |
189 | break; | 191 | countDiagnostics(severity: Diagnostic['severity']): number { |
190 | case 'warning': | 192 | return this.diagnostics.update({ |
191 | this.warningCount += 1; | 193 | filter: (_from, _to, value) => value.eq(DiagnosticValue.VALUES[severity]), |
192 | break; | 194 | }).size; |
193 | case 'info': | 195 | } |
194 | this.infoCount += 1; | 196 | |
195 | break; | 197 | get errorCount(): number { |
196 | default: | 198 | return this.countDiagnostics('error'); |
197 | throw new Error('Unknown severity'); | 199 | } |
198 | } | 200 | |
199 | }); | 201 | get warningCount(): number { |
202 | return this.countDiagnostics('warning'); | ||
203 | } | ||
204 | |||
205 | get infoCount(): number { | ||
206 | return this.countDiagnostics('info'); | ||
200 | } | 207 | } |
201 | 208 | ||
202 | nextDiagnostic(): void { | 209 | nextDiagnostic(): void { |