#ifndef LLVM_LINEEDITOR_LINEEDITOR_H
#define LLVM_LINEEDITOR_LINEEDITOR_H
#include "llvm/ADT/Optional.h"
#include "llvm/ADT/StringRef.h"
#include <cstdio>
#include <memory>
#include <string>
#include <vector>
namespace llvm {
class LineEditor {
public:
LineEditor(StringRef ProgName, StringRef HistoryPath = "", FILE *In = stdin,
FILE *Out = stdout, FILE *Err = stderr);
~LineEditor();
llvm::Optional<std::string> readLine() const;
void saveHistory();
void loadHistory();
static std::string getDefaultHistoryPath(StringRef ProgName);
struct CompletionAction {
enum ActionKind {
AK_Insert,
AK_ShowCompletions
};
ActionKind Kind;
std::string Text;
std::vector<std::string> Completions;
};
struct Completion {
Completion() {}
Completion(const std::string &TypedText, const std::string &DisplayText)
: TypedText(TypedText), DisplayText(DisplayText) {}
std::string TypedText;
std::string DisplayText;
};
template <typename T> void setCompleter(T Comp) {
Completer.reset(new CompleterModel<T>(Comp));
}
template <typename T> void setListCompleter(T Comp) {
Completer.reset(new ListCompleterModel<T>(Comp));
}
CompletionAction getCompletionAction(StringRef Buffer, size_t Pos) const;
const std::string &getPrompt() const { return Prompt; }
void setPrompt(const std::string &P) { Prompt = P; }
struct InternalData;
private:
std::string Prompt;
std::string HistoryPath;
std::unique_ptr<InternalData> Data;
struct CompleterConcept {
virtual ~CompleterConcept();
virtual CompletionAction complete(StringRef Buffer, size_t Pos) const = 0;
};
struct ListCompleterConcept : CompleterConcept {
~ListCompleterConcept();
CompletionAction complete(StringRef Buffer, size_t Pos) const override;
static std::string getCommonPrefix(const std::vector<Completion> &Comps);
virtual std::vector<Completion> getCompletions(StringRef Buffer,
size_t Pos) const = 0;
};
template <typename T>
struct CompleterModel : CompleterConcept {
CompleterModel(T Value) : Value(Value) {}
CompletionAction complete(StringRef Buffer, size_t Pos) const override {
return Value(Buffer, Pos);
}
T Value;
};
template <typename T>
struct ListCompleterModel : ListCompleterConcept {
ListCompleterModel(T Value) : Value(Value) {}
std::vector<Completion> getCompletions(StringRef Buffer,
size_t Pos) const override {
return Value(Buffer, Pos);
}
T Value;
};
std::unique_ptr<const CompleterConcept> Completer;
};
}
#endif