1919//! and auto-completion for file name during creating external table.
2020
2121use std:: borrow:: Cow ;
22+ use std:: cell:: Cell ;
2223
2324use 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
4550impl 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 {
133143fn 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
144151impl Completer for CliHelper {
@@ -161,7 +168,9 @@ impl Completer for CliHelper {
161168impl 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