11 #include "llvm/Config/config.h" 32 LineEditor::CompleterConcept::~CompleterConcept() {}
33 LineEditor::ListCompleterConcept::~ListCompleterConcept() {}
35 std::string LineEditor::ListCompleterConcept::getCommonPrefix(
36 const std::vector<Completion> &Comps) {
39 std::string CommonPrefix = Comps[0].TypedText;
40 for (std::vector<Completion>::const_iterator
I = Comps.begin() + 1,
43 size_t Len = std::min(CommonPrefix.size(),
I->TypedText.size());
45 for (; CommonLen != Len; ++CommonLen) {
46 if (CommonPrefix[CommonLen] !=
I->TypedText[CommonLen])
49 CommonPrefix.resize(CommonLen);
55 LineEditor::ListCompleterConcept::complete(
StringRef Buffer,
size_t Pos)
const {
57 std::vector<Completion> Comps = getCompletions(Buffer, Pos);
63 std::string CommonPrefix = getCommonPrefix(Comps);
70 if (CommonPrefix.empty()) {
72 for (std::vector<Completion>::iterator
I = Comps.begin(),
E = Comps.end();
77 Action.
Text = CommonPrefix;
91 return Completer->complete(Buffer, Pos);
112 const char *ElGetPromptFn(EditLine *EL) {
114 if (el_get(EL, EL_CLIENTDATA, &Data) == 0)
123 unsigned char ElCompletionFn(EditLine *EL,
int ch) {
125 if (el_get(EL, EL_CLIENTDATA, &Data) == 0) {
128 FILE *Out = Data->
Out;
136 std::string Prevs(Data->
PrevCount,
'\02');
137 ::el_push(EL, const_cast<char *>(Prevs.c_str()));
144 const LineInfo *LI = ::el_line(EL);
146 StringRef(LI->buffer, LI->lastchar - LI->buffer),
147 LI->cursor - LI->buffer);
148 switch (Action.
Kind) {
150 ::el_insertstr(EL, Action.
Text.c_str());
155 return CC_REFRESH_BEEP;
164 ::el_push(EL, const_cast<char *>(
"\05\t"));
173 for (std::vector<std::string>::iterator
I = Action.
Completions.begin(),
183 <<
StringRef(LI->buffer, LI->lastchar - LI->buffer);
187 Data->
PrevCount = LI->lastchar - LI->cursor;
199 FILE *Out, FILE *Err)
200 : Prompt((ProgName +
"> ").str()), HistoryPath(HistoryPath),
202 if (HistoryPath.
empty())
208 Data->Hist = ::history_init();
211 Data->EL = ::el_init(ProgName.
str().c_str(),
In, Out, Err);
214 ::el_set(Data->EL, EL_PROMPT, ElGetPromptFn);
215 ::el_set(Data->EL, EL_EDITOR,
"emacs");
216 ::el_set(Data->EL, EL_HIST, history, Data->Hist);
217 ::el_set(Data->EL, EL_ADDFN,
"tab_complete",
"Tab completion function",
219 ::el_set(Data->EL, EL_BIND,
"\t",
"tab_complete", NULL);
220 ::el_set(Data->EL, EL_BIND,
"^r",
"em-inc-search-prev",
222 ::el_set(Data->EL, EL_BIND,
"^w",
"ed-delete-prev-word",
224 ::el_set(Data->EL, EL_BIND,
"\033[3~",
"ed-delete-next-char",
226 ::el_set(Data->EL, EL_CLIENTDATA, Data.get());
229 ::history(Data->Hist, &HE, H_SETSIZE, 800);
230 ::history(Data->Hist, &HE, H_SETUNIQUE, 1);
237 ::history_end(Data->Hist);
239 ::fwrite(
"\n", 1, 1, Data->Out);
243 if (!HistoryPath.empty()) {
245 ::history(Data->Hist, &HE, H_SAVE, HistoryPath.c_str());
250 if (!HistoryPath.empty()) {
252 ::history(Data->Hist, &HE, H_LOAD, HistoryPath.c_str());
259 const char *Line = ::el_gets(Data->EL, &LineLen);
262 if (!Line || LineLen == 0)
266 while (LineLen > 0 &&
267 (Line[LineLen - 1] ==
'\n' || Line[LineLen - 1] ==
'\r'))
272 ::history(Data->Hist, &HE, H_ENTER, Line);
274 return std::string(Line, LineLen);
277 #else // HAVE_LIBEDIT 287 FILE *Out, FILE *Err)
294 ::fwrite(
"\n", 1, 1,
Data->Out);
301 ::fprintf(
Data->Out,
"%s", Prompt.c_str());
306 char *Res = ::fgets(Buf,
sizeof(Buf),
Data->In);
314 }
while (Line.empty() ||
315 (Line[Line.size() - 1] !=
'\n' && Line[Line.size() - 1] !=
'\r'));
317 while (!Line.empty() &&
318 (Line[Line.size() - 1] ==
'\n' || Line[Line.size() - 1] ==
'\r'))
319 Line.resize(Line.size() - 1);
324 #endif // HAVE_LIBEDIT
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
This class represents lattice values for constants.
CompletionAction getCompletionAction(StringRef Buffer, size_t Pos) const
Use the current completer to produce a CompletionAction for the given completion request.
std::string ContinuationOutput
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
LLVM_NODISCARD bool empty() const
empty - Check if the string is empty.
StringRef str() const
Explicit conversion to StringRef.
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Insert Text at the cursor position.
LineEditor(StringRef ProgName, StringRef HistoryPath="", FILE *In=stdin, FILE *Out=stdout, FILE *Err=stderr)
Create a LineEditor object.
static std::string getDefaultHistoryPath(StringRef ProgName)
The action to perform upon a completion request.
Show Completions, or beep if the list is empty.
std::vector< std::string > Completions
The list of completions to show.
std::string Text
The text to insert.
llvm::Optional< std::string > readLine() const
Reads a line.
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
const std::string & getPrompt() const
bool home_directory(SmallVectorImpl< char > &result)
Get the user's home directory.
StringRef - Represent a constant reference to a string, i.e.