|
1 | | -import 'dart:convert'; |
2 | | - |
3 | 1 | import 'package:flutter/material.dart'; |
| 2 | +import 'package:querya_desktop/core/theme/parser/token_colors_highlighter_config.dart'; |
| 3 | +import 'package:querya_desktop/core/theme/parser/vscode_theme_manifest.dart'; |
4 | 4 | import 'package:querya_desktop/core/theme/querya_editor_theme.dart'; |
5 | 5 | import 'package:syntax_highlight/syntax_highlight.dart'; |
6 | 6 |
|
7 | | -/// Builds a [HighlighterTheme] from [QueryaEditorTheme] token colors. |
8 | | -HighlighterTheme highlighterThemeFromQueryaEditor(QueryaEditorTheme editor) { |
| 7 | +/// Builds a [HighlighterTheme] from [QueryaEditorTheme] and optional VS Code rules. |
| 8 | +HighlighterTheme highlighterThemeFromQueryaEditor( |
| 9 | + QueryaEditorTheme editor, { |
| 10 | + List<TokenColorRule> tokenColors = const [], |
| 11 | +}) { |
9 | 12 | final wrapper = TextStyle( |
10 | 13 | color: editor.foreground, |
11 | 14 | fontFamily: editor.fontFamily, |
12 | 15 | fontSize: editor.fontSize, |
13 | 16 | ); |
14 | 17 |
|
15 | | - final config = jsonEncode({ |
16 | | - 'settings': [ |
17 | | - { |
18 | | - 'scope': [ |
19 | | - 'comment', |
20 | | - 'comment.line', |
21 | | - 'comment.block', |
22 | | - ], |
23 | | - 'settings': {'foreground': _hex(editor.comment)}, |
24 | | - }, |
25 | | - { |
26 | | - 'scope': [ |
27 | | - 'keyword', |
28 | | - 'keyword.control', |
29 | | - 'keyword.operator', |
30 | | - 'storage.type', |
31 | | - ], |
32 | | - 'settings': {'foreground': _hex(editor.keyword)}, |
33 | | - }, |
34 | | - { |
35 | | - 'scope': [ |
36 | | - 'string', |
37 | | - 'string.quoted', |
38 | | - 'string.quoted.single', |
39 | | - 'string.quoted.double', |
40 | | - ], |
41 | | - 'settings': {'foreground': _hex(editor.string)}, |
42 | | - }, |
43 | | - { |
44 | | - 'scope': [ |
45 | | - 'constant.numeric', |
46 | | - 'constant.numeric.json', |
47 | | - 'number', |
48 | | - ], |
49 | | - 'settings': {'foreground': _hex(editor.number)}, |
50 | | - }, |
51 | | - { |
52 | | - 'scope': [ |
53 | | - 'support.type.property-name', |
54 | | - 'support.type.property-name.json', |
55 | | - ], |
56 | | - 'settings': {'foreground': _hex(editor.type)}, |
57 | | - }, |
58 | | - { |
59 | | - 'scope': [ |
60 | | - 'constant.language', |
61 | | - 'constant.language.json', |
62 | | - ], |
63 | | - 'settings': {'foreground': _hex(editor.keyword)}, |
64 | | - }, |
65 | | - { |
66 | | - 'scope': ['entity.name.function', 'support.function'], |
67 | | - 'settings': {'foreground': _hex(editor.function)}, |
68 | | - }, |
69 | | - { |
70 | | - 'scope': ['entity.name.type', 'support.type'], |
71 | | - 'settings': {'foreground': _hex(editor.type)}, |
72 | | - }, |
73 | | - { |
74 | | - 'scope': ['constant.language', 'variable.language'], |
75 | | - 'settings': {'foreground': _hex(editor.keyword)}, |
76 | | - }, |
77 | | - { |
78 | | - 'settings': {'foreground': _hex(editor.foreground)}, |
79 | | - }, |
80 | | - ], |
81 | | - }); |
| 18 | + final config = tokenColors.isNotEmpty |
| 19 | + ? buildHighlighterConfigFromTokenColors(tokenColors, editor.foreground) |
| 20 | + : buildDefaultEditorHighlighterConfig(editor); |
82 | 21 |
|
83 | 22 | return HighlighterTheme.fromConfiguration(config, wrapper); |
84 | 23 | } |
85 | 24 |
|
86 | | -String _hex(Color c) { |
87 | | - final a = (c.a * 255).round().clamp(0, 255); |
88 | | - final r = (c.r * 255).round().clamp(0, 255); |
89 | | - final g = (c.g * 255).round().clamp(0, 255); |
90 | | - final b = (c.b * 255).round().clamp(0, 255); |
91 | | - if (a < 255) { |
92 | | - return '#${r.toRadixString(16).padLeft(2, '0')}' |
93 | | - '${g.toRadixString(16).padLeft(2, '0')}' |
94 | | - '${b.toRadixString(16).padLeft(2, '0')}' |
95 | | - '${a.toRadixString(16).padLeft(2, '0')}'; |
96 | | - } |
97 | | - return '#${r.toRadixString(16).padLeft(2, '0')}' |
98 | | - '${g.toRadixString(16).padLeft(2, '0')}' |
99 | | - '${b.toRadixString(16).padLeft(2, '0')}'; |
| 25 | +/// JSON config for isolate/off-thread highlighting. |
| 26 | +String highlighterThemeConfigJson( |
| 27 | + QueryaEditorTheme editor, { |
| 28 | + List<TokenColorRule> tokenColors = const [], |
| 29 | +}) { |
| 30 | + return tokenColors.isNotEmpty |
| 31 | + ? buildHighlighterConfigFromTokenColors(tokenColors, editor.foreground) |
| 32 | + : buildDefaultEditorHighlighterConfig(editor); |
100 | 33 | } |
0 commit comments