25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。
TypeScript aicliclickhousedatabasedatabase-clientdatabase-managementdockerguimcpmongodbmysqlpostgresqlredisrustsql-serversqlitetaurivue
fix(sql): refine current statement cursor resolution
t8y2065f0a4234eb
diff --git a/crates/dbx-core/src/sql.rs b/crates/dbx-core/src/sql.rsindex ea222f4436..db882308ed 100644--- a/crates/dbx-core/src/sql.rs+++ b/crates/dbx-core/src/sql.rs@@ -267,19 +267,35 @@ pub fn find_statement_at_cursor(sql: &str, cursor_pos: usize) -> String {let statements = split_sql_statement_ranges(sql);let cursor = utf16_offset_to_byte_index(sql, cursor_pos);- for statement in &statements {- if cursor >= statement.start && cursor <= statement.end {+ for (idx, statement) in statements.iter().enumerate() {+ if cursor > statement.start && cursor < statement.end {return statement.text.clone();}- }- if let Some(last) = statements.last() {- if cursor >= last.end {- return last.text.clone();+ if cursor == statement.start {+ if cursor_has_sql_after_cursor_on_line(sql, cursor) {+ return statement.text.clone();+ }+ if let Some(prev) = idx.checked_sub(1).and_then(|prev_idx| statements.get(prev_idx)) {+ return prev.text.clone();+ }+ return statement.text.clone();+ }++ if cursor < statement.start {+ if let Some(prev) = idx.checked_sub(1).and_then(|prev_idx| statements.get(prev_idx)) {+ return prev.text.clone();+ }+ return statement.text.clone();}}- sql.trim().to_string()+ statements.last().map(|statement| statement.text.clone()).unwrap_or_else(|| sql.trim().to_string())+}++fn cursor_has_sql_after_cursor_on_line(sql: &str, cursor: usize) -> bool {+ let line_end = sql[cursor..].find('\n').map_or(sql.len(), |offset| cursor + offset);+ sql[cursor..line_end].chars().any(|ch| !ch.is_whitespace())}fn split_sql_statement_ranges(sql: &str) -> Vec<SqlStatementRange> {@@ -1208,6 +1224,21 @@ DELIMITER ;";assert_eq!(super::find_statement_at_cursor(sql, cursor), "SELECT 2");}+ #[test]+ fn finds_statement_at_cursor_after_semicolon_with_blank_line_stays_on_previous_statement() {+ let sql = "SELECT 1;\n\nSELECT 2;";+ let cursor = sql[..sql.find(';').unwrap() + 1].encode_utf16().count();+ assert_eq!(super::find_statement_at_cursor(sql, cursor), "SELECT 1");+ }++ #[test]+ fn finds_statement_at_cursor_after_semicolon_same_line_moves_to_next_statement() {+ let sql = "SELECT 1; SELECT 2;";+ let cursor = sql[..sql.find("SELECT 2").unwrap()].encode_utf16().count();++ assert_eq!(super::find_statement_at_cursor(sql, cursor), "SELECT 2");+ }+#[test]fn finds_statement_with_dollar_quote() {let sql = "SELECT $$a;b$$; SELECT 2";