2026-02-18 23:49:01 -06:00
|
|
|
# Python imports
|
|
|
|
|
|
|
|
|
|
# Lib imports
|
|
|
|
|
|
|
|
|
|
# Application imports
|
|
|
|
|
from .mixins.code_comment_tags_mixin import CodeCommentTagsMixin
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Commenter(CodeCommentTagsMixin):
|
|
|
|
|
def __init__(self):
|
|
|
|
|
...
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def keyboard_tggl_comment(self, buffer):
|
2026-04-15 01:54:56 -05:00
|
|
|
language = buffer.get_language()
|
|
|
|
|
if not language: return
|
2026-02-18 23:49:01 -06:00
|
|
|
|
|
|
|
|
start_tag, end_tag = self.get_comment_tags(language)
|
2026-04-15 01:54:56 -05:00
|
|
|
if not (start_tag or end_tag): return
|
2026-02-18 23:49:01 -06:00
|
|
|
|
2026-04-15 01:54:56 -05:00
|
|
|
start_tag += " "
|
|
|
|
|
end_tag = end_tag or ""
|
|
|
|
|
bounds = buffer.get_selection_bounds()
|
2026-02-18 23:49:01 -06:00
|
|
|
|
2026-04-15 01:54:56 -05:00
|
|
|
(self._bounds_comment if bounds else self._line_comment)(
|
|
|
|
|
buffer, start_tag, end_tag, bounds
|
|
|
|
|
)
|
2026-02-18 23:49:01 -06:00
|
|
|
|
2026-04-15 01:54:56 -05:00
|
|
|
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()
|
2026-02-18 23:49:01 -06:00
|
|
|
|
2026-04-15 01:54:56 -05:00
|
|
|
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}"
|
2026-02-18 23:49:01 -06:00
|
|
|
|
|
|
|
|
buffer.begin_user_action()
|
2026-04-15 01:54:56 -05:00
|
|
|
buffer.delete(start, end)
|
|
|
|
|
buffer.insert(start, indent + stripped)
|
2026-02-18 23:49:01 -06:00
|
|
|
buffer.end_user_action()
|
|
|
|
|
|
2026-04-15 01:54:56 -05:00
|
|
|
buffer.place_cursor(buffer.get_iter_at_line_offset(line, col))
|
2026-02-18 23:49:01 -06:00
|
|
|
|
2026-04-15 01:54:56 -05:00
|
|
|
def _bounds_comment(self, buffer, start_tag: str, end_tag: str, bounds):
|
|
|
|
|
def indent_len(s): return len(s) - len(s.lstrip())
|
2026-02-18 23:49:01 -06:00
|
|
|
|
2026-04-15 01:54:56 -05:00
|
|
|
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))
|
2026-02-18 23:49:01 -06:00
|
|
|
|
|
|
|
|
buffer.begin_user_action()
|
2026-04-15 01:54:56 -05:00
|
|
|
buffer.delete(start, end)
|
|
|
|
|
buffer.insert(start, new_text)
|
2026-02-18 23:49:01 -06:00
|
|
|
buffer.end_user_action()
|
|
|
|
|
|
2026-04-15 01:54:56 -05:00
|
|
|
buffer.select_range(
|
|
|
|
|
buffer.get_iter_at_line_offset(sline, scol),
|
|
|
|
|
buffer.get_iter_at_line_offset(eline, ecol),
|
|
|
|
|
)
|