- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试使用 Glib::Regex 和 Gtk::TextView 以及 Gtk::TextBuffer-s,我正在尝试使用 Gtk::TextTag-s 进行语法高亮。
我的语法更新代码(它在行首和行尾接收迭代器)
void MainWindow::update_syntax(const Gtk::TextBuffer::iterator& start, const Gtk::TextBuffer::iterator& end) {
std::vector<Glib::ustring> keywords;
keywords.push_back("class");
keywords.push_back("struct");
Glib::MatchInfo info;
auto regex = Glib::Regex::create(R"((\w+))");
auto ok = regex->match(start.get_visible_text(end), info);
std::map<Glib::ustring, std::pair<Glib::RefPtr<Gtk::TextMark>, Glib::RefPtr<Gtk::TextMark>>> marks;
do {
std::cout << "word: " << info.fetch(1) << std::endl;
for (auto kw : keywords) {
if (info.fetch(1) == kw) {
int start_offset, end_offset;
info.fetch_pos(1, start_offset, end_offset);
std::cout << info.fetch(1) << " (at: [" << start_offset << ";" << end_offset << "])" << std::endl;
marks["keyword"] = std::make_pair(
this->m_buffer->create_mark(
this->m_buffer->get_iter_at_offset(start.get_offset() + start_offset)
),
this->m_buffer->create_mark(
this->m_buffer->get_iter_at_offset(start.get_offset() + end_offset)
)
);
}
}
} while(info.next());
for (auto mark : marks) {
this->m_buffer->apply_tag_by_name(mark.first,
mark.second.first->get_iter(), mark.second.second->get_iter());
}
}
所以流程是我创建一个简单的正则表达式来匹配该行中的每个单词,然后创建一个标记映射,稍后将给出要设置标签的范围。我在这里使用 Gtk::Mark,因为每次修改缓冲区时迭代器都会失效。
为了说明这里出了什么问题,我将发布此函数的一些调试输出,以及之前的插槽 on_insert
;
void MainWindow::on_insert(const Gtk::TextBuffer::iterator& pos,
const Glib::ustring& text, int bytes)
{
std::cout << text << " (added at[" << pos.get_offset() <<
"]; with [" << bytes << "]bytes)" << std::endl << std::endl;
因此将 class class
写入 TextView 的输出结果是第一个被突出显示,第二个没有被拾取,记录:
c (added at[1]; with [1]bytes)
word: c
l (added at[2]; with [1]bytes)
word: cl
a (added at[3]; with [1]bytes)
word: cla
s (added at[4]; with [1]bytes)
word: clas
s (added at[5]; with [1]bytes)
word: class
class (keyword at: [0;5])
(added at[6]; with [1]bytes)
word: class
class (keyword at: [0;5])
word: r
c (added at[7]; with [1]bytes)
word: class
class (keyword at: [0;5])
word: rd
l (added at[8]; with [1]bytes)
word: class
class (keyword at: [0;5])
word: rd
a (added at[9]; with [1]bytes)
word: class
class (keyword at: [0;5])
word: rd
word: a
s (added at[10]; with [1]bytes)
word: class
class (keyword at: [0;5])
word: rd
word: as
s (added at[11]; with [1]bytes)
word: class
class (keyword at: [0;5])
word: rd
word: ass
很容易注意到最后一行表明它移动了两个偏移量。可能是应用了标签。另外,这里不清楚的是:word: rd
。我使用 keyword
作为标签的名称。当此代码仍在使用迭代器时,info.fetch(1)
返回了 “keyword”
,那么正则表达式是否也匹配标签?
我希望有 Glib 和 Gtk 经验的人知道答案,谢谢。
最佳答案
我还没有使用过这个特定的 API1,但我认为您遇到了对象生命周期方面的问题。 iter->get_visible_text()
返回一个字符串对象,该对象在调用 regex->match()
后被销毁。这是一个问题,因为 Glib::MatchInfo::next()
期望该字符串仍然存在 2。这可能就是为什么你在第二场比赛中得到垃圾的原因。我认为您可以安全地执行以下操作:
....
auto visbuf = start.get_visible_text(end);
auto ok = regex->match(visbuf, info); // existing line
...
关于c++ - Glib::Regex 获取 TextTag-s,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12007319/
因此,我尝试在 PyGTK 中编写富文本编辑器,并且最初使用 Gourmet 的较旧的第三方脚本 InteractivePangoBuffer 来执行此操作。虽然它工作正常,但仍然存在很多错误,有时使
我正在尝试使用 Glib::Regex 和 Gtk::TextView 以及 Gtk::TextBuffer-s,我正在尝试使用 Gtk::TextTag-s 进行语法高亮。 我的语法更新代码(它在行
我是一名优秀的程序员,十分优秀!