codis 1.1.1__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.
Files changed (329) hide show
  1. codis-1.1.1.dist-info/METADATA +194 -0
  2. codis-1.1.1.dist-info/RECORD +329 -0
  3. codis-1.1.1.dist-info/WHEEL +5 -0
  4. codis-1.1.1.dist-info/entry_points.txt +5 -0
  5. codis-1.1.1.dist-info/licenses/LICENSE +21 -0
  6. codis-1.1.1.dist-info/top_level.txt +2 -0
  7. run.py +190 -0
  8. src/__init__.py +1 -0
  9. src/__main__.py +81 -0
  10. src/_app_build.py +1507 -0
  11. src/_app_editor.py +1274 -0
  12. src/_app_kui.py +825 -0
  13. src/_app_lsp.py +625 -0
  14. src/_app_recovery.py +189 -0
  15. src/_app_settings.py +1385 -0
  16. src/_app_tools.py +2471 -0
  17. src/_app_webpreview.py +126 -0
  18. src/_font_safety.py +127 -0
  19. src/_shells.py +172 -0
  20. src/app.py +1488 -0
  21. src/chat/__init__.py +1 -0
  22. src/chat/adapters/__init__.py +33 -0
  23. src/chat/adapters/base.py +209 -0
  24. src/chat/adapters/discord.py +190 -0
  25. src/chat/adapters/http.py +78 -0
  26. src/chat/adapters/irc.py +360 -0
  27. src/chat/adapters/matrix.py +219 -0
  28. src/chat/adapters/mattermost.py +178 -0
  29. src/chat/adapters/mqtt.py +165 -0
  30. src/chat/adapters/slack.py +204 -0
  31. src/chat/adapters/telegram.py +117 -0
  32. src/chat/adapters/ws.py +154 -0
  33. src/chat/adapters/xmpp.py +337 -0
  34. src/chat/history.py +76 -0
  35. src/chat/models.py +207 -0
  36. src/chat/probe.py +79 -0
  37. src/chat/session.py +446 -0
  38. src/chat/util.py +125 -0
  39. src/collab/__init__.py +1 -0
  40. src/collab/net.py +411 -0
  41. src/collab/ot.py +311 -0
  42. src/collab/session.py +141 -0
  43. src/collab/tk_adapter.py +134 -0
  44. src/config.py +335 -0
  45. src/constants.py +2552 -0
  46. src/dep_catalog.py +446 -0
  47. src/diff/__init__.py +1 -0
  48. src/diff/engine.py +247 -0
  49. src/dpi.py +30 -0
  50. src/editor/__init__.py +1 -0
  51. src/editor/_pane_lsp.py +2937 -0
  52. src/editor/_pane_utils.py +171 -0
  53. src/editor/autocomplete.py +607 -0
  54. src/editor/code_folding.py +836 -0
  55. src/editor/color_preview.py +382 -0
  56. src/editor/complexity_lens.py +281 -0
  57. src/editor/diff_pane.py +623 -0
  58. src/editor/file_watcher.py +144 -0
  59. src/editor/focus_block.py +236 -0
  60. src/editor/heatmap.py +141 -0
  61. src/editor/inlay_hints.py +366 -0
  62. src/editor/inline_blame.py +429 -0
  63. src/editor/kdocs.py +3376 -0
  64. src/editor/large_file.py +488 -0
  65. src/editor/ligatures.py +180 -0
  66. src/editor/line_numbers.py +598 -0
  67. src/editor/minimap.py +691 -0
  68. src/editor/multi_cursor.py +269 -0
  69. src/editor/pane.py +2082 -0
  70. src/editor/rhythm.py +206 -0
  71. src/editor/save_hooks.py +43 -0
  72. src/editor/smart_selection.py +425 -0
  73. src/editor/smart_tips.py +719 -0
  74. src/editor/snippets.py +411 -0
  75. src/editor/sticky_note_gutter.py +410 -0
  76. src/editor/sticky_scroll.py +385 -0
  77. src/editor/suggest.py +464 -0
  78. src/editor/text_transforms.py +310 -0
  79. src/editor/vim_mode.py +1074 -0
  80. src/events/__init__.py +84 -0
  81. src/events/bus.py +427 -0
  82. src/events/correlator.py +244 -0
  83. src/events/kinds.py +125 -0
  84. src/events/pipeline.py +264 -0
  85. src/events/stream.py +416 -0
  86. src/events/window.py +204 -0
  87. src/extcompat/__init__.py +19 -0
  88. src/extcompat/code/__init__.py +13 -0
  89. src/extcompat/code/consent.py +162 -0
  90. src/extcompat/code/host.py +353 -0
  91. src/extcompat/code/hosts/py_host.py +763 -0
  92. src/extcompat/code/locate.py +105 -0
  93. src/extcompat/code/manager.py +325 -0
  94. src/extcompat/code/permissions.py +154 -0
  95. src/extcompat/code/sandbox.py +60 -0
  96. src/extcompat/colors.py +131 -0
  97. src/extcompat/common.py +184 -0
  98. src/extcompat/cson.py +269 -0
  99. src/extcompat/formats/__init__.py +22 -0
  100. src/extcompat/formats/_tm.py +189 -0
  101. src/extcompat/formats/_util.py +31 -0
  102. src/extcompat/formats/base16.py +66 -0
  103. src/extcompat/formats/brackets.py +172 -0
  104. src/extcompat/formats/eclipse.py +193 -0
  105. src/extcompat/formats/emacs.py +361 -0
  106. src/extcompat/formats/geany.py +147 -0
  107. src/extcompat/formats/generic.py +184 -0
  108. src/extcompat/formats/gtksource.py +117 -0
  109. src/extcompat/formats/helix.py +91 -0
  110. src/extcompat/formats/jetbrains.py +343 -0
  111. src/extcompat/formats/kate.py +130 -0
  112. src/extcompat/formats/micro.py +131 -0
  113. src/extcompat/formats/nano.py +111 -0
  114. src/extcompat/formats/notepadpp.py +140 -0
  115. src/extcompat/formats/pulsar.py +165 -0
  116. src/extcompat/formats/pyides.py +163 -0
  117. src/extcompat/formats/sublime.py +340 -0
  118. src/extcompat/jsonc.py +65 -0
  119. src/extcompat/lessish.py +128 -0
  120. src/extcompat/manager.py +122 -0
  121. src/extcompat/market/__init__.py +63 -0
  122. src/extcompat/market/sources.py +211 -0
  123. src/extcompat/model.py +88 -0
  124. src/extcompat/net.py +228 -0
  125. src/extcompat/openvsx.py +63 -0
  126. src/extcompat/readers.py +362 -0
  127. src/extcompat/registry.py +73 -0
  128. src/extcompat/safexml.py +63 -0
  129. src/extcompat/sexp.py +187 -0
  130. src/extcompat/sources.py +105 -0
  131. src/extcompat/store.py +276 -0
  132. src/extcompat/textmate.py +214 -0
  133. src/extcompat/themes.py +487 -0
  134. src/extcompat/yamlite.py +294 -0
  135. src/fileio.py +94 -0
  136. src/fs_ops.py +106 -0
  137. src/i18n.py +233 -0
  138. src/ia/__init__.py +1 -0
  139. src/ia/actions.py +295 -0
  140. src/ia/approval_gate.py +672 -0
  141. src/ia/checkpoints.py +308 -0
  142. src/ia/context_builder.py +921 -0
  143. src/ia/dialdiff.py +408 -0
  144. src/ia/iaspace.py +2078 -0
  145. src/ia/pair.py +296 -0
  146. src/ia/providers.py +954 -0
  147. src/ia/session.py +335 -0
  148. src/ia/toolbox.py +2524 -0
  149. src/launcher_actions.py +204 -0
  150. src/lint/__init__.py +1 -0
  151. src/lint/manager.py +1058 -0
  152. src/locales/.translate_manifest.json +64170 -0
  153. src/locales/de.json +4936 -0
  154. src/locales/en.json +4936 -0
  155. src/locales/es.json +4936 -0
  156. src/locales/fr.json +4936 -0
  157. src/locales/it.json +4936 -0
  158. src/locales/ja.json +4936 -0
  159. src/locales/ko.json +4936 -0
  160. src/locales/nl.json +4936 -0
  161. src/locales/pl.json +4936 -0
  162. src/locales/pt-BR.json +4936 -0
  163. src/locales/ru.json +4936 -0
  164. src/locales/tr.json +4936 -0
  165. src/locales/uk.json +4936 -0
  166. src/locales/zh-CN.json +4936 -0
  167. src/lsp/__init__.py +26 -0
  168. src/lsp/client.py +2300 -0
  169. src/lsp/dap_client.py +480 -0
  170. src/lsp/edits.py +84 -0
  171. src/lsp/framing.py +73 -0
  172. src/lsp/protocol.py +931 -0
  173. src/main.py +260 -0
  174. src/memtrim.py +91 -0
  175. src/plugins/__init__.py +1 -0
  176. src/plugins/api.py +1761 -0
  177. src/plugins/loader.py +571 -0
  178. src/plugins/process_runner.py +519 -0
  179. src/plugins/registry.py +342 -0
  180. src/plugins/safety.py +367 -0
  181. src/plugins/startup.py +195 -0
  182. src/plugins/store.py +1178 -0
  183. src/proc_registry.py +278 -0
  184. src/pyexec.py +394 -0
  185. src/recovery.py +259 -0
  186. src/stall_trace.py +92 -0
  187. src/standalone.py +1070 -0
  188. src/startup_profile.py +35 -0
  189. src/syntax/__init__.py +1 -0
  190. src/syntax/highlighter.py +776 -0
  191. src/syntax/patterns.py +584 -0
  192. src/system_profile.py +430 -0
  193. src/terminal/backends.py +658 -0
  194. src/terminal/internal.py +346 -0
  195. src/terminal/keys.py +138 -0
  196. src/terminal/palette.py +67 -0
  197. src/terminal/session.py +283 -0
  198. src/terminal/shells.py +79 -0
  199. src/terminal/view.py +489 -0
  200. src/terminal/vt.py +870 -0
  201. src/textfmt.py +22 -0
  202. src/theme/__init__.py +1 -0
  203. src/theme/base.py +1281 -0
  204. src/theme/colors.py +49 -0
  205. src/theme/manager.py +602 -0
  206. src/theme/utils.py +312 -0
  207. src/toolbox.py +329 -0
  208. src/trash_core.py +531 -0
  209. src/ui/__init__.py +1 -0
  210. src/ui/_perf_utils.py +285 -0
  211. src/ui/_persistence_utils.py +93 -0
  212. src/ui/accessibility.py +843 -0
  213. src/ui/activity_bar.py +860 -0
  214. src/ui/app_embed.py +434 -0
  215. src/ui/archive_panel.py +608 -0
  216. src/ui/bookmarks.py +314 -0
  217. src/ui/breadcrumb.py +383 -0
  218. src/ui/calculator_panel.py +982 -0
  219. src/ui/calendar_panel.py +1218 -0
  220. src/ui/chat_panel.py +1271 -0
  221. src/ui/chat_profile_dialog.py +278 -0
  222. src/ui/chat_render.py +128 -0
  223. src/ui/clock_panel.py +810 -0
  224. src/ui/code_outline.py +364 -0
  225. src/ui/code_runner.py +729 -0
  226. src/ui/code_stats.py +546 -0
  227. src/ui/collab_panel.py +1009 -0
  228. src/ui/color_picker.py +547 -0
  229. src/ui/command_palette.py +452 -0
  230. src/ui/cursor_manager.py +750 -0
  231. src/ui/custom_keywords_panel.py +413 -0
  232. src/ui/debug_panel.py +1037 -0
  233. src/ui/dependency_graph.py +692 -0
  234. src/ui/diff_panel.py +381 -0
  235. src/ui/docs_panel.py +514 -0
  236. src/ui/doom_embed.py +349 -0
  237. src/ui/dropdown.py +321 -0
  238. src/ui/easter_eggs.py +979 -0
  239. src/ui/env_manager.py +666 -0
  240. src/ui/extcode_bridge.py +215 -0
  241. src/ui/extcode_dialogs.py +412 -0
  242. src/ui/extcompat_dialog.py +583 -0
  243. src/ui/file_timeline.py +319 -0
  244. src/ui/file_tree.py +1208 -0
  245. src/ui/find_in_files.py +1355 -0
  246. src/ui/flowchart_panel.py +727 -0
  247. src/ui/formatter_panel.py +490 -0
  248. src/ui/git_auth.py +763 -0
  249. src/ui/git_clone_dialog.py +487 -0
  250. src/ui/git_panel.py +1483 -0
  251. src/ui/grab_safe.py +84 -0
  252. src/ui/internal_python_dialog.py +98 -0
  253. src/ui/k_ui/__init__.py +1305 -0
  254. src/ui/k_ui/_bgrenderer.py +317 -0
  255. src/ui/k_ui/_colors.py +213 -0
  256. src/ui/k_ui/_dock.py +545 -0
  257. src/ui/k_ui/_easter.py +290 -0
  258. src/ui/k_ui/_engine.py +5448 -0
  259. src/ui/k_ui/_perf.py +333 -0
  260. src/ui/k_ui/_tkutil.py +7 -0
  261. src/ui/k_ui/_tooltip.py +365 -0
  262. src/ui/k_ui/_widgets.py +1204 -0
  263. src/ui/k_ui/_world.py +837 -0
  264. src/ui/key_remapper.py +856 -0
  265. src/ui/launcher.py +1252 -0
  266. src/ui/lazy_panel.py +73 -0
  267. src/ui/macro_recorder.py +610 -0
  268. src/ui/menu_bar.py +898 -0
  269. src/ui/output_panel.py +321 -0
  270. src/ui/paint_canvas.py +1000 -0
  271. src/ui/paint_engine.py +535 -0
  272. src/ui/paint_panel.py +888 -0
  273. src/ui/panel_detach.py +112 -0
  274. src/ui/performance_panel.py +861 -0
  275. src/ui/pip_manager.py +1758 -0
  276. src/ui/plugin_panel.py +1586 -0
  277. src/ui/pomodoro_panel.py +447 -0
  278. src/ui/problems.py +566 -0
  279. src/ui/process_manager.py +1657 -0
  280. src/ui/project_templates.py +775 -0
  281. src/ui/recovery_dialog.py +122 -0
  282. src/ui/regex_playground.py +304 -0
  283. src/ui/remote_adapters.py +1398 -0
  284. src/ui/remote_browser.py +1224 -0
  285. src/ui/remote_profile_dialog.py +342 -0
  286. src/ui/render_panel.py +1293 -0
  287. src/ui/rest_client.py +549 -0
  288. src/ui/scratchpad.py +548 -0
  289. src/ui/search_bar.py +1274 -0
  290. src/ui/settings_editor.py +3349 -0
  291. src/ui/settings_window.py +1554 -0
  292. src/ui/snippets_panel.py +597 -0
  293. src/ui/sql_studio.py +1815 -0
  294. src/ui/status_bar.py +438 -0
  295. src/ui/tab_bar.py +654 -0
  296. src/ui/task_manager.py +1773 -0
  297. src/ui/terminal.py +525 -0
  298. src/ui/theme_preview_panel.py +851 -0
  299. src/ui/thread_safe_tk.py +152 -0
  300. src/ui/time_tracker.py +574 -0
  301. src/ui/tkbind.py +43 -0
  302. src/ui/todo_panel.py +358 -0
  303. src/ui/toolbox_panels.py +658 -0
  304. src/ui/trash_manager.py +657 -0
  305. src/ui/ui_scale.py +294 -0
  306. src/ui/ux_enhancements.py +41 -0
  307. src/ui/web_preview.py +397 -0
  308. src/ui/webdev.py +630 -0
  309. src/ui/welcome.py +567 -0
  310. src/ui/window_embed.py +1500 -0
  311. src/ui/wizard.py +1550 -0
  312. src/ui/workspace_switcher.py +418 -0
  313. src/ui/workspace_trust.py +370 -0
  314. src/ui/zen_audio.py +354 -0
  315. src/ui/zen_media.py +2271 -0
  316. src/ui/zen_media_client.py +303 -0
  317. src/ui/zen_media_host.py +329 -0
  318. src/ui/zen_player.py +907 -0
  319. src/ui/zen_player_widgets.py +700 -0
  320. src/updater/__init__.py +11 -0
  321. src/updater/assets.py +145 -0
  322. src/updater/backup.py +101 -0
  323. src/updater/channel.py +221 -0
  324. src/updater/dialog.py +492 -0
  325. src/updater/integrity.py +111 -0
  326. src/updater/manager.py +727 -0
  327. src/updater/result.py +114 -0
  328. src/venv_runtime.py +531 -0
  329. src/workspace_state.py +25 -0
@@ -0,0 +1,727 @@
1
+ """Codis - Flowchart Generator panel.
2
+
3
+ Parses a Python file with the `ast` module and renders an interactive
4
+ flowchart on a tkinter Canvas. No external dependencies required.
5
+
6
+ Nodes:
7
+ ▭ Rectangle - function def / class def / assign / expr
8
+ ◇ Diamond - if / elif / else branches
9
+ ⬡ Hexagon - for / while loops (rendered as parallelogram)
10
+ ○ Oval - function entry / return / raise / exit
11
+ """
12
+
13
+ from __future__ import annotations
14
+
15
+ import ast
16
+ import contextlib
17
+ import tkinter as tk
18
+ from collections.abc import Callable
19
+ from pathlib import Path
20
+ from tkinter import filedialog, ttk
21
+ from typing import Any
22
+
23
+ from src.config import ConfigManager
24
+ from src.constants import FONT_UI
25
+ from src.i18n import _t
26
+ from src.theme.manager import ThemeManager
27
+
28
+ _BOX_W = 160
29
+ _BOX_H = 36
30
+ _DIAMOND_W = 170
31
+ _DIAMOND_H = 48
32
+ _OVAL_W = 120
33
+ _OVAL_H = 32
34
+ _H_GAP = 60
35
+ _V_GAP = 54
36
+ _INDENT = 90
37
+
38
+ _COLORS = {
39
+ "func": ("#3b8fd4", "#cfe8ff"),
40
+ "class": ("#9b59b6", "#e8d5f0"),
41
+ "if": ("#e67e22", "#fde8cc"),
42
+ "loop": ("#27ae60", "#cff0dc"),
43
+ "return": ("#e74c3c", "#fad7d7"),
44
+ "assign": ("#555", "#e8e8e8"),
45
+ "expr": ("#666", "#f0f0f0"),
46
+ "start": ("#1abc9c", "#d5f5ef"),
47
+ "end": ("#e74c3c", "#ffd7d7"),
48
+ }
49
+ _DARK_COLORS = {
50
+ "func": ("#5aafff", "#1a3a52"),
51
+ "class": ("#c084fc", "#2d1a3a"),
52
+ "if": ("#fb923c", "#3a2010"),
53
+ "loop": ("#4ade80", "#0f2e18"),
54
+ "return": ("#f87171", "#2d1010"),
55
+ "assign": ("#888", "#282828"),
56
+ "expr": ("#777", "#242424"),
57
+ "start": ("#2dd4bf", "#0d2e2a"),
58
+ "end": ("#f87171", "#2d1010"),
59
+ }
60
+
61
+
62
+ def _is_dark(bg_hex: str) -> bool:
63
+ bg_hex = bg_hex.lstrip("#")
64
+ r, g, b = int(bg_hex[0:2], 16), int(bg_hex[2:4], 16), int(bg_hex[4:6], 16)
65
+ return (0.299 * r + 0.587 * g + 0.114 * b) < 128
66
+
67
+
68
+ class FlowNode:
69
+ """Class docstring."""
70
+
71
+ __slots__ = (
72
+ "kind",
73
+ "label",
74
+ "lineno",
75
+ "children_true",
76
+ "children_false",
77
+ "children_body",
78
+ "x",
79
+ "y",
80
+ "w",
81
+ "h",
82
+ "id",
83
+ )
84
+ _counter = 0
85
+
86
+ def __init__(self, kind: str, label: str, lineno: int = 0) -> None:
87
+ FlowNode._counter += 1
88
+ self.id = FlowNode._counter
89
+ self.kind = kind
90
+ self.label = label
91
+ self.lineno = lineno
92
+ self.children_true: list[FlowNode] = []
93
+ self.children_false: list[FlowNode] = []
94
+ self.children_body: list[FlowNode] = []
95
+ self.x = self.y = 0
96
+ self.w = _BOX_W
97
+ self.h = _BOX_H
98
+
99
+
100
+ def _truncate(s: str, n: int = 26) -> str:
101
+ return s if len(s) <= n else s[: n - 1] + "..."
102
+
103
+
104
+ def _node_label(node: ast.AST) -> tuple[str, str]:
105
+ """Return (kind, label) for an AST node."""
106
+ if isinstance(node, ast.FunctionDef | ast.AsyncFunctionDef):
107
+ return "func", f"def {node.name}(...)"
108
+ if isinstance(node, ast.ClassDef):
109
+ return "class", f"class {node.name}"
110
+ if isinstance(node, ast.If):
111
+ try:
112
+ cond = ast.unparse(node.test)
113
+ except (ValueError, AttributeError):
114
+ cond = "condition"
115
+ return "if", _truncate(f"if {cond}")
116
+ if isinstance(node, ast.For | ast.AsyncFor):
117
+ try:
118
+ target = ast.unparse(node.target)
119
+ iter_ = ast.unparse(node.iter)
120
+ label = f"for {target} in {iter_}"
121
+ except (ValueError, AttributeError):
122
+ label = "for"
123
+ return "loop", _truncate(label)
124
+ if isinstance(node, ast.While):
125
+ try:
126
+ cond = ast.unparse(node.test)
127
+ except (ValueError, AttributeError):
128
+ cond = "condition"
129
+ return "loop", _truncate(f"while {cond}")
130
+ if isinstance(node, ast.Return):
131
+ try:
132
+ val = ast.unparse(node.value) if node.value else ""
133
+ label = f"return {val}" if val else "return"
134
+ except (ValueError, AttributeError):
135
+ label = "return"
136
+ return "return", _truncate(label)
137
+ if isinstance(node, ast.Raise):
138
+ return "return", "raise"
139
+ if isinstance(node, ast.Assign | ast.AugAssign | ast.AnnAssign):
140
+ try:
141
+ label = ast.unparse(node)
142
+ except (ValueError, AttributeError):
143
+ label = "assign"
144
+ return "assign", _truncate(label)
145
+ if isinstance(node, ast.Expr):
146
+ try:
147
+ label = ast.unparse(node)
148
+ except (ValueError, AttributeError):
149
+ label = "expr"
150
+ return "expr", _truncate(label)
151
+ return "expr", type(node).__name__
152
+
153
+
154
+ def _build_nodes(
155
+ stmts: list[ast.stmt], depth: int = 0, max_depth: int = 4
156
+ ) -> list[FlowNode]:
157
+ """Recursively convert AST statements to FlowNode list."""
158
+ nodes: list[FlowNode] = []
159
+ if depth > max_depth:
160
+ return nodes
161
+ for stmt in stmts:
162
+ if isinstance(stmt, ast.FunctionDef | ast.AsyncFunctionDef | ast.ClassDef):
163
+ kind, label = _node_label(stmt)
164
+ n = FlowNode(kind, label, getattr(stmt, "lineno", 0))
165
+ body_stmts = stmt.body if hasattr(stmt, "body") else []
166
+ entry = FlowNode("start", "Entry")
167
+ exit_ = FlowNode("end", "Return")
168
+ n.children_body = (
169
+ [entry] + _build_nodes(body_stmts, depth +
170
+ 1, max_depth) + [exit_]
171
+ )
172
+ nodes.append(n)
173
+ elif isinstance(stmt, ast.If):
174
+ kind, label = _node_label(stmt)
175
+ n = FlowNode(kind, label, getattr(stmt, "lineno", 0))
176
+ n.children_true = _build_nodes(stmt.body, depth + 1, max_depth)
177
+ n.children_false = _build_nodes(stmt.orelse, depth + 1, max_depth)
178
+ nodes.append(n)
179
+ elif isinstance(stmt, ast.For | ast.AsyncFor | ast.While):
180
+ kind, label = _node_label(stmt)
181
+ n = FlowNode(kind, label, getattr(stmt, "lineno", 0))
182
+ n.children_body = _build_nodes(stmt.body, depth + 1, max_depth)
183
+ nodes.append(n)
184
+ elif isinstance(stmt, ast.Return | ast.Raise):
185
+ kind, label = _node_label(stmt)
186
+ nodes.append(FlowNode(kind, label, getattr(stmt, "lineno", 0)))
187
+ break
188
+ else:
189
+ kind, label = _node_label(stmt)
190
+ nodes.append(FlowNode(kind, label, getattr(stmt, "lineno", 0)))
191
+ return nodes
192
+
193
+
194
+ class _Layout:
195
+ """Assigns (x, y) coordinates to nodes using a simple top-down layout."""
196
+
197
+ def __init__(self, nodes: list[FlowNode], origin_x: int, origin_y: int) -> None:
198
+ """Inicializa o motor de layout automático do flowchart."""
199
+
200
+ def layout(self, nodes: list[FlowNode], x0: int, y0: int) -> int:
201
+ """Return bottom-most y after laying out nodes."""
202
+ y = y0
203
+ for n in nodes:
204
+ if n.kind == "if":
205
+ n.x, n.y = x0, y
206
+ n.w, n.h = _DIAMOND_W, _DIAMOND_H
207
+ y += _DIAMOND_H + _V_GAP // 2
208
+
209
+ y_true = self.layout(
210
+ n.children_true, x0 - _H_GAP - _BOX_W // 2, y)
211
+
212
+ y_false = self.layout(
213
+ n.children_false, x0 + _H_GAP + _BOX_W // 2, y)
214
+ y = max(y_true, y_false) + _V_GAP
215
+ elif n.kind in ("loop", "func", "class") and n.children_body:
216
+ n.x, n.y = x0, y
217
+ n.w = _BOX_W
218
+ n.h = _BOX_H
219
+ y += _BOX_H + _V_GAP // 2
220
+ y = self.layout(n.children_body, x0 + _INDENT // 2, y)
221
+ y += _V_GAP // 2
222
+ elif n.kind in ("start", "end", "return"):
223
+ n.x, n.y = x0, y
224
+ n.w, n.h = _OVAL_W, _OVAL_H
225
+ y += _OVAL_H + _V_GAP
226
+ else:
227
+ n.x, n.y = x0, y
228
+ n.w, n.h = _BOX_W, _BOX_H
229
+ y += _BOX_H + _V_GAP
230
+ return y
231
+
232
+
233
+ class _FlowCanvas(tk.Canvas):
234
+ """Zoomable, scrollable canvas that renders FlowNode trees."""
235
+
236
+ def __init__(self, parent: tk.Widget, **kw: Any) -> None:
237
+ super().__init__(parent, **kw)
238
+ self._scale = 1.0
239
+ self._nodes: list[FlowNode] = []
240
+ self._dark = False
241
+ self._on_click: Callable[[int], None] | None = None
242
+ self._pan_x = self._pan_y = 0
243
+ self._drag_start: tuple[int, int] | None = None
244
+
245
+ self.bind("<ButtonPress-1>", self._on_press)
246
+ self.bind("<B1-Motion>", self._on_drag)
247
+ self.bind("<MouseWheel>", self._on_wheel)
248
+ self.bind("<Button-4>", self._on_wheel)
249
+ self.bind("<Button-5>", self._on_wheel)
250
+
251
+ def set_nodes(self, nodes: list[FlowNode], dark: bool = False) -> None:
252
+ """Define os nós a desenhar e a paleta (escura ou clara)."""
253
+ self._nodes = nodes
254
+ self._dark = dark
255
+ self.render()
256
+
257
+ def render(self) -> None:
258
+ """Redesenha os nós com o pan e o zoom atuais."""
259
+ self.delete("all")
260
+ pal = _DARK_COLORS if self._dark else _COLORS
261
+ s = self._scale
262
+ px, py = self._pan_x, self._pan_y
263
+
264
+ def sx(v: float) -> float:
265
+ return v * s + px
266
+
267
+ def sy(v: float) -> float:
268
+ return v * s + py
269
+
270
+ for n in self._flat(self._nodes):
271
+ x, y, w, h = n.x, n.y, n.w, n.h
272
+ border, fill = pal.get(n.kind, pal["expr"])
273
+ fg = "#ffffff" if self._dark else "#111111"
274
+
275
+ if n.kind == "if":
276
+ cx, cy = sx(x + w / 2), sy(y + h / 2)
277
+ pts = [cx, sy(y), sx(x + w), cy, cx, sy(y + h), sx(x), cy]
278
+ self.create_polygon(
279
+ *pts, fill=fill, outline=border, width=2, tags=("node", f"n{n.id}")
280
+ )
281
+ elif n.kind in ("start", "end", "return"):
282
+ self.create_oval(
283
+ sx(x),
284
+ sy(y),
285
+ sx(x + w),
286
+ sy(y + h),
287
+ fill=fill,
288
+ outline=border,
289
+ width=2,
290
+ tags=("node", f"n{n.id}"),
291
+ )
292
+ elif n.kind in ("loop",):
293
+ off = h * s * 0.3
294
+ pts = [
295
+ sx(x) + off,
296
+ sy(y),
297
+ sx(x + w),
298
+ sy(y),
299
+ sx(x + w) - off,
300
+ sy(y + h),
301
+ sx(x),
302
+ sy(y + h),
303
+ ]
304
+ self.create_polygon(
305
+ *pts, fill=fill, outline=border, width=2, tags=("node", f"n{n.id}")
306
+ )
307
+ else:
308
+ r = 6 * s
309
+ self.create_rectangle(
310
+ sx(x) + r,
311
+ sy(y),
312
+ sx(x + w) - r,
313
+ sy(y + h),
314
+ fill=fill,
315
+ outline="",
316
+ tags=f"n{n.id}",
317
+ )
318
+ self.create_rectangle(
319
+ sx(x),
320
+ sy(y) + r,
321
+ sx(x + w),
322
+ sy(y + h) - r,
323
+ fill=fill,
324
+ outline="",
325
+ tags=f"n{n.id}",
326
+ )
327
+ self.create_rectangle(
328
+ sx(x),
329
+ sy(y),
330
+ sx(x + w),
331
+ sy(y + h),
332
+ fill="",
333
+ outline=border,
334
+ width=2,
335
+ tags=("node", f"n{n.id}"),
336
+ )
337
+
338
+ self.create_text(
339
+ sx(x + w / 2),
340
+ sy(y + h / 2),
341
+ text=n.label,
342
+ fill=fg,
343
+ font=(FONT_UI, max(7, int(9 * s))),
344
+ tags=f"n{n.id}",
345
+ )
346
+
347
+ self._draw_arrows(n, s, px, py, border)
348
+
349
+ bbox = self.bbox("all")
350
+ if bbox:
351
+ pad = 40
352
+ self.config(
353
+ scrollregion=(
354
+ bbox[0] - pad,
355
+ bbox[1] - pad,
356
+ bbox[2] + pad,
357
+ bbox[3] + pad,
358
+ )
359
+ )
360
+
361
+ def _draw_arrows(
362
+ self, n: FlowNode, s: float, px: float, py: float, color: str
363
+ ) -> None:
364
+
365
+ def sx(v: float) -> float:
366
+ return v * s + px
367
+
368
+ def sy(v: float) -> float:
369
+ return v * s + py
370
+
371
+ def arrow(x1: float, y1: float, x2: float, y2: float, label: str = "") -> None:
372
+ self.create_line(
373
+ sx(x1),
374
+ sy(y1),
375
+ sx(x2),
376
+ sy(y2),
377
+ fill=color,
378
+ width=max(1, int(1.5 * s)),
379
+ arrow="last",
380
+ arrowshape=(8 * s, 10 * s, 3 * s),
381
+ )
382
+ if label:
383
+ mx, my = (x1 + x2) / 2, (y1 + y2) / 2
384
+ self.create_text(
385
+ sx(mx + 6),
386
+ sy(my),
387
+ text=label,
388
+ fill=color,
389
+ font=(FONT_UI, max(6, int(7 * s))),
390
+ anchor="w",
391
+ )
392
+
393
+ bx, by, bw, bh = n.x, n.y, n.w, n.h
394
+
395
+ if n.kind == "if":
396
+ if n.children_true:
397
+ c = n.children_true[0]
398
+ arrow(bx, by + bh / 2, c.x + c.w / 2, c.y, "Yes")
399
+
400
+ if n.children_false:
401
+ c = n.children_false[0]
402
+ arrow(bx + bw, by + bh / 2, c.x + c.w / 2, c.y, "No")
403
+ elif n.children_body:
404
+ c = n.children_body[0]
405
+ arrow(bx + bw / 2, by + bh, c.x + c.w / 2, c.y)
406
+ for a, b in zip(n.children_body, n.children_body[1:], strict=False):
407
+ arrow(a.x + a.w / 2, a.y + a.h, b.x + b.w / 2, b.y)
408
+
409
+ def _flat(self, nodes: list[FlowNode]) -> list[FlowNode]:
410
+ out: list[FlowNode] = []
411
+ for n in nodes:
412
+ out.append(n)
413
+ out.extend(self._flat(n.children_true))
414
+ out.extend(self._flat(n.children_false))
415
+ out.extend(self._flat(n.children_body))
416
+ return out
417
+
418
+ def zoom(self, factor: float) -> None:
419
+ """Aplica zoom no canvas do flowchart."""
420
+ self._scale = max(0.2, min(4.0, self._scale * factor))
421
+ self.render()
422
+
423
+ def fit(self) -> None:
424
+ """Fit all nodes to canvas."""
425
+ nodes = self._flat(self._nodes)
426
+ if not nodes:
427
+ return
428
+ min_x = min(n.x for n in nodes)
429
+ min_y = min(n.y for n in nodes)
430
+ max_x = max(n.x + n.w for n in nodes)
431
+ max_y = max(n.y + n.h for n in nodes)
432
+ cw = self.winfo_width() or 400
433
+ ch = self.winfo_height() or 400
434
+ pad = 40
435
+ content_w = max_x - min_x + pad * 2
436
+ content_h = max_y - min_y + pad * 2
437
+ sx = cw / content_w if content_w > 0 else 1
438
+ sy = ch / content_h if content_h > 0 else 1
439
+ self._scale = max(0.2, min(2.0, sx, sy))
440
+ self._pan_x = pad - min_x * self._scale
441
+ self._pan_y = pad - min_y * self._scale
442
+ self.render()
443
+
444
+ def _on_press(self, e: Any) -> None:
445
+ self._drag_start = (e.x, e.y)
446
+
447
+ def _on_drag(self, e: Any) -> None:
448
+ if self._drag_start:
449
+ dx = e.x - self._drag_start[0]
450
+ dy = e.y - self._drag_start[1]
451
+ self._pan_x += dx
452
+ self._pan_y += dy
453
+ self._drag_start = (e.x, e.y)
454
+ self.render()
455
+
456
+ def _on_wheel(self, e: Any) -> None:
457
+ factor = 1.1
458
+ if e.num == 5 or (hasattr(e, "delta") and e.delta < 0):
459
+ factor = 1 / factor
460
+ self.zoom(factor)
461
+
462
+ def export_svg(self, path: str) -> None:
463
+ """Very basic SVG export."""
464
+ nodes = self._flat(self._nodes)
465
+ if not nodes:
466
+ return
467
+ min_x = min(n.x for n in nodes) - 20
468
+ min_y = min(n.y for n in nodes) - 20
469
+ max_x = max(n.x + n.w for n in nodes) + 20
470
+ max_y = max(n.y + n.h for n in nodes) + 20
471
+ W = max_x - min_x
472
+ H = max_y - min_y
473
+ pal = _DARK_COLORS if self._dark else _COLORS
474
+
475
+ lines = [
476
+ '<svg xmlns="http://www.w3.org/2000/svg" '
477
+ f'width="{W}" height="{H}" viewBox="{min_x} {min_y} {W} {H}">',
478
+ "<style>text{font-family:sans-serif;font-size:10px;}</style>",
479
+ ]
480
+ for n in nodes:
481
+ border, fill = pal.get(n.kind, pal["expr"])
482
+ cx = n.x + n.w / 2 - min_x
483
+ cy = n.y + n.h / 2 - min_y
484
+ x = n.x - min_x
485
+ y = n.y - min_y
486
+ if n.kind == "if":
487
+ pts = f"{cx},{y} {x + n.w},{cy} {cx},{y + n.h} {x},{cy}"
488
+ lines.append(
489
+ f'<polygon points="{pts}" fill="{fill}" stroke="{border}" stroke-width="2"/>'
490
+ )
491
+ elif n.kind in ("start", "end", "return"):
492
+ lines.append(
493
+ f'<ellipse cx="{cx}" cy="{cy}" rx="{n.w / 2}" ry="{n.h / 2}" '
494
+ f'fill="{fill}" stroke="{border}" stroke-width="2"/>'
495
+ )
496
+ else:
497
+ lines.append(
498
+ f'<rect x="{x}" y="{y}" width="{n.w}" height="{n.h}" rx="5" '
499
+ f'fill="{fill}" stroke="{border}" stroke-width="2"/>'
500
+ )
501
+ fg = "#ffffff" if self._dark else "#111"
502
+ lines.append(
503
+ f'<text x="{cx}" y="{cy + 4}" text-anchor="middle" fill="{fg}">{n.label}</text>'
504
+ )
505
+ lines.append("</svg>")
506
+ Path(path).write_text("\n".join(lines), encoding="utf-8")
507
+
508
+
509
+ class FlowchartPanel(tk.Frame):
510
+ """Sidebar panel that generates an interactive flowchart from the active file."""
511
+
512
+ def __init__(
513
+ self, parent: tk.Widget, tmgr: ThemeManager, cfg: ConfigManager
514
+ ) -> None:
515
+ """Cria o painel de geração de fluxogramas do código.
516
+
517
+ Args:
518
+ parent: Widget pai Tkinter.
519
+ cfg: Instância de ConfigManager.
520
+ """
521
+ t = tmgr.theme
522
+ super().__init__(parent, bg=t.panel_bg)
523
+ self._tmgr = tmgr
524
+ self._cfg = cfg
525
+ self._current_path: str | None = None
526
+ self._nodes: list[FlowNode] = []
527
+ self._max_depth = tk.IntVar(value=3)
528
+ self._build()
529
+
530
+ def _build(self) -> None:
531
+ t = self._tmgr.theme
532
+
533
+ for child in self.winfo_children():
534
+ with contextlib.suppress(tk.TclError):
535
+ child.destroy()
536
+
537
+ tb = tk.Frame(self, bg=t.panel_bg)
538
+ tb.pack(fill="x", padx=6, pady=(6, 2))
539
+
540
+ tk.Label(
541
+ tb,
542
+ text=f"🔀 {_t('flowchart.title')}",
543
+ bg=t.panel_bg,
544
+ fg=t.fg,
545
+ font=(FONT_UI, 10, "bold"),
546
+ ).pack(side="left")
547
+
548
+ for txt, cmd in (
549
+ (_t("flowchart.generate"), self._generate),
550
+ (_t("flowchart.export_svg"), self._export_svg),
551
+ ):
552
+ b = tk.Label(
553
+ tb,
554
+ text=txt,
555
+ bg=t.input_bg,
556
+ fg=t.fg,
557
+ font=(FONT_UI, 8),
558
+ cursor="hand2",
559
+ padx=6,
560
+ pady=2,
561
+ )
562
+ b.pack(side="right", padx=2)
563
+ b.bind("<Button-1>", lambda _, c=cmd: c())
564
+ b.bind("<Enter>", lambda _, w=b: w.config(bg=t.sel_bg))
565
+ b.bind("<Leave>", lambda _, w=b: w.config(bg=t.input_bg))
566
+
567
+ dep_row = tk.Frame(self, bg=t.panel_bg)
568
+ dep_row.pack(fill="x", padx=8, pady=(0, 4))
569
+ tk.Label(
570
+ dep_row,
571
+ text=_t("flowchart.depth"),
572
+ bg=t.panel_bg,
573
+ fg=t.fg_muted,
574
+ font=(FONT_UI, 8),
575
+ ).pack(side="left")
576
+ tk.Spinbox(
577
+ dep_row,
578
+ from_=1,
579
+ to=8,
580
+ textvariable=self._max_depth,
581
+ font=(FONT_UI, 8),
582
+ width=3,
583
+ bg=t.input_bg,
584
+ fg=t.fg,
585
+ buttonbackground=t.border,
586
+ relief="flat",
587
+ ).pack(side="left", padx=4)
588
+
589
+ zoom_row = tk.Frame(self, bg=t.panel_bg)
590
+ zoom_row.pack(fill="x", padx=8, pady=(0, 2))
591
+ for txt, cmd in (
592
+ (
593
+ _t("flowchart.zoom_in"),
594
+ lambda: self._canvas.zoom(1.2),
595
+ ), # pylint: disable=access-member-before-definition
596
+ (_t("flowchart.zoom_out"), lambda: self._canvas.zoom(1 / 1.2)),
597
+ (
598
+ _t("flowchart.fit"),
599
+ lambda: self._canvas.fit(),
600
+ ), # pylint: disable=access-member-before-definition,unnecessary-lambda
601
+ ):
602
+ b = tk.Label(
603
+ zoom_row,
604
+ text=txt,
605
+ bg=t.input_bg,
606
+ fg=t.fg,
607
+ font=(FONT_UI, 8),
608
+ cursor="hand2",
609
+ padx=6,
610
+ pady=2,
611
+ )
612
+ b.pack(side="left", padx=2)
613
+ b.bind("<Button-1>", lambda _, c=cmd: c())
614
+ b.bind("<Enter>", lambda _, w=b: w.config(bg=t.sel_bg))
615
+ b.bind("<Leave>", lambda _, w=b: w.config(bg=t.input_bg))
616
+
617
+ tk.Frame(self, bg=t.border, height=1).pack(fill="x", padx=4)
618
+
619
+ self._status_var = tk.StringVar(value=_t("flowchart.no_file"))
620
+ tk.Label(
621
+ self,
622
+ textvariable=self._status_var,
623
+ bg=t.panel_bg,
624
+ fg=t.fg_muted,
625
+ font=(FONT_UI, 8),
626
+ wraplength=220,
627
+ ).pack(pady=4)
628
+
629
+ canvas_frame = tk.Frame(self, bg=t.panel_bg)
630
+ canvas_frame.pack(fill="both", expand=True)
631
+
632
+ dark = _is_dark(t.panel_bg)
633
+ bg_canvas = "#1a1a2e" if dark else "#f4f4f8"
634
+
635
+ self._canvas = _FlowCanvas(
636
+ canvas_frame,
637
+ bg=bg_canvas,
638
+ highlightthickness=0,
639
+ )
640
+ vsb = ttk.Scrollbar(
641
+ canvas_frame,
642
+ orient="vertical",
643
+ command=self._canvas.yview,
644
+ style="Thin.Vertical.TScrollbar",
645
+ )
646
+ hsb = ttk.Scrollbar(
647
+ canvas_frame,
648
+ orient="horizontal",
649
+ command=self._canvas.xview,
650
+ style="Thin.Horizontal.TScrollbar",
651
+ )
652
+ self._canvas.config(xscrollcommand=hsb.set, yscrollcommand=vsb.set)
653
+ vsb.pack(side="right", fill="y")
654
+ hsb.pack(side="bottom", fill="x")
655
+ self._canvas.pack(fill="both", expand=True)
656
+
657
+ def notify_active_file(self, path: str | None) -> None:
658
+ """Atualiza o fluxograma quando a aba ativa muda."""
659
+ self._current_path = path
660
+ auto = self._cfg.get("flowchart_auto", False)
661
+ if auto and path and path.endswith(".py"):
662
+ self._generate()
663
+ elif path and not path.endswith(".py"):
664
+ self._status_var.set(_t("flowchart.unsupported"))
665
+ else:
666
+ self._status_var.set(_t("flowchart.no_file"))
667
+
668
+ def notify_file_saved(self, path: str) -> None:
669
+ """Regera o fluxograma ao salvar o arquivo atual, se a geração automática estiver ligada."""
670
+ auto = self._cfg.get("flowchart_auto", False)
671
+ if auto and path.endswith(".py") and path == self._current_path:
672
+ self._generate()
673
+
674
+ def _generate(self) -> None:
675
+ """Gera o fluxograma do arquivo ativo e o exibe no canvas."""
676
+ p = self._current_path
677
+ if not p:
678
+ self._status_var.set(_t("flowchart.no_file"))
679
+ return
680
+ if not p.endswith(".py"):
681
+ self._status_var.set(_t("flowchart.unsupported"))
682
+ return
683
+ self._status_var.set(_t("flowchart.generating"))
684
+ self.update_idletasks()
685
+ try:
686
+ source = Path(p).read_text(encoding="utf-8", errors="replace")
687
+ tree = ast.parse(source, filename=p)
688
+ FlowNode._counter = 0
689
+ nodes = _build_nodes(tree.body, max_depth=self._max_depth.get())
690
+ if not nodes:
691
+ self._status_var.set(_t("flowchart.empty"))
692
+ self._canvas.set_nodes([], _is_dark(self._tmgr.theme.panel_bg))
693
+ return
694
+ layout = _Layout(nodes, 60, 60)
695
+ layout.layout(nodes, 60, 60)
696
+ dark = _is_dark(self._tmgr.theme.panel_bg)
697
+ self._nodes = nodes
698
+ self._canvas.set_nodes(nodes, dark)
699
+ self._status_var.set(f"{Path(p).name}")
700
+ self.after(100, self._canvas.fit)
701
+ except SyntaxError as e:
702
+ self._status_var.set(_t("flowchart.error").format(err=str(e)))
703
+ except (ValueError, OSError) as e:
704
+ self._status_var.set(_t("flowchart.error").format(err=str(e)))
705
+
706
+ def _export_svg(self) -> None:
707
+ """Exporta o fluxograma como arquivo SVG."""
708
+ if not self._nodes:
709
+ return
710
+ path = filedialog.asksaveasfilename(
711
+ defaultextension=".svg",
712
+ filetypes=[("SVG files", "*.svg"), ("All files", "*.*")],
713
+ initialfile="flowchart.svg",
714
+ )
715
+ if path:
716
+ self._canvas.export_svg(path)
717
+
718
+ def apply_theme(self, tmgr: ThemeManager) -> None:
719
+ self._tmgr = tmgr
720
+ t = tmgr.theme
721
+ self.config(bg=t.panel_bg)
722
+ for w in self.winfo_children():
723
+ w.destroy()
724
+ self._build()
725
+ if self._nodes:
726
+ dark = _is_dark(t.panel_bg)
727
+ self._canvas.set_nodes(self._nodes, dark)