One place to pick a buffer's highlighter
Filetype detection (extension or shebang) was written out three times (two open paths in Editor and the ImGui frontend's per-frame check) and "highlighter for this filetype, else the null one" five times (Buffer's copy constructor and assignment, Editor::SwitchTo, :set filetype). Buffer::ApplyDetectedFiletype and InstallFiletypeHighlighter replace them. The InvalidateFrom(0) calls after SetHighlighter are dropped: SetHighlighter already clears the caches. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_015rcVQiiWyTzoNrsvLSaAfB
This commit is contained in:
@@ -377,23 +377,8 @@ Buffer::Buffer(const Buffer &other)
|
||||
undo_sys_ = std::make_unique<UndoSystem>(*this, *undo_tree_);
|
||||
|
||||
// Recreate a highlighter engine for this copy based on filetype/syntax state
|
||||
if (syntax_enabled_) {
|
||||
// Allocate engine and install an appropriate highlighter
|
||||
highlighter_ = std::make_unique<kte::HighlighterEngine>();
|
||||
if (!filetype_.empty()) {
|
||||
auto hl = kte::HighlighterRegistry::CreateFor(filetype_);
|
||||
if (hl) {
|
||||
highlighter_->SetHighlighter(std::move(hl));
|
||||
} else {
|
||||
// Unsupported filetype -> NullHighlighter keeps syntax pipeline active
|
||||
highlighter_->SetHighlighter(std::make_unique<kte::NullHighlighter>());
|
||||
}
|
||||
} else {
|
||||
// No filetype -> keep syntax enabled but use NullHighlighter
|
||||
highlighter_->SetHighlighter(std::make_unique<kte::NullHighlighter>());
|
||||
}
|
||||
// Fresh engine has empty caches; nothing to invalidate
|
||||
}
|
||||
if (syntax_enabled_)
|
||||
InstallFiletypeHighlighter();
|
||||
}
|
||||
|
||||
|
||||
@@ -428,19 +413,8 @@ Buffer::operator=(const Buffer &other)
|
||||
|
||||
// Recreate highlighter engine consistent with syntax settings
|
||||
highlighter_.reset();
|
||||
if (syntax_enabled_) {
|
||||
highlighter_ = std::make_unique<kte::HighlighterEngine>();
|
||||
if (!filetype_.empty()) {
|
||||
auto hl = kte::HighlighterRegistry::CreateFor(filetype_);
|
||||
if (hl) {
|
||||
highlighter_->SetHighlighter(std::move(hl));
|
||||
} else {
|
||||
highlighter_->SetHighlighter(std::make_unique<kte::NullHighlighter>());
|
||||
}
|
||||
} else {
|
||||
highlighter_->SetHighlighter(std::make_unique<kte::NullHighlighter>());
|
||||
}
|
||||
}
|
||||
if (syntax_enabled_)
|
||||
InstallFiletypeHighlighter();
|
||||
return *this;
|
||||
}
|
||||
|
||||
@@ -768,6 +742,29 @@ Buffer::insert_text(int row, int col, std::string_view text)
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Buffer::ApplyDetectedFiletype()
|
||||
{
|
||||
EnsureHighlighter();
|
||||
const std::string first = Nrows() > 0 ? GetLineString(0) : std::string();
|
||||
const std::string ft = kte::HighlighterRegistry::DetectForPath(filename_, first);
|
||||
SetFiletype(ft);
|
||||
SetSyntaxEnabled(true);
|
||||
InstallFiletypeHighlighter();
|
||||
}
|
||||
|
||||
|
||||
void
|
||||
Buffer::InstallFiletypeHighlighter()
|
||||
{
|
||||
EnsureHighlighter();
|
||||
auto hl = filetype_.empty() ? nullptr : kte::HighlighterRegistry::CreateFor(filetype_);
|
||||
if (!hl)
|
||||
hl = std::make_unique<kte::NullHighlighter>();
|
||||
highlighter_->SetHighlighter(std::move(hl));
|
||||
}
|
||||
|
||||
|
||||
// ===== Adapter helpers for PieceTable-backed Buffer =====
|
||||
std::string
|
||||
Buffer::Bytes() const
|
||||
|
||||
Reference in New Issue
Block a user