Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syntax highlight for keywords #149

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 Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ tokio-util = "0.7.10"
toml = "0.8.12"
tonic = { version = "0.11.0", optional = true }
tui-logger = {version = "0.12", features = ["tracing-support"]}
tui-textarea = "0.6.1"
tui-textarea = { version = "0.6.1", features = ["search"] }
url = { version = "2.5.2", optional = true }

[dev-dependencies]
Expand Down
6 changes: 5 additions & 1 deletion src/app/state/tabs/flightsql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use ratatui::style::Style;
use ratatui::widgets::TableState;
use tui_textarea::TextArea;

use crate::app::state::tabs::sql;
use crate::execution::ExecutionStats;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -115,7 +116,8 @@ impl<'app> FlightSQLTabState<'app> {
// TODO: Enable vim mode from config?
let mut textarea = TextArea::new(empty_text);
textarea.set_style(Style::default().fg(tailwind::WHITE));

textarea.set_search_pattern(sql::keyword_regex()).unwrap();
textarea.set_search_style(sql::keyword_style());
Self {
editor: textarea,
editor_editable: false,
Expand Down Expand Up @@ -152,6 +154,8 @@ impl<'app> FlightSQLTabState<'app> {
pub fn clear_editor(&mut self) {
let mut textarea = TextArea::new(vec!["".to_string()]);
textarea.set_style(Style::default().fg(tailwind::WHITE));
textarea.set_search_pattern(sql::keyword_regex()).unwrap();
textarea.set_search_style(sql::keyword_style());
self.editor = textarea;
}

Expand Down
28 changes: 27 additions & 1 deletion src/app/state/tabs/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ use core::cell::RefCell;
use std::time::Duration;

use datafusion::arrow::array::RecordBatch;
use datafusion::sql::sqlparser::keywords;
use ratatui::crossterm::event::KeyEvent;
use ratatui::style::palette::tailwind;
use ratatui::style::Style;
use ratatui::style::{Modifier, Style};
use ratatui::widgets::TableState;
use tui_textarea::TextArea;

Expand Down Expand Up @@ -101,6 +102,27 @@ impl Query {
}
}

pub fn get_keywords() -> Vec<String> {
keywords::ALL_KEYWORDS
.iter()
.map(|k| k.to_string())
.collect()
}

pub fn keyword_regex() -> String {
format!(
"(?i)(^|[^a-zA-Z0-9\'\"`._]*?)({})($|[^a-zA-Z0-9\'\"`._]*)",
get_keywords().join("|")
)
}

pub fn keyword_style() -> Style {
Style::default()
.bg(tailwind::BLACK)
.fg(tailwind::YELLOW.c100)
.add_modifier(Modifier::BOLD)
}

#[derive(Debug, Default)]
pub struct SQLTabState<'app> {
editor: TextArea<'app>,
Expand All @@ -115,6 +137,8 @@ impl<'app> SQLTabState<'app> {
// TODO: Enable vim mode from config?
let mut textarea = TextArea::new(empty_text);
textarea.set_style(Style::default().fg(tailwind::WHITE));
textarea.set_search_pattern(keyword_regex()).unwrap();
textarea.set_search_style(keyword_style());
Self {
editor: textarea,
editor_editable: false,
Expand Down Expand Up @@ -151,6 +175,8 @@ impl<'app> SQLTabState<'app> {
pub fn clear_editor(&mut self) {
let mut textarea = TextArea::new(vec!["".to_string()]);
textarea.set_style(Style::default().fg(tailwind::WHITE));
textarea.set_search_pattern(keyword_regex()).unwrap();
textarea.set_search_style(keyword_style());
self.editor = textarea;
}

Expand Down
Loading