Skip to content

Commit bf4d5a0

Browse files
authored
#9056 Auto-insert closing bracket in YDB CLI (#17774)
Signed-off-by: vityaman <vityaman.dev@yandex.ru>
1 parent a3a111e commit bf4d5a0

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

ydb/apps/ydb/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* Brackets are now inserted in pairs in YDB CLI interactive mode
12
* Added `--scale` option to `ydb workload tpch init` and `ydb workload tpcds init` commands. Sets the percentage of the benchmark's data size and workload to use, relative to full scale.
23
* Added "--no-discovery" option. It allows to skip discovery and use user provided endpoint to connect to YDB cluster.
34
* Added `--retries` to `ydb workload <clickbenh|tpch|tpcds> run` command.

ydb/public/lib/ydb_cli/commands/interactive/line_reader.cpp

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include <yql/essentials/sql/v1/complete/string_util.h>
88

99
#include <util/generic/string.h>
10+
#include <util/generic/hash.h>
1011
#include <util/system/file.h>
1112

1213
#include <contrib/restricted/patched/replxx/include/replxx.hxx>
@@ -64,6 +65,8 @@ TLineReader::TLineReader(std::string prompt, std::string historyFilePath)
6465
{
6566
Rx.install_window_change_handler();
6667

68+
Rx.set_complete_on_empty(true);
69+
Rx.set_word_break_characters(NSQLComplete::WordBreakCharacters);
6770
Rx.set_completion_callback([this](const std::string& prefix, int& contextLen) {
6871
return YQLCompleter->Apply(prefix, contextLen);
6972
});
@@ -74,27 +77,47 @@ TLineReader::TLineReader(std::string prompt, std::string historyFilePath)
7477
}
7578
return hints;
7679
});
80+
7781
Rx.set_highlighter_callback([this](const auto& text, auto& colors) {
7882
YQLHighlighter->Apply(text, colors);
7983
});
80-
Rx.enable_bracketed_paste();
81-
Rx.set_unique_history(true);
82-
Rx.set_complete_on_empty(true);
83-
Rx.set_word_break_characters(NSQLComplete::WordBreakCharacters);
84-
Rx.bind_key(replxx::Replxx::KEY::control('N'), [&](char32_t code) { return Rx.invoke(replxx::Replxx::ACTION::HISTORY_NEXT, code); });
85-
Rx.bind_key(replxx::Replxx::KEY::control('P'), [&](char32_t code) { return Rx.invoke(replxx::Replxx::ACTION::HISTORY_PREVIOUS, code); });
86-
Rx.bind_key(replxx::Replxx::KEY::control('D'), [](char32_t) { return replxx::Replxx::ACTION_RESULT::BAIL; });
87-
auto commit_action = [&](char32_t code) {
84+
85+
Rx.bind_key(replxx::Replxx::KEY::control('N'), [&](char32_t code) {
86+
return Rx.invoke(replxx::Replxx::ACTION::HISTORY_NEXT, code);
87+
});
88+
Rx.bind_key(replxx::Replxx::KEY::control('P'), [&](char32_t code) {
89+
return Rx.invoke(replxx::Replxx::ACTION::HISTORY_PREVIOUS, code);
90+
});
91+
Rx.bind_key(replxx::Replxx::KEY::control('D'), [](char32_t) {
92+
return replxx::Replxx::ACTION_RESULT::BAIL;
93+
});
94+
Rx.bind_key(replxx::Replxx::KEY::control('J'), [&](char32_t code) {
8895
return Rx.invoke(replxx::Replxx::ACTION::COMMIT_LINE, code);
89-
};
90-
Rx.bind_key(replxx::Replxx::KEY::control('J'), commit_action);
96+
});
97+
98+
for (const auto [lhs, rhs] : THashMap<char, char>{
99+
{'(', ')'},
100+
{'[', ']'},
101+
{'{', '}'},
102+
{'`', '`'},
103+
{'\'', '\''},
104+
{'"', '"'},
105+
}) {
106+
Rx.bind_key(lhs, [&, lhs, rhs](char32_t) {
107+
Rx.invoke(replxx::Replxx::ACTION::INSERT_CHARACTER, lhs);
108+
Rx.invoke(replxx::Replxx::ACTION::INSERT_CHARACTER, rhs);
109+
return Rx.invoke(replxx::Replxx::ACTION::MOVE_CURSOR_LEFT, 0);
110+
});
111+
}
91112

92113
auto fileLockGuard = LockFile(HistoryFileHandle);
93114
if (!fileLockGuard) {
94115
Rx.print("Lock of history file failed: %s\n", strerror(errno));
95116
return;
96117
}
97118

119+
Rx.enable_bracketed_paste();
120+
Rx.set_unique_history(true);
98121
if (!Rx.history_load(HistoryFilePath)) {
99122
Rx.print("Loading history failed: %s\n", strerror(errno));
100123
}

0 commit comments

Comments
 (0)