feat(code): improve comment toggling, terminal navigation, and editor event wiring
- Refactor Commenter toggle logic for line and multi-line comments - Preserve indentation and cursor position - Improve handling of existing comment detection and removal - Simplify bounds vs line comment dispatch - Enhance terminal project navigation - Add project marker detection via Gio file traversal - Implement go-to-project-or-home behavior (Home key shortcut) - Automatically `cd` into detected project root or home directory - Wire terminal widget navigation through VteWidget - Improve terminal integration - Pass emit_to into terminals view for event dispatching - Add ability for VteWidget to trigger project navigation - Update split pane shortcut - Change close split view binding to Alt+\ - Add editor event support - Emit `text_insert` event from SourceFile on insert - Add new TextInsertEvent DTO and register in event system - Misc cleanup - Improve imports and structure in terminals module - Add project marker list and filesystem traversal helpers
This commit is contained in:
@@ -13,54 +13,95 @@ class Commenter(CodeCommentTagsMixin):
|
||||
|
||||
|
||||
def keyboard_tggl_comment(self, buffer):
|
||||
language = buffer.get_language()
|
||||
if language is None: return
|
||||
language = buffer.get_language()
|
||||
if not language: return
|
||||
|
||||
start_tag, end_tag = self.get_comment_tags(language)
|
||||
# Note: Only handling line comment tag- no block comment option
|
||||
if not start_tag and not end_tag: return
|
||||
if not (start_tag or end_tag): return
|
||||
|
||||
bounds = buffer.get_selection_bounds()
|
||||
if bounds:
|
||||
self._bounds_comment(
|
||||
start_tag, end_tag, bounds, buffer
|
||||
)
|
||||
else:
|
||||
self._line_comment(start_tag, end_tag, buffer)
|
||||
start_tag += " "
|
||||
end_tag = end_tag or ""
|
||||
bounds = buffer.get_selection_bounds()
|
||||
|
||||
|
||||
def _line_comment(self, start_tag, end_tag, buffer):
|
||||
start_itr = buffer.get_iter_at_mark( buffer.get_insert() ).copy()
|
||||
end_itr = start_itr.copy()
|
||||
if not start_itr.starts_line():
|
||||
start_itr.set_line_offset(0)
|
||||
if not end_itr.ends_line():
|
||||
end_itr.forward_to_line_end()
|
||||
|
||||
text = buffer.get_text(start_itr, end_itr, True)
|
||||
text = text.replace(start_tag, "") if text.startswith(start_tag) else start_tag + text
|
||||
|
||||
buffer.begin_user_action()
|
||||
buffer.delete(start_itr, end_itr)
|
||||
buffer.insert(start_itr, text)
|
||||
buffer.end_user_action()
|
||||
|
||||
|
||||
def _bounds_comment(self, start_tag, end_tag, bounds, buffer):
|
||||
start_itr, end_itr = bounds
|
||||
if not start_itr.starts_line():
|
||||
start_itr.set_line_offset(0)
|
||||
if not end_itr.ends_line():
|
||||
end_itr.forward_to_line_end()
|
||||
|
||||
text = buffer.get_text(start_itr, end_itr, True)
|
||||
text = "\n".join(
|
||||
line.replace(start_tag, "") if line.startswith(start_tag) else start_tag + line
|
||||
for line in text.splitlines()
|
||||
(self._bounds_comment if bounds else self._line_comment)(
|
||||
buffer, start_tag, end_tag, bounds
|
||||
)
|
||||
|
||||
def _line_comment(self, buffer, start_tag: str, end_tag: str, bounds):
|
||||
start = buffer.get_iter_at_mark(buffer.get_insert()).copy()
|
||||
end = start.copy()
|
||||
line, col = start.get_line() + 1, start.get_line_offset()
|
||||
|
||||
if not start.starts_line():
|
||||
start.set_line_offset(0)
|
||||
if not end.ends_line():
|
||||
end.forward_to_line_end()
|
||||
|
||||
text = buffer.get_text(start, end, True)
|
||||
stripped = text.lstrip()
|
||||
indent = text[:-len(stripped)] if stripped else text
|
||||
|
||||
if stripped.startswith(start_tag):
|
||||
stripped = stripped[len(start_tag):].lstrip().replace(end_tag, "", 1)
|
||||
else:
|
||||
stripped = f"{start_tag}{stripped}{end_tag}"
|
||||
|
||||
buffer.begin_user_action()
|
||||
buffer.delete(start_itr, end_itr)
|
||||
buffer.insert(start_itr, text)
|
||||
buffer.delete(start, end)
|
||||
buffer.insert(start, indent + stripped)
|
||||
buffer.end_user_action()
|
||||
|
||||
buffer.place_cursor(buffer.get_iter_at_line_offset(line, col))
|
||||
|
||||
def _bounds_comment(self, buffer, start_tag: str, end_tag: str, bounds):
|
||||
def indent_len(s): return len(s) - len(s.lstrip())
|
||||
|
||||
def insert(line, idx):
|
||||
return f"{line[:idx]}{start_tag}{line[idx:]}{end_tag}"
|
||||
|
||||
def process(lines):
|
||||
base_indent = min(
|
||||
(indent_len(l) for l in lines if l.strip()),
|
||||
default = 0
|
||||
)
|
||||
|
||||
is_commented = all(
|
||||
l.lstrip().startswith(start_tag)
|
||||
for l in lines if l.strip()
|
||||
)
|
||||
|
||||
if is_commented:
|
||||
return [
|
||||
l.replace(start_tag, "", 1).replace(end_tag, "", 1)
|
||||
if l.lstrip().startswith(start_tag.lstrip())
|
||||
else l
|
||||
for l in lines
|
||||
]
|
||||
|
||||
return [
|
||||
l if not l.strip()
|
||||
else insert(l, base_indent)
|
||||
for l in lines
|
||||
]
|
||||
|
||||
start, end = bounds
|
||||
sline, scol = start.get_line(), start.get_line_offset()
|
||||
eline, ecol = end.get_line(), end.get_line_offset()
|
||||
|
||||
if not start.starts_line():
|
||||
start.set_line_offset(0)
|
||||
if not end.ends_line():
|
||||
end.forward_to_line_end()
|
||||
|
||||
lines = buffer.get_text(start, end, True).splitlines()
|
||||
new_text = "\n".join(process(lines))
|
||||
|
||||
buffer.begin_user_action()
|
||||
buffer.delete(start, end)
|
||||
buffer.insert(start, new_text)
|
||||
buffer.end_user_action()
|
||||
|
||||
buffer.select_range(
|
||||
buffer.get_iter_at_line_offset(sline, scol),
|
||||
buffer.get_iter_at_line_offset(eline, ecol),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user