File indexing completed on 2026-05-10 08:44:11
0001
0002
0003
0004
0005
0006
0007
0008
0009 #ifndef LLVM_LINEEDITOR_LINEEDITOR_H
0010 #define LLVM_LINEEDITOR_LINEEDITOR_H
0011
0012 #include "llvm/ADT/StringRef.h"
0013 #include <cstdio>
0014 #include <memory>
0015 #include <optional>
0016 #include <string>
0017 #include <utility>
0018 #include <vector>
0019
0020 namespace llvm {
0021
0022 class LineEditor {
0023 public:
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033 LineEditor(StringRef ProgName, StringRef HistoryPath = "", FILE *In = stdin,
0034 FILE *Out = stdout, FILE *Err = stderr);
0035 ~LineEditor();
0036
0037
0038
0039
0040 std::optional<std::string> readLine() const;
0041
0042 void saveHistory();
0043 void loadHistory();
0044 void setHistorySize(int size);
0045
0046 static std::string getDefaultHistoryPath(StringRef ProgName);
0047
0048
0049 struct CompletionAction {
0050 enum ActionKind {
0051
0052 AK_Insert,
0053
0054 AK_ShowCompletions
0055 };
0056
0057 ActionKind Kind;
0058
0059
0060 std::string Text;
0061
0062
0063 std::vector<std::string> Completions;
0064 };
0065
0066
0067 struct Completion {
0068 Completion() = default;
0069 Completion(const std::string &TypedText, const std::string &DisplayText)
0070 : TypedText(TypedText), DisplayText(DisplayText) {}
0071
0072
0073
0074 std::string TypedText;
0075
0076
0077
0078 std::string DisplayText;
0079 };
0080
0081
0082
0083
0084
0085 template <typename T> void setCompleter(T Comp) {
0086 Completer.reset(new CompleterModel<T>(Comp));
0087 }
0088
0089
0090
0091
0092
0093 template <typename T> void setListCompleter(T Comp) {
0094 Completer.reset(new ListCompleterModel<T>(Comp));
0095 }
0096
0097
0098
0099
0100
0101
0102
0103
0104 CompletionAction getCompletionAction(StringRef Buffer, size_t Pos) const;
0105
0106 const std::string &getPrompt() const { return Prompt; }
0107 void setPrompt(const std::string &P) { Prompt = P; }
0108
0109
0110 struct InternalData;
0111
0112 private:
0113 std::string Prompt;
0114 std::string HistoryPath;
0115 std::unique_ptr<InternalData> Data;
0116
0117 struct CompleterConcept {
0118 virtual ~CompleterConcept();
0119 virtual CompletionAction complete(StringRef Buffer, size_t Pos) const = 0;
0120 };
0121
0122 struct ListCompleterConcept : CompleterConcept {
0123 ~ListCompleterConcept() override;
0124 CompletionAction complete(StringRef Buffer, size_t Pos) const override;
0125 static std::string getCommonPrefix(const std::vector<Completion> &Comps);
0126 virtual std::vector<Completion> getCompletions(StringRef Buffer,
0127 size_t Pos) const = 0;
0128 };
0129
0130 template <typename T>
0131 struct CompleterModel : CompleterConcept {
0132 CompleterModel(T Value) : Value(Value) {}
0133 CompletionAction complete(StringRef Buffer, size_t Pos) const override {
0134 return Value(Buffer, Pos);
0135 }
0136 T Value;
0137 };
0138
0139 template <typename T>
0140 struct ListCompleterModel : ListCompleterConcept {
0141 ListCompleterModel(T Value) : Value(std::move(Value)) {}
0142 std::vector<Completion> getCompletions(StringRef Buffer,
0143 size_t Pos) const override {
0144 return Value(Buffer, Pos);
0145 }
0146 T Value;
0147 };
0148
0149 std::unique_ptr<const CompleterConcept> Completer;
0150 };
0151
0152 }
0153
0154 #endif