|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from pathlib import Path |
| 4 | +from typing import TYPE_CHECKING |
| 5 | + |
| 6 | +if TYPE_CHECKING: |
| 7 | + from je_editor.pyside_ui.code.plaintext_code_edit.code_edit_plaintext import CodeEditor |
| 8 | + |
1 | 9 | from PySide6.QtCore import QRegularExpression
|
2 | 10 | from PySide6.QtGui import QSyntaxHighlighter
|
3 | 11 | from PySide6.QtGui import QTextCharFormat
|
4 | 12 |
|
5 |
| -from je_editor.pyside_ui.code.syntax.syntax_setting import syntax_word_setting_dict, syntax_rule_setting_dict |
| 13 | +from je_editor.pyside_ui.code.syntax.syntax_setting import syntax_word_setting_dict, syntax_rule_setting_dict, \ |
| 14 | + syntax_extend_setting_dict |
6 | 15 | from je_editor.utils.logging.loggin_instance import jeditor_logger
|
7 | 16 |
|
8 | 17 |
|
9 | 18 | class PythonHighlighter(QSyntaxHighlighter):
|
10 |
| - def __init__(self, parent=None): |
| 19 | + def __init__(self, parent=None, main_window: CodeEditor = None): |
11 | 20 | jeditor_logger.info(f"Init PythonHighlighter parent: {parent}")
|
12 | 21 | super().__init__(parent)
|
13 | 22 |
|
14 | 23 | self.highlight_rules = []
|
15 |
| - |
16 |
| - # Highlight |
17 |
| - for rule_variable_dict in syntax_word_setting_dict.values(): |
18 |
| - color = rule_variable_dict.get("color") |
19 |
| - text_char_format = QTextCharFormat() |
20 |
| - text_char_format.setForeground(color) |
21 |
| - for word in rule_variable_dict.get("words"): |
22 |
| - pattern = QRegularExpression(rf"\b{word}\b") |
23 |
| - self.highlight_rules.append((pattern, text_char_format)) |
24 |
| - |
25 |
| - for rule_variable_dict in syntax_rule_setting_dict.values(): |
26 |
| - color = rule_variable_dict.get("color") |
27 |
| - text_char_format = QTextCharFormat() |
28 |
| - text_char_format.setForeground(color) |
29 |
| - for rule in rule_variable_dict.get("rules"): |
30 |
| - pattern = QRegularExpression(rule) |
31 |
| - self.highlight_rules.append((pattern, text_char_format)) |
| 24 | + if main_window.current_file is not None: |
| 25 | + current_file_suffix = Path(main_window.current_file).suffix |
| 26 | + else: |
| 27 | + current_file_suffix = ".py" |
| 28 | + if current_file_suffix == ".py": |
| 29 | + # Highlight |
| 30 | + for rule_variable_dict in syntax_word_setting_dict.values(): |
| 31 | + color = rule_variable_dict.get("color") |
| 32 | + text_char_format = QTextCharFormat() |
| 33 | + text_char_format.setForeground(color) |
| 34 | + for word in rule_variable_dict.get("words"): |
| 35 | + pattern = QRegularExpression(rf"\b{word}\b") |
| 36 | + self.highlight_rules.append((pattern, text_char_format)) |
| 37 | + for rule_variable_dict in syntax_rule_setting_dict.values(): |
| 38 | + color = rule_variable_dict.get("color") |
| 39 | + text_char_format = QTextCharFormat() |
| 40 | + text_char_format.setForeground(color) |
| 41 | + for rule in rule_variable_dict.get("rules"): |
| 42 | + pattern = QRegularExpression(rule) |
| 43 | + self.highlight_rules.append((pattern, text_char_format)) |
| 44 | + else: |
| 45 | + if syntax_extend_setting_dict.get(current_file_suffix): |
| 46 | + for rule_variable_dict in syntax_extend_setting_dict.get(current_file_suffix).values(): |
| 47 | + color = rule_variable_dict.get("color") |
| 48 | + text_char_format = QTextCharFormat() |
| 49 | + text_char_format.setForeground(color) |
| 50 | + for word in rule_variable_dict.get("words"): |
| 51 | + pattern = QRegularExpression(rf"\b{word}\b") |
| 52 | + self.highlight_rules.append((pattern, text_char_format)) |
| 53 | + else: |
| 54 | + pass |
32 | 55 |
|
33 | 56 | def highlightBlock(self, text) -> None:
|
34 | 57 | jeditor_logger.info(f"PythonHighlighter highlightBlock text: {text}")
|
|
0 commit comments