Skip to content

Commit

Permalink
Experimental syntax highlighting (#160)
Browse files Browse the repository at this point in the history
  • Loading branch information
matthewmturner authored Sep 23, 2024
1 parent fbb039f commit 9ff201c
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 16 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,3 +269,15 @@ The FlightSQL config is where you can define the connection URL for the FlightSQ
[flight_sql]
connection_url = "http://localhost:50051"
```
#### Editor Config
The editor config is where you can set your preferred editor settings.
Currently only syntax highlighting is supported. It is experimental because currently the regex that is used to determine keywords only works in simple cases.
```toml
[editor]
experimental_syntax_highlighting = true
```
2 changes: 1 addition & 1 deletion src/app/handlers/flightsql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn normal_mode_handler(app: &mut App, key: KeyEvent) {
| KeyCode::Char('3')
| KeyCode::Char('4')
| KeyCode::Char('5')) => tab_navigation_handler(app, tab),
KeyCode::Char('c') => app.state.flightsql_tab.clear_editor(),
KeyCode::Char('c') => app.state.flightsql_tab.clear_editor(&app.state.config),
KeyCode::Char('e') => {
info!("Handling");
let editor = app.state.flightsql_tab.editor();
Expand Down
2 changes: 1 addition & 1 deletion src/app/handlers/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn normal_mode_handler(app: &mut App, key: KeyEvent) {
| KeyCode::Char('3')
| KeyCode::Char('4')
| KeyCode::Char('5')) => tab_navigation_handler(app, tab),
KeyCode::Char('c') => app.state.sql_tab.clear_editor(),
KeyCode::Char('c') => app.state.sql_tab.clear_editor(&app.state.config),
KeyCode::Char('e') => {
let editor = app.state.sql_tab.editor();
let lines = editor.lines();
Expand Down
4 changes: 2 additions & 2 deletions src/app/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ impl<'app> AppState<'app> {
pub fn new(config: AppConfig) -> Self {
let tabs = Tabs::default();

let sql_tab_state = SQLTabState::new();
let sql_tab_state = SQLTabState::new(&config);
#[cfg(feature = "flightsql")]
let flightsql_tab_state = FlightSQLTabState::new();
let flightsql_tab_state = FlightSQLTabState::new(&config);
let logs_tab_state = LogsTabState::default();
let history_tab_state = HistoryTabState::default();

Expand Down
17 changes: 11 additions & 6 deletions src/app/state/tabs/flightsql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use ratatui::widgets::TableState;
use tui_textarea::TextArea;

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

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -111,13 +112,15 @@ pub struct FlightSQLTabState<'app> {
}

impl<'app> FlightSQLTabState<'app> {
pub fn new() -> Self {
pub fn new(config: &AppConfig) -> Self {
let empty_text = vec!["Enter a query here.".to_string()];
// 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());
if config.editor.experimental_syntax_highlighting {
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 @@ -151,11 +154,13 @@ impl<'app> FlightSQLTabState<'app> {
}
}

pub fn clear_editor(&mut self) {
pub fn clear_editor(&mut self, config: &AppConfig) {
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());
if config.editor.experimental_syntax_highlighting {
textarea.set_search_pattern(sql::keyword_regex()).unwrap();
textarea.set_search_style(sql::keyword_style());
};
self.editor = textarea;
}

Expand Down
17 changes: 11 additions & 6 deletions src/app/state/tabs/sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ use ratatui::style::{Modifier, Style};
use ratatui::widgets::TableState;
use tui_textarea::TextArea;

use crate::config::AppConfig;
use crate::execution::ExecutionStats;

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -132,13 +133,15 @@ pub struct SQLTabState<'app> {
}

impl<'app> SQLTabState<'app> {
pub fn new() -> Self {
pub fn new(config: &AppConfig) -> Self {
let empty_text = vec!["Enter a query here.".to_string()];
// 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());
if config.editor.experimental_syntax_highlighting {
textarea.set_search_pattern(keyword_regex()).unwrap();
textarea.set_search_style(keyword_style());
};
Self {
editor: textarea,
editor_editable: false,
Expand Down Expand Up @@ -172,11 +175,13 @@ impl<'app> SQLTabState<'app> {
}
}

pub fn clear_editor(&mut self) {
pub fn clear_editor(&mut self, config: &AppConfig) {
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());
if config.editor.experimental_syntax_highlighting {
textarea.set_search_pattern(keyword_regex()).unwrap();
textarea.set_search_style(keyword_style());
};
self.editor = textarea;
}

Expand Down
6 changes: 6 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ pub struct AppConfig {
#[cfg(feature = "flightsql")]
#[serde(default = "default_flightsql_config")]
pub flightsql: FlightSQLConfig,
pub editor: EditorConfig,
}

fn default_execution_config() -> ExecutionConfig {
Expand Down Expand Up @@ -197,3 +198,8 @@ impl Default for FlightSQLConfig {
pub fn default_connection_url() -> String {
"http://localhost:50051".to_string()
}

#[derive(Debug, Default, Deserialize)]
pub struct EditorConfig {
pub experimental_syntax_highlighting: bool,
}

0 comments on commit 9ff201c

Please sign in to comment.