je-editor 0.0.220__py3-none-any.whl → 0.0.222__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of je-editor might be problematic. Click here for more details.
- je_editor/code_scan/ruff_thread.py +33 -6
- je_editor/code_scan/watchdog_implement.py +42 -20
- je_editor/code_scan/watchdog_thread.py +54 -9
- je_editor/git_client/commit_graph.py +32 -43
- je_editor/git_client/{git.py → git_action.py} +1 -1
- je_editor/git_client/git_cli.py +4 -4
- je_editor/git_client/github.py +36 -5
- je_editor/pyside_ui/browser/browser_download_window.py +41 -5
- je_editor/pyside_ui/browser/browser_serach_lineedit.py +25 -1
- je_editor/pyside_ui/browser/browser_view.py +42 -1
- je_editor/pyside_ui/browser/browser_widget.py +43 -14
- je_editor/pyside_ui/git_ui/code_diff_compare/__init__.py +0 -0
- je_editor/pyside_ui/git_ui/code_diff_compare/code_diff_viewer_widget.py +90 -0
- je_editor/pyside_ui/git_ui/code_diff_compare/line_number_code_viewer.py +141 -0
- je_editor/pyside_ui/git_ui/code_diff_compare/multi_file_diff_viewer.py +88 -0
- je_editor/pyside_ui/git_ui/code_diff_compare/side_by_side_diff_widget.py +271 -0
- je_editor/pyside_ui/git_ui/git_client/__init__.py +0 -0
- je_editor/pyside_ui/git_ui/{git_branch_tree_widget.py → git_client/git_branch_tree_widget.py} +17 -14
- je_editor/pyside_ui/git_ui/git_client/git_client_gui.py +734 -0
- je_editor/pyside_ui/main_ui/ai_widget/langchain_interface.py +1 -1
- je_editor/pyside_ui/main_ui/main_editor.py +0 -3
- je_editor/pyside_ui/main_ui/menu/dock_menu/build_dock_menu.py +14 -3
- je_editor/pyside_ui/main_ui/menu/tab_menu/build_tab_menu.py +19 -4
- je_editor/utils/multi_language/english.py +2 -1
- je_editor/utils/multi_language/traditional_chinese.py +2 -2
- {je_editor-0.0.220.dist-info → je_editor-0.0.222.dist-info}/METADATA +4 -4
- {je_editor-0.0.220.dist-info → je_editor-0.0.222.dist-info}/RECORD +32 -26
- je_editor/pyside_ui/git_ui/git_client_gui.py +0 -291
- /je_editor/pyside_ui/git_ui/{commit_table.py → git_client/commit_table.py} +0 -0
- /je_editor/pyside_ui/git_ui/{graph_view.py → git_client/graph_view.py} +0 -0
- {je_editor-0.0.220.dist-info → je_editor-0.0.222.dist-info}/WHEEL +0 -0
- {je_editor-0.0.220.dist-info → je_editor-0.0.222.dist-info}/licenses/LICENSE +0 -0
- {je_editor-0.0.220.dist-info → je_editor-0.0.222.dist-info}/top_level.txt +0 -0
|
@@ -8,53 +8,82 @@ from je_editor.utils.multi_language.multi_language_wrapper import language_wrapp
|
|
|
8
8
|
|
|
9
9
|
|
|
10
10
|
class BrowserWidget(QWidget):
|
|
11
|
+
"""
|
|
12
|
+
A composite browser widget with navigation buttons, search bar, and embedded BrowserView.
|
|
13
|
+
瀏覽器元件:包含導覽按鈕、搜尋列,以及內嵌的 BrowserView。
|
|
14
|
+
"""
|
|
11
15
|
|
|
12
16
|
def __init__(self, start_url: str = "https://www.google.com/",
|
|
13
17
|
search_prefix: str = "https://www.google.com.tw/search?q="):
|
|
18
|
+
"""
|
|
19
|
+
Initialize the browser widget with a start URL and a search prefix.
|
|
20
|
+
使用起始網址與搜尋前綴字串初始化瀏覽器元件。
|
|
21
|
+
"""
|
|
14
22
|
super().__init__()
|
|
15
23
|
jeditor_logger.info("Init BrowserWidget "
|
|
16
24
|
f"start_url: {start_url} "
|
|
17
25
|
f"search_prefix: {search_prefix}")
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
self.
|
|
26
|
+
|
|
27
|
+
# --- Browser setting / 瀏覽器設定 ---
|
|
28
|
+
self.browser = BrowserView(start_url) # 內嵌的瀏覽器視圖
|
|
29
|
+
self.search_prefix = search_prefix # 搜尋引擎前綴字串
|
|
21
30
|
self.setAttribute(Qt.WidgetAttribute.WA_DeleteOnClose)
|
|
22
|
-
|
|
31
|
+
|
|
32
|
+
# --- Top bar buttons / 上方工具列按鈕 ---
|
|
23
33
|
self.back_button = QPushButton(language_wrapper.language_word_dict.get("browser_back_button"))
|
|
24
|
-
self.back_button.clicked.connect(self.browser.back)
|
|
34
|
+
self.back_button.clicked.connect(self.browser.back) # 返回上一頁
|
|
35
|
+
|
|
25
36
|
self.forward_button = QPushButton(language_wrapper.language_word_dict.get("browser_forward_button"))
|
|
26
|
-
self.forward_button.clicked.connect(self.browser.forward)
|
|
37
|
+
self.forward_button.clicked.connect(self.browser.forward) # 前往下一頁
|
|
38
|
+
|
|
27
39
|
self.reload_button = QPushButton(language_wrapper.language_word_dict.get("browser_reload_button"))
|
|
28
|
-
self.reload_button.clicked.connect(self.browser.reload)
|
|
40
|
+
self.reload_button.clicked.connect(self.browser.reload) # 重新整理
|
|
41
|
+
|
|
29
42
|
self.search_button = QPushButton(language_wrapper.language_word_dict.get("browser_search_button"))
|
|
30
|
-
self.search_button.clicked.connect(self.search)
|
|
43
|
+
self.search_button.clicked.connect(self.search) # 搜尋按鈕
|
|
44
|
+
|
|
45
|
+
# URL / Search input line (custom QLineEdit)
|
|
31
46
|
self.url_input = BrowserLineSearch(self)
|
|
32
|
-
|
|
47
|
+
|
|
48
|
+
# --- Action: Ctrl+F to find text / 快捷鍵 Ctrl+F 搜尋文字 ---
|
|
33
49
|
self.find_action = QAction()
|
|
34
50
|
self.find_action.setShortcut("Ctrl+f")
|
|
35
51
|
self.find_action.triggered.connect(self.find_text)
|
|
36
52
|
self.addAction(self.find_action)
|
|
37
|
-
|
|
53
|
+
|
|
54
|
+
# --- Layout / 版面配置 ---
|
|
38
55
|
self.grid_layout = QGridLayout()
|
|
39
56
|
self.grid_layout.addWidget(self.back_button, 0, 0)
|
|
40
57
|
self.grid_layout.addWidget(self.forward_button, 0, 1)
|
|
41
58
|
self.grid_layout.addWidget(self.reload_button, 0, 2)
|
|
42
59
|
self.grid_layout.addWidget(self.url_input, 0, 3)
|
|
43
60
|
self.grid_layout.addWidget(self.search_button, 0, 4)
|
|
44
|
-
self.grid_layout.addWidget(self.browser, 1, 0, -1, -1)
|
|
61
|
+
self.grid_layout.addWidget(self.browser, 1, 0, -1, -1) # 瀏覽器視圖佔滿下方
|
|
45
62
|
self.setLayout(self.grid_layout)
|
|
46
63
|
|
|
47
64
|
def search(self):
|
|
65
|
+
"""
|
|
66
|
+
Perform a search using the text in the input line.
|
|
67
|
+
使用輸入框的文字進行搜尋,將字串附加到 search_prefix 後送出。
|
|
68
|
+
"""
|
|
48
69
|
jeditor_logger.info("BrowserWidget Search")
|
|
49
70
|
self.browser.setUrl(f"{self.search_prefix}{self.url_input.text()}")
|
|
50
71
|
|
|
51
72
|
def find_text(self):
|
|
73
|
+
"""
|
|
74
|
+
Open a dialog to find text in the current page.
|
|
75
|
+
開啟輸入對話框,在當前頁面中搜尋文字。
|
|
76
|
+
- 如果按下 OK,搜尋輸入的文字
|
|
77
|
+
- 如果取消,清除搜尋
|
|
78
|
+
"""
|
|
52
79
|
jeditor_logger.info("BrowserWidget Find Text")
|
|
53
80
|
search_box = QInputDialog(self)
|
|
54
81
|
search_text, press_ok = search_box.getText(
|
|
55
|
-
self,
|
|
56
|
-
language_wrapper.language_word_dict.get("
|
|
82
|
+
self,
|
|
83
|
+
language_wrapper.language_word_dict.get("browser_find_text"),
|
|
84
|
+
language_wrapper.language_word_dict.get("browser_find_text_input")
|
|
85
|
+
)
|
|
57
86
|
if press_ok:
|
|
58
87
|
self.browser.findText(search_text)
|
|
59
88
|
else:
|
|
60
|
-
self.browser.findText("")
|
|
89
|
+
self.browser.findText("")
|
|
File without changes
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
from PySide6.QtGui import QAction, QActionGroup
|
|
2
|
+
from PySide6.QtWidgets import (
|
|
3
|
+
QWidget, QVBoxLayout, QMenuBar, QFileDialog, QMessageBox
|
|
4
|
+
)
|
|
5
|
+
from git import Repo
|
|
6
|
+
|
|
7
|
+
from je_editor.pyside_ui.git_ui.code_diff_compare.multi_file_diff_viewer import MultiFileDiffViewer
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class DiffViewerWidget(QWidget):
|
|
11
|
+
"""
|
|
12
|
+
Git Diff Viewer Application
|
|
13
|
+
Git 差異檢視器應用程式
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def __init__(self):
|
|
17
|
+
super().__init__()
|
|
18
|
+
self.setWindowTitle("Git Diff Viewer")
|
|
19
|
+
|
|
20
|
+
# === Main diff viewer widget / 主要的差異檢視元件 ===
|
|
21
|
+
self.viewer = MultiFileDiffViewer()
|
|
22
|
+
|
|
23
|
+
# === Menu bar / 選單列 ===
|
|
24
|
+
self.menubar = QMenuBar(self)
|
|
25
|
+
file_menu = self.menubar.addMenu("File") # File 選單
|
|
26
|
+
view_menu = self.menubar.addMenu("View") # View 選單
|
|
27
|
+
|
|
28
|
+
# --- File → Open Git Repo ---
|
|
29
|
+
# Action to open a Git repository
|
|
30
|
+
# 開啟 Git 專案的動作
|
|
31
|
+
open_repo_action = QAction("Open Git Repo", self)
|
|
32
|
+
open_repo_action.triggered.connect(self.open_repo)
|
|
33
|
+
file_menu.addAction(open_repo_action)
|
|
34
|
+
|
|
35
|
+
# === View → Theme switching (exclusive) / 主題切換(單選模式) ===
|
|
36
|
+
theme_group = QActionGroup(self)
|
|
37
|
+
theme_group.setExclusive(True) # 確保只能選擇一個主題
|
|
38
|
+
|
|
39
|
+
dark_action = QAction("Dark Mode", self, checkable=True) # 深色模式
|
|
40
|
+
light_action = QAction("Light Mode", self, checkable=True) # 淺色模式
|
|
41
|
+
|
|
42
|
+
theme_group.addAction(dark_action)
|
|
43
|
+
theme_group.addAction(light_action)
|
|
44
|
+
|
|
45
|
+
# Default to dark mode / 預設為深色模式
|
|
46
|
+
dark_action.setChecked(True)
|
|
47
|
+
self.viewer.set_dark_theme()
|
|
48
|
+
|
|
49
|
+
# Connect theme actions / 綁定主題切換事件
|
|
50
|
+
dark_action.triggered.connect(lambda: self.set_theme("dark"))
|
|
51
|
+
light_action.triggered.connect(lambda: self.set_theme("light"))
|
|
52
|
+
|
|
53
|
+
view_menu.addAction(dark_action)
|
|
54
|
+
view_menu.addAction(light_action)
|
|
55
|
+
|
|
56
|
+
# === Layout / 版面配置 ===
|
|
57
|
+
layout = QVBoxLayout(self)
|
|
58
|
+
layout.setMenuBar(self.menubar) # 把選單列放在上方
|
|
59
|
+
layout.addWidget(self.viewer) # 把差異檢視器放在主要區域
|
|
60
|
+
|
|
61
|
+
def open_repo(self):
|
|
62
|
+
"""
|
|
63
|
+
Open a Git repository and display its diff.
|
|
64
|
+
開啟一個 Git 專案並顯示差異。
|
|
65
|
+
"""
|
|
66
|
+
path = QFileDialog.getExistingDirectory(self, "Select Git Repository")
|
|
67
|
+
if not path:
|
|
68
|
+
return
|
|
69
|
+
try:
|
|
70
|
+
repo = Repo(path) # 嘗試載入 Git 專案
|
|
71
|
+
diff_text = repo.git.diff() # 取得差異文字
|
|
72
|
+
if not diff_text.strip():
|
|
73
|
+
# 如果沒有差異,顯示提示訊息
|
|
74
|
+
QMessageBox.information(self, "Info", "No changes in repo.")
|
|
75
|
+
else:
|
|
76
|
+
# 如果有差異,顯示在 viewer 中
|
|
77
|
+
self.viewer.set_diff_text(diff_text)
|
|
78
|
+
except Exception as e:
|
|
79
|
+
# 如果開啟失敗,顯示錯誤訊息
|
|
80
|
+
QMessageBox.critical(self, "Error", f"Failed to open repo:\n{e}")
|
|
81
|
+
|
|
82
|
+
def set_theme(self, mode: str):
|
|
83
|
+
"""
|
|
84
|
+
Switch between dark and light themes.
|
|
85
|
+
切換深色與淺色主題。
|
|
86
|
+
"""
|
|
87
|
+
if mode == "dark":
|
|
88
|
+
self.viewer.set_dark_theme()
|
|
89
|
+
else:
|
|
90
|
+
self.viewer.set_light_theme()
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
from PySide6.QtCore import QRect, QSize, Qt
|
|
2
|
+
from PySide6.QtGui import QPainter, QColor
|
|
3
|
+
from PySide6.QtWidgets import QPlainTextEdit, QWidget, QTextEdit
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class LineNumberArea(QWidget):
|
|
7
|
+
"""
|
|
8
|
+
Side widget for displaying line numbers.
|
|
9
|
+
用來顯示行號的側邊元件。
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
def __init__(self, editor):
|
|
13
|
+
super().__init__(editor)
|
|
14
|
+
self.code_editor = editor # 綁定主要的編輯器 / Bind to main editor
|
|
15
|
+
|
|
16
|
+
def sizeHint(self):
|
|
17
|
+
"""
|
|
18
|
+
Suggest width for line number area.
|
|
19
|
+
建議行號區域的寬度。
|
|
20
|
+
"""
|
|
21
|
+
return QSize(self.code_editor.line_number_area_width(), 0)
|
|
22
|
+
|
|
23
|
+
def paintEvent(self, event):
|
|
24
|
+
"""
|
|
25
|
+
Delegate paint event to the main editor.
|
|
26
|
+
將繪製事件交給主要編輯器處理。
|
|
27
|
+
"""
|
|
28
|
+
self.code_editor.line_number_area_paint_event(event)
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
class LineNumberedCodeViewer(QPlainTextEdit):
|
|
32
|
+
"""
|
|
33
|
+
QPlainTextEdit with line numbers.
|
|
34
|
+
帶有行號顯示的文字編輯器。
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
def __init__(self, parent=None):
|
|
38
|
+
super().__init__(parent)
|
|
39
|
+
self.lineNumberArea = LineNumberArea(self)
|
|
40
|
+
|
|
41
|
+
# === 信號連接 / Signal connections ===
|
|
42
|
+
self.blockCountChanged.connect(self.update_line_number_area_width) # 當行數改變時更新行號區寬度
|
|
43
|
+
self.updateRequest.connect(self.update_line_number_area) # 當需要重繪時更新行號區
|
|
44
|
+
self.cursorPositionChanged.connect(self.highlight_current_line) # 當游標移動時高亮當前行
|
|
45
|
+
|
|
46
|
+
# 初始化行號區寬度與高亮
|
|
47
|
+
self.update_line_number_area_width(0)
|
|
48
|
+
self.highlight_current_line()
|
|
49
|
+
|
|
50
|
+
def line_number_area_width(self):
|
|
51
|
+
"""
|
|
52
|
+
Calculate width of line number area based on digit count.
|
|
53
|
+
根據行數位數計算行號區寬度。
|
|
54
|
+
"""
|
|
55
|
+
digits = len(str(max(1, self.blockCount())))
|
|
56
|
+
space = 3 + self.fontMetrics().horizontalAdvance('9') * digits
|
|
57
|
+
return space
|
|
58
|
+
|
|
59
|
+
def update_line_number_area_width(self, _):
|
|
60
|
+
"""
|
|
61
|
+
Adjust viewport margins to fit line number area.
|
|
62
|
+
調整視口邊界以容納行號區。
|
|
63
|
+
"""
|
|
64
|
+
self.setViewportMargins(self.line_number_area_width(), 0, 0, 0)
|
|
65
|
+
|
|
66
|
+
def update_line_number_area(self, rect, dy):
|
|
67
|
+
"""
|
|
68
|
+
Update/redraw line number area when scrolling or editing.
|
|
69
|
+
當滾動或編輯時更新行號區。
|
|
70
|
+
"""
|
|
71
|
+
if dy:
|
|
72
|
+
self.lineNumberArea.scroll(0, dy)
|
|
73
|
+
else:
|
|
74
|
+
self.lineNumberArea.update(0, rect.y(), self.lineNumberArea.width(), rect.height())
|
|
75
|
+
if rect.contains(self.viewport().rect()):
|
|
76
|
+
self.update_line_number_area_width(0)
|
|
77
|
+
|
|
78
|
+
def resizeEvent(self, event):
|
|
79
|
+
"""
|
|
80
|
+
Resize line number area when editor resizes.
|
|
81
|
+
編輯器大小改變時調整行號區。
|
|
82
|
+
"""
|
|
83
|
+
super().resizeEvent(event)
|
|
84
|
+
cr = self.contentsRect()
|
|
85
|
+
self.lineNumberArea.setGeometry(QRect(cr.left(), cr.top(), self.line_number_area_width(), cr.height()))
|
|
86
|
+
|
|
87
|
+
def line_number_area_paint_event(self, event):
|
|
88
|
+
"""
|
|
89
|
+
Paint line numbers.
|
|
90
|
+
繪製行號。
|
|
91
|
+
"""
|
|
92
|
+
painter = QPainter(self.lineNumberArea)
|
|
93
|
+
# 背景顏色依主題切換 / Background color depends on theme
|
|
94
|
+
painter.fillRect(event.rect(), QColor(40, 40, 40) if getattr(self, "is_dark", False) else QColor(230, 230, 230))
|
|
95
|
+
|
|
96
|
+
block = self.firstVisibleBlock()
|
|
97
|
+
blockNumber = block.blockNumber()
|
|
98
|
+
top = int(self.blockBoundingGeometry(block).translated(self.contentOffset()).top())
|
|
99
|
+
bottom = top + int(self.blockBoundingRect(block).height())
|
|
100
|
+
|
|
101
|
+
while block.isValid() and top <= event.rect().bottom():
|
|
102
|
+
if block.isVisible() and bottom >= event.rect().top():
|
|
103
|
+
number = str(blockNumber + 1)
|
|
104
|
+
# 行號顏色依主題切換 / Line number color depends on theme
|
|
105
|
+
painter.setPen(QColor("#d4d4d4") if getattr(self, "is_dark", False) else Qt.GlobalColor.black)
|
|
106
|
+
painter.drawText(
|
|
107
|
+
0, top, self.lineNumberArea.width() - 2, self.fontMetrics().height(),
|
|
108
|
+
Qt.AlignmentFlag.AlignRight, number
|
|
109
|
+
)
|
|
110
|
+
block = block.next()
|
|
111
|
+
top = bottom
|
|
112
|
+
bottom = top + int(self.blockBoundingRect(block).height())
|
|
113
|
+
blockNumber += 1
|
|
114
|
+
|
|
115
|
+
def highlight_current_line(self):
|
|
116
|
+
"""
|
|
117
|
+
Highlight the current line where the cursor is.
|
|
118
|
+
高亮顯示游標所在的行。
|
|
119
|
+
"""
|
|
120
|
+
selection = QTextEdit.ExtraSelection()
|
|
121
|
+
lineColor = QColor(50, 50, 50) if getattr(self, "is_dark", False) else QColor(232, 232, 255)
|
|
122
|
+
selection.format.setBackground(lineColor)
|
|
123
|
+
selection.cursor = self.textCursor()
|
|
124
|
+
selection.cursor.clearSelection()
|
|
125
|
+
|
|
126
|
+
# Keep current-line extras for merging
|
|
127
|
+
self._current_line_extras = [selection]
|
|
128
|
+
|
|
129
|
+
# Merge with diff extras if present
|
|
130
|
+
if hasattr(self, "_diff_extras"):
|
|
131
|
+
merged = self._diff_extras + self._current_line_extras
|
|
132
|
+
else:
|
|
133
|
+
merged = self._current_line_extras
|
|
134
|
+
|
|
135
|
+
self.setExtraSelections(merged)
|
|
136
|
+
|
|
137
|
+
def apply_theme_to_editor(self, dark: bool):
|
|
138
|
+
self.setStyleSheet("QPlainTextEdit { background-color: #1e1e1e; color: #d4d4d4; }" if dark
|
|
139
|
+
else "QPlainTextEdit { background-color: white; color: black; }")
|
|
140
|
+
# Re-trigger current line highlight with the new color
|
|
141
|
+
self.highlight_current_line()
|
|
@@ -0,0 +1,88 @@
|
|
|
1
|
+
from PySide6.QtWidgets import QTabWidget, QVBoxLayout, QWidget
|
|
2
|
+
|
|
3
|
+
from je_editor.pyside_ui.git_ui.code_diff_compare.side_by_side_diff_widget import SideBySideDiffWidget
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class MultiFileDiffViewer(QWidget):
|
|
7
|
+
"""
|
|
8
|
+
Multi-file diff viewer using tabs to display each file.
|
|
9
|
+
多檔案差異檢視器,使用分頁 (tab) 來顯示每個檔案的差異。
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
def __init__(self, parent=None):
|
|
13
|
+
"""
|
|
14
|
+
Initialize the multi-file diff viewer.
|
|
15
|
+
初始化多檔案差異檢視器。
|
|
16
|
+
"""
|
|
17
|
+
super().__init__(parent)
|
|
18
|
+
# Tab widget to hold each file's diff
|
|
19
|
+
# 用來存放每個檔案差異的分頁元件
|
|
20
|
+
self.tabs = QTabWidget()
|
|
21
|
+
|
|
22
|
+
# Layout: vertical box with only the tab widget
|
|
23
|
+
# 版面配置:垂直佈局,僅包含分頁元件
|
|
24
|
+
layout = QVBoxLayout(self)
|
|
25
|
+
layout.addWidget(self.tabs)
|
|
26
|
+
|
|
27
|
+
def set_diff_text(self, diff_text: str):
|
|
28
|
+
"""
|
|
29
|
+
Set the diff text and split it into per-file tabs.
|
|
30
|
+
設定差異文字,並依檔案拆分成多個分頁。
|
|
31
|
+
"""
|
|
32
|
+
self.tabs.clear()
|
|
33
|
+
file_diffs = self._split_by_file(diff_text)
|
|
34
|
+
for file_name, ftext in file_diffs:
|
|
35
|
+
viewer = SideBySideDiffWidget() # 每個檔案使用 side-by-side viewer
|
|
36
|
+
viewer.set_diff_text(ftext)
|
|
37
|
+
self.tabs.addTab(viewer, file_name) # 新增分頁,標題為檔名
|
|
38
|
+
|
|
39
|
+
def _split_by_file(self, diff_text: str):
|
|
40
|
+
"""
|
|
41
|
+
Split a unified diff text into chunks per file.
|
|
42
|
+
將 unified diff 文字依檔案切分。
|
|
43
|
+
"""
|
|
44
|
+
chunks = []
|
|
45
|
+
current = []
|
|
46
|
+
file_name = None
|
|
47
|
+
|
|
48
|
+
for line in diff_text.splitlines():
|
|
49
|
+
if line.startswith("diff --git"):
|
|
50
|
+
# 如果遇到新的檔案 diff,先把前一個檔案的內容存起來
|
|
51
|
+
if current and file_name:
|
|
52
|
+
chunks.append((file_name, "\n".join(current)))
|
|
53
|
+
current = []
|
|
54
|
+
# 解析檔名,格式通常為 "diff --git a/file b/file"
|
|
55
|
+
parts = line.split()
|
|
56
|
+
if len(parts) >= 4:
|
|
57
|
+
file_name = parts[2][2:] # 去掉 "a/"
|
|
58
|
+
else:
|
|
59
|
+
file_name = "unknown"
|
|
60
|
+
current.append(line)
|
|
61
|
+
else:
|
|
62
|
+
current.append(line)
|
|
63
|
+
|
|
64
|
+
# 最後一個檔案也要加入結果
|
|
65
|
+
if current and file_name:
|
|
66
|
+
chunks.append((file_name, "\n".join(current)))
|
|
67
|
+
|
|
68
|
+
return chunks
|
|
69
|
+
|
|
70
|
+
def set_dark_theme(self):
|
|
71
|
+
"""
|
|
72
|
+
Apply dark theme to all tabs.
|
|
73
|
+
對所有分頁套用深色主題。
|
|
74
|
+
"""
|
|
75
|
+
for i in range(self.tabs.count()):
|
|
76
|
+
w = self.tabs.widget(i)
|
|
77
|
+
if isinstance(w, SideBySideDiffWidget):
|
|
78
|
+
w.set_dark_theme()
|
|
79
|
+
|
|
80
|
+
def set_light_theme(self):
|
|
81
|
+
"""
|
|
82
|
+
Apply light theme to all tabs.
|
|
83
|
+
對所有分頁套用淺色主題。
|
|
84
|
+
"""
|
|
85
|
+
for i in range(self.tabs.count()):
|
|
86
|
+
w = self.tabs.widget(i)
|
|
87
|
+
if isinstance(w, SideBySideDiffWidget):
|
|
88
|
+
w.set_light_theme()
|
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
from PySide6.QtGui import QColor, QTextCursor, QTextCharFormat, QFont
|
|
2
|
+
from PySide6.QtWidgets import QPlainTextEdit, QTextEdit, QWidget, QHBoxLayout, QVBoxLayout, QLabel
|
|
3
|
+
|
|
4
|
+
from je_editor.pyside_ui.git_ui.code_diff_compare.line_number_code_viewer import LineNumberedCodeViewer
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class SideBySideDiffWidget(QWidget):
|
|
8
|
+
"""
|
|
9
|
+
Side-by-side diff viewer widget.
|
|
10
|
+
左右對照的差異檢視元件。
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
def __init__(self, parent=None):
|
|
14
|
+
super().__init__(parent)
|
|
15
|
+
|
|
16
|
+
# === 顏色設定 / Color configuration ===
|
|
17
|
+
self.is_dark = True
|
|
18
|
+
# 刪除行背景 / Deleted line background
|
|
19
|
+
self.color_del = QColor("#ffcccc") # 淡紅色,深淺背景都清楚
|
|
20
|
+
# 新增行背景 / Added line background
|
|
21
|
+
self.color_add = QColor("#ccffcc") # 淡綠色,對比度佳
|
|
22
|
+
# Hunk header 背景
|
|
23
|
+
self.color_hunk = QColor("#cce5ff") # 淡藍色,醒目但不刺眼
|
|
24
|
+
# Diff header 背景
|
|
25
|
+
self.color_header = QColor("#e0e0e0") # 淺灰,適合標題區塊
|
|
26
|
+
|
|
27
|
+
# === 左右檔名標籤 / File name labels ===
|
|
28
|
+
self.leftLabel = QLabel("Left: (old)")
|
|
29
|
+
self.rightLabel = QLabel("Right: (new)")
|
|
30
|
+
font = QFont()
|
|
31
|
+
font.setBold(True)
|
|
32
|
+
self.leftLabel.setFont(font)
|
|
33
|
+
self.rightLabel.setFont(font)
|
|
34
|
+
|
|
35
|
+
# === 左右文字編輯器 / Left and right code editors ===
|
|
36
|
+
self.leftEdit = LineNumberedCodeViewer()
|
|
37
|
+
self.rightEdit = LineNumberedCodeViewer()
|
|
38
|
+
for edit in (self.leftEdit, self.rightEdit):
|
|
39
|
+
edit.setReadOnly(True) # 設為唯讀 / Read-only
|
|
40
|
+
edit.setLineWrapMode(QPlainTextEdit.LineWrapMode.NoWrap) # 不自動換行 / Disable line wrap
|
|
41
|
+
mono = QFont("Consolas") # 使用等寬字型 / Monospaced font
|
|
42
|
+
mono.setStyleHint(QFont.StyleHint.Monospace)
|
|
43
|
+
edit.setFont(mono)
|
|
44
|
+
|
|
45
|
+
# === 版面配置 / Layout ===
|
|
46
|
+
leftBox = QVBoxLayout()
|
|
47
|
+
leftBox.addWidget(self.leftLabel)
|
|
48
|
+
leftBox.addWidget(self.leftEdit)
|
|
49
|
+
|
|
50
|
+
rightBox = QVBoxLayout()
|
|
51
|
+
rightBox.addWidget(self.rightLabel)
|
|
52
|
+
rightBox.addWidget(self.rightEdit)
|
|
53
|
+
|
|
54
|
+
main = QHBoxLayout(self)
|
|
55
|
+
leftContainer = QWidget()
|
|
56
|
+
leftContainer.setLayout(leftBox)
|
|
57
|
+
rightContainer = QWidget()
|
|
58
|
+
rightContainer.setLayout(rightBox)
|
|
59
|
+
main.addWidget(leftContainer)
|
|
60
|
+
main.addWidget(rightContainer)
|
|
61
|
+
|
|
62
|
+
# 同步左右捲軸 / Sync scrollbars
|
|
63
|
+
self._sync_scrollbars()
|
|
64
|
+
|
|
65
|
+
# 預設深色模式 / Default to dark theme
|
|
66
|
+
self.set_dark_theme()
|
|
67
|
+
|
|
68
|
+
def _sync_scrollbars(self):
|
|
69
|
+
"""
|
|
70
|
+
Synchronize scrollbars between left and right editors.
|
|
71
|
+
同步左右編輯器的捲軸。
|
|
72
|
+
"""
|
|
73
|
+
self.leftEdit.verticalScrollBar().valueChanged.connect(
|
|
74
|
+
self.rightEdit.verticalScrollBar().setValue
|
|
75
|
+
)
|
|
76
|
+
self.rightEdit.verticalScrollBar().valueChanged.connect(
|
|
77
|
+
self.leftEdit.verticalScrollBar().setValue
|
|
78
|
+
)
|
|
79
|
+
self.leftEdit.horizontalScrollBar().valueChanged.connect(
|
|
80
|
+
self.rightEdit.horizontalScrollBar().setValue
|
|
81
|
+
)
|
|
82
|
+
self.rightEdit.horizontalScrollBar().valueChanged.connect(
|
|
83
|
+
self.leftEdit.horizontalScrollBar().setValue
|
|
84
|
+
)
|
|
85
|
+
|
|
86
|
+
def set_diff_text(self, diff_text: str):
|
|
87
|
+
"""
|
|
88
|
+
Parse unified diff text and display it in side-by-side editors.
|
|
89
|
+
解析 unified diff 文字並顯示在左右編輯器。
|
|
90
|
+
"""
|
|
91
|
+
left_lines, right_lines, left_marks, right_marks, left_name, right_name = \
|
|
92
|
+
self._parse_unified_diff(diff_text)
|
|
93
|
+
|
|
94
|
+
self.leftLabel.setText(f"Left: {left_name or '(old)'}")
|
|
95
|
+
self.rightLabel.setText(f"Right: {right_name or '(new)'}")
|
|
96
|
+
|
|
97
|
+
self._set_text_with_highlights(self.leftEdit, left_lines, left_marks)
|
|
98
|
+
self._set_text_with_highlights(self.rightEdit, right_lines, right_marks)
|
|
99
|
+
|
|
100
|
+
# 游標移到開頭 / Move cursor to start
|
|
101
|
+
self.leftEdit.moveCursor(QTextCursor.MoveOperation.Start)
|
|
102
|
+
self.rightEdit.moveCursor(QTextCursor.MoveOperation.Start)
|
|
103
|
+
|
|
104
|
+
def _set_text_with_highlights(self, edit: QPlainTextEdit, lines, marks):
|
|
105
|
+
"""
|
|
106
|
+
Set text and apply syntax highlighting based on diff marks.
|
|
107
|
+
設定文字並依 diff 標記加上背景色。
|
|
108
|
+
"""
|
|
109
|
+
edit.setPlainText("\n".join(lines))
|
|
110
|
+
|
|
111
|
+
diff_extras = []
|
|
112
|
+
for i, mark in enumerate(marks):
|
|
113
|
+
fmt = QTextCharFormat()
|
|
114
|
+
# Always set foreground so it won't fall back
|
|
115
|
+
# 永遠設定前景色,避免 fallback
|
|
116
|
+
fmt.setForeground(QColor("#d4d4d4") if self.is_dark else QColor("black"))
|
|
117
|
+
|
|
118
|
+
if mark == "DEL":
|
|
119
|
+
fmt.setBackground(self.color_del)
|
|
120
|
+
elif mark == "ADD":
|
|
121
|
+
fmt.setBackground(self.color_add)
|
|
122
|
+
elif mark == "HUNK":
|
|
123
|
+
fmt.setBackground(self.color_hunk)
|
|
124
|
+
elif mark == "HDR":
|
|
125
|
+
fmt.setBackground(self.color_header)
|
|
126
|
+
else:
|
|
127
|
+
continue
|
|
128
|
+
|
|
129
|
+
sel = self._line_selection(edit, i, fmt)
|
|
130
|
+
diff_extras.append(sel)
|
|
131
|
+
|
|
132
|
+
# 保留 diff selections,方便主題切換時重用
|
|
133
|
+
setattr(edit, "_diff_extras", diff_extras)
|
|
134
|
+
|
|
135
|
+
# 嘗試合併其他高亮(例如 LineNumberedCodeViewer 的當前行高亮)
|
|
136
|
+
if hasattr(edit, "_current_line_extras"):
|
|
137
|
+
merged = diff_extras + edit._current_line_extras
|
|
138
|
+
else:
|
|
139
|
+
merged = diff_extras
|
|
140
|
+
|
|
141
|
+
edit.setExtraSelections(merged)
|
|
142
|
+
|
|
143
|
+
def _line_selection(self, edit: QPlainTextEdit, line_index: int, fmt: QTextCharFormat):
|
|
144
|
+
"""
|
|
145
|
+
Create a selection for a specific line with given format.
|
|
146
|
+
建立某一行的選取區並套用格式。
|
|
147
|
+
"""
|
|
148
|
+
sel = QTextEdit.ExtraSelection()
|
|
149
|
+
sel.format = fmt
|
|
150
|
+
cursor = edit.textCursor()
|
|
151
|
+
cursor.movePosition(QTextCursor.MoveOperation.Start)
|
|
152
|
+
for _ in range(line_index):
|
|
153
|
+
cursor.movePosition(QTextCursor.MoveOperation.Down)
|
|
154
|
+
cursor.select(QTextCursor.SelectionType.LineUnderCursor)
|
|
155
|
+
sel.cursor = cursor
|
|
156
|
+
return sel
|
|
157
|
+
|
|
158
|
+
def _parse_unified_diff(self, diff_text: str):
|
|
159
|
+
"""
|
|
160
|
+
Parse unified diff into left/right lines and marks.
|
|
161
|
+
將 unified diff 解析成左右行與標記。
|
|
162
|
+
"""
|
|
163
|
+
left_lines, right_lines, left_marks, right_marks = [], [], [], []
|
|
164
|
+
left_name, right_name = None, None
|
|
165
|
+
|
|
166
|
+
def add_left(line, mark=None):
|
|
167
|
+
left_lines.append(line)
|
|
168
|
+
left_marks.append(mark or "CTX")
|
|
169
|
+
|
|
170
|
+
def add_right(line, mark=None):
|
|
171
|
+
right_lines.append(line)
|
|
172
|
+
right_marks.append(mark or "CTX")
|
|
173
|
+
|
|
174
|
+
def align():
|
|
175
|
+
# 對齊左右行數 / Align left and right line counts
|
|
176
|
+
if len(left_lines) < len(right_lines):
|
|
177
|
+
for _ in range(len(right_lines) - len(left_lines)):
|
|
178
|
+
add_left("")
|
|
179
|
+
elif len(right_lines) < len(left_lines):
|
|
180
|
+
for _ in range(len(left_lines) - len(right_lines)):
|
|
181
|
+
add_right("")
|
|
182
|
+
|
|
183
|
+
for raw in diff_text.splitlines():
|
|
184
|
+
if raw.startswith("diff "):
|
|
185
|
+
add_left(raw, "HDR"); add_right(raw, "HDR"); align()
|
|
186
|
+
elif raw.startswith("--- "):
|
|
187
|
+
left_name = raw[4:].strip()
|
|
188
|
+
add_left(raw, "HDR"); add_right("", "HDR"); align()
|
|
189
|
+
elif raw.startswith("+++ "):
|
|
190
|
+
right_name = raw[4:].strip()
|
|
191
|
+
add_left("", "HDR"); add_right(raw, "HDR"); align()
|
|
192
|
+
elif raw.startswith("@@"):
|
|
193
|
+
add_left(raw, "HUNK"); add_right(raw, "HUNK"); align()
|
|
194
|
+
elif raw.startswith("-"):
|
|
195
|
+
add_left(raw, "DEL"); add_right("", None); align()
|
|
196
|
+
elif raw.startswith("+"):
|
|
197
|
+
add_left("", None); add_right(raw, "ADD"); align()
|
|
198
|
+
else:
|
|
199
|
+
add_left(raw, None); add_right(raw, None); align()
|
|
200
|
+
|
|
201
|
+
return left_lines, right_lines, left_marks, right_marks, left_name, right_name
|
|
202
|
+
|
|
203
|
+
def _reapply_highlights_for_theme(self):
|
|
204
|
+
"""
|
|
205
|
+
Reapply highlights when theme changes.
|
|
206
|
+
主題切換時重新套用高亮。
|
|
207
|
+
"""
|
|
208
|
+
for edit in (self.leftEdit, self.rightEdit):
|
|
209
|
+
if hasattr(edit, "_diff_extras"):
|
|
210
|
+
updated = []
|
|
211
|
+
for sel in edit._diff_extras:
|
|
212
|
+
fmt: QTextCharFormat = QTextCharFormat(sel.format)
|
|
213
|
+
|
|
214
|
+
# 前景色依主題切換
|
|
215
|
+
fmt.setForeground(QColor("#d4d4d4") if self.is_dark else QColor("black"))
|
|
216
|
+
|
|
217
|
+
# 依照 mark 更新背景色
|
|
218
|
+
cursor = sel.cursor
|
|
219
|
+
cursor.select(QTextCursor.SelectionType.LineUnderCursor)
|
|
220
|
+
text = cursor.selectedText()
|
|
221
|
+
|
|
222
|
+
if text.startswith("-"):
|
|
223
|
+
fmt.setBackground(self.color_del)
|
|
224
|
+
elif text.startswith("+"):
|
|
225
|
+
fmt.setBackground(self.color_add)
|
|
226
|
+
elif text.startswith("@@"):
|
|
227
|
+
fmt.setBackground(self.color_hunk)
|
|
228
|
+
elif text.startswith("diff") or text.startswith("---") or text.startswith("+++"):
|
|
229
|
+
fmt.setBackground(self.color_header)
|
|
230
|
+
|
|
231
|
+
sel.format = fmt
|
|
232
|
+
updated.append(sel)
|
|
233
|
+
|
|
234
|
+
edit._diff_extras = updated
|
|
235
|
+
|
|
236
|
+
if hasattr(edit, "_current_line_extras"):
|
|
237
|
+
merged = updated + edit._current_line_extras
|
|
238
|
+
else:
|
|
239
|
+
merged = updated
|
|
240
|
+
edit.setExtraSelections(merged)
|
|
241
|
+
|
|
242
|
+
def set_dark_theme(self):
|
|
243
|
+
"""
|
|
244
|
+
Apply dark theme colors.
|
|
245
|
+
套用深色主題配色。
|
|
246
|
+
"""
|
|
247
|
+
self.is_dark = True
|
|
248
|
+
self.color_del = QColor(60, 20, 20)
|
|
249
|
+
self.color_add = QColor(20, 60, 20)
|
|
250
|
+
self.color_hunk = QColor(25, 25, 60)
|
|
251
|
+
self.color_header = QColor(50, 50, 50)
|
|
252
|
+
self.setStyleSheet("""QWidget { background-color: #1e1e1e; color: #d4d4d4; }""")
|
|
253
|
+
self._reapply_highlights_for_theme()
|
|
254
|
+
self.leftEdit.apply_theme_to_editor(dark=self.is_dark)
|
|
255
|
+
self.rightEdit.apply_theme_to_editor(dark=self.is_dark)
|
|
256
|
+
|
|
257
|
+
def set_light_theme(self):
|
|
258
|
+
"""
|
|
259
|
+
Apply light theme colors.
|
|
260
|
+
套用淺色主題配色。
|
|
261
|
+
"""
|
|
262
|
+
self.is_dark = False
|
|
263
|
+
self.color_del = QColor(255, 230, 230)
|
|
264
|
+
self.color_add = QColor(230, 255, 230)
|
|
265
|
+
self.color_hunk = QColor(230, 230, 255)
|
|
266
|
+
self.color_header = QColor(240, 240, 240)
|
|
267
|
+
self.setStyleSheet("""QWidget { background-color: white; color: black; }""")
|
|
268
|
+
self._reapply_highlights_for_theme()
|
|
269
|
+
self.leftEdit.apply_theme_to_editor(dark=self.is_dark)
|
|
270
|
+
self.rightEdit.apply_theme_to_editor(dark=self.is_dark)
|
|
271
|
+
|