Skip to content

Commit 2fdcb6d

Browse files
devajithvsRoot Persona
authored andcommitted
Replace all occurrences of TextInput with LineEditor
- Update tests as errors/warnings will add a new line in TextInput - Add `CLING_HISTSIZE` functionality with the newly added LLVM patch. - Update `cling_history` test to match encoded libedit history
1 parent 317410d commit 2fdcb6d

File tree

10 files changed

+61
-109
lines changed

10 files changed

+61
-109
lines changed

lib/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88

99
add_subdirectory(Interpreter)
1010
add_subdirectory(MetaProcessor)
11-
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/UserInterface/textinput OR
12-
CLING_INCLUDE_TESTS)
11+
if(CLING_INCLUDE_TESTS)
1312
add_subdirectory(UserInterface)
1413
endif()
1514
add_subdirectory(Utils)

lib/UserInterface/CMakeLists.txt

Lines changed: 1 addition & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,12 @@
77
#------------------------------------------------------------------------------
88

99
set( LLVM_LINK_COMPONENTS
10+
LineEditor
1011
support
1112
)
1213

13-
if(NOT EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/textinput)
14-
set(TEXTINPUTSRC ${CMAKE_SOURCE_DIR}/core/textinput/src/)
15-
include_directories(${TEXTINPUTSRC})
16-
else()
17-
# For cling, install textinput *.h in include/cling/UserInterface/textinput.
18-
install(DIRECTORY ${TEXTINPUTSRC}textinput
19-
DESTINATION include/cling/UserInterface
20-
FILES_MATCHING
21-
PATTERN "CMakeFiles" EXCLUDE
22-
PATTERN "*.cpp" EXCLUDE
23-
PATTERN "doc" EXCLUDE
24-
PATTERN "*.h"
25-
)
26-
endif()
27-
2814
add_cling_library(clingUserInterface
2915
UserInterface.cpp
30-
${TEXTINPUTSRC}textinput/Editor.cpp
31-
${TEXTINPUTSRC}textinput/History.cpp
32-
${TEXTINPUTSRC}textinput/KeyBinding.cpp
33-
${TEXTINPUTSRC}textinput/Range.cpp
34-
${TEXTINPUTSRC}textinput/SignalHandler.cpp
35-
${TEXTINPUTSRC}textinput/StreamReader.cpp
36-
${TEXTINPUTSRC}textinput/StreamReaderUnix.cpp
37-
${TEXTINPUTSRC}textinput/StreamReaderWin.cpp
38-
${TEXTINPUTSRC}textinput/TerminalConfigUnix.cpp
39-
${TEXTINPUTSRC}textinput/TerminalDisplay.cpp
40-
${TEXTINPUTSRC}textinput/TerminalDisplayUnix.cpp
41-
${TEXTINPUTSRC}textinput/TerminalDisplayWin.cpp
42-
${TEXTINPUTSRC}textinput/TextInput.cpp
43-
${TEXTINPUTSRC}textinput/TextInputContext.cpp
4416

4517
LINK_LIBS
4618
clingMetaProcessor

lib/UserInterface/UserInterface.cpp

Lines changed: 43 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@
1212
#include "cling/Interpreter/Exception.h"
1313
#include "cling/MetaProcessor/MetaProcessor.h"
1414
#include "cling/Utils/Output.h"
15-
#include "textinput/Callbacks.h"
16-
#include "textinput/History.h"
17-
#include "textinput/StreamReader.h"
18-
#include "textinput/TerminalDisplay.h"
19-
#include "textinput/TextInput.h"
2015

2116
#include "llvm/ADT/SmallString.h"
17+
#include "llvm/LineEditor/LineEditor.h"
2218
#include "llvm/Support/ErrorHandling.h"
2319
#include "llvm/Support/Path.h"
2420

@@ -30,44 +26,40 @@ namespace {
3026
/// to code complete through its own textinput mechanism which is part of the
3127
/// UserInterface.
3228
///
33-
class UITabCompletion : public textinput::TabCompletion {
34-
const cling::Interpreter& m_ParentInterpreter;
35-
36-
public:
37-
UITabCompletion(const cling::Interpreter& Parent) :
38-
m_ParentInterpreter(Parent) {}
39-
~UITabCompletion() {}
40-
41-
bool Complete(textinput::Text& Line /*in+out*/,
42-
size_t& Cursor /*in+out*/,
43-
textinput::EditorRange& R /*out*/,
44-
std::vector<std::string>& Completions /*out*/) override {
45-
m_ParentInterpreter.codeComplete(Line.GetText(), Cursor, Completions);
46-
return true;
47-
}
48-
};
4929

50-
///\brief Delays ~TextInput until after ~StreamReader and ~TerminalDisplay
51-
///
52-
class TextInputHolder {
53-
textinput::StreamReader* m_Reader;
54-
textinput::TerminalDisplay* m_Display;
55-
textinput::TextInput m_Input;
56-
57-
public:
58-
TextInputHolder(llvm::SmallString<512>& Hist)
59-
: m_Reader(textinput::StreamReader::Create()),
60-
m_Display(textinput::TerminalDisplay::Create()),
61-
m_Input(*m_Reader, *m_Display, Hist.empty() ? 0 : Hist.c_str()) {}
62-
63-
~TextInputHolder() {
64-
delete m_Reader;
65-
delete m_Display;
66-
}
30+
struct ClingListCompleter {
31+
const cling::Interpreter& MainInterp;
32+
33+
ClingListCompleter(const cling::Interpreter& Interp) : MainInterp(Interp) {}
6734

68-
textinput::TextInput* operator -> () { return &m_Input; }
35+
std::vector<llvm::LineEditor::Completion> operator()(llvm::StringRef Buffer,
36+
size_t Pos) const;
37+
std::vector<llvm::LineEditor::Completion>
38+
operator()(llvm::StringRef Buffer, size_t Pos, llvm::Error& ErrRes) const;
6939
};
7040

41+
std::vector<llvm::LineEditor::Completion>
42+
ClingListCompleter::operator()(llvm::StringRef Buffer, size_t Pos) const {
43+
auto Err = llvm::Error::success();
44+
auto res = (*this)(Buffer, Pos, Err);
45+
if (Err)
46+
llvm::logAllUnhandledErrors(std::move(Err), llvm::errs(), "error: ");
47+
return res;
48+
}
49+
50+
std::vector<llvm::LineEditor::Completion>
51+
ClingListCompleter::operator()(llvm::StringRef Buffer, size_t Pos,
52+
llvm::Error& ErrRes) const {
53+
std::vector<llvm::LineEditor::Completion> Comps;
54+
std::vector<std::string> Results;
55+
56+
MainInterp.codeComplete(Buffer.str(), Pos, Results);
57+
for (auto c : Results) {
58+
Comps.push_back(llvm::LineEditor::Completion(c, c));
59+
}
60+
return Comps;
61+
}
62+
7163
llvm::SmallString<512> GetHistoryFilePath() {
7264
if (std::getenv("CLING_NOHISTORY")) {
7365
return {};
@@ -123,42 +115,27 @@ namespace cling {
123115
}
124116

125117
auto histfilePath{GetHistoryFilePath()};
126-
127-
const auto Completion =
128-
std::make_unique<UITabCompletion>(m_MetaProcessor->getInterpreter());
129-
TextInputHolder TI(histfilePath);
130-
131-
TI->SetCompletion(Completion.get());
118+
llvm::LineEditor LE("cling", histfilePath);
119+
std::string Input;
120+
std::string Prompt("[cling]$ ");
132121

133122
if (const char* HistSizeEnvvar = std::getenv("CLING_HISTSIZE")) {
134123
const size_t HistSize = std::strtoull(HistSizeEnvvar, nullptr, 0);
135124

136125
// std::strtoull() returns 0 if the parsing fails.
137126
// zero HistSize will disable history logging to file.
138-
// refer to textinput::History::AppendToFile()
139-
TI->SetHistoryMaxDepth(HistSize);
140-
TI->SetHistoryPruneLength(
141-
static_cast<size_t>(textinput::History::kPruneLengthDefault));
127+
LE.setHistorySize(HistSize);
142128
}
143129

144-
bool Done = false;
145-
std::string Line;
146-
std::string Prompt("[cling]$ ");
130+
LE.setPrompt(Prompt);
147131

148-
while (!Done) {
132+
LE.setListCompleter(ClingListCompleter(m_MetaProcessor->getInterpreter()));
133+
while (std::optional<std::string> Line = LE.readLine()) {
149134
try {
150-
m_MetaProcessor->getOuts().flush();
151-
{
152-
MetaProcessor::MaybeRedirectOutputRAII RAII(*m_MetaProcessor);
153-
TI->SetPrompt(Prompt.c_str());
154-
Done = TI->ReadInput() == textinput::TextInput::kRREOF;
155-
TI->TakeInput(Line);
156-
if (Done && Line.empty())
157-
break;
158-
}
135+
m_MetaProcessor->getOuts().flush(); // Do we need this now?
159136

160137
cling::Interpreter::CompilationResult compRes;
161-
const int indent = m_MetaProcessor->process(Line, compRes);
138+
const int indent = m_MetaProcessor->process(*Line, compRes);
162139

163140
// Quit requested?
164141
if (indent < 0)
@@ -186,8 +163,10 @@ namespace cling {
186163
catch(...) {
187164
cling::errs() << "Exception occurred. Recovering...\n";
188165
}
166+
167+
LE.setPrompt(Prompt);
189168
}
190-
m_MetaProcessor->getOuts().flush();
169+
m_MetaProcessor->getOuts().flush(); // Do we need this now?
191170
}
192171

193172
void UserInterface::PrintLogo() {

test/CodeGeneration/Statics.C

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,5 @@ inst02();
3737
// expected-no-diagnostics
3838
.q
3939

40-
// CHECK-NEXT: TERD::~TERD::inst02
40+
// CHECK: TERD::~TERD::inst02
4141
// CHECK-NEXT: TERD::~TERD::inst01

test/CodeGeneration/const.C

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ a.val
3333
a.getVal()
3434
// CHECK-NEXT: (int) 1
3535

36-
// CHECK-NEXT: ~A(1), this = [[PTR]]
36+
// CHECK: ~A(1), this = [[PTR]]
3737
// CHECK-NOT: ~A

test/CodeUnloading/AtExit.C

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ struct ShutdownRecursion {
115115

116116
// Reversed registration order
117117

118-
// CHECK-NEXT: ~ShutdownRecursion
118+
// CHECK: ~ShutdownRecursion
119119
// CHECK-NEXT: ShutdownRecursion2
120120
// CHECK-NEXT: ShutdownRecursion1
121121
// CHECK-NEXT: ShutdownRecursion0

test/Driver/Inputs/cling_history

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
/*
2-
comment
3-
* */
4-
static const int i = 37;
5-
};
6-
// another comment
7-
using std::cout;
8-
std::cout << "test\n";
1+
_HiStOrY_V2_
2+
/*\012
3+
\011\011comment\012
4+
\040*\040*/\012
5+
\011static\040const\040int\040i\040=\04037;\012
6+
};\012
7+
//\040another\040comment\012
8+
using\040std::cout;\012
9+
std::cout\040<<\040"test\134n";\012

test/Interfaces/evaluate.C

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,8 @@ gCling->evaluate("arrV", V);
224224

225225
// Destruct the variables with static storage:
226226
// Destruct arrV:
227-
//CHECK-NEXT: MADE{7}:dtor
227+
// The error `err_verify_no_directives` will add a new line here.
228+
//CHECK: MADE{7}:dtor
228229
//CHECK-NEXT: MADE{6}:dtor
229230
//CHECK-NEXT: MADE{5}:dtor
230231

test/Prompt/Continuation.C

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ CLING_MULTILINE_MACRO("DOWHILE");
7272
#define CLING_MULTILINE_TRAILING_SPACE \
7373
"Trailing Space " \
7474
"And A Tab" \
75-
" End" // expected-warning@1 {{backslash and newline separated by space}} // expected-warning@2 {{backslash and newline separated by space}}
75+
" End" // expected-warning@1 {{backslash and newline separated by space}} // expected-warning@2 {{backslash and newline separated by space}} // expected-warning@3 {{backslash and newline separated by space}}
7676

7777
CLING_MULTILINE_TRAILING_SPACE
7878
// CHECK-NEXT: (const char[29]) "Trailing Space And A Tab End"

test/Prompt/DontWrap.C

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,6 @@ N::X<int> funcReturnsXint() {
350350
funcReturnsXint()
351351
// CHECK-NEXT: (N::X<int>) @0x{{.*}}
352352

353-
// CHECK-NEXT: Nested::~Nested(80)
353+
// CHECK: Nested::~Nested(80)
354354

355355
// expected-no-diagnostics

0 commit comments

Comments
 (0)