|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with the License. You may obtain a copy of the License at |
| 8 | +// |
| 9 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +// |
| 11 | +// Unless required by applicable law or agreed to in writing, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +//! The syntax highlighter. |
| 19 | +
|
| 20 | +use std::{ |
| 21 | + borrow::Cow::{self, Borrowed}, |
| 22 | + fmt::Display, |
| 23 | +}; |
| 24 | + |
| 25 | +use datafusion::sql::sqlparser::{ |
| 26 | + dialect::{dialect_from_str, Dialect, GenericDialect}, |
| 27 | + keywords::Keyword, |
| 28 | + tokenizer::{Token, Tokenizer}, |
| 29 | +}; |
| 30 | +use rustyline::highlight::Highlighter; |
| 31 | + |
| 32 | +/// The syntax highlighter. |
| 33 | +pub struct SyntaxHighlighter { |
| 34 | + dialect: Box<dyn Dialect>, |
| 35 | +} |
| 36 | + |
| 37 | +impl SyntaxHighlighter { |
| 38 | + pub fn new(dialect: &str) -> Self { |
| 39 | + let dialect = match dialect_from_str(dialect) { |
| 40 | + Some(dialect) => dialect, |
| 41 | + None => Box::new(GenericDialect {}), |
| 42 | + }; |
| 43 | + Self { dialect } |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +impl Highlighter for SyntaxHighlighter { |
| 48 | + fn highlight<'l>(&self, line: &'l str, _: usize) -> Cow<'l, str> { |
| 49 | + let mut out_line = String::new(); |
| 50 | + |
| 51 | + // `with_unescape(false)` since we want to rebuild the original string. |
| 52 | + let mut tokenizer = |
| 53 | + Tokenizer::new(self.dialect.as_ref(), line).with_unescape(false); |
| 54 | + let tokens = tokenizer.tokenize(); |
| 55 | + match tokens { |
| 56 | + Ok(tokens) => { |
| 57 | + for token in tokens.iter() { |
| 58 | + match token { |
| 59 | + Token::Word(w) if w.keyword != Keyword::NoKeyword => { |
| 60 | + out_line.push_str(&Color::red(token)); |
| 61 | + } |
| 62 | + Token::SingleQuotedString(_) => { |
| 63 | + out_line.push_str(&Color::green(token)); |
| 64 | + } |
| 65 | + other => out_line.push_str(&format!("{other}")), |
| 66 | + } |
| 67 | + } |
| 68 | + out_line.into() |
| 69 | + } |
| 70 | + Err(_) => Borrowed(line), |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + fn highlight_char(&self, line: &str, _: usize) -> bool { |
| 75 | + !line.is_empty() |
| 76 | + } |
| 77 | +} |
| 78 | + |
| 79 | +/// Convenient utility to return strings with [ANSI color](https://gist.github.com/JBlond/2fea43a3049b38287e5e9cefc87b2124). |
| 80 | +struct Color {} |
| 81 | + |
| 82 | +impl Color { |
| 83 | + fn green(s: impl Display) -> String { |
| 84 | + format!("\x1b[92m{s}\x1b[0m") |
| 85 | + } |
| 86 | + |
| 87 | + fn red(s: impl Display) -> String { |
| 88 | + format!("\x1b[91m{s}\x1b[0m") |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +#[cfg(test)] |
| 93 | +mod tests { |
| 94 | + use super::SyntaxHighlighter; |
| 95 | + use rustyline::highlight::Highlighter; |
| 96 | + |
| 97 | + #[test] |
| 98 | + fn highlighter_valid() { |
| 99 | + let s = "SElect col_a from tab_1;"; |
| 100 | + let highlighter = SyntaxHighlighter::new("generic"); |
| 101 | + let out = highlighter.highlight(s, s.len()); |
| 102 | + assert_eq!( |
| 103 | + "\u{1b}[91mSElect\u{1b}[0m col_a \u{1b}[91mfrom\u{1b}[0m tab_1;", |
| 104 | + out |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn highlighter_valid_with_new_line() { |
| 110 | + let s = "SElect col_a from tab_1\n WHERE col_b = 'なにか';"; |
| 111 | + let highlighter = SyntaxHighlighter::new("generic"); |
| 112 | + let out = highlighter.highlight(s, s.len()); |
| 113 | + assert_eq!( |
| 114 | + "\u{1b}[91mSElect\u{1b}[0m col_a \u{1b}[91mfrom\u{1b}[0m tab_1\n \u{1b}[91mWHERE\u{1b}[0m col_b = \u{1b}[92m'なにか'\u{1b}[0m;", |
| 115 | + out |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + #[test] |
| 120 | + fn highlighter_invalid() { |
| 121 | + let s = "SElect col_a from tab_1 WHERE col_b = ';"; |
| 122 | + let highlighter = SyntaxHighlighter::new("generic"); |
| 123 | + let out = highlighter.highlight(s, s.len()); |
| 124 | + assert_eq!("SElect col_a from tab_1 WHERE col_b = ';", out); |
| 125 | + } |
| 126 | +} |
0 commit comments