12#include "llvm/Config/config.h"
28 return std::string(Path);
33LineEditor::CompleterConcept::~CompleterConcept() =
default;
34LineEditor::ListCompleterConcept::~ListCompleterConcept() =
default;
36std::string LineEditor::ListCompleterConcept::getCommonPrefix(
37 const std::vector<Completion> &Comps) {
40 std::string CommonPrefix = Comps[0].TypedText;
42 size_t Len = std::min(CommonPrefix.size(),
C.TypedText.size());
44 for (; CommonLen != Len; ++CommonLen) {
45 if (CommonPrefix[CommonLen] !=
C.TypedText[CommonLen])
48 CommonPrefix.resize(CommonLen);
54LineEditor::ListCompleterConcept::complete(
StringRef Buffer,
size_t Pos)
const {
56 std::vector<Completion> Comps = getCompletions(Buffer, Pos);
58 Action.Kind = CompletionAction::AK_ShowCompletions;
62 std::string CommonPrefix = getCommonPrefix(Comps);
69 if (CommonPrefix.empty()) {
70 Action.Kind = CompletionAction::AK_ShowCompletions;
71 for (
const Completion &Comp : Comps)
72 Action.Completions.push_back(Comp.DisplayText);
74 Action.Kind = CompletionAction::AK_Insert;
75 Action.Text = CommonPrefix;
89 return Completer->complete(Buffer, Pos);
103 std::string ContinuationOutput;
110const char *ElGetPromptFn(EditLine *EL) {
112 if (el_get(EL, EL_CLIENTDATA, &
Data) == 0)
113 return Data->LE->getPrompt().c_str();
121unsigned char ElCompletionFn(EditLine *EL,
int ch) {
123 if (el_get(EL, EL_CLIENTDATA, &
Data) == 0) {
124 if (!
Data->ContinuationOutput.empty()) {
126 FILE *Out =
Data->Out;
129 ::fwrite(
Data->ContinuationOutput.c_str(),
130 Data->ContinuationOutput.size(), 1, Out);
134 std::string Prevs(
Data->PrevCount,
'\02');
135 ::el_push(EL,
const_cast<char *
>(Prevs.c_str()));
137 Data->ContinuationOutput.clear();
142 const LineInfo *LI = ::el_line(EL);
144 StringRef(LI->buffer, LI->lastchar - LI->buffer),
145 LI->cursor - LI->buffer);
148 ::el_insertstr(EL,
Action.Text.c_str());
152 if (
Action.Completions.empty()) {
153 return CC_REFRESH_BEEP;
162 ::el_push(EL,
const_cast<char *
>(
"\05\t"));
171 for (
const std::string &Completion :
Action.Completions)
172 OS << Completion <<
"\n";
177 OS <<
Data->LE->getPrompt()
178 <<
StringRef(LI->buffer, LI->lastchar - LI->buffer);
182 Data->PrevCount = LI->lastchar - LI->cursor;
194 FILE *Out, FILE *Err)
195 : Prompt((ProgName +
"> ").str()), HistoryPath(
std::
string(HistoryPath)),
196 Data(new InternalData) {
197 if (HistoryPath.
empty())
203 Data->Hist = ::history_init();
206 Data->EL = ::el_init(ProgName.
str().c_str(), In, Out, Err);
209 ::el_set(Data->EL, EL_PROMPT, ElGetPromptFn);
210 ::el_set(Data->EL, EL_EDITOR,
"emacs");
211 ::el_set(Data->EL, EL_HIST, history, Data->Hist);
212 ::el_set(Data->EL, EL_ADDFN,
"tab_complete",
"Tab completion function",
214 ::el_set(Data->EL, EL_BIND,
"\t",
"tab_complete", NULL);
215 ::el_set(Data->EL, EL_BIND,
"^r",
"em-inc-search-prev",
217 ::el_set(Data->EL, EL_BIND,
"^w",
"ed-delete-prev-word",
219 ::el_set(Data->EL, EL_BIND,
"\033[3~",
"ed-delete-next-char",
221 ::el_set(Data->EL, EL_CLIENTDATA, Data.get());
224 ::history(Data->Hist, &HE, H_SETSIZE, 800);
225 ::history(Data->Hist, &HE, H_SETUNIQUE, 1);
232 ::history_end(Data->Hist);
234 ::fwrite(
"\n", 1, 1, Data->Out);
238 if (!HistoryPath.
empty()) {
240 ::history(Data->Hist, &HE, H_SAVE, HistoryPath.c_str());
245 if (!HistoryPath.
empty()) {
247 ::history(Data->Hist, &HE, H_LOAD, HistoryPath.c_str());
254 const char *
Line = ::el_gets(Data->EL, &LineLen);
257 if (!Line || LineLen == 0)
261 while (LineLen > 0 &&
262 (Line[LineLen - 1] ==
'\n' || Line[LineLen - 1] ==
'\r'))
267 ::history(Data->Hist, &HE, H_ENTER, Line);
269 return std::string(Line, LineLen);
282 FILE *Out, FILE *Err)
289 ::fwrite(
"\n", 1, 1, Data->Out);
296 ::fprintf(Data->Out,
"%s", Prompt.c_str());
301 char *Res = ::fgets(Buf,
sizeof(Buf), Data->In);
309 }
while (Line.empty() ||
310 (Line[Line.size() - 1] !=
'\n' && Line[Line.size() - 1] !=
'\r'));
312 while (!Line.empty() &&
313 (Line[Line.size() - 1] ==
'\n' || Line[Line.size() - 1] ==
'\r'))
314 Line.resize(Line.size() - 1);
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file defines the SmallString class.
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.
ManagedStatic< cl::opt< FnT >, OptCreatorT > Action
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.
@ AK_ShowCompletions
Show Completions, or beep if the list is empty.
@ AK_Insert
Insert Text at the cursor position.