Skip to content

Commit

Permalink
Syntax highlight for keywords (#149)
Browse files Browse the repository at this point in the history
Adds basic keyboard driven syntax highlighting

<img width="606" alt="image"
src="https://github.com/user-attachments/assets/3d51a18d-7f4d-44ec-8953-9969f051a98f">
  • Loading branch information
matthewmturner authored Sep 20, 2024
1 parent e71eaa4 commit 3c3daa1
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 3 deletions.
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

0 comments on commit 3c3daa1

Please sign in to comment.