Skip to content

Commit 43584ca

Browse files
authored
cli: Fix datafusion-cli hint edge cases (#20609)
## Which issue does this PR close? <!-- We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123. --> - Closes #. ## Rationale for this change The hint show condition relied on just checking if the input string is empty which caused visual issues like <img width="428" height="236" alt="image" src="https://github.com/user-attachments/assets/2b3a5c61-f910-4313-8844-35e1b0475692" /> <!-- Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed. Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes. --> ## What changes are included in this PR? ``` helper.rs: - Added a show_hint: Cell<bool> field to CliHelper. Uses Cell for interior mutability since hint() takes &self. - hint() now sets show_hint to false the moment line is non-empty. The hint only displays when show_hint is still true and the line is empty — i.e., only on the fresh prompt before any key is pressed. - validate() calls reset_hint() after validation so the hint reappears on the next prompt after submission. - Added pub fn reset_hint(&self) for external reset. exec.rs: - On Ctrl+C (ReadlineError::Interrupted), calls rl.helper().unwrap().reset_hint() so the next prompt still shows the hint. ``` <!-- There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR. --> ## Are these changes tested? <!-- We typically require tests for all PRs in order to: 1. Prevent the code from being accidentally broken by subsequent changes 2. Serve as another way to document the expected behavior of the code If tests are not included in your PR, please explain why (for example, are they covered by existing tests)? --> ## Are there any user-facing changes? <!-- If there are user-facing changes then we may require documentation to be updated before approving the PR. --> <!-- If there are any breaking changes to public APIs, please add the `api change` label. -->
1 parent 0af9ff5 commit 43584ca

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

datafusion-cli/src/exec.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ pub async fn exec_from_repl(
196196
}
197197
Err(ReadlineError::Interrupted) => {
198198
println!("^C");
199+
rl.helper().unwrap().reset_hint();
199200
continue;
200201
}
201202
Err(ReadlineError::Eof) => {

datafusion-cli/src/helper.rs

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
//! and auto-completion for file name during creating external table.
2020
2121
use std::borrow::Cow;
22+
use std::cell::Cell;
2223

2324
use crate::highlighter::{Color, NoSyntaxHighlighter, SyntaxHighlighter};
2425

@@ -40,6 +41,10 @@ pub struct CliHelper {
4041
completer: FilenameCompleter,
4142
dialect: Dialect,
4243
highlighter: Box<dyn Highlighter>,
44+
/// Tracks whether to show the default hint. Set to `false` once the user
45+
/// types anything, so the hint doesn't reappear after deleting back to
46+
/// an empty line. Reset to `true` when the line is submitted.
47+
show_hint: Cell<bool>,
4348
}
4449

4550
impl CliHelper {
@@ -53,6 +58,7 @@ impl CliHelper {
5358
completer: FilenameCompleter::new(),
5459
dialect: *dialect,
5560
highlighter,
61+
show_hint: Cell::new(true),
5662
}
5763
}
5864

@@ -62,6 +68,11 @@ impl CliHelper {
6268
}
6369
}
6470

71+
/// Re-enable the default hint for the next prompt.
72+
pub fn reset_hint(&self) {
73+
self.show_hint.set(true);
74+
}
75+
6576
fn validate_input(&self, input: &str) -> Result<ValidationResult> {
6677
if let Some(sql) = input.strip_suffix(';') {
6778
let dialect = match dialect_from_str(self.dialect) {
@@ -119,12 +130,11 @@ impl Hinter for CliHelper {
119130
type Hint = String;
120131

121132
fn hint(&self, line: &str, _pos: usize, _ctx: &Context<'_>) -> Option<String> {
122-
if line.trim().is_empty() {
123-
let suggestion = Color::gray(DEFAULT_HINT_SUGGESTION);
124-
Some(suggestion)
125-
} else {
126-
None
133+
if !line.is_empty() {
134+
self.show_hint.set(false);
127135
}
136+
(self.show_hint.get() && line.trim().is_empty())
137+
.then(|| Color::gray(DEFAULT_HINT_SUGGESTION))
128138
}
129139
}
130140

@@ -133,12 +143,9 @@ impl Hinter for CliHelper {
133143
fn is_open_quote_for_location(line: &str, pos: usize) -> bool {
134144
let mut sql = line[..pos].to_string();
135145
sql.push('\'');
136-
if let Ok(stmts) = DFParser::parse_sql(&sql)
137-
&& let Some(Statement::CreateExternalTable(_)) = stmts.back()
138-
{
139-
return true;
140-
}
141-
false
146+
DFParser::parse_sql(&sql).is_ok_and(|stmts| {
147+
matches!(stmts.back(), Some(Statement::CreateExternalTable(_)))
148+
})
142149
}
143150

144151
impl Completer for CliHelper {
@@ -161,7 +168,9 @@ impl Completer for CliHelper {
161168
impl Validator for CliHelper {
162169
fn validate(&self, ctx: &mut ValidationContext<'_>) -> Result<ValidationResult> {
163170
let input = ctx.input().trim_end();
164-
self.validate_input(input)
171+
let result = self.validate_input(input);
172+
self.reset_hint();
173+
result
165174
}
166175
}
167176

0 commit comments

Comments
 (0)