Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions datafusion-cli/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ pub async fn exec_from_repl(
}
Err(ReadlineError::Interrupted) => {
println!("^C");
rl.helper().unwrap().reset_hint();
continue;
}
Err(ReadlineError::Eof) => {
Expand Down
33 changes: 21 additions & 12 deletions datafusion-cli/src/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
//! and auto-completion for file name during creating external table.

use std::borrow::Cow;
use std::cell::Cell;

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

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

impl CliHelper {
Expand All @@ -53,6 +58,7 @@ impl CliHelper {
completer: FilenameCompleter::new(),
dialect: *dialect,
highlighter,
show_hint: Cell::new(true),
}
}

Expand All @@ -62,6 +68,11 @@ impl CliHelper {
}
}

/// Re-enable the default hint for the next prompt.
pub fn reset_hint(&self) {
self.show_hint.set(true);
}

fn validate_input(&self, input: &str) -> Result<ValidationResult> {
if let Some(sql) = input.strip_suffix(';') {
let dialect = match dialect_from_str(self.dialect) {
Expand Down Expand Up @@ -119,12 +130,11 @@ impl Hinter for CliHelper {
type Hint = String;

fn hint(&self, line: &str, _pos: usize, _ctx: &Context<'_>) -> Option<String> {
if line.trim().is_empty() {
let suggestion = Color::gray(DEFAULT_HINT_SUGGESTION);
Some(suggestion)
} else {
None
if !line.is_empty() {
self.show_hint.set(false);
}
(self.show_hint.get() && line.trim().is_empty())
.then(|| Color::gray(DEFAULT_HINT_SUGGESTION))
}
}

Expand All @@ -133,12 +143,9 @@ impl Hinter for CliHelper {
fn is_open_quote_for_location(line: &str, pos: usize) -> bool {
let mut sql = line[..pos].to_string();
sql.push('\'');
if let Ok(stmts) = DFParser::parse_sql(&sql)
&& let Some(Statement::CreateExternalTable(_)) = stmts.back()
{
return true;
}
false
DFParser::parse_sql(&sql).is_ok_and(|stmts| {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

its just simplification

matches!(stmts.back(), Some(Statement::CreateExternalTable(_)))
})
}

impl Completer for CliHelper {
Expand All @@ -161,7 +168,9 @@ impl Completer for CliHelper {
impl Validator for CliHelper {
fn validate(&self, ctx: &mut ValidationContext<'_>) -> Result<ValidationResult> {
let input = ctx.input().trim_end();
self.validate_input(input)
let result = self.validate_input(input);
self.reset_hint();
result
}
}

Expand Down