如何在 Gedit 插件中获取当前行的长度(或行尾的偏移量)

发布于 2024-12-17 00:08:31 字数 259 浏览 0 评论 0原文

在用 Python 编写的 Gedit 插件中,我可以获取当前行开头的偏移量,

document = window.get_active_document()
offset = document.get_iter_at_mark(document.get_insert())

如何获取同一行末尾的偏移量?现在我正在使用一种解决方法:获取下一行的偏移量并从中减去所需行的偏移量,然后减去 1(对最后一行进行特殊情况处理)。有更好的方法吗?

In a Gedit plugin written in Python, I can get the offset of the beginning of the current line with

document = window.get_active_document()
offset = document.get_iter_at_mark(document.get_insert())

How could I get the offset of the end of this same line? For now I am using a workaround: I get the offset of the next line and subtract the offset of the desired line from it, and subtract 1 (with an special case treated for the last line). Is there a better way of doing it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小伙你站住 2024-12-24 00:08:32

我知道有点晚了,但迟到总比不到好。我正在运行 gedit 3.2.3,我不知道这些东西从一个版本到另一个版本有多少变化,但这对我有用:

line_types = {"cr-lf": '\r\n',
              "lf": '\n',
              "cr": '\r'}
document = window.get_active_document()
newline = line_types[document.get_newline_type().value_nick]
insert_mark = document.get_insert()
offset = document.get_iter_at_mark(insert_mark)
line = offset.get_line()
# we subtract 1 because get_chars_in_line() counts the newline character
# there is a special case with the last line if it doesn't have a newline at the end
# we deal with that later
line_length = offset.get_chars_in_line() - len(newline)
end_of_line = document.get_iter_at_line_offset(line, line_length)
if end_of_line.get_char() != newline[0]:
    end_of_line = document.get_iter_at_offset(end_of_line.get_offset()+len(newline))
# if the above code is correct this should select from the current insert point
# to the end of line
document.move_mark(insert_mark, end_of_line)

编辑 1:没有考虑到文件没有被换行符终止的情况

编辑2:考虑行尾的不同定义

PS:无论这个解决方案还是您当前的解决方案是“更干净”还是“更好”,我不知道,我想这是主观的。

A bit late, I know, but better late than never. I am running gedit 3.2.3 and I don't know how much these things change from one version to another, but this works for me:

line_types = {"cr-lf": '\r\n',
              "lf": '\n',
              "cr": '\r'}
document = window.get_active_document()
newline = line_types[document.get_newline_type().value_nick]
insert_mark = document.get_insert()
offset = document.get_iter_at_mark(insert_mark)
line = offset.get_line()
# we subtract 1 because get_chars_in_line() counts the newline character
# there is a special case with the last line if it doesn't have a newline at the end
# we deal with that later
line_length = offset.get_chars_in_line() - len(newline)
end_of_line = document.get_iter_at_line_offset(line, line_length)
if end_of_line.get_char() != newline[0]:
    end_of_line = document.get_iter_at_offset(end_of_line.get_offset()+len(newline))
# if the above code is correct this should select from the current insert point
# to the end of line
document.move_mark(insert_mark, end_of_line)

Edit 1: Was not accounting for the case where the file wasn't terminated by a newline character

Edit 2: Account for different definitions of the end of line

PS: Whether this or your currently solution is "cleaner" or "better", I don't know, I guess that's subjective.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文