MCPcopy Create free account
hub / github.com/Bbalduzz/fletmint / apply_syntax_highlighting

Method apply_syntax_highlighting

fletmint/code.py:228–281  ·  view source on GitHub ↗
(self, text, language)

Source from the content-addressed store, hash-verified

226 print("Max retries reached. Operation failed.")
227
228 def apply_syntax_highlighting(self, text, language):
229 rules = self.syntax_rules.get(language, {})
230 formatted_lines = []
231 full_lines = ft.ListView(spacing=1)
232
233 lines = text.split("\n")
234 for idx, line in enumerate(lines):
235 parts = []
236 last_idx = 0
237 matches = []
238
239 for element, (pattern, color) in rules.items():
240 for match in re.finditer(pattern, line):
241 matches.append((match.start(), match.end(), match.group(0), color))
242
243 matches.sort(key=lambda x: (x[0], -x[1]))
244
245 # Improved overlap handling
246 for start, end, matched_text, color in matches:
247 if start >= last_idx:
248 if start > last_idx:
249 parts.append(
250 ft.Text(
251 value=line[last_idx:start],
252 style=ft.TextStyle(size=14),
253 font_family=self.font,
254 )
255 )
256 parts.append(
257 ft.Text(
258 value=matched_text,
259 style=ft.TextStyle(color=color, size=14),
260 font_family=self.font,
261 )
262 )
263 last_idx = end
264
265 # Handle remaining text after last match
266 if last_idx < len(line):
267 parts.append(
268 ft.Text(
269 value=line[last_idx:],
270 style=ft.TextStyle(size=14),
271 font_family=self.font,
272 )
273 )
274
275 # Create a Row and add all parts to it without introducing new spaces
276 line_number = ft.Container(ft.Text(f"{idx + 1}", color="#60676f"), width=40)
277 line_widgets = ft.Row(controls=[line_number] + parts, wrap=None, spacing=0)
278 formatted_lines.append(line_widgets)
279
280 full_lines.controls.extend(formatted_lines)
281 return full_lines
282
283 def update_highlight(self, e=None):
284 highlighted_code = self.apply_syntax_highlighting(

Callers 2

__init__Method · 0.95
update_highlightMethod · 0.95

Calls

no outgoing calls

Tested by

no test coverage detected