#include "brackets.hpp" #include #include #include #include #include "highlighter.hpp" #include "styles.hpp" namespace codepad { namespace { constexpr int32_t kStepLimit = 100000; constexpr int32_t kWindowLines = 256; char16_t partner_of(char16_t c) { switch (c) { case u'(': return u')'; case u'[': return u']'; case u'{': return u'}'; case u')': return u'('; case u']': return u'['; case u'}': return u'{'; default: return 0; } } // Whether an offset sits in a string or a comment, from the highlighter's own captures, in windows of lines. class Skip { public: Skip(const Document &doc, const cp_snapshot *snapshot) : doc_(doc), snapshot_(snapshot) {} bool operator()(uint32_t offset) { if (snapshot_ == nullptr) return false; int32_t line = doc_.line_at(offset); int32_t lo = line / kWindowLines * kWindowLines; auto found = windows_.find(lo); if (found == windows_.end()) found = windows_.emplace(lo, spans(lo)).first; const std::vector &runs = found->second[static_cast(line - lo)]; auto column = static_cast(offset - doc_.line_start(line)); for (size_t i = 0; i + 2 < runs.size(); i += 3) { if (column < runs[i]) return false; if (column < runs[i + 1]) return quoted(runs[i + 2]); } return false; } private: std::vector> spans(int32_t lo) { int32_t count = std::min(kWindowLines, doc_.line_count() - lo); Lines lines = scan_lines(*snapshot_->text, doc_.line_start(lo), count); std::vector flat; highlight(*snapshot_, lines, 0, static_cast(count), flat); std::vector> out(static_cast(count)); size_t i = 0; for (auto &line : out) { auto n = static_cast(flat[i++]) * 3; line.assign(flat.begin() + static_cast(i), flat.begin() + static_cast(i + n)); i += n; } return out; } bool quoted(int32_t style) { auto found = quoted_.find(style); if (found != quoted_.end()) return found->second; std::string name = style >= 0 ? style_name(style) : std::string(); bool yes = name.rfind("string", 0) == 0 || name.rfind("comment", 0) == 0; quoted_.emplace(style, yes); return yes; } const Document &doc_; const cp_snapshot *snapshot_; std::unordered_map>> windows_; std::unordered_map quoted_; }; } // namespace BracketPair match_bracket(const Document &doc, uint32_t caret, const cp_snapshot *snapshot) { if (snapshot != nullptr && snapshot->revision != doc.revision()) snapshot = nullptr; Skip skip(doc, snapshot); auto len = static_cast(doc.length()); auto c0 = static_cast(caret); int32_t at; if (c0 < len && partner_of(doc.unit_at(caret)) != 0 && !skip(caret)) { at = c0; } else if (c0 > 0 && c0 <= len && partner_of(doc.unit_at(caret - 1)) != 0 && !skip(caret - 1)) { at = c0 - 1; } else { return {}; } char16_t c = doc.unit_at(static_cast(at)); char16_t partner = partner_of(c); int32_t step = c == u'(' || c == u'[' || c == u'{' ? 1 : -1; int32_t depth = 0; for (int32_t i = at, steps = 0; i >= 0 && i < len && steps < kStepLimit; i += step, steps++) { char16_t ch = doc.unit_at(static_cast(i)); if ((ch != c && ch != partner) || skip(static_cast(i))) continue; if (ch == c) { depth++; } else if (--depth == 0) { return {at, i}; } } return {}; } } // namespace codepad