12#include "llvm/Config/config.h"
20constexpr int DefaultHistorySize = 800;
29 return std::string(Path);
34LineEditor::CompleterConcept::~CompleterConcept() =
default;
35LineEditor::ListCompleterConcept::~ListCompleterConcept() =
default;
37std::string LineEditor::ListCompleterConcept::getCommonPrefix(
38 const std::vector<Completion> &Comps) {
41 std::string CommonPrefix = Comps[0].TypedText;
43 size_t Len = std::min(CommonPrefix.size(),
C.TypedText.size());
45 for (; CommonLen != Len; ++CommonLen) {
46 if (CommonPrefix[CommonLen] !=
C.TypedText[CommonLen])
49 CommonPrefix.resize(CommonLen);
55LineEditor::ListCompleterConcept::complete(
StringRef Buffer,
size_t Pos)
const {
56 CompletionAction Action;
57 std::vector<Completion> Comps = getCompletions(Buffer, Pos);
59 Action.Kind = CompletionAction::AK_ShowCompletions;
63 std::string CommonPrefix = getCommonPrefix(Comps);
70 if (CommonPrefix.empty()) {
71 Action.Kind = CompletionAction::AK_ShowCompletions;
72 for (
const Completion &Comp : Comps)
73 Action.Completions.push_back(Comp.DisplayText);
75 Action.Kind = CompletionAction::AK_Insert;
76 Action.Text = CommonPrefix;
90 return Completer->complete(Buffer, Pos);
104 std::string ContinuationOutput;
111const char *ElGetPromptFn(EditLine *EL) {
113 if (el_get(EL, EL_CLIENTDATA, &
Data) == 0)
114 return Data->LE->getPrompt().c_str();
122unsigned char ElCompletionFn(EditLine *EL,
int ch) {
124 if (el_get(EL, EL_CLIENTDATA, &
Data) == 0) {
125 if (!
Data->ContinuationOutput.empty()) {
127 FILE *Out =
Data->Out;
130 ::fwrite(
Data->ContinuationOutput.c_str(),
131 Data->ContinuationOutput.size(), 1, Out);
135 std::string Prevs(
Data->PrevCount,
'\02');
136 ::el_push(EL,
const_cast<char *
>(Prevs.c_str()));
138 Data->ContinuationOutput.clear();
143 const LineInfo *LI = ::el_line(EL);
145 StringRef(LI->buffer, LI->lastchar - LI->buffer),
146 LI->cursor - LI->buffer);
147 switch (Action.
Kind) {
149 ::el_insertstr(EL, Action.
Text.c_str());
154 return CC_REFRESH_BEEP;
163 ::el_push(EL,
const_cast<char *
>(
"\05\t"));
172 for (
const std::string &Completion : Action.
Completions)
173 OS << Completion <<
"\n";
178 OS <<
Data->LE->getPrompt()
179 <<
StringRef(LI->buffer, LI->lastchar - LI->buffer);
183 Data->PrevCount = LI->lastchar - LI->cursor;
195 FILE *Out, FILE *Err)
196 : Prompt((ProgName +
"> ").str()), HistoryPath(
std::
string(HistoryPath)),
197 Data(new InternalData) {
198 if (HistoryPath.
empty())
204 Data->Hist = ::history_init();
207 Data->EL = ::el_init(ProgName.
str().c_str(), In, Out, Err);
210 ::el_set(Data->EL, EL_PROMPT, ElGetPromptFn);
211 ::el_set(Data->EL, EL_EDITOR,
"emacs");
212 ::el_set(Data->EL, EL_HIST, history, Data->Hist);
213 ::el_set(Data->EL, EL_ADDFN,
"tab_complete",
"Tab completion function",
215 ::el_set(Data->EL, EL_BIND,
"\t",
"tab_complete", NULL);
216 ::el_set(Data->EL, EL_BIND,
"^r",
"em-inc-search-prev",
218 ::el_set(Data->EL, EL_BIND,
"^w",
"ed-delete-prev-word",
220 ::el_set(Data->EL, EL_BIND,
"\033[3~",
"ed-delete-next-char",
222 ::el_set(Data->EL, EL_CLIENTDATA, Data.get());
226 ::history(Data->Hist, &HE, H_SETUNIQUE, 1);
233 ::history_end(Data->Hist);
235 ::fwrite(
"\n", 1, 1, Data->Out);
239 if (!HistoryPath.
empty()) {
241 ::history(Data->Hist, &HE, H_SAVE, HistoryPath.c_str());
246 if (!HistoryPath.
empty()) {
248 ::history(Data->Hist, &HE, H_LOAD, HistoryPath.c_str());
254 ::history(Data->Hist, &HE, H_SETSIZE, size);
260 const char *
Line = ::el_gets(Data->EL, &LineLen);
263 if (!Line || LineLen == 0)
267 while (LineLen > 0 &&
268 (Line[LineLen - 1] ==
'\n' || Line[LineLen - 1] ==
'\r'))
273 ::history(Data->Hist, &HE, H_ENTER, Line);
275 return std::string(Line, LineLen);
288 FILE *Out, FILE *Err)
295 ::fwrite(
"\n", 1, 1, Data->Out);
303 ::fprintf(Data->Out,
"%s", Prompt.c_str());
308 char *Res = ::fgets(Buf,
sizeof(Buf), Data->In);
316 }
while (Line.empty() ||
317 (Line[Line.size() - 1] !=
'\n' && Line[Line.size() - 1] !=
'\r'));
319 while (!Line.empty() &&
320 (Line[Line.size() - 1] ==
'\n' || Line[Line.size() - 1] ==
'\r'))
321 Line.resize(Line.size() - 1);
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallString class.
void setHistorySize(int size)
CompletionAction getCompletionAction(StringRef Buffer, size_t Pos) const
Use the current completer to produce a CompletionAction for the given completion request.
static std::string getDefaultHistoryPath(StringRef ProgName)
LineEditor(StringRef ProgName, StringRef HistoryPath="", FILE *In=stdin, FILE *Out=stdout, FILE *Err=stderr)
Create a LineEditor object.
std::optional< std::string > readLine() const
Reads a line.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
StringRef - Represent a constant reference to a string, i.e.
std::string str() const
str - Get the contents as an std::string.
constexpr bool empty() const
empty - Check if the string is empty.
A raw_ostream that writes to an std::string.
@ C
The default llvm calling convention, compatible with C.
bool home_directory(SmallVectorImpl< char > &result)
Get the user's home directory.
void append(SmallVectorImpl< char > &path, const Twine &a, const Twine &b="", const Twine &c="", const Twine &d="")
Append to path.
This is an optimization pass for GlobalISel generic memory operations.
auto drop_begin(T &&RangeOrContainer, size_t N=1)
Return a range covering RangeOrContainer with the first N elements excluded.
Implement std::hash so that hash_code can be used in STL containers.
The action to perform upon a completion request.
std::string Text
The text to insert.
std::vector< std::string > Completions
The list of completions to show.
@ AK_ShowCompletions
Show Completions, or beep if the list is empty.
@ AK_Insert
Insert Text at the cursor position.