|
|
|||
File indexing completed on 2026-05-10 08:42:45
0001 //===-- Highlighter.h -------------------------------------------*- C++ -*-===// 0002 // 0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 0004 // See https://llvm.org/LICENSE.txt for license information. 0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 0006 // 0007 //===----------------------------------------------------------------------===// 0008 0009 #ifndef LLDB_CORE_HIGHLIGHTER_H 0010 #define LLDB_CORE_HIGHLIGHTER_H 0011 0012 #include <optional> 0013 #include <utility> 0014 #include <vector> 0015 0016 #include "lldb/Utility/Stream.h" 0017 #include "lldb/lldb-enumerations.h" 0018 #include "llvm/ADT/StringRef.h" 0019 0020 namespace lldb_private { 0021 0022 /// Represents style that the highlighter should apply to the given source code. 0023 /// Stores information about how every kind of token should be annotated. 0024 struct HighlightStyle { 0025 0026 /// A pair of strings that should be placed around a certain token. Usually 0027 /// stores color codes in these strings (the suffix string is often used for 0028 /// resetting the terminal attributes back to normal). 0029 class ColorStyle { 0030 std::string m_prefix; 0031 std::string m_suffix; 0032 0033 public: 0034 ColorStyle() = default; 0035 ColorStyle(llvm::StringRef prefix, llvm::StringRef suffix) { 0036 Set(prefix, suffix); 0037 } 0038 0039 /// Applies this style to the given value. 0040 /// \param s 0041 /// The stream to which the result should be appended. 0042 /// \param value 0043 /// The value that we should place our strings around. 0044 void Apply(Stream &s, llvm::StringRef value) const; 0045 0046 /// Sets the prefix and suffix strings. 0047 void Set(llvm::StringRef prefix, llvm::StringRef suffix); 0048 }; 0049 0050 /// The style for the token which is below the cursor of the user. Note that 0051 /// this style is overwritten by the SourceManager with the values of 0052 /// stop-show-column-ansi-prefix/stop-show-column-ansi-suffix. 0053 ColorStyle selected; 0054 0055 /// Matches identifiers to variable or functions. 0056 ColorStyle identifier; 0057 /// Matches any string or character literals in the language: "foo" or 'f' 0058 ColorStyle string_literal; 0059 /// Matches scalar value literals like '42' or '0.1'. 0060 ColorStyle scalar_literal; 0061 /// Matches all reserved keywords in the language. 0062 ColorStyle keyword; 0063 /// Matches any comments in the language. 0064 ColorStyle comment; 0065 /// Matches commas: ',' 0066 ColorStyle comma; 0067 /// Matches one colon: ':' 0068 ColorStyle colon; 0069 /// Matches any semicolon: ';' 0070 ColorStyle semicolons; 0071 /// Matches operators like '+', '-', '%', '&', '=' 0072 ColorStyle operators; 0073 0074 /// Matches '{' or '}' 0075 ColorStyle braces; 0076 /// Matches '[' or ']' 0077 ColorStyle square_brackets; 0078 /// Matches '(' or ')' 0079 ColorStyle parentheses; 0080 0081 // C language specific options 0082 0083 /// Matches directives to a preprocessor (if the language has any). 0084 ColorStyle pp_directive; 0085 0086 /// Returns a HighlightStyle that is based on vim's default highlight style. 0087 static HighlightStyle MakeVimStyle(); 0088 }; 0089 0090 /// Annotates source code with color attributes. 0091 class Highlighter { 0092 public: 0093 Highlighter() = default; 0094 virtual ~Highlighter() = default; 0095 Highlighter(const Highlighter &) = delete; 0096 const Highlighter &operator=(const Highlighter &) = delete; 0097 0098 /// Returns a human readable name for the selected highlighter. 0099 virtual llvm::StringRef GetName() const = 0; 0100 0101 /// Highlights the given line 0102 /// \param options 0103 /// The highlight options. 0104 /// \param line 0105 /// The user supplied line that needs to be highlighted. 0106 /// \param cursor_pos 0107 /// The cursor position of the user in this line, starting at 0 (which 0108 /// means the cursor is on the first character in 'line'). 0109 /// \param previous_lines 0110 /// Any previous lines the user has written which we should only use 0111 /// for getting the context of the Highlighting right. 0112 /// \param s 0113 /// The stream to which the highlighted version of the user string should 0114 /// be written. 0115 virtual void Highlight(const HighlightStyle &options, llvm::StringRef line, 0116 std::optional<size_t> cursor_pos, 0117 llvm::StringRef previous_lines, Stream &s) const = 0; 0118 0119 /// Utility method for calling Highlight without a stream. 0120 std::string Highlight(const HighlightStyle &options, llvm::StringRef line, 0121 std::optional<size_t> cursor_pos, 0122 llvm::StringRef previous_lines = "") const; 0123 }; 0124 0125 /// A default highlighter that only highlights the user cursor, but doesn't 0126 /// do any other highlighting. 0127 class DefaultHighlighter : public Highlighter { 0128 public: 0129 llvm::StringRef GetName() const override { return "none"; } 0130 0131 void Highlight(const HighlightStyle &options, llvm::StringRef line, 0132 std::optional<size_t> cursor_pos, 0133 llvm::StringRef previous_lines, Stream &s) const override; 0134 }; 0135 0136 /// Manages the available highlighters. 0137 class HighlighterManager { 0138 DefaultHighlighter m_default; 0139 0140 public: 0141 /// Queries all known highlighter for one that can highlight some source code. 0142 /// \param language_type 0143 /// The language type that the caller thinks the source code was given in. 0144 /// \param path 0145 /// The path to the file the source code is from. Used as a fallback when 0146 /// the user can't provide a language. 0147 /// \return 0148 /// The highlighter that wants to highlight the source code. Could be an 0149 /// empty highlighter that does nothing. 0150 const Highlighter &getHighlighterFor(lldb::LanguageType language_type, 0151 llvm::StringRef path) const; 0152 const Highlighter &getDefaultHighlighter() const { return m_default; } 0153 }; 0154 0155 } // namespace lldb_private 0156 0157 #endif // LLDB_CORE_HIGHLIGHTER_H
| [ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
|
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |
|