Skip to content

Commit 8dead0e

Browse files
committed
Edit syntax method
Edit syntax method * Now can check file suffix * Now extend use another dict
1 parent 99cf605 commit 8dead0e

File tree

9 files changed

+67
-35
lines changed

9 files changed

+67
-35
lines changed

je_editor/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
from je_editor.pyside_ui.code.code_process.code_exec import ExecManager
33
from je_editor.pyside_ui.code.shell_process.shell_exec import ShellManager
44
from je_editor.pyside_ui.code.syntax.python_syntax import PythonHighlighter
5-
from je_editor.pyside_ui.code.syntax.syntax_setting import syntax_word_setting_dict, syntax_rule_setting_dict
5+
from je_editor.pyside_ui.code.syntax.syntax_setting import syntax_extend_setting_dict, syntax_rule_setting_dict
66
from je_editor.pyside_ui.main_ui.editor.editor_widget import EditorWidget
77
from je_editor.pyside_ui.main_ui.editor.editor_widget_dock import FullEditorWidget
88
from je_editor.pyside_ui.main_ui.main_editor import EDITOR_EXTEND_TAB
@@ -26,7 +26,7 @@
2626
"start_editor", "EditorMain", "EDITOR_EXTEND_TAB",
2727
"JEditorException", "JEditorExecException", "FullEditorWidget",
2828
"JEditorRunOnShellException", "JEditorSaveFileException", "syntax_rule_setting_dict",
29-
"JEditorOpenFileException", "JEditorContentFileException", "syntax_word_setting_dict",
29+
"JEditorOpenFileException", "JEditorContentFileException", "syntax_extend_setting_dict",
3030
"JEditorCantFindLanguageException", "JEditorJsonException", "PythonHighlighter",
3131
"user_setting_dict", "user_setting_color_dict", "EditorWidget", "BrowserWidget",
3232
"ExecManager", "ShellManager", "traditional_chinese_word_dict", "english_word_dict",

je_editor/__main__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# argparse
33
import argparse
44

5-
from je_editor.tkinter_ui.editor_main_ui.tkinter_editor import start_editor
5+
from je_editor.start_editor import start_editor
66

77
argparse_event_dict = {
88
"start": start_editor,

je_editor/pyside_ui/code/plaintext_code_edit/code_edit_plaintext.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ def __init__(self, main_window: Union[EditorWidget, FullEditorWidget]):
4343
self.check_env()
4444
# Self main window (parent)
4545
self.main_window = main_window
46+
self.current_file = main_window.current_file
4647

4748
self.skip_popup_behavior_list = [
4849
Qt.Key.Key_Enter, Qt.Key.Key_Return, Qt.Key.Key_Up, Qt.Key.Key_Down,
@@ -64,7 +65,7 @@ def __init__(self, main_window: Union[EditorWidget, FullEditorWidget]):
6465
self.setTabStopDistance(
6566
QtGui.QFontMetricsF(self.font()).horizontalAdvance(" ")
6667
)
67-
self.highlighter = PythonHighlighter(self.document())
68+
self.highlighter = PythonHighlighter(self.document(), main_window=self)
6869
self.highlight_current_line()
6970
self.setLineWrapMode(self.LineWrapMode.NoWrap)
7071
# Search Text
@@ -79,6 +80,10 @@ def __init__(self, main_window: Union[EditorWidget, FullEditorWidget]):
7980
self.completer: Union[None, QCompleter] = None
8081
self.set_complete([])
8182

83+
def reset_highlighter(self):
84+
self.highlighter = PythonHighlighter(self.document(), main_window=self)
85+
self.highlight_current_line()
86+
8287
def check_env(self):
8388
jeditor_logger.info("CodeEditor check_env")
8489
path = venv_check()

je_editor/pyside_ui/code/syntax/python_syntax.py

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,57 @@
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+
19
from PySide6.QtCore import QRegularExpression
210
from PySide6.QtGui import QSyntaxHighlighter
311
from PySide6.QtGui import QTextCharFormat
412

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
615
from je_editor.utils.logging.loggin_instance import jeditor_logger
716

817

918
class PythonHighlighter(QSyntaxHighlighter):
10-
def __init__(self, parent=None):
19+
def __init__(self, parent=None, main_window: CodeEditor = None):
1120
jeditor_logger.info(f"Init PythonHighlighter parent: {parent}")
1221
super().__init__(parent)
1322

1423
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
3255

3356
def highlightBlock(self, text) -> None:
3457
jeditor_logger.info(f"PythonHighlighter highlightBlock text: {text}")

je_editor/pyside_ui/code/syntax/syntax_setting.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,7 @@
6161
"words": ("self",),
6262
"color": QColor(204, 0, 204)
6363
}
64+
}
6465

66+
syntax_extend_setting_dict: dict = {
6567
}

je_editor/pyside_ui/main_ui/main_editor.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ def startup_setting(self) -> None:
181181
if last_file_path.is_file() and last_file_path.exists() and widget.code_save_thread is None:
182182
init_new_auto_save_thread(str(last_file_path), widget)
183183
widget.code_edit.setPlainText(read_file(widget.current_file)[1])
184+
widget.code_edit.reset_highlighter()
184185
file_is_open_manager_dict.update({str(last_file_path): str(last_file_path.name)})
185186
widget.rename_self_tab()
186187

je_editor/utils/redirect_manager/redirect_manager_class.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,9 @@ def set_redirect() -> None:
5252
sys.stderr = redirect_err
5353
default_logger = logging.getLogger("RedirectManager")
5454
default_logger.addHandler(redirect_err)
55+
skip_logger_list = ["JEditor", "FrontEngine", "AutomationIDE"]
5556
for name in logging.root.manager.loggerDict.keys():
56-
if name == "JEditor":
57+
if name in skip_logger_list:
5758
continue
5859
else:
5960
logging.getLogger(name).addHandler(redirect_err)

pyproject.toml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Rename to build stable version
2-
# This is stable version
1+
# Rename to build dev version
2+
# This is dev version
33
[build-system]
4-
requires = ["setuptools>=61.0"]
4+
requires = ["setuptools"]
55
build-backend = "setuptools.build_meta"
66

77
[project]
8-
name = "je_editor"
9-
version = "0.0.186"
8+
name = "je_editor_dev"
9+
version = "0.0.205"
1010
authors = [
1111
{ name = "JE-Chen", email = "[email protected]" },
1212
]
@@ -26,7 +26,6 @@ classifiers = [
2626
"Operating System :: OS Independent"
2727
]
2828

29-
3029
[project.urls]
3130
Homepage = "https://github.com/JE-Chen/je_editor"
3231
Documentation = "https://je-editor.readthedocs.io/en/latest/"

dev.toml renamed to stable.toml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Rename to build dev version
2-
# This is dev version
1+
# Rename to build stable version
2+
# This is stable version
33
[build-system]
4-
requires = ["setuptools"]
4+
requires = ["setuptools>=61.0"]
55
build-backend = "setuptools.build_meta"
66

77
[project]
8-
name = "je_editor_dev"
9-
version = "0.0.203"
8+
name = "je_editor"
9+
version = "0.0.186"
1010
authors = [
1111
{ name = "JE-Chen", email = "[email protected]" },
1212
]
@@ -26,6 +26,7 @@ classifiers = [
2626
"Operating System :: OS Independent"
2727
]
2828

29+
2930
[project.urls]
3031
Homepage = "https://github.com/JE-Chen/je_editor"
3132
Documentation = "https://je-editor.readthedocs.io/en/latest/"

0 commit comments

Comments
 (0)