aboutsummaryrefslogtreecommitdiffstats
path: root/language-web/src/main/js/xtext/HighlightingService.ts
diff options
context:
space:
mode:
Diffstat (limited to 'language-web/src/main/js/xtext/HighlightingService.ts')
-rw-r--r--language-web/src/main/js/xtext/HighlightingService.ts43
1 files changed, 43 insertions, 0 deletions
diff --git a/language-web/src/main/js/xtext/HighlightingService.ts b/language-web/src/main/js/xtext/HighlightingService.ts
new file mode 100644
index 00000000..fc3e9e53
--- /dev/null
+++ b/language-web/src/main/js/xtext/HighlightingService.ts
@@ -0,0 +1,43 @@
1import type { EditorStore } from '../editor/EditorStore';
2import type { IHighlightRange } from '../editor/semanticHighlighting';
3import type { UpdateService } from './UpdateService';
4import { getLogger } from '../utils/logger';
5import { isHighlightingResult } from './xtextServiceResults';
6
7const log = getLogger('xtext.ValidationService');
8
9export class HighlightingService {
10 private readonly store: EditorStore;
11
12 private readonly updateService: UpdateService;
13
14 constructor(store: EditorStore, updateService: UpdateService) {
15 this.store = store;
16 this.updateService = updateService;
17 }
18
19 onPush(push: unknown): void {
20 if (!isHighlightingResult(push)) {
21 log.error('Invalid highlighting result', push);
22 return;
23 }
24 const allChanges = this.updateService.computeChangesSinceLastUpdate();
25 const ranges: IHighlightRange[] = [];
26 push.regions.forEach(({ offset, length, styleClasses }) => {
27 if (styleClasses.length === 0) {
28 return;
29 }
30 const from = allChanges.mapPos(offset);
31 const to = allChanges.mapPos(offset + length);
32 if (to <= from) {
33 return;
34 }
35 ranges.push({
36 from,
37 to,
38 classes: styleClasses,
39 });
40 });
41 this.store.updateSemanticHighlighting(ranges);
42 }
43}