LLDB mainline
TreeSitterHighlighter.h
Go to the documentation of this file.
1//===----------------------------------------------------------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef LLDB_SOURCE_PLUGINS_HIGHLIGHTER_TREESITTER_TREESITTERHIGHLIGHTER_H
10#define LLDB_SOURCE_PLUGINS_HIGHLIGHTER_TREESITTER_TREESITTERHIGHLIGHTER_H
11
13#include "lldb/Utility/Stream.h"
14#include "llvm/ADT/StringRef.h"
15
16#include <optional>
17#include <tree_sitter/api.h>
18
19namespace lldb_private {
20
21struct TSState;
22
24public:
26 ~TreeSitterHighlighter() override = default;
27
28 /// Highlights a single line of code using tree-sitter parsing.
29 void Highlight(const HighlightStyle &options, llvm::StringRef line,
30 std::optional<size_t> cursor_pos,
31 llvm::StringRef previous_lines, Stream &s) const override;
32
33protected:
34 /// Returns the tree-sitter language for this highlighter.
35 virtual const TSLanguage *GetLanguage() const = 0;
36
37 /// Returns the tree-sitter highlight query for this language.
38 virtual llvm::StringRef GetHighlightQuery() const = 0;
39
40private:
41 /// Maps a tree-sitter capture name to a HighlightStyle color.
43 GetStyleForCapture(llvm::StringRef capture_name,
44 const HighlightStyle &options) const;
45
46 /// Applies syntax highlighting to a range of text.
47 void HighlightRange(const HighlightStyle &options, llvm::StringRef text,
48 uint32_t start_byte, uint32_t end_byte,
49 const HighlightStyle::ColorStyle &style,
50 std::optional<size_t> cursor_pos,
51 bool &highlighted_cursor, Stream &s) const;
52
58
59 struct TSState {
60 TSState() = default;
61 TSState &operator=(const TSState &) = delete;
62 TSState(const TSState &) = delete;
63 ~TSState();
64
65 explicit operator bool() const;
66 TSParser *parser = nullptr;
67 TSQuery *query = nullptr;
68 };
69
70 /// Lazily creates a tree-sitter state (TSState).
71 TSState &GetTSState() const;
72 mutable std::optional<TSState> m_ts_state;
73};
74
75} // namespace lldb_private
76
77#endif // LLDB_SOURCE_PLUGINS_LANGUAGE_TREESITTERCOMMON_TREESITTERHIGHLIGHTER_H
A pair of strings that should be placed around a certain token.
Definition Highlighter.h:30
A stream class that can stream formatted output to a file.
Definition Stream.h:28
virtual llvm::StringRef GetHighlightQuery() const =0
Returns the tree-sitter highlight query for this language.
virtual const TSLanguage * GetLanguage() const =0
Returns the tree-sitter language for this highlighter.
void Highlight(const HighlightStyle &options, llvm::StringRef line, std::optional< size_t > cursor_pos, llvm::StringRef previous_lines, Stream &s) const override
Highlights a single line of code using tree-sitter parsing.
void HighlightRange(const HighlightStyle &options, llvm::StringRef text, uint32_t start_byte, uint32_t end_byte, const HighlightStyle::ColorStyle &style, std::optional< size_t > cursor_pos, bool &highlighted_cursor, Stream &s) const
Applies syntax highlighting to a range of text.
~TreeSitterHighlighter() override=default
TSState & GetTSState() const
Lazily creates a tree-sitter state (TSState).
HighlightStyle::ColorStyle GetStyleForCapture(llvm::StringRef capture_name, const HighlightStyle &options) const
Maps a tree-sitter capture name to a HighlightStyle color.
A class that represents a running process on the host machine.
Represents style that the highlighter should apply to the given source code.
Definition Highlighter.h:25
TSState & operator=(const TSState &)=delete