1 //===-- llvm/LineEditor/LineEditor.h - line editor --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 #ifndef LLVM_LINEEDITOR_LINEEDITOR_H
11 #define LLVM_LINEEDITOR_LINEEDITOR_H
13 #include "llvm/ADT/Optional.h"
14 #include "llvm/ADT/StringRef.h"
25 /// Create a LineEditor object.
27 /// \param ProgName The name of the current program. Used to form a default
29 /// \param HistoryPath Path to the file in which to store history data, if
31 /// \param In The input stream used by the editor.
32 /// \param Out The output stream used by the editor.
33 /// \param Err The error stream used by the editor.
34 LineEditor(StringRef ProgName, StringRef HistoryPath = "", FILE *In = stdin,
35 FILE *Out = stdout, FILE *Err = stderr);
40 /// \return The line, or llvm::Optional<std::string>() on EOF.
41 llvm::Optional<std::string> readLine() const;
46 static std::string getDefaultHistoryPath(StringRef ProgName);
48 /// The action to perform upon a completion request.
49 struct CompletionAction {
51 /// Insert Text at the cursor position.
53 /// Show Completions, or beep if the list is empty.
59 /// The text to insert.
62 /// The list of completions to show.
63 std::vector<std::string> Completions;
66 /// A possible completion at a given cursor position.
69 Completion(const std::string &TypedText, const std::string &DisplayText)
70 : TypedText(TypedText), DisplayText(DisplayText) {}
72 /// The text to insert. If the user has already input some of the
73 /// completion, this should only include the rest of the text.
74 std::string TypedText;
76 /// A description of this completion. This may be the completion itself, or
77 /// maybe a summary of its type or arguments.
78 std::string DisplayText;
81 /// Set the completer for this LineEditor. A completer is a function object
82 /// which takes arguments of type StringRef (the string to complete) and
83 /// size_t (the zero-based cursor position in the StringRef) and returns a
85 template <typename T> void setCompleter(T Comp) {
86 Completer.reset(new CompleterModel<T>(Comp));
89 /// Set the completer for this LineEditor to the given list completer.
90 /// A list completer is a function object which takes arguments of type
91 /// StringRef (the string to complete) and size_t (the zero-based cursor
92 /// position in the StringRef) and returns a std::vector<Completion>.
93 template <typename T> void setListCompleter(T Comp) {
94 Completer.reset(new ListCompleterModel<T>(Comp));
97 /// Use the current completer to produce a CompletionAction for the given
98 /// completion request. If the current completer is a list completer, this
99 /// will return an AK_Insert CompletionAction if each completion has a common
100 /// prefix, or an AK_ShowCompletions CompletionAction otherwise.
102 /// \param Buffer The string to complete
103 /// \param Pos The zero-based cursor position in the StringRef
104 CompletionAction getCompletionAction(StringRef Buffer, size_t Pos) const;
106 const std::string &getPrompt() const { return Prompt; }
107 void setPrompt(const std::string &P) { Prompt = P; }
109 // Public so callbacks in LineEditor.cpp can use it.
114 std::string HistoryPath;
115 std::unique_ptr<InternalData> Data;
117 struct CompleterConcept {
118 virtual ~CompleterConcept();
119 virtual CompletionAction complete(StringRef Buffer, size_t Pos) const = 0;
122 struct ListCompleterConcept : CompleterConcept {
123 ~ListCompleterConcept() override;
124 CompletionAction complete(StringRef Buffer, size_t Pos) const override;
125 static std::string getCommonPrefix(const std::vector<Completion> &Comps);
126 virtual std::vector<Completion> getCompletions(StringRef Buffer,
127 size_t Pos) const = 0;
130 template <typename T>
131 struct CompleterModel : CompleterConcept {
132 CompleterModel(T Value) : Value(Value) {}
133 CompletionAction complete(StringRef Buffer, size_t Pos) const override {
134 return Value(Buffer, Pos);
139 template <typename T>
140 struct ListCompleterModel : ListCompleterConcept {
141 ListCompleterModel(T Value) : Value(std::move(Value)) {}
142 std::vector<Completion> getCompletions(StringRef Buffer,
143 size_t Pos) const override {
144 return Value(Buffer, Pos);
149 std::unique_ptr<const CompleterConcept> Completer;