diff --git a/Cargo.lock b/Cargo.lock index 183a5427d7..910999cb9d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7736,10 +7736,12 @@ dependencies = [ "serde", "serde_json", "sha2 0.11.0", + "sqlformat", "sqlparser", "thiserror 2.0.20", "tokio", "tokio-util", + "unicode-normalization", "unicode-segmentation", "unicode-width", "url", diff --git a/Cargo.toml b/Cargo.toml index 4bd92fa5fa..5df7181391 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -65,6 +65,8 @@ regex = "1" # Edits a TOML config file and keeps its comments and layout. toml_edit = "0.25.15" sqlparser = { version = "0.63", default-features = false, features = ["std"] } +# The core's SQL formatter, checked statement by statement against the script lexer. +sqlformat = "0.5" tokio = { version = "1", features = ["rt-multi-thread", "macros", "net", "time", "sync", "process"] } tokio-util = { version = "0.7", default-features = false, features = ["codec", "rt"] } secrecy = { version = "0.10", features = ["serde"] } diff --git a/core/ARCHITECTURE.md b/core/ARCHITECTURE.md index f24d9e304d..300b44abec 100644 --- a/core/ARCHITECTURE.md +++ b/core/ARCHITECTURE.md @@ -114,8 +114,8 @@ Each boundary error has a stable `code()`, such as `driver.timeout` or `ssh.host `ffi` is the library an app outside Rust loads: the Windows app as a DLL, the Apple apps as a static library in an XCFramework, built without the `engines` feature by `apple/scripts/build-core.sh`. Core ADRs 0002, on the bindings, 0003, on result windows, and 0009, on the macOS slices, hold the reasons. -- One UniFFI API. `Core` lists the engines with the family each is listed under, opens a file, connects a saved connection or tests one from a form, and parses a connection URL with `tablepro_core::connection_url`, refusing one that needs a setting its record has no field for. Its `ConnectionLibrary` reads and changes the saved connections, groups, favourites and tags in `connections.json` and stores their secrets, and gives the Apple apps the library's CloudKit records and merges a pull into it (`tablepro_storage::cloudkit`). A `DatabaseSession` reads the catalog one level at a time, writes a table's sorted page at an offset and counts the table, writes a query re-run with a sort (`docs/decisions/0007-sorting-a-result.md`), picks the statement at a caret with the session's grammar (`statement_at`), says what Safe Mode lets an editor run do at the session's level and takes a level saved while it is open (`gate_run`, over `SafeModeLevel::gate`, and `set_safe_mode`), reports and waits on the session's health, opens a `QueryEditor`, and opens a `TableEditor`, which plans a table tab's typed edits into a `RowSave` and runs it (`docs/decisions/0019-row-edits-through-the-ffi.md`). `QueryEditor::run` returns a `QueryRun` at once; the app drains its events after a `RunListener` call, which comes only when no earlier one is waiting to be drained, and the last says which transaction the editor is left in. Each result set is a `ResultSet`, whose `sorted` gives the rows it holds in a column's order. -- `Core::open_account` opens the TablePro account as an `AccountService`, whose `AccountListener` only signals that `status()` changed. It also shares links: `create_share` builds a result's snapshot from a `ResultSet` in Rust, and a table page's `view_sql` is the query its view stands for ([ADR 0027](docs/decisions/0027-share-links.md)). `create_share` takes the share sheet's Keep, Mask or Remove for each column, and `ResultSet::share_masking`, `share_preview` and `share_summary` start the sheet's columns from the detectors and show and count what a share sends ([ADR 0028](docs/decisions/0028-data-masking.md)). `scan_sql_for_secrets`, `scan_columns_for_secrets`, `scan_sql_for_personal_data` and `share_token_from_link` are functions of their own. +- One UniFFI API. `Core` lists the engines with the family each is listed under, opens a file, connects a saved connection or tests one from a form, and parses a connection URL with `tablepro_core::connection_url`, refusing one that needs a setting its record has no field for. Its `ConnectionLibrary` reads and changes the saved connections, groups, favourites and tags in `connections.json` and stores their secrets, and gives the Apple apps the library's CloudKit records and merges a pull into it (`tablepro_storage::cloudkit`). A `DatabaseSession` reads the catalog one level at a time, writes a table's sorted page at an offset and counts the table, writes a query re-run with a sort (`docs/decisions/0007-sorting-a-result.md`), picks the statement at a caret with the session's grammar (`statement_at`), says what completes at a caret for an editor in one of the server's databases as a `CompletionList` that ranks again for each word typed, read from the catalog the other views share (`complete`, over `tablepro_core::sql_syntax::completion` and `tablepro_session::completion`), says what Safe Mode lets an editor run do at the session's level and takes a level saved while it is open (`gate_run`, over `SafeModeLevel::gate`, and `set_safe_mode`), reports and waits on the session's health, opens a `QueryEditor`, and opens a `TableEditor`, which plans a table tab's typed edits into a `RowSave` and runs it (`docs/decisions/0019-row-edits-through-the-ffi.md`). `QueryEditor::run` returns a `QueryRun` at once; the app drains its events after a `RunListener` call, which comes only when no earlier one is waiting to be drained, and the last says which transaction the editor is left in. Each result set is a `ResultSet`, whose `sorted` gives the rows it holds in a column's order. +- `Core::open_account` opens the TablePro account as an `AccountService`, whose `AccountListener` only signals that `status()` changed. It also shares links: `create_share` builds a result's snapshot from a `ResultSet` in Rust, and a table page's `view_sql` is the query its view stands for ([ADR 0027](docs/decisions/0027-share-links.md)). `create_share` takes the share sheet's Keep, Mask or Remove for each column, and `ResultSet::share_masking`, `share_preview` and `share_summary` start the sheet's columns from the detectors and show and count what a share sends ([ADR 0028](docs/decisions/0028-data-masking.md)). `scan_sql_for_secrets`, `scan_columns_for_secrets`, `scan_sql_for_personal_data` and `share_token_from_link` are functions of their own. So are `format_sql` and `format_sql_selection`, which reformat a script or a selection through `tablepro_core::sql_syntax::format` and keep a statement as typed where the formatter would change its tokens. - `Core::ai_assistant` gives an `AiAssistant` over the core's one AI client and its secret store: it says whether a provider's key is kept and keeps or removes one (the key never crosses back), lists the provider's models, reads a session's catalog into the system prompt of a conversation (`start_conversation`), and streams a reply as an `AiReply`, drained after an `AiReplyListener` call as a run is. Both refuse a saved connection whose AI policy is Never with `ai.blocked`; asking under Ask is the app's ([ADR 0032](docs/decisions/0032-ai-through-the-ffi.md)). - `Core::mcp_server` gives an `McpService` that runs the MCP server on the sessions `connect_saved` opened, keeps its token in the Core's secret store, and asks the app to open a connection or approve a write through `McpAppHost`; `McpListener` only signals that `status()` or `seen_clients()` changed. The package also builds `tablepro-cli`, the Windows package's command ([0036](docs/decisions/0036-mcp-and-the-command-line-through-the-ffi.md)). - The grid reads a `ResultSet` through the C ABI in `src/window_abi.rs`: `tablepro_cursor_fetch` packs the display text of one visible range, and `tablepro_cursor_free` ends the cursor. The editor reads SQL there from its own UTF-16 text: `tablepro_syntax_tokens` gives the highlight tokens, and `tablepro_script_statements` every statement as `ScriptPlan` splits the text for `statement_at` and the drivers. diff --git a/core/crates/core/Cargo.toml b/core/crates/core/Cargo.toml index 9d8daae1c4..17140968d1 100644 --- a/core/crates/core/Cargo.toml +++ b/core/crates/core/Cargo.toml @@ -24,10 +24,12 @@ secrecy.workspace = true serde.workspace = true serde_json.workspace = true sha2.workspace = true +sqlformat.workspace = true sqlparser.workspace = true thiserror.workspace = true tokio = { workspace = true, features = ["io-util", "rt"] } tokio-util.workspace = true +unicode-normalization.workspace = true unicode-segmentation.workspace = true unicode-width.workspace = true url.workspace = true diff --git a/core/crates/core/src/sql_syntax/completion/catalog.rs b/core/crates/core/src/sql_syntax/completion/catalog.rs new file mode 100644 index 0000000000..03e47d11d7 --- /dev/null +++ b/core/crates/core/src/sql_syntax/completion/catalog.rs @@ -0,0 +1,24 @@ +use std::sync::Arc; + +use crate::ColumnInfo; +use crate::meta::{ObjectInfo, SchemaInfo, TableRef}; + +// What a session's catalog holds for one completion; a list it could not read is None or left out. +#[derive(Debug, Default)] +pub struct CatalogAnswer { + pub schemas: Option>, + pub objects: Option>, + pub columns: Vec<(TableRef, Arc<[ColumnInfo]>)>, +} + +// Where an unqualified name resolves for an editor that may run in another of the server's databases. +#[derive(Debug, Clone, Default, PartialEq, Eq)] +pub enum CatalogHome { + // The connection's default schema. + #[default] + Connection, + // Another of the server's databases, which the catalog lists as a schema (MySQL). + Schema(String), + // A database the connection's catalog does not describe, so nothing read from it would be right. + Elsewhere(String), +} diff --git a/core/crates/core/src/sql_syntax/completion/fuzzy.rs b/core/crates/core/src/sql_syntax/completion/fuzzy.rs new file mode 100644 index 0000000000..928b9f0df9 --- /dev/null +++ b/core/crates/core/src/sql_syntax/completion/fuzzy.rs @@ -0,0 +1,156 @@ +// GtkSourceView's fuzzy ranking over characters rather than bytes: its strchr lookup never matches a needle outside +// ASCII, so "đơn" would not find "đơn_hàng". + +use std::ops::Range; + +// Each character lowercased on its own, so a letter the matcher compares one to one never becomes two, as GLib's +// casefold turns ß into ss. +pub fn casefold(needle: &str) -> String { + needle + .chars() + .map(|c| { + let mut lower = c.to_lowercase(); + let first = lower.next().unwrap_or(c); + if lower.next().is_some() { c } else { first } + }) + .collect() +} + +// How far `haystack` is from `casefold_needle`, lower being closer; None when it does not match. +pub fn fuzzy_match(haystack: &str, casefold_needle: &str) -> Option { + matched(haystack, casefold_needle).map(|found| found.score) +} + +// Everything matches while nothing is typed. +pub fn matches_word(haystack: &str, word: &str) -> bool { + word.is_empty() || fuzzy_match(haystack, &casefold(word)).is_some() +} + +// Byte ranges of the matched characters, adjacent ones joined, for a list to mark; empty without a match. +pub fn matched_runs(haystack: &str, casefold_needle: &str) -> Vec> { + matched(haystack, casefold_needle).map_or_else(Vec::new, |found| found.runs) +} + +struct Match { + score: u32, + runs: Vec>, +} + +// A skipped character costs two, a match only by case one, and every character left at the end one, so a prefix +// ranks first. +fn matched(haystack: &str, casefold_needle: &str) -> Option { + if haystack.is_empty() { + return None; + } + let mut chars = haystack.char_indices(); + let mut score: u32 = 0; + let mut runs: Vec> = Vec::new(); + for wanted in casefold_needle.chars() { + let mut skipped: u32 = 0; + let (at, found) = loop { + let (at, found) = chars.next()?; + if same_letter(found, wanted) { + break (at, found); + } + skipped += 1; + }; + score = score.saturating_add(skipped.saturating_mul(2)); + if found != wanted { + score = score.saturating_add(1); + } + let end = at + found.len_utf8(); + match runs.last_mut() { + Some(run) if run.end == at => run.end = end, + _ => runs.push(at..end), + } + } + let rest = u32::try_from(chars.count()).unwrap_or(u32::MAX); + Some(Match { + score: score.saturating_add(rest), + runs, + }) +} + +fn same_letter(found: char, wanted: char) -> bool { + // The general case builds two iterators a character, which ranking a catalog of 50,000 tables feels. + if found.is_ascii() && wanted.is_ascii() { + return found.eq_ignore_ascii_case(&wanted); + } + found == wanted || found.to_lowercase().eq(wanted.to_lowercase()) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn fuzzy_match_ranks_prefix_higher() { + let needle = casefold("us"); + + let prefix = fuzzy_match("users", &needle).expect("a match"); + let scattered = fuzzy_match("product_status", &needle).expect("a match"); + + assert!(prefix < scattered, "{prefix} is not closer than {scattered}"); + assert_eq!(fuzzy_match("orders", &needle), None); + assert_eq!(fuzzy_match("su", &needle), None, "the order of the letters was ignored"); + } + + #[test] + fn an_exact_case_match_ranks_above_one_by_case() { + let needle = casefold("id"); + + assert!(fuzzy_match("id", &needle) < fuzzy_match("ID", &needle)); + } + + #[test] + fn non_ascii_needle_is_casefolded() { + let needle = casefold("ĐƠN"); + + assert_eq!(needle, "đơn"); + assert!(fuzzy_match("đơn_hàng", &needle).is_some()); + assert!(fuzzy_match("ĐƠN_HÀNG", &needle).is_some()); + assert_eq!(fuzzy_match("don_hang", &needle), None); + } + + #[test] + fn a_proposal_is_offered_only_for_a_word_it_matches() { + assert!(matches_word("orders", "")); + assert!(matches_word("orders", "ORD")); + assert!(!matches_word("orders", "ordu"), "a proposal ranked for the last word"); + } + + #[test] + fn an_empty_needle_matches_everything() { + assert!(fuzzy_match("users", "").is_some()); + assert_eq!(fuzzy_match("", ""), None); + } + + #[test] + fn matched_runs_join_adjacent_characters() { + assert_eq!(matched_runs("users", &casefold("US")), [Range { start: 0, end: 2 }]); + assert_eq!(matched_runs("users", ""), Vec::>::new()); + assert_eq!(matched_runs("orders", &casefold("us")), Vec::>::new()); + } + + #[test] + fn matched_runs_mark_whole_multibyte_characters() { + let text = "bảng_đơn"; + let d = text.find('đ').expect("đ"); + let n = text.rfind('n').expect("n"); + + assert_eq!(matched_runs(text, &casefold("đn")), [d..d + 2, n..n + 1]); + } + + #[test] + fn a_dotted_i_keeps_one_source_character() { + assert_eq!(casefold("İ"), "İ"); + assert!(matches_word("İstanbul", "İ")); + assert_eq!(matched_runs("İstanbul", &casefold("İst")), [Range { start: 0, end: 4 }]); + } + + #[test] + fn a_sharp_s_typed_finds_the_name_it_is_in() { + assert_eq!(casefold("STRAẞE"), "straße"); + assert!(matches_word("straße_nr", "straß")); + } +} diff --git a/core/crates/core/src/sql_syntax/completion/mod.rs b/core/crates/core/src/sql_syntax/completion/mod.rs new file mode 100644 index 0000000000..120f3e4ce0 --- /dev/null +++ b/core/crates/core/src/sql_syntax/completion/mod.rs @@ -0,0 +1,14 @@ +// What the text before a caret asks for, how closely a name matches the word typed, and the order a list shows them +// in. An app reads the catalog (tablepro_session::completion), builds its own rows and inserts the choice. + +mod catalog; +mod fuzzy; +mod ranking; +mod scope; +mod word; + +pub use catalog::{CatalogAnswer, CatalogHome}; +pub use fuzzy::{casefold, fuzzy_match, matched_runs, matches_word}; +pub use ranking::{Ranked, Ranking}; +pub use scope::{CompletionScope, analyze, tables_named}; +pub use word::{is_word_char, word_start}; diff --git a/core/crates/core/src/sql_syntax/completion/ranking.rs b/core/crates/core/src/sql_syntax/completion/ranking.rs new file mode 100644 index 0000000000..365986050b --- /dev/null +++ b/core/crates/core/src/sql_syntax/completion/ranking.rs @@ -0,0 +1,102 @@ +use std::sync::Arc; + +use super::fuzzy; + +// What a list ranks. The proposal itself is each app's, so it only names its label and its order among equal matches. +pub trait Ranked { + fn label(&self) -> &str; + + // Lower comes first among equally close matches: what the statement already names before what it might. + fn kind_rank(&self) -> u8; +} + +// The closest match first, then by kind, ties in the catalog's order, which lists a table's columns as it defines them. +#[derive(Debug)] +pub struct Ranking { + candidates: Arc<[C]>, + needle: String, + ranked: Vec, +} + +impl Ranking { + // `needle` is casefolded. With nothing typed every candidate matches equally: the matcher would rank by length + // alone and put every short keyword above the columns. + pub fn new(candidates: Arc<[C]>, needle: &str) -> Self { + let mut scored: Vec<(u32, u8, u32)> = candidates + .iter() + .enumerate() + .filter_map(|(at, candidate)| { + let score = if needle.is_empty() { + 0 + } else { + fuzzy::fuzzy_match(candidate.label(), needle)? + }; + Some((score, candidate.kind_rank(), u32::try_from(at).ok()?)) + }) + .collect(); + scored.sort_unstable(); + Self { + ranked: scored.into_iter().map(|(_, _, at)| at).collect(), + candidates, + needle: needle.to_owned(), + } + } +} + +impl Ranking { + pub fn candidates(&self) -> &Arc<[C]> { + &self.candidates + } + + pub fn needle(&self) -> &str { + &self.needle + } + + // Indexes into the candidates the word matches, closest first. + pub fn ranked(&self) -> &[u32] { + &self.ranked + } +} + +#[cfg(test)] +mod tests { + use super::*; + + struct Named(&'static str, u8); + + impl Ranked for Named { + fn label(&self) -> &str { + self.0 + } + + fn kind_rank(&self) -> u8 { + self.1 + } + } + + const COLUMN: u8 = 0; + const TABLE: u8 = 1; + const KEYWORD: u8 = 5; + + #[test] + fn closer_matches_rank_first_then_by_kind_then_in_catalog_order() { + let candidates: Arc<[Named]> = vec![ + Named("USING", KEYWORD), + Named("product_status", TABLE), + Named("users", TABLE), + Named("user_id", COLUMN), + Named("status", COLUMN), + ] + .into(); + + let order = |needle: &str| Ranking::new(candidates.clone(), &fuzzy::casefold(needle)).ranked; + + assert_eq!(order("us"), [2, 3, 0, 4, 1]); + assert_eq!( + order(""), + [3, 4, 1, 2, 0], + "nothing typed ranks by kind, then catalog order" + ); + assert_eq!(order("zz"), Vec::::new()); + } +} diff --git a/core/crates/core/src/sql_syntax/completion/scope.rs b/core/crates/core/src/sql_syntax/completion/scope.rs new file mode 100644 index 0000000000..0419188524 --- /dev/null +++ b/core/crates/core/src/sql_syntax/completion/scope.rs @@ -0,0 +1,780 @@ +use std::ops::Range; + +use crate::meta::TableRef; +use crate::sql_syntax::SqlGrammar; +use crate::sql_syntax::script::{LexicalSettings, ScriptPlan, ScriptToken, ScriptTokenKind}; + +// What the text before the caret asks for. +#[derive(Debug, Clone, PartialEq, Eq)] +pub enum CompletionScope { + // A table or view, from `schema` or the default one. + Objects { schema: Option }, + // A column of one of these tables. + Columns { tables: Vec }, + // After `a.b.`: the columns of table `b` in schema `a`, or the objects of schema `b` when `a` names the database, + // as SQL Server's `db.dbo.` does. + Qualified { qualifier: String, name: String }, + // Keywords, objects, and the columns of the tables the statement already names. + General { tables_in_statement: Vec }, +} + +impl CompletionScope { + // After FROM or a dot the place alone asks for a name, so a list is worth showing before anything is typed. + pub fn names_a_place(&self) -> bool { + !matches!(self, Self::General { .. }) + } +} + +const RELATION_WORDS: [&str; 5] = ["FROM", "JOIN", "INTO", "UPDATE", "TABLE"]; + +// The first word of a parenthesised query, inside which a FROM names tables again. +const QUERY_WORDS: [&str; 5] = ["SELECT", "WITH", "INSERT", "UPDATE", "DELETE"]; + +// Never an alias, though the grammar's keyword list leaves them out. +const CLAUSE_WORDS: [&str; 12] = [ + "FETCH", + "FINAL", + "FOR", + "LATERAL", + "LIMIT", + "NATURAL", + "OFFSET", + "ONLY", + "PREWHERE", + "RETURNING", + "TABLESAMPLE", + "WINDOW", +]; + +// None inside a string, comment, quoted name or dollar-quoted body, where the text is not SQL the editor reads, and +// inside a number or after a dot that follows no name, where nothing has a name. +pub fn analyze(text: &str, cursor: usize, grammar: SqlGrammar, settings: LexicalSettings) -> Option { + let cursor = floor_char_boundary(text, cursor); + let plan = ScriptPlan::build(text, grammar, settings); + if plan + .tokens(cursor.saturating_sub(1)..cursor + 1) + .any(|token| encloses(&token, text, cursor)) + { + return None; + } + let Some(statement) = statement_around(&plan, cursor) else { + return Some(CompletionScope::General { + tables_in_statement: Vec::new(), + }); + }; + // What a MySQL executable comment holds is code, its markers are not. + let lexemes: Vec> = plan + .tokens(statement) + .filter(|token| !token.kind.is_trivia() && token.kind != ScriptTokenKind::ExecutableMarker) + .filter_map(|token| Lexeme::new(text, token)) + .collect(); + let tables = TableMap::collect(&lexemes, grammar); + let mut before = lexemes.iter().take_while(|lexeme| lexeme.end <= cursor).count(); + if before + .checked_sub(1) + .and_then(|last| lexemes.get(last)) + .is_some_and(|last| last.kind == ScriptTokenKind::Word && last.end == cursor) + { + before -= 1; + } + scope_after(lexemes.get(..before).unwrap_or_default(), &tables, grammar) +} + +fn scope_after(before: &[Lexeme<'_>], tables: &TableMap, grammar: SqlGrammar) -> Option { + let Some((last, rest)) = before.split_last() else { + return Some(tables.general()); + }; + if last.is_punct(".") { + return after_dot(rest, tables); + } + let relation = relation_word(before, rest.len()).is_some(); + if relation || (last.is_punct(",") && in_from_list(rest, grammar)) { + return Some(CompletionScope::Objects { schema: None }); + } + Some(tables.general()) +} + +// `name.` names a table's columns when the statement knows the name and a schema's objects otherwise; `a.b.` may be +// either, and `a.b.c.` is table `b.c`. None after a string or a bracket. +fn after_dot(rest: &[Lexeme<'_>], tables: &TableMap) -> Option { + let (name, rest) = rest.split_last()?; + let name = name.identifier()?; + Some(match name_before_dot(rest) { + Some((schema, rest)) if name_before_dot(rest).is_some() => CompletionScope::Columns { + tables: vec![TableRef::new(Some(schema), name)], + }, + Some((qualifier, _)) => CompletionScope::Qualified { qualifier, name }, + None => match tables.lookup(&name, &open_groups(rest)) { + Some(table) => CompletionScope::Columns { + tables: vec![table.clone()], + }, + None => CompletionScope::Objects { schema: Some(name) }, + }, + }) +} + +fn name_before_dot<'s, 'a>(lexemes: &'s [Lexeme<'a>]) -> Option<(String, &'s [Lexeme<'a>])> { + let [rest @ .., name, dot] = lexemes else { + return None; + }; + if !dot.is_punct(".") { + return None; + } + Some((name.identifier()?, rest)) +} + +fn relation_word(lexemes: &[Lexeme<'_>], index: usize) -> Option<&'static str> { + let lexeme = lexemes.get(index)?; + let word = RELATION_WORDS.into_iter().find(|word| lexeme.is_word(word))?; + (word != "FROM" || from_names_tables(lexemes, index)).then_some(word) +} + +// A FROM that ends `IS DISTINCT FROM` or sits in a call such as `EXTRACT(YEAR FROM created_at)` names no table. +fn from_names_tables(lexemes: &[Lexeme<'_>], index: usize) -> bool { + let follows_distinct = index + .checked_sub(1) + .and_then(|before| lexemes.get(before)) + .is_some_and(|before| before.is_word("DISTINCT")); + if follows_distinct { + return false; + } + match enclosing_group(lexemes, index) { + None => true, + Some(open) => lexemes + .get(open + 1) + .is_some_and(|first| QUERY_WORDS.iter().any(|word| first.is_word(word))), + } +} + +fn enclosing_group(lexemes: &[Lexeme<'_>], index: usize) -> Option { + let mut depth = 0_usize; + for (at, lexeme) in lexemes.get(..index)?.iter().enumerate().rev() { + match lexeme.kind { + ScriptTokenKind::CloseParen => depth += 1, + ScriptTokenKind::OpenParen if depth == 0 => return Some(at), + ScriptTokenKind::OpenParen => depth -= 1, + _ => {} + } + } + None +} + +// The `(` still open at the end of `lexemes`, outermost first. +fn open_groups(lexemes: &[Lexeme<'_>]) -> Vec { + let mut open = Vec::new(); + for (at, lexeme) in lexemes.iter().enumerate() { + match lexeme.kind { + ScriptTokenKind::OpenParen => open.push(at), + ScriptTokenKind::CloseParen => { + open.pop(); + } + _ => {} + } + } + open +} + +// Whether a comma continues the table list of a FROM clause. +fn in_from_list(rest: &[Lexeme<'_>], grammar: SqlGrammar) -> bool { + for (index, lexeme) in rest.iter().enumerate().rev() { + if lexeme.is_word("FROM") { + return from_names_tables(rest, index); + } + let part_of_list = lexeme.is_punct(",") + || lexeme.is_punct(".") + || lexeme.is_word("AS") + || (lexeme.identifier().is_some() && !lexeme.is_keyword(grammar)); + if !part_of_list { + return false; + } + } + false +} + +// Every table `text` names, in the order its statements name them. +pub fn tables_named(text: &str, grammar: SqlGrammar, settings: LexicalSettings) -> Vec { + let plan = ScriptPlan::build(text, grammar, settings); + let mut tables: Vec = Vec::new(); + for statement in plan.statements() { + let lexemes: Vec> = plan + .tokens(statement.range.clone()) + .filter(|token| !token.kind.is_trivia() && token.kind != ScriptTokenKind::ExecutableMarker) + .filter_map(|token| Lexeme::new(text, token)) + .collect(); + for table in TableMap::collect(&lexemes, grammar).in_order() { + if !tables.contains(&table) { + tables.push(table); + } + } + } + tables +} + +// The tables a statement names, under their aliases and their own names. +#[derive(Debug, Default)] +struct TableMap { + entries: Vec, +} + +// `group` is the `(` the reference sits in, None at the statement's top, so a subquery's alias shadows an outer one. +#[derive(Debug)] +struct TableEntry { + key: String, + table: TableRef, + group: Option, +} + +impl TableMap { + // Every relation word starts a reference, those in a subquery too, so a caret in the subquery sees its tables. + fn collect(lexemes: &[Lexeme<'_>], grammar: SqlGrammar) -> Self { + let mut map = Self::default(); + for index in 0..lexemes.len() { + let Some(keyword) = relation_word(lexemes, index) else { + continue; + }; + let mut at = index + 1; + loop { + if lexemes.get(at).is_some_and(|next| next.is_word("ONLY")) { + at += 1; + } + let table = if lexemes.get(at).is_some_and(Lexeme::opens_group) { + at = after_group(lexemes, at); + None + } else { + let Some((table, next)) = table_ref_at(lexemes, at, grammar) else { + break; + }; + at = next; + // A set-returning call in FROM is not a table, and its arguments are no alias. + let called = lexemes.get(at).is_some_and(Lexeme::opens_group); + if called && matches!(keyword, "FROM" | "JOIN") { + at = after_group(lexemes, at); + None + } else { + Some(table) + } + }; + let (alias, next) = alias_at(lexemes, at, grammar); + at = next; + if let Some(table) = table { + map.insert(table, alias, enclosing_group(lexemes, index)); + } + if keyword == "FROM" && lexemes.get(at).is_some_and(|next| next.is_punct(",")) { + at += 1; + continue; + } + break; + } + } + map + } + + fn insert(&mut self, table: TableRef, alias: Option, group: Option) { + if let Some(alias) = alias { + self.entries.push(TableEntry { + key: alias, + table: table.clone(), + group, + }); + } + self.entries.push(TableEntry { + key: table.name.clone(), + table, + group, + }); + } + + // As written first, then ignoring case, the way an unquoted name is read, from the innermost query around the + // caret that names it. `open` holds the `(` around the caret, outermost first. + fn lookup(&self, name: &str, open: &[usize]) -> Option<&TableRef> { + let folded = name.to_lowercase(); + self.nearest(open, |key| key == name) + .or_else(|| self.nearest(open, |key| key.to_lowercase() == folded)) + // A reference from a query the caret is not in, as in text still being written. + .or_else(|| { + self.entries + .iter() + .find(|entry| entry.key.to_lowercase() == folded) + .map(|entry| &entry.table) + }) + } + + fn nearest(&self, open: &[usize], named: impl Fn(&str) -> bool) -> Option<&TableRef> { + let mut found: Option<(usize, &TableRef)> = None; + for entry in self.entries.iter().filter(|entry| named(&entry.key)) { + let depth = match entry.group { + None => 0, + Some(group) => match open.iter().position(|&at| at == group) { + Some(at) => at + 1, + None => continue, + }, + }; + if found.is_none_or(|(best, _)| depth > best) { + found = Some((depth, &entry.table)); + } + } + found.map(|(_, table)| table) + } + + fn general(&self) -> CompletionScope { + CompletionScope::General { + tables_in_statement: self.in_order(), + } + } + + fn in_order(&self) -> Vec { + let mut tables: Vec = Vec::new(); + for entry in &self.entries { + if !tables.contains(&entry.table) { + tables.push(entry.table.clone()); + } + } + tables + } +} + +// `name`, `schema.name` or `catalog.schema.name` at `index`, and the index after it. +fn table_ref_at(lexemes: &[Lexeme<'_>], index: usize, grammar: SqlGrammar) -> Option<(TableRef, usize)> { + let mut parts = Vec::new(); + let mut at = index; + loop { + let lexeme = lexemes.get(at)?; + // A table may take a word the grammar lists for completion, such as `count`, but not a reserved one. + if lexeme.is_reserved(grammar) { + break; + } + parts.push(lexeme.identifier()?); + at += 1; + match lexemes.get(at) { + Some(dot) if dot.is_punct(".") => at += 1, + _ => break, + } + } + let name = parts.pop()?; + Some((TableRef::new(parts.pop(), name), at)) +} + +fn after_group(lexemes: &[Lexeme<'_>], index: usize) -> usize { + let mut depth = 0_usize; + for (at, lexeme) in lexemes.iter().enumerate().skip(index) { + match lexeme.kind { + ScriptTokenKind::OpenParen => depth += 1, + ScriptTokenKind::CloseParen => { + depth = depth.saturating_sub(1); + if depth == 0 { + return at + 1; + } + } + _ => {} + } + } + lexemes.len() +} + +fn alias_at(lexemes: &[Lexeme<'_>], index: usize, grammar: SqlGrammar) -> (Option, usize) { + let Some(lexeme) = lexemes.get(index) else { + return (None, index); + }; + if lexeme.is_word("AS") { + let alias = lexemes.get(index + 1).and_then(Lexeme::identifier); + return (alias, index + 2); + } + match lexeme.identifier() { + Some(alias) if !lexeme.is_keyword(grammar) => (Some(alias), index + 1), + _ => (None, index), + } +} + +// The statement the caret is in or just after, unless a terminator already ended it. +fn statement_around(plan: &ScriptPlan, cursor: usize) -> Option> { + let statement = plan.statement_at(cursor)?; + if cursor <= statement.range.end { + return Some(statement.range.clone()); + } + let ended = plan + .tokens(statement.range.end..cursor) + .any(|token| matches!(token.kind, ScriptTokenKind::Semicolon | ScriptTokenKind::Directive)); + (!ended).then_some(statement.range.start..cursor) +} + +// A line comment and an unclosed construct run to their end, so the end counts as inside; a closed quote ends before +// its last character; a number holds the caret up to and just after its decimal point. +fn encloses(token: &ScriptToken, text: &str, cursor: usize) -> bool { + let inside = token.range.start < cursor && cursor < token.range.end; + let at_open_end = token.range.start < cursor && cursor == token.range.end; + match token.kind { + ScriptTokenKind::LineComment | ScriptTokenKind::Unterminated | ScriptTokenKind::Directive => { + inside || at_open_end + } + ScriptTokenKind::Number => { + inside + || (at_open_end + && text + .get(token.range.clone()) + .is_some_and(|number| number.ends_with('.'))) + } + ScriptTokenKind::StringLiteral + | ScriptTokenKind::QuotedIdentifier + | ScriptTokenKind::DollarQuoted + | ScriptTokenKind::BlockComment + | ScriptTokenKind::ExecutableMarker => inside, + _ => false, + } +} + +fn floor_char_boundary(text: &str, byte: usize) -> usize { + let mut byte = byte.min(text.len()); + while !text.is_char_boundary(byte) { + byte -= 1; + } + byte +} + +#[derive(Debug)] +struct Lexeme<'a> { + kind: ScriptTokenKind, + text: &'a str, + end: usize, +} + +impl<'a> Lexeme<'a> { + fn new(source: &'a str, token: ScriptToken) -> Option { + Some(Self { + kind: token.kind, + text: source.get(token.range.clone())?, + end: token.range.end, + }) + } + + fn is_word(&self, word: &str) -> bool { + self.kind == ScriptTokenKind::Word && self.text.eq_ignore_ascii_case(word) + } + + fn is_punct(&self, punct: &str) -> bool { + self.kind == ScriptTokenKind::Punct && self.text == punct + } + + fn opens_group(&self) -> bool { + self.kind == ScriptTokenKind::OpenParen + } + + fn is_keyword(&self, grammar: SqlGrammar) -> bool { + self.kind == ScriptTokenKind::Word + && (grammar.keywords().any(|keyword| self.is_word(keyword)) + || CLAUSE_WORDS.iter().any(|keyword| self.is_word(keyword))) + } + + fn is_reserved(&self, grammar: SqlGrammar) -> bool { + self.kind == ScriptTokenKind::Word + && (grammar.is_reserved(self.text) || CLAUSE_WORDS.iter().any(|keyword| self.is_word(keyword))) + } + + fn identifier(&self) -> Option { + match self.kind { + ScriptTokenKind::Word => Some(self.text.to_owned()), + ScriptTokenKind::QuotedIdentifier => Some(unquote(self.text)), + _ => None, + } + } +} + +fn unquote(text: &str) -> String { + let close = match text.chars().next() { + Some('"') => '"', + Some('`') => '`', + Some('[') => ']', + _ => return text.to_owned(), + }; + let inner = text + .get(1..text.len().saturating_sub(close.len_utf8())) + .unwrap_or_default(); + inner.replace(&format!("{close}{close}"), &close.to_string()) +} + +#[cfg(test)] +mod tests { + use super::*; + + // The scope at the `|` in `marked`. + fn scope_in(grammar: SqlGrammar, marked: &str) -> Option { + let cursor = marked.find('|').expect("a cursor mark"); + let text = marked.replacen('|', "", 1); + analyze(&text, cursor, grammar, LexicalSettings::default_for(grammar)) + } + + fn scope(marked: &str) -> Option { + scope_in(SqlGrammar::PostgreSql, marked) + } + + fn table(schema: Option<&str>, name: &str) -> TableRef { + TableRef::new(schema.map(str::to_owned), name) + } + + fn columns(tables: Vec) -> Option { + Some(CompletionScope::Columns { tables }) + } + + fn general(tables: Vec) -> Option { + Some(CompletionScope::General { + tables_in_statement: tables, + }) + } + + const OBJECTS: Option = Some(CompletionScope::Objects { schema: None }); + + #[test] + fn every_statements_tables_are_named_once_in_order() { + let text = "SELECT * FROM orders o JOIN customers c ON c.id = o.customer_id;\nUPDATE sales.orders SET x = 1;\nSELECT 1 FROM orders;"; + + let tables = tables_named( + text, + SqlGrammar::PostgreSql, + LexicalSettings::default_for(SqlGrammar::PostgreSql), + ); + + assert_eq!( + tables, + [ + table(None, "orders"), + table(None, "customers"), + table(Some("sales"), "orders") + ] + ); + } + + #[test] + fn after_from_suggests_objects() { + assert_eq!(scope("SELECT * FROM |"), OBJECTS); + assert_eq!(scope("SELECT * FROM us|"), OBJECTS); + assert_eq!(scope("select * from users u join |"), OBJECTS); + assert_eq!(scope("SELECT * FROM users u, |"), OBJECTS); + assert_eq!(scope("INSERT INTO |"), OBJECTS); + assert_eq!(scope("UPDATE |"), OBJECTS); + } + + #[test] + fn alias_dot_suggests_that_tables_columns() { + assert_eq!(scope("SELECT u.| FROM users u"), columns(vec![table(None, "users")])); + assert_eq!( + scope("SELECT u.na| FROM public.users AS u"), + columns(vec![table(Some("public"), "users")]) + ); + assert_eq!(scope("SELECT users.| FROM users"), columns(vec![table(None, "users")])); + assert_eq!(scope("SELECT t1.| FROM t1"), columns(vec![table(None, "t1")])); + assert_eq!( + scope_in(SqlGrammar::MsSql, "SELECT shop.dbo.users.| FROM x"), + columns(vec![table(Some("dbo"), "users")]) + ); + } + + #[test] + fn a_two_part_prefix_is_a_table_or_a_databases_schema() { + assert_eq!( + scope("SELECT public.users.| FROM x"), + Some(CompletionScope::Qualified { + qualifier: "public".to_owned(), + name: "users".to_owned() + }) + ); + assert_eq!( + scope_in(SqlGrammar::MsSql, "SELECT * FROM shop.dbo.|"), + Some(CompletionScope::Qualified { + qualifier: "shop".to_owned(), + name: "dbo".to_owned() + }) + ); + } + + #[test] + fn a_dot_after_no_name_proposes_nothing() { + assert_eq!(scope("SELECT * FROM items WHERE price > 0.|"), None); + assert_eq!(scope("SELECT * FROM items WHERE price > 0.|5"), None); + assert_eq!(scope("SELECT * FROM items WHERE price > .|"), None); + assert_eq!(scope("SELECT 'a'.|"), None); + assert_eq!(scope("SELECT (1).|"), None); + } + + #[test] + fn schema_dot_suggests_schema_objects() { + assert_eq!( + scope("SELECT * FROM sales.|"), + Some(CompletionScope::Objects { + schema: Some("sales".to_owned()) + }) + ); + assert_eq!( + scope("SELECT * FROM sales.ord|"), + Some(CompletionScope::Objects { + schema: Some("sales".to_owned()) + }) + ); + } + + #[test] + fn quoted_alias_is_unquoted() { + assert_eq!( + scope("SELECT \"Đơn\".| FROM \"Order \"\"Items\"\"\" AS \"Đơn\""), + columns(vec![table(None, "Order \"Items\"")]) + ); + assert_eq!( + scope_in(SqlGrammar::MySql, "SELECT o.| FROM `shop`.`order items` o"), + columns(vec![table(Some("shop"), "order items")]) + ); + assert_eq!( + scope_in(SqlGrammar::MsSql, "SELECT o.| FROM [dbo].[order]]s] o"), + columns(vec![table(Some("dbo"), "order]s")]) + ); + } + + #[test] + fn join_aliases_collected() { + let text = "SELECT | FROM users u JOIN sales.orders o ON o.user_id = u.id LEFT JOIN items ON true"; + assert_eq!( + scope(text), + general(vec![ + table(None, "users"), + table(Some("sales"), "orders"), + table(None, "items") + ]) + ); + assert_eq!( + scope("SELECT * FROM users u JOIN orders o ON o.| = u.id"), + columns(vec![table(None, "orders")]) + ); + assert_eq!( + scope("SELECT * FROM users U WHERE u.|"), + columns(vec![table(None, "users")]), + "an unquoted alias is matched without case" + ); + } + + #[test] + fn a_subquerys_alias_shadows_the_outer_one_and_the_outer_stays_visible() { + assert_eq!( + scope("SELECT * FROM users u WHERE EXISTS (SELECT 1 FROM orders u WHERE u.|)"), + columns(vec![table(None, "orders")]) + ); + assert_eq!( + scope("SELECT * FROM users u WHERE EXISTS (SELECT 1 FROM orders o WHERE o.user_id = u.|)"), + columns(vec![table(None, "users")]) + ); + assert_eq!( + scope("SELECT u.| FROM users u WHERE EXISTS (SELECT 1 FROM orders u)"), + columns(vec![table(None, "users")]) + ); + } + + #[test] + fn a_table_may_take_a_word_that_is_not_reserved() { + assert_eq!(scope("SELECT c.| FROM count c"), columns(vec![table(None, "count")])); + assert_eq!( + scope("SELECT * FROM where |"), + general(Vec::new()), + "a reserved word was taken for a table" + ); + } + + #[test] + fn dollar_quoted_text_is_not_analyzed() { + assert_eq!(scope("SELECT $$ FROM $$ |"), general(Vec::new())); + assert_eq!(scope("SELECT $$ FROM |$$"), None); + assert_eq!(scope("SELECT $$ FROM |"), None); + assert_eq!( + scope("CREATE FUNCTION f() RETURNS int AS $body$ SELECT * FROM | $body$ LANGUAGE sql"), + None + ); + } + + #[test] + fn a_mysql_executable_comment_is_code() { + assert_eq!( + scope_in(SqlGrammar::MySql, "SELECT * FROM orders /*! AS */ o WHERE o.|"), + columns(vec![table(None, "orders")]) + ); + assert_eq!(scope_in(SqlGrammar::MySql, "SELECT /*!40|1 x */ 1"), None); + } + + #[test] + fn strings_and_comments_are_not_completed() { + assert_eq!(scope("SELECT 'FROM |'"), None); + assert_eq!(scope("SELECT 1 -- FROM |"), None); + assert_eq!(scope("SELECT /* FROM | */ 1"), None); + assert_eq!(scope("SELECT \"us|"), None); + assert_eq!(scope("SELECT 'a' |"), general(Vec::new())); + } + + #[test] + fn a_new_statement_starts_after_the_terminator() { + assert_eq!(scope("SELECT * FROM users;\n|"), general(Vec::new())); + assert_eq!(scope("SELECT * FROM users;\nsel|"), general(Vec::new())); + assert_eq!( + scope("SELECT * FROM users u WHERE |;\nSELECT 2"), + general(vec![table(None, "users")]) + ); + } + + #[test] + fn a_comma_outside_from_is_not_a_table_list() { + assert_eq!(scope("SELECT a, | FROM users"), general(vec![table(None, "users")])); + assert_eq!( + scope("SELECT * FROM users WHERE a IN (1, |)"), + general(vec![table(None, "users")]) + ); + } + + #[test] + fn a_from_after_distinct_or_inside_a_call_names_no_table() { + let orders = || general(vec![table(None, "orders")]); + assert_eq!(scope("SELECT * FROM orders WHERE a IS DISTINCT FROM |"), orders()); + assert_eq!(scope("SELECT * FROM orders WHERE a IS NOT DISTINCT FROM |"), orders()); + assert_eq!(scope("SELECT EXTRACT(YEAR FROM |) FROM orders"), orders()); + assert_eq!(scope("SELECT SUBSTRING(name FROM |) FROM orders"), orders()); + assert_eq!(scope("SELECT TRIM(BOTH ' ' FROM |) FROM orders"), orders()); + assert_eq!(scope("SELECT TRIM(FROM |) FROM orders"), orders()); + assert_eq!( + scope("SELECT EXTRACT(YEAR FROM created_at) FROM orders WHERE |"), + orders(), + "a call's argument was taken for a table" + ); + assert_eq!(scope("SELECT * FROM orders WHERE id IN (SELECT id FROM |"), OBJECTS); + assert_eq!(scope("SELECT EXISTS(SELECT 1 FROM |"), OBJECTS); + assert_eq!(scope("WITH gone AS (DELETE FROM |"), OBJECTS); + } + + #[test] + fn a_call_or_subquery_in_from_is_not_a_table() { + assert_eq!( + scope("SELECT | FROM generate_series(1, 3) g, users"), + general(vec![table(None, "users")]) + ); + assert_eq!( + scope("SELECT | FROM (SELECT * FROM orders) o, users"), + general(vec![table(None, "users"), table(None, "orders")]) + ); + assert_eq!(scope("INSERT INTO users (|"), general(vec![table(None, "users")])); + } + + #[test] + fn a_multibyte_cursor_does_not_split_a_character() { + let text = "SELECT * FROM đơn"; + let inside_a_character = text.len() - 2; + + let scope = analyze( + text, + inside_a_character, + SqlGrammar::PostgreSql, + LexicalSettings::default_for(SqlGrammar::PostgreSql), + ); + + assert_eq!(scope, OBJECTS); + } + + #[test] + fn only_a_place_that_takes_a_name_asks_for_one_by_itself() { + assert!(scope("SELECT * FROM |").is_some_and(|scope| scope.names_a_place())); + assert!(scope("SELECT u.| FROM users u").is_some_and(|scope| scope.names_a_place())); + assert!(scope("SELECT * FROM sales.|").is_some_and(|scope| scope.names_a_place())); + assert!(!scope("SELECT |").is_some_and(|scope| scope.names_a_place())); + assert!(!scope("SELECT 1;\n|").is_some_and(|scope| scope.names_a_place())); + } +} diff --git a/core/crates/core/src/sql_syntax/completion/word.rs b/core/crates/core/src/sql_syntax/completion/word.rs new file mode 100644 index 0000000000..17415b9151 --- /dev/null +++ b/core/crates/core/src/sql_syntax/completion/word.rs @@ -0,0 +1,49 @@ +// The byte where the word ending at byte `cursor` starts: what a proposal replaces and what the list ranks for. +pub fn word_start(text: &str, cursor: usize) -> usize { + let mut cursor = cursor.min(text.len()); + while !text.is_char_boundary(cursor) { + cursor -= 1; + } + text.get(..cursor) + .unwrap_or_default() + .char_indices() + .rev() + .take_while(|(_, c)| is_word_char(*c)) + .last() + .map_or(cursor, |(at, _)| at) +} + +// A combining mark belongs to the letter before it, so a decomposed "café" is one word. +pub fn is_word_char(c: char) -> bool { + c.is_alphanumeric() || c == '_' || unicode_normalization::char::is_combining_mark(c) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_word_keeps_combining_marks_with_their_base() { + let text = "SELECT * FROM cafe\u{301}"; + + assert_eq!(word_start(text, text.len()), 14); + assert!(is_word_char('\u{903}')); + assert!(is_word_char('\u{20dd}')); + } + + #[test] + fn a_word_runs_back_over_letters_digits_and_underscores() { + assert_eq!(word_start("SELECT * FROM us", 16), 14); + assert_eq!(word_start("SELECT u.user_id2", 17), 9); + assert_eq!(word_start("SELECT ", 7), 7); + assert_eq!(word_start("", 0), 0); + } + + #[test] + fn a_word_holds_whole_characters_outside_ascii() { + let text = "SELECT * FROM đơn"; + + assert_eq!(word_start(text, text.len()), 14); + assert_eq!(word_start(text, text.len() - 2), 14, "a cursor inside a character"); + } +} diff --git a/core/crates/core/src/sql_syntax/format/mod.rs b/core/crates/core/src/sql_syntax/format/mod.rs new file mode 100644 index 0000000000..0dea3cf6f1 --- /dev/null +++ b/core/crates/core/src/sql_syntax/format/mod.rs @@ -0,0 +1,458 @@ +// Reformatting a SQL script without changing what it says. + +mod significant_tokens; + +use std::ops::Range; + +use crate::sql_syntax::SqlGrammar; +use crate::sql_syntax::script::{LexicalSettings, ScriptPlan, ScriptToken, ScriptTokenKind, copies_from_stdin}; + +use self::significant_tokens::significant_tokens; + +// sqlformat has its own lexer, which does not know every dialect: it reads `//` as an operator, ends a nested block +// comment at the first `*/`, treats a backslash as an escape inside quotes, takes `$$` for a keyword, and spaces `+=`, +// `b'01'` and `GROUP_CONCAT(`. So each statement is checked against its own tokens afterwards, and one that came back +// different is written out as typed. +pub fn format_script(text: &str, grammar: SqlGrammar, settings: LexicalSettings) -> String { + let plan = ScriptPlan::build(text, grammar, settings); + let psql = psql_lines(text, &plan, grammar); + let mut out = String::with_capacity(text.len()); + let mut cursor = 0usize; + for statement in plan.statements() { + let range = statement.range.clone(); + if range.start < cursor || range.end > text.len() { + continue; + } + // Terminators, GO and DELIMITER lines and the blank lines around them are the user's. + out.push_str(text.get(cursor..range.start).unwrap_or_default()); + let source = text.get(range.clone()).unwrap_or_default(); + if overlaps(&psql, &range) { + out.push_str(source); + } else { + out.push_str(&format_statement(source, grammar, settings)); + } + cursor = range.end; + } + out.push_str(text.get(cursor..).unwrap_or_default()); + out +} + +// The formatted script and where byte `caret` lands in it. A format changes only blanks, so the caret keeps the +// characters around it: one touching the next character stays before it, one in blanks after a character after that. +pub fn format_with_caret(text: &str, grammar: SqlGrammar, settings: LexicalSettings, caret: usize) -> (String, usize) { + let formatted = format_script(text, grammar, settings); + let caret = map_caret(text, &formatted, grammar, settings, caret); + (formatted, caret) +} + +// The selected bytes of `text` formatted, or as they are when the result would change the whole text's tokens: a +// selection can start inside a comment or a string its own text cannot show. +pub fn format_selection(text: &str, grammar: SqlGrammar, settings: LexicalSettings, range: Range) -> String { + let Some(source) = text.get(range.clone()) else { + return String::new(); + }; + let plan = ScriptPlan::build(text, grammar, settings); + if overlaps(&psql_lines(text, &plan, grammar), &range) { + return source.to_owned(); + } + let formatted = format_script(source, grammar, settings); + let mut replaced = text.to_owned(); + replaced.replace_range(range, &formatted); + if reads_the_same(&replaced, text, grammar, settings) { + formatted + } else { + source.to_owned() + } +} + +// What psql reads as no SQL on PostgreSQL: a `COPY ... FROM stdin`'s rows up to its `\.` line, where a tab or a line +// break is data, and backslash command lines such as pg_dump's `\restrict`. The script lexer reads both as statements. +fn psql_lines(text: &str, plan: &ScriptPlan, grammar: SqlGrammar) -> Vec> { + if grammar != SqlGrammar::PostgreSql { + return Vec::new(); + } + let mut lines = Vec::new(); + let mut line_start = 0; + for line in text.split_inclusive('\n') { + if line.trim_start_matches([' ', '\t']).starts_with('\\') { + lines.push(line_start..line_start + line.len()); + } + line_start += line.len(); + } + for statement in plan.statements() { + let tokens: Vec = plan.tokens(statement.range.clone()).collect(); + if copies_from_stdin(text, &tokens) { + lines.push(statement.range.end..copy_rows_end(text, statement.range.end)); + } + } + lines +} + +// psql reads the rows from the line after the COPY's, up to a `\.` line or the end of the text. +fn copy_rows_end(text: &str, copy_end: usize) -> usize { + let Some(newline) = text.get(copy_end..).and_then(|rest| rest.find('\n')) else { + return text.len(); + }; + let mut at = copy_end + newline + 1; + for line in text.get(at..).unwrap_or_default().split_inclusive('\n') { + at += line.len(); + if line.trim_end_matches(['\n', '\r']) == "\\." { + return at; + } + } + text.len() +} + +// A session's sql_mode or standard_conforming_strings decides whether a backslash escapes in a string, and the editor +// does not know which the server has, so a format keeps the tokens both ways. +fn reads_the_same(changed: &str, original: &str, grammar: SqlGrammar, settings: LexicalSettings) -> bool { + let other = LexicalSettings { + backslash_escapes: !settings.backslash_escapes, + }; + [settings, other].into_iter().all(|settings| { + significant_tokens(changed, grammar, settings) == significant_tokens(original, grammar, settings) + }) +} + +fn overlaps(lines: &[Range], range: &Range) -> bool { + lines + .iter() + .any(|line| line.start < range.end && range.start < line.end) +} + +fn format_statement(source: &str, grammar: SqlGrammar, settings: LexicalSettings) -> String { + if source.trim().is_empty() { + return source.to_owned(); + } + let options = sqlformat::FormatOptions { + indent: sqlformat::Indent::Spaces(4), + // sqlformat's reserved list uppercases STATUS and LEVEL, ordinary identifiers on ClickHouse and case-sensitive + // table names on MySQL under Linux. + uppercase: None, + lines_between_queries: 1, + dialect: format_dialect(grammar), + ..sqlformat::FormatOptions::default() + }; + let formatted = sqlformat::format(source, &sqlformat::QueryParams::None, &options); + if reads_the_same(&formatted, source, grammar, settings) { + formatted + } else { + source.to_owned() + } +} + +fn format_dialect(grammar: SqlGrammar) -> sqlformat::Dialect { + match grammar { + SqlGrammar::PostgreSql => sqlformat::Dialect::PostgreSql, + SqlGrammar::MsSql => sqlformat::Dialect::SQLServer, + _ => sqlformat::Dialect::Generic, + } +} + +fn map_caret(text: &str, formatted: &str, grammar: SqlGrammar, settings: LexicalSettings, caret: usize) -> usize { + let original = ScriptPlan::build(text, grammar, settings); + let changed = ScriptPlan::build(formatted, grammar, settings); + let tokens = original + .tokens(0..text.len()) + .filter(|token| token.kind != ScriptTokenKind::Whitespace); + let mapped = changed + .tokens(0..formatted.len()) + .filter(|token| token.kind != ScriptTokenKind::Whitespace); + for (token, target) in tokens.zip(mapped) { + // Spaces inside a token are its contents, not the formatter's indentation. + if token.range.contains(&caret) { + return target.range.start + (caret - token.range.start).min(target.range.len()); + } + } + let before = text.get(..caret.min(text.len())).unwrap_or(text); + let kept = before.chars().filter(|c| !c.is_whitespace()).count(); + if kept == 0 { + // The blanks before the first statement are kept as they are. + let first = formatted.find(|c: char| !c.is_whitespace()).unwrap_or(formatted.len()); + return before.len().min(first); + } + let touches_next = text + .get(before.len()..) + .and_then(|rest| rest.chars().next()) + .is_none_or(|next| !next.is_whitespace()); + let mut seen = 0usize; + for (at, c) in formatted.char_indices() { + if c.is_whitespace() { + continue; + } + if touches_next && seen == kept { + return at; + } + seen += 1; + if !touches_next && seen == kept { + return at + c.len_utf8(); + } + } + formatted.len() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn format(text: &str, grammar: SqlGrammar) -> String { + format_script(text, grammar, LexicalSettings::default_for(grammar)) + } + + fn format_at(marked: &str, grammar: SqlGrammar) -> String { + let caret = marked.find('|').expect("a caret mark"); + let text = marked.replacen('|', "", 1); + let (mut out, at) = format_with_caret(&text, grammar, LexicalSettings::default_for(grammar), caret); + out.insert(at, '|'); + out + } + + fn select(source: &str, selection: &str) -> String { + let start = source.find(selection).expect("the selected text"); + format_selection( + source, + SqlGrammar::PostgreSql, + LexicalSettings::default_for(SqlGrammar::PostgreSql), + start..start + selection.len(), + ) + } + + #[test] + fn plain_select_is_reflowed_case_preserved() { + let out = format("select a, b from t where a = 1", SqlGrammar::PostgreSql); + + assert!(out.contains('\n'), "the statement was not reflowed: {out}"); + assert!(out.contains("select"), "the keyword case was changed: {out}"); + assert!(!out.contains("SELECT"), "the keyword case was changed: {out}"); + } + + #[test] + fn format_is_idempotent_for_plain_statements() { + let once = format("select a, b from t where a = 1", SqlGrammar::PostgreSql); + let twice = format(&once, SqlGrammar::PostgreSql); + + assert_eq!(once, twice); + } + + #[test] + fn dollar_tag_body_is_untouched() { + let source = "CREATE FUNCTION f() RETURNS int AS $body$ SELECT 1; $body$ LANGUAGE sql"; + + let out = format(source, SqlGrammar::PostgreSql); + + assert!( + out.contains("$body$ SELECT 1; $body$"), + "the body was rewritten: {out}" + ); + } + + #[test] + fn a_comment_that_would_swallow_the_code_after_it_keeps_the_statement_verbatim() { + // sqlformat has no `//` comment, so it pulls the comma onto the comment's line, where ClickHouse never sees it. + let source = "SELECT a // x\n, b FROM t"; + + assert_eq!(format(source, SqlGrammar::ClickHouse), source); + } + + #[test] + fn a_slash_comment_at_the_end_of_a_statement_still_ends_it() { + let out = format("SELECT 1 // note", SqlGrammar::ClickHouse); + + assert!(out.ends_with("// note"), "the comment stopped ending the line: {out}"); + } + + #[test] + fn nested_block_comment_kept_verbatim() { + let source = "SELECT /* outer /* inner */ still outer */ 1"; + + assert_eq!(format(source, SqlGrammar::PostgreSql), source); + } + + #[test] + fn standard_conforming_backslash_string_kept_verbatim() { + let source = r"SELECT 'C:\' AS a, 'x'"; + + assert_eq!(format(source, SqlGrammar::PostgreSql), source); + } + + #[test] + fn mysql_minus_minus_arithmetic_stays_arithmetic() { + // MySQL needs a space after `--` before it is a comment, so a space there would comment out the rest. + let out = format("SELECT 1--1", SqlGrammar::MySql); + + assert!(out.contains("--1"), "the operator was broken up: {out}"); + assert!(!out.contains("-- 1"), "the arithmetic became a comment: {out}"); + } + + #[test] + fn go_lines_preserved() { + let source = "select 1\nGO\nselect 2\nGO\n"; + + let out = format(source, SqlGrammar::MsSql); + + assert_eq!(out.matches("GO").count(), 2, "a batch separator was lost: {out}"); + assert!(out.ends_with("GO\n"), "the trailing separator moved: {out}"); + } + + #[test] + fn delimiter_procedure_preserved() { + let source = "DELIMITER $$\nCREATE PROCEDURE p() BEGIN SELECT 1; END$$\nDELIMITER ;\n"; + + let out = format(source, SqlGrammar::MySql); + + assert!(out.contains("DELIMITER $$"), "the delimiter line was lost: {out}"); + assert!(out.contains("DELIMITER ;"), "the delimiter reset was lost: {out}"); + } + + #[test] + fn statements_sqlformat_would_respace_into_something_else_kept_verbatim() { + let cases = [ + // `+=` would become `+ =`. + ("SET @x += 1", SqlGrammar::MsSql), + // A bit literal and a charset introducer would become a column with a string alias. + ("select b'01', _utf8mb4'x' from t", SqlGrammar::MySql), + // `@@ version` is not a system variable. + ("select @@version", SqlGrammar::MySql), + // `U & '…'` is a bitwise and, not a Unicode escape string. + (r"select U&'d\0061t'", SqlGrammar::PostgreSql), + // A string continued on the next line is one string; on one line the two are a syntax error. + ("select 'a'\n'b'", SqlGrammar::PostgreSql), + // A query parameter's name, colon and type are one unit. + ("SELECT {id:UInt32}", SqlGrammar::ClickHouse), + ]; + + for (source, grammar) in cases { + assert_eq!(format(source, grammar), source, "{grammar:?}"); + } + } + + #[test] + fn a_mysql_call_keeps_its_name_against_its_paren() { + // sqlformat writes `GROUP_CONCAT (a)`, which MySQL reads as a stored function of that name. + let out = format("SELECT GROUP_CONCAT(a) FROM t", SqlGrammar::MySql); + + assert!( + out.contains("GROUP_CONCAT(a)"), + "the call was split from its paren: {out}" + ); + } + + #[test] + fn mysql_statements_with_untouched_calls_are_still_formatted() { + for source in [ + "select count(*), max(a) from t", + "insert into t (a, b) values (1, 2)", + "select ifnull(a, 0) from t where a in(1, 2)", + ] { + let out = format(source, SqlGrammar::MySql); + + assert!(out.contains('\n'), "the statement was not reflowed: {out}"); + } + } + + #[test] + fn a_string_keeps_its_spaces_however_the_server_reads_a_backslash() { + // Under NO_BACKSLASH_ESCAPES the first string ends at its backslash and `' foo '` is a string of its own. + let out = format("SELECT 'a\\' AS x, ' foo '", SqlGrammar::MySql); + + assert!(out.contains("' foo '"), "a string's spaces changed: {out}"); + } + + #[test] + fn copy_rows_and_backslash_lines_stay_as_psql_reads_them() { + let rows = "1\tAda;x\n2\tGrace Hopper\n\\.\n"; + let source = format!("copy t (a, b) from stdin;\n{rows}select a,b from t;\n\\restrict key\nselect 1;\n"); + + let out = format(&source, SqlGrammar::PostgreSql); + + assert!(out.contains(&format!(";\n{rows}")), "the rows changed: {out}"); + assert!( + out.contains("\n\\restrict key\nselect 1;"), + "the backslash line changed: {out}" + ); + } + + #[test] + fn copy_rows_with_no_end_marker_run_to_the_end() { + let out = format("copy t from stdin;\nselect 1,2\n", SqlGrammar::PostgreSql); + + assert!(out.ends_with(";\nselect 1,2\n"), "rows were formatted as SQL: {out}"); + } + + #[test] + fn a_selection_in_copy_rows_is_kept_as_it_is() { + let rows = "1\tAda Lovelace\n2\tGrace"; + let source = format!("copy t from stdin;\n{rows}\n\\.\n"); + + assert_eq!(select(&source, rows), rows); + } + + #[test] + fn an_empty_script_stays_empty() { + assert_eq!(format("", SqlGrammar::PostgreSql), ""); + assert_eq!(format(" \n\n", SqlGrammar::PostgreSql), " \n\n"); + } + + #[test] + fn the_caret_keeps_the_characters_around_it() { + assert_eq!( + format_at("select a, b from t where a = 1|", SqlGrammar::PostgreSql), + format("select a, b from t where a = 1", SqlGrammar::PostgreSql) + "|" + ); + let touching = format_at("select a, |b from t", SqlGrammar::PostgreSql); + assert!(touching.contains("|b"), "the caret left the name it was at: {touching}"); + let inside = format_at("select a, bc|d from t", SqlGrammar::PostgreSql); + assert!( + inside.contains("bc|d"), + "the caret left its place in the name: {inside}" + ); + let after = format_at("select a,| b from t", SqlGrammar::PostgreSql); + assert!(after.contains("a,|"), "the caret left the comma it followed: {after}"); + assert_eq!(format_at("|select 1", SqlGrammar::PostgreSql).find('|'), Some(0)); + } + + #[test] + fn the_caret_stays_put_in_a_statement_kept_as_typed() { + let source = "SELECT a // x\n, b| FROM t"; + + assert_eq!(format_at(source, SqlGrammar::ClickHouse), source); + } + + #[test] + fn the_caret_in_blanks_at_the_start_stays_at_the_start() { + assert_eq!(format_at(" | ", SqlGrammar::PostgreSql), " | "); + assert_eq!(format_at("|", SqlGrammar::PostgreSql), "|"); + } + + #[test] + fn the_caret_keeps_spaces_inside_preserved_tokens() { + for token in ["'a | b'", "\"a | b\"", "/* a | b */", "'😀 | b'"] { + let source = format!("select {token}, c from t"); + let out = format_at(&source, SqlGrammar::PostgreSql); + assert!(out.contains(token), "the caret moved inside a token: {out}"); + } + } + + #[test] + fn a_selection_inside_a_comment_string_or_body_is_kept_as_it_is() { + let selection = "select a,b from t"; + for source in [ + format!("-- {selection}"), + format!("select '{selection}'"), + format!("/* {selection} */"), + format!("select $body${selection}$body$"), + ] { + assert_eq!(select(&source, selection), selection); + } + } + + #[test] + fn a_selected_statement_is_reflowed_alone() { + let selection = "select a,b from t"; + + let out = select(&format!("select 1; {selection}; select 2"), selection); + + assert_eq!(out, format(selection, SqlGrammar::PostgreSql)); + assert!(out.contains('\n')); + } +} diff --git a/core/crates/core/src/sql_syntax/format/significant_tokens.rs b/core/crates/core/src/sql_syntax/format/significant_tokens.rs new file mode 100644 index 0000000000..cd6bd1f7c5 --- /dev/null +++ b/core/crates/core/src/sql_syntax/format/significant_tokens.rs @@ -0,0 +1,233 @@ +use crate::sql_syntax::SqlGrammar; +use crate::sql_syntax::script::{LexicalSettings, ScriptPlan, ScriptTokenKind}; + +// Two texts with the same list are one statement spaced differently, the only change a formatter may make. +pub fn significant_tokens(text: &str, grammar: SqlGrammar, settings: LexicalSettings) -> Vec { + let plan = ScriptPlan::build(text, grammar, settings); + let mut tokens: Vec = Vec::new(); + let mut spacing = Spacing::default(); + // A MySQL executable comment is compared as written: the formatter takes it for a comment, and which of its text + // is a string depends on the server. + let mut executable: Option = None; + for token in plan.tokens(0..text.len()) { + let slice = text.get(token.range.clone()).unwrap_or_default(); + if token.kind == ScriptTokenKind::ExecutableMarker || executable.is_some() { + let start = *executable.get_or_insert(token.range.start); + if token.kind == ScriptTokenKind::ExecutableMarker && slice == "*/" { + executable = None; + push( + &mut tokens, + &mut spacing, + grammar, + token.kind, + text.get(start..token.range.end).unwrap_or_default(), + ); + } + continue; + } + if token.kind == ScriptTokenKind::Whitespace { + spacing.spaced = true; + spacing.line_break |= slice.contains('\n'); + continue; + } + let body = match token.kind { + // A formatter may move the spaces at the end of a comment without changing what it says. + ScriptTokenKind::LineComment | ScriptTokenKind::BlockComment => slice.trim_end(), + _ => slice, + }; + push(&mut tokens, &mut spacing, grammar, token.kind, body); + } + if let Some(start) = executable { + let rest = text.get(start..).unwrap_or_default(); + push( + &mut tokens, + &mut spacing, + grammar, + ScriptTokenKind::ExecutableMarker, + rest, + ); + } + tokens +} + +fn push( + tokens: &mut Vec, + spacing: &mut Spacing, + grammar: SqlGrammar, + kind: ScriptTokenKind, + body: &str, +) { + let gap = tokens.last().map_or(Gap::Free, |previous| { + gap_between(grammar, previous, kind, body, *spacing) + }); + tokens.push(SignificantToken { + kind, + text: body.to_owned(), + gap, + }); + *spacing = Spacing::default(); +} + +#[derive(Debug, Clone, PartialEq, Eq)] +pub struct SignificantToken { + kind: ScriptTokenKind, + text: String, + gap: Gap, +} + +// Kept only where an engine reads two tokens differently with and without a space: the lexer reads `+=` and `->>` +// one character at a time, and `b'01'` as a word and a string, where MySQL reads one bit literal. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +enum Gap { + Free, + Touching, + Spaced, + // Two string literals with a line break between are one string in PostgreSQL; on one line, a syntax error. + LineBreak, +} + +// Words sqlformat spaces from their paren that MySQL 8.4 and MariaDB 11.4 were checked to read the same either way. +const MYSQL_SPACED_CALLS: [&str; 7] = ["EXISTS", "IF", "IFNULL", "IN", "REPLACE", "USING", "VALUES"]; + +#[derive(Debug, Clone, Copy, Default)] +struct Spacing { + spaced: bool, + line_break: bool, +} + +fn gap_between( + grammar: SqlGrammar, + previous: &SignificantToken, + kind: ScriptTokenKind, + text: &str, + spacing: Spacing, +) -> Gap { + let strings = previous.kind == ScriptTokenKind::StringLiteral && kind == ScriptTokenKind::StringLiteral; + if strings && spacing.line_break { + return Gap::LineBreak; + } + let operator = previous.kind == ScriptTokenKind::Punct + && kind == ScriptTokenKind::Punct + && previous.text != "," + && text != ","; + // Without IGNORE_SPACE, MySQL reads `count(` as the built-in and `count (` as a stored function named count. + let call = grammar == SqlGrammar::MySql + && previous.kind == ScriptTokenKind::Word + && kind == ScriptTokenKind::OpenParen + && !MYSQL_SPACED_CALLS + .iter() + .any(|word| word.eq_ignore_ascii_case(&previous.text)); + let free = spacing_is_free(previous.kind, &previous.text) || spacing_is_free(kind, text); + if !strings && !operator && !call && free { + return Gap::Free; + } + if spacing.spaced { Gap::Spaced } else { Gap::Touching } +} + +// A bracket, a separator, a comment, or an operator character that is never part of a name, literal or parameter. +fn spacing_is_free(kind: ScriptTokenKind, text: &str) -> bool { + match kind { + ScriptTokenKind::OpenParen + | ScriptTokenKind::CloseParen + | ScriptTokenKind::Semicolon + | ScriptTokenKind::LineComment + | ScriptTokenKind::BlockComment + | ScriptTokenKind::ExecutableMarker => true, + ScriptTokenKind::Punct => matches!( + text, + "," | "." | "=" | "<" | ">" | "+" | "-" | "*" | "/" | "%" | "^" | "|" | "!" | "~" | "[" | "]" + ), + _ => false, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn tokens_for(text: &str, grammar: SqlGrammar) -> Vec { + significant_tokens(text, grammar, LexicalSettings::default_for(grammar)) + } + + fn tokens(text: &str) -> Vec { + tokens_for(text, SqlGrammar::PostgreSql) + } + + #[test] + fn a_mysql_executable_comment_must_stay_as_written() { + // Under NO_BACKSLASH_ESCAPES the indented line is inside a string. + let source = "SELECT 1 /*! , 'a\\' AS x, '\n foo\n' */"; + let reindented = "SELECT 1 /*! , 'a\\' AS x, '\n foo\n' */"; + + assert_ne!( + tokens_for(source, SqlGrammar::MySql), + tokens_for(reindented, SqlGrammar::MySql) + ); + assert_eq!( + tokens_for(source, SqlGrammar::MySql), + tokens_for(&source.replace("1 /*!", "1\n/*!"), SqlGrammar::MySql) + ); + } + + #[test] + fn spacing_alone_does_not_change_a_statement() { + assert_eq!(tokens("select a ,b from t"), tokens("select a, b\nfrom t")); + assert_eq!(tokens("select a=-1,(1,-1)"), tokens("select a =- 1, (1, -1)")); + } + + #[test] + fn a_changed_word_changes_the_statement() { + assert_ne!(tokens("select a from t"), tokens("SELECT a from t")); + } + + #[test] + fn a_comment_keeps_its_text_but_not_its_trailing_spaces() { + assert_eq!(tokens("select 1 -- note "), tokens("select 1 -- note")); + assert_ne!(tokens("select 1 -- note"), tokens("select 1 -- other")); + } + + #[test] + fn an_operator_split_by_a_space_changes_the_statement() { + assert_ne!( + tokens_for("SET @x += 1", SqlGrammar::MsSql), + tokens_for("SET @x + = 1", SqlGrammar::MsSql) + ); + assert_ne!(tokens("select a->>'k'"), tokens("select a- >>'k'")); + assert_eq!(tokens("select a->>'k'"), tokens("select a ->> 'k'")); + } + + #[test] + fn a_prefix_split_from_its_string_changes_the_statement() { + assert_ne!( + tokens_for("select b'01'", SqlGrammar::MySql), + tokens_for("select b '01'", SqlGrammar::MySql) + ); + assert_ne!( + tokens_for("select @@version", SqlGrammar::MySql), + tokens_for("select @@ version", SqlGrammar::MySql) + ); + } + + #[test] + fn a_space_before_a_mysql_call_changes_the_statement() { + assert_ne!( + tokens_for("select count(a) from t", SqlGrammar::MySql), + tokens_for("select count (a) from t", SqlGrammar::MySql) + ); + assert_eq!( + tokens_for("select count(a) from t", SqlGrammar::MySql), + tokens_for("select\n count(a)\nfrom t", SqlGrammar::MySql) + ); + assert_eq!(tokens("select count(a) from t"), tokens("select count (a) from t")); + assert_eq!( + tokens_for("select ifnull(a, 0) from t where a in(1)", SqlGrammar::MySql), + tokens_for("select ifnull (a, 0) from t where a in (1)", SqlGrammar::MySql) + ); + } + + #[test] + fn strings_joined_onto_one_line_change_the_statement() { + assert_ne!(tokens("select 'a'\n'b'"), tokens("select 'a' 'b'")); + assert_eq!(tokens("select 'a'\n'b'"), tokens("select\n 'a'\n 'b'")); + } +} diff --git a/core/crates/core/src/sql_syntax/mod.rs b/core/crates/core/src/sql_syntax/mod.rs index 6095b99886..31a7c7ca3c 100644 --- a/core/crates/core/src/sql_syntax/mod.rs +++ b/core/crates/core/src/sql_syntax/mod.rs @@ -1,8 +1,10 @@ mod builtin_snippet; mod catalog_change; +pub mod completion; mod created_object; mod credential_rules; mod errors; +pub mod format; mod grammar; mod highlight; mod history_recording; diff --git a/core/crates/core/src/sql_syntax/script/copy_rows.rs b/core/crates/core/src/sql_syntax/script/copy_rows.rs index bb4364a016..50b0b4c5b5 100644 --- a/core/crates/core/src/sql_syntax/script/copy_rows.rs +++ b/core/crates/core/src/sql_syntax/script/copy_rows.rs @@ -11,6 +11,11 @@ pub(super) const COPY_PIECE_BYTES: usize = 256 * 1024; /// PostgreSQL 18 reads it as data in CSV. const END_MARKER: &str = "\\."; +// The formatter keeps these rows as psql reads them, by the reader's own rule. +pub(crate) fn copies_from_stdin(text: &str, tokens: &[ScriptToken]) -> bool { + CopyRows::starting(text, tokens).is_some() +} + /// The rows of a `COPY ... FROM stdin` block, passed on as the file holds /// them. A piece is cut where the rows reach `COPY_PIECE_BYTES`, not where /// a read of the file ended, so a file splits the same however it is read. diff --git a/core/crates/core/src/sql_syntax/script/mod.rs b/core/crates/core/src/sql_syntax/script/mod.rs index 753aa1a703..a241eaa414 100644 --- a/core/crates/core/src/sql_syntax/script/mod.rs +++ b/core/crates/core/src/sql_syntax/script/mod.rs @@ -19,6 +19,7 @@ pub mod text_offsets; pub mod unicode_spaces; pub use batch_error_policy::BatchErrorPolicy; +pub(crate) use copy_rows::copies_from_stdin; pub use lexical_settings::LexicalSettings; pub use open_construct::OpenConstruct; pub use script_batch::ScriptBatch; diff --git a/core/crates/ffi/src/completion.rs b/core/crates/ffi/src/completion.rs new file mode 100644 index 0000000000..de30792d3f --- /dev/null +++ b/core/crates/ffi/src/completion.rs @@ -0,0 +1,653 @@ +use std::collections::HashSet; +use std::sync::Arc; + +use tablepro_core::dialect::{DatabaseLayout, SqlDialect}; +use tablepro_core::meta::{ObjectInfo, ObjectKind, TableRef}; +use tablepro_core::sql_syntax::SqlGrammar; +use tablepro_core::sql_syntax::completion::{ + CatalogAnswer, CatalogHome, CompletionScope, Ranked, Ranking, analyze, casefold, matched_runs, word_start, +}; +use tablepro_core::sql_syntax::format::{format_selection, format_with_caret}; +use tablepro_core::sql_syntax::script::LexicalSettings; + +use crate::catalog::TableName; +use crate::statement_at::byte_at_unit; +use crate::syntax::{SqlGrammarTag, Utf16Offsets}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)] +pub enum CompletionKindTag { + Keyword, + Schema, + Table, + View, + MaterializedView, + Column, + Function, +} + +// UTF-16 units of the label. +#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Record)] +pub struct MatchedSpan { + pub start: u32, + pub length: u32, +} + +// Label and insert text are cased for the word ranked: a keyword follows the case the user types keywords in. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct CompletionItem { + pub label: String, + pub insert_text: String, + pub kind: CompletionKindTag, + pub data_type: Option, + pub table: Option, + pub matched: Vec, +} + +// `total` counts every match; `items` holds the closest of them. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct CompletionPage { + pub total: u32, + pub items: Vec, +} + +// The caret counts UTF-16 units. +#[derive(Debug, Clone, PartialEq, Eq, uniffi::Record)] +pub struct FormattedSql { + pub text: String, + pub caret: u32, +} + +// What one completion offers, ranked again for each word typed without reading the catalog again. +#[derive(Debug, uniffi::Object)] +pub struct CompletionList { + candidates: Arc<[Candidate]>, + word_start: u32, + word: String, +} + +#[uniffi::export] +impl CompletionList { + // In UTF-16 units of the text the completion was asked for. + pub fn word_start(&self) -> u32 { + self.word_start + } + + pub fn word(&self) -> String { + self.word.clone() + } + + // The closest `limit` matches for `word`, and how many matched in all. + pub fn rank(&self, word: String, limit: u32) -> CompletionPage { + let ranking = Ranking::new(self.candidates.clone(), &casefold(&word)); + let items = ranking + .ranked() + .iter() + .take(usize::try_from(limit).unwrap_or(usize::MAX)) + .filter_map(|&at| self.candidates.get(usize::try_from(at).ok()?)) + .map(|candidate| candidate.item(&word, ranking.needle())) + .collect(); + CompletionPage { + total: u32::try_from(ranking.ranked().len()).unwrap_or(u32::MAX), + items, + } + } +} + +// The editor's own proposal; Linux keeps its own, with its snippets, until it moves onto these. +#[derive(Debug, Clone, PartialEq, Eq)] +enum ProposalKind { + Keyword, + Schema, + Table, + View, + MaterializedView, + Column { data_type: String, table: TableRef }, + Function, +} + +impl ProposalKind { + // A sequence, a trigger or a key is never named on its own in a statement. + fn for_object(kind: ObjectKind) -> Option { + match kind { + ObjectKind::Table | ObjectKind::PartitionedTable | ObjectKind::ForeignTable | ObjectKind::Dictionary => { + Some(Self::Table) + } + ObjectKind::View => Some(Self::View), + ObjectKind::MaterializedView => Some(Self::MaterializedView), + ObjectKind::Function | ObjectKind::Procedure => Some(Self::Function), + _ => None, + } + } +} + +#[derive(Debug, Clone, PartialEq, Eq)] +struct Candidate { + kind: ProposalKind, + label: String, + insert_text: String, +} + +impl Ranked for Candidate { + fn label(&self) -> &str { + &self.label + } + + fn kind_rank(&self) -> u8 { + match self.kind { + ProposalKind::Column { .. } => 0, + ProposalKind::Table | ProposalKind::View | ProposalKind::MaterializedView => 1, + ProposalKind::Schema => 2, + ProposalKind::Function => 3, + ProposalKind::Keyword => 5, + } + } +} + +impl Candidate { + fn new(kind: ProposalKind, label: &str, insert_text: String) -> Self { + Self { + kind, + label: label.to_owned(), + insert_text, + } + } + + fn keyword(keyword: &str) -> Self { + Self::new(ProposalKind::Keyword, keyword, keyword.to_owned()) + } + + // A keyword goes in lower case once the word typed is all lower case, and as listed otherwise; a name keeps the + // case the catalog has. + fn cased(&self, text: &str, word: &str) -> String { + let lowercase = word.chars().any(char::is_lowercase) && !word.chars().any(char::is_uppercase); + if self.kind == ProposalKind::Keyword && lowercase { + text.to_lowercase() + } else { + text.to_owned() + } + } + + fn item(&self, word: &str, needle: &str) -> CompletionItem { + let label = self.cased(&self.label, word); + let mut units = Utf16Offsets::new(&label); + let matched = matched_runs(&label, needle) + .into_iter() + .filter_map(|run| { + let start = units.at(run.start); + let end = units.at(run.end); + Some(MatchedSpan { + start: u32::try_from(start).ok()?, + length: u32::try_from(end - start).ok()?, + }) + }) + .collect(); + let (kind, data_type, table) = match &self.kind { + ProposalKind::Column { data_type, table } => ( + CompletionKindTag::Column, + Some(data_type.clone()), + Some(TableName { + schema: table.schema.clone(), + name: table.name.clone(), + }), + ), + ProposalKind::Keyword => (CompletionKindTag::Keyword, None, None), + ProposalKind::Schema => (CompletionKindTag::Schema, None, None), + ProposalKind::Table => (CompletionKindTag::Table, None, None), + ProposalKind::View => (CompletionKindTag::View, None, None), + ProposalKind::MaterializedView => (CompletionKindTag::MaterializedView, None, None), + ProposalKind::Function => (CompletionKindTag::Function, None, None), + }; + CompletionItem { + insert_text: self.cased(&self.insert_text, word), + label, + kind, + data_type, + table, + matched, + } + } +} + +// What the text before the caret asks for, read before the catalog is. +pub(crate) struct CompletionRequest { + pub(crate) scope: CompletionScope, + word_start: u32, + word: String, +} + +impl CompletionRequest { + // None where nothing completes, and where nothing is typed yet in a place that takes anything, unless asked. + pub(crate) fn new(text: &str, caret: u32, grammar: SqlGrammar, asked: bool) -> Option { + if !grammar.is_sql() { + return None; + } + let caret = byte_at_unit(text, usize::try_from(caret).ok()?); + let start = word_start(text, caret); + // A number is no name, and a name a `$` joins is longer than the word a list replaces. + let word = text.get(start..caret).unwrap_or_default().to_owned(); + let joined = text.get(..start).and_then(|before| before.chars().next_back()); + if word.starts_with(|c: char| c.is_ascii_digit()) || joined.is_some_and(|c| joins_a_name(c, grammar)) { + return None; + } + let scope = analyze(text, start, grammar, LexicalSettings::default_for(grammar))?; + if word.is_empty() && !asked && !scope.names_a_place() { + return None; + } + Some(Self { + scope, + word_start: u32::try_from(Utf16Offsets::new(text).at(start)).ok()?, + word, + }) + } + + pub(crate) fn list( + self, + answer: &CatalogAnswer, + grammar: SqlGrammar, + dialect: Option<&dyn SqlDialect>, + ) -> CompletionList { + CompletionList { + candidates: candidates(&self.scope, answer, grammar, dialect).into(), + word_start: self.word_start, + word: self.word, + } + } +} + +// Characters a bare name holds past its first beside letters, digits and `_`, as `SqlGrammar::is_bare_identifier` reads. +fn joins_a_name(c: char, grammar: SqlGrammar) -> bool { + match c { + '$' => !matches!(grammar, SqlGrammar::ClickHouse | SqlGrammar::Cql), + '#' | '@' => grammar == SqlGrammar::MsSql, + _ => false, + } +} + +// Where unqualified names resolve for an editor in `database`: the connection's own, or the schema the catalog lists +// the database as, or nowhere the catalog describes. +pub(crate) fn catalog_home(layout: Option, own: Option<&str>, database: Option<&str>) -> CatalogHome { + match database.filter(|name| !name.is_empty()) { + None => CatalogHome::Connection, + Some(name) if Some(name) == own => CatalogHome::Connection, + Some(name) if layout == Some(DatabaseLayout::AsSchemas) => CatalogHome::Schema(name.to_owned()), + Some(name) => CatalogHome::Elsewhere(name.to_owned()), + } +} + +// In the catalog's order. A name goes in quoted only where it would read as something else bare. +fn candidates( + scope: &CompletionScope, + answer: &CatalogAnswer, + grammar: SqlGrammar, + dialect: Option<&dyn SqlDialect>, +) -> Vec { + let identifier = |name: &str| match dialect { + Some(dialect) if !grammar.is_bare_identifier(name) => dialect.quote_identifier(name), + _ => name.to_owned(), + }; + let mut proposals = Vec::new(); + let mut columns: HashSet<&str> = HashSet::new(); + for (table, list) in &answer.columns { + for column in list.iter() { + // The same name in two joined tables is one thing to type. + if !columns.insert(&column.name) { + continue; + } + let kind = ProposalKind::Column { + data_type: column.column_type.name.clone(), + table: table.clone(), + }; + proposals.push(Candidate::new(kind, &column.name, identifier(&column.name))); + } + } + if matches!(scope, CompletionScope::Objects { schema: None }) { + for schema in answer.schemas.iter().flat_map(|schemas| schemas.iter()) { + if !schema.is_system { + proposals.push(Candidate::new( + ProposalKind::Schema, + &schema.name, + identifier(&schema.name), + )); + } + } + } + let general = matches!(scope, CompletionScope::General { .. }); + for object in answer.objects.iter().flat_map(|objects| objects.iter()) { + if let Some(kind) = object_kind(object, general) { + proposals.push(Candidate::new(kind, &object.name, identifier(&object.name))); + } + } + if general { + proposals.extend(grammar.keywords().map(Candidate::keyword)); + } + proposals +} + +// A relation anywhere a table goes; a function only where an expression can. +fn object_kind(object: &ObjectInfo, general: bool) -> Option { + let kind = ProposalKind::for_object(object.kind)?; + (kind != ProposalKind::Function || general).then_some(kind) +} + +// A format changes only blanks, so the caret, in UTF-16 units, keeps the characters around it. A language that is not +// SQL comes back as it is. +#[uniffi::export] +pub fn format_sql(text: String, grammar: SqlGrammarTag, caret: u32) -> FormattedSql { + let grammar = SqlGrammar::from(grammar); + let caret = byte_at_unit(&text, usize::try_from(caret).unwrap_or(usize::MAX)); + let (formatted, caret) = if grammar.is_sql() { + format_with_caret(&text, grammar, LexicalSettings::default_for(grammar), caret) + } else { + (text, caret) + }; + let caret = Utf16Offsets::new(&formatted).at(caret); + FormattedSql { + caret: u32::try_from(caret).unwrap_or(u32::MAX), + text: formatted, + } +} + +// The text between UTF-16 units `start` and `end` formatted, or as it is where the whole text would read differently. +#[uniffi::export] +pub fn format_sql_selection(text: String, grammar: SqlGrammarTag, start: u32, end: u32) -> String { + let grammar = SqlGrammar::from(grammar); + let start = byte_at_unit(&text, usize::try_from(start).unwrap_or(usize::MAX)); + let end = byte_at_unit(&text, usize::try_from(end).unwrap_or(usize::MAX)).max(start); + if grammar.is_sql() { + format_selection(&text, grammar, LexicalSettings::default_for(grammar), start..end) + } else { + text.get(start..end).unwrap_or_default().to_owned() + } +} + +#[cfg(test)] +mod tests { + use tablepro_core::ColumnInfo; + use tablepro_core::column::{ColumnDefault, ColumnType}; + use tablepro_core::meta::SchemaInfo; + + use super::*; + + fn request(marked: &str, asked: bool) -> Option { + let caret = marked.find('|').expect("a caret mark"); + let text = marked.replacen('|', "", 1); + let units = u32::try_from(text[..caret].encode_utf16().count()).expect("a short text"); + CompletionRequest::new(&text, units, SqlGrammar::PostgreSql, asked) + } + + fn object(name: &str, kind: ObjectKind) -> ObjectInfo { + ObjectInfo { + schema: Some("public".to_owned()), + name: name.to_owned(), + kind, + parent_table: None, + } + } + + fn column(name: &str) -> ColumnInfo { + ColumnInfo { + name: name.to_owned(), + column_type: ColumnType::untyped_text("integer"), + nullable: true, + primary_key: false, + is_auto_increment: false, + is_generated: false, + partition_scoped: false, + default: ColumnDefault::None, + comment: None, + } + } + + fn answer() -> CatalogAnswer { + CatalogAnswer { + schemas: Some(Arc::from([ + SchemaInfo { + name: "sales".to_owned(), + is_default: false, + is_system: false, + }, + SchemaInfo { + name: "pg_catalog".to_owned(), + is_default: false, + is_system: true, + }, + ])), + objects: Some(Arc::from([ + object("users", ObjectKind::Table), + object("Order Items", ObjectKind::View), + object("refresh", ObjectKind::Function), + object("ids", ObjectKind::Sequence), + ])), + columns: vec![ + ( + TableRef::unqualified("users"), + Arc::from([column("id"), column("name")]), + ), + ( + TableRef::unqualified("orders"), + Arc::from([column("id"), column("total")]), + ), + ], + } + } + + fn labels(candidates: &[Candidate]) -> Vec<&str> { + candidates.iter().map(|candidate| candidate.label.as_str()).collect() + } + + fn list(candidates: Vec) -> CompletionList { + CompletionList { + candidates: candidates.into(), + word_start: 0, + word: String::new(), + } + } + + #[test] + fn a_place_that_takes_anything_waits_for_a_letter_unless_asked() { + assert!(request("SELECT |", false).is_none()); + assert!(request("SELECT |", true).is_some()); + assert!(request("SELECT * FROM |", false).is_some()); + assert!(request("SELECT 's|'", true).is_none(), "a string completes nothing"); + } + + #[test] + fn the_word_start_counts_utf16_units() { + let found = request("SELECT '😀', đơn|", false).expect("a request"); + + assert_eq!(found.word, "đơn"); + assert_eq!(found.word_start, 13); + } + + #[test] + fn a_decomposed_name_is_one_word() { + let found = request("SELECT * FROM cafe\u{301}|", true).expect("a request"); + + assert_eq!(found.word_start, 14); + assert_eq!(found.word, "cafe\u{301}"); + } + + #[test] + fn a_number_or_part_of_a_longer_name_completes_nothing() { + assert!( + request("SELECT 1|", true).is_none(), + "a number would be replaced by a name" + ); + assert!(request("SELECT 1.5|", true).is_none()); + assert!( + request("SELECT * FROM foo$ba|", true).is_none(), + "the name's head would stay" + ); + assert!(request("SELECT * FROM foo_ba|", true).is_some()); + } + + #[test] + fn a_language_that_is_not_sql_completes_nothing() { + assert!(CompletionRequest::new("GET k", 5, SqlGrammar::Redis, true).is_none()); + } + + #[test] + fn a_table_place_offers_relations_and_user_schemas_quoting_where_a_bare_name_would_not_read() { + let dialect = tablepro_test_support::FakeDialect; + + let found = candidates( + &CompletionScope::Objects { schema: None }, + &answer(), + SqlGrammar::PostgreSql, + Some(&dialect), + ); + + assert_eq!(labels(&found), ["id", "name", "total", "sales", "users", "Order Items"]); + let view = found.iter().find(|candidate| candidate.label == "Order Items"); + assert_eq!(view.map(|view| view.insert_text.as_str()), Some("\"Order Items\"")); + } + + #[test] + fn anywhere_else_offers_functions_and_keywords_but_no_schemas_or_sequences() { + let found = candidates( + &CompletionScope::General { + tables_in_statement: Vec::new(), + }, + &answer(), + SqlGrammar::PostgreSql, + None, + ); + + let labels = labels(&found); + assert!(labels.contains(&"refresh")); + assert!(labels.contains(&"SELECT")); + assert!(!labels.contains(&"sales")); + assert!(!labels.contains(&"ids")); + } + + #[test] + fn a_keyword_follows_the_case_typed_and_a_name_keeps_its_own() { + let words = list(vec![ + Candidate::keyword("SELECT"), + Candidate::keyword("DELETE"), + Candidate::new(ProposalKind::Table, "Selections", "\"Selections\"".to_owned()), + ]); + + let lower = words.rank("sel".to_owned(), 10); + let upper = words.rank("SEL".to_owned(), 10); + + assert_eq!(lower.total, 2); + let first = lower.items.first().expect("a match"); + assert_eq!( + (first.label.as_str(), first.insert_text.as_str(), first.kind), + ("select", "select", CompletionKindTag::Keyword) + ); + assert_eq!(first.matched, [MatchedSpan { start: 0, length: 3 }]); + assert_eq!( + upper.items.first().map(|item| item.insert_text.as_str()), + Some("SELECT") + ); + let table = lower.items.iter().find(|item| item.kind == CompletionKindTag::Table); + assert_eq!(table.map(|item| item.insert_text.as_str()), Some("\"Selections\"")); + assert_eq!(words.rank("sel".to_owned(), 1).items.len(), 1); + } + + #[test] + fn a_column_carries_its_type_and_table() { + let columns = list(vec![Candidate::new( + ProposalKind::Column { + data_type: "integer".to_owned(), + table: TableRef::new(Some("public".to_owned()), "users"), + }, + "id", + "id".to_owned(), + )]); + + let item = columns + .rank(String::new(), 10) + .items + .into_iter() + .next() + .expect("the column"); + + assert_eq!(item.kind, CompletionKindTag::Column); + assert_eq!(item.data_type.as_deref(), Some("integer")); + assert_eq!( + item.table, + Some(TableName { + schema: Some("public".to_owned()), + name: "users".to_owned() + }) + ); + } + + #[test] + fn a_tab_in_another_database_reads_names_where_the_catalog_lists_it() { + assert_eq!( + catalog_home(Some(DatabaseLayout::AsSchemas), Some("shop"), None), + CatalogHome::Connection + ); + assert_eq!( + catalog_home(Some(DatabaseLayout::AsSchemas), Some("shop"), Some("shop")), + CatalogHome::Connection + ); + assert_eq!( + catalog_home(Some(DatabaseLayout::AsSchemas), Some("shop"), Some("analytics")), + CatalogHome::Schema("analytics".to_owned()) + ); + assert_eq!( + catalog_home(Some(DatabaseLayout::WithSchemas), Some("shop"), Some("analytics")), + CatalogHome::Elsewhere("analytics".to_owned()) + ); + } + + #[test] + fn a_format_keeps_the_caret_in_utf16_units() { + let text = "select '😀', a from t"; + let caret = u32::try_from(text.encode_utf16().count()).expect("a short text"); + + let formatted = format_sql(text.to_owned(), SqlGrammarTag::PostgreSql, caret); + + assert!(formatted.text.contains('\n'), "{}", formatted.text); + assert_eq!( + usize::try_from(formatted.caret).expect("a caret"), + formatted.text.encode_utf16().count() + ); + } + + #[test] + fn a_caret_inside_a_literal_keeps_its_place() { + let text = "select '😀 b', a from t"; + let caret = u32::try_from("select '😀 ".encode_utf16().count()).expect("a short text"); + + let formatted = format_sql(text.to_owned(), SqlGrammarTag::PostgreSql, caret); + + let byte = byte_at_unit(&formatted.text, usize::try_from(formatted.caret).expect("a caret")); + assert!(formatted.text[..byte].ends_with("'😀 "), "{}", formatted.text); + assert!(formatted.text[byte..].starts_with(" b'"), "{}", formatted.text); + } + + #[test] + fn a_selection_counts_utf16_units_and_keeps_its_context() { + let selected = "select a,b from t"; + for (prefix, suffix) in [("select '😀'; ", ";"), ("select '😀 ", "'")] { + let text = format!("{prefix}{selected}{suffix}"); + let start = u32::try_from(prefix.encode_utf16().count()).expect("a short prefix"); + let end = start + u32::try_from(selected.encode_utf16().count()).expect("a short selection"); + + let formatted = format_sql_selection(text, SqlGrammarTag::PostgreSql, start, end); + + if prefix.ends_with("; ") { + assert!(formatted.contains('\n'), "{formatted}"); + } else { + assert_eq!(formatted, selected); + } + } + } + + #[test] + fn a_language_that_is_not_sql_is_left_as_it_is() { + let formatted = format_sql("GET key".to_owned(), SqlGrammarTag::Redis, 3); + + assert_eq!((formatted.text.as_str(), formatted.caret), ("GET key", 3)); + assert_eq!( + format_sql_selection("GET key".to_owned(), SqlGrammarTag::Redis, 0, 8), + "GET key" + ); + } +} diff --git a/core/crates/ffi/src/lib.rs b/core/crates/ffi/src/lib.rs index 790686e2b9..b07a5c0689 100644 --- a/core/crates/ffi/src/lib.rs +++ b/core/crates/ffi/src/lib.rs @@ -8,6 +8,7 @@ mod catalog; mod catalog_change; mod cloud; pub mod command_line; +mod completion; mod connection_url; mod core_handle; #[cfg(windows)] @@ -66,6 +67,10 @@ pub use catalog_change::{ DatabaseChoicesInfo, DatabaseDraftArg, DatabaseFieldsInfo, ReplicationArg, }; pub use cloud::{CloudConnectionFields, CloudGroupFields, CloudLibraryPull, CloudLibraryRecords}; +pub use completion::{ + CompletionItem, CompletionKindTag, CompletionList, CompletionPage, FormattedSql, MatchedSpan, format_sql, + format_sql_selection, +}; pub use connection_url::{ParsedConnectionUrl, parse_connection_url}; pub use core_handle::{Core, FileOpenRequest}; pub use definition::ObjectSource; @@ -120,6 +125,7 @@ pub use session::{ }; pub use statement_at::StatementSpan; pub use structure::{StructureColumn, StructureForeignKey, StructureIndex, TableStructure}; +pub use syntax::SqlGrammarTag; use secrecy::{ExposeSecret, SecretString}; diff --git a/core/crates/ffi/src/session.rs b/core/crates/ffi/src/session.rs index 4737b26cb1..feb4cd5300 100644 --- a/core/crates/ffi/src/session.rs +++ b/core/crates/ffi/src/session.rs @@ -12,12 +12,14 @@ use tablepro_core::sql_syntax::{SortReference, sort_reference, with_order_by}; use tablepro_core::{ ByteLimit, CallOptions, ColumnInfo, EditorSessionOptions, QueryResult, ResultEvent, ResultSink, Truncation, }; +use tablepro_session::completion::read_catalog; use tablepro_session::runtime::{Tasks, catch_panic}; use tablepro_session::{FailureCause, MonitorSignal, OperationError, OperationKind, Session, SessionHealth}; use tokio_util::sync::CancellationToken; use crate::cancel_token::{CancelToken, token}; use crate::catalog::{ColumnEntry, ObjectEntry, ObjectKindTag, ObjectScopeArg, SchemaEntry, TableName}; +use crate::completion::{CompletionList, CompletionRequest, catalog_home}; use crate::definition::ObjectSource; use crate::editor::QueryEditor; use crate::error::{BoxedErrorInfo, CoreError}; @@ -538,6 +540,35 @@ impl DatabaseSession { statement_at::statement_at(&sql, caret, self.session.grammar()) } + // What completes at `caret`, in UTF-16 units of `text`, for an editor in `database` (None for the connection's). + // None where nothing does, and where nothing is typed in a place that takes anything unless `asked`. The catalog + // reads are the ones the object tree shares, so typing does not reach the server again. + pub async fn complete( + &self, + text: String, + caret: u32, + asked: bool, + database: Option, + cancel: Option>, + ) -> Result>, CoreError> { + let session = self.session.clone(); + self.cancellable(cancel, async move { + let grammar = session.grammar(); + let Some(request) = CompletionRequest::new(&text, caret, grammar, asked) else { + return Ok(None); + }; + let dialect = session.current_dialect(); + let home = catalog_home( + dialect.as_ref().map(|dialect| dialect.database_layout()), + session.database(), + database.as_deref(), + ); + let answer = read_catalog(&session, request.scope.clone(), home).await; + Ok(Some(Arc::new(request.list(&answer, grammar, dialect.as_deref())))) + }) + .await + } + // A level saved while the session is open applies to it at once. False when `level` is on the other side of // Read-only, which takes a new connection; the session keeps its level until then. pub fn set_safe_mode(&self, level: SafeModeTag) -> bool { diff --git a/core/crates/ffi/src/statement_at.rs b/core/crates/ffi/src/statement_at.rs index 390d9ef5bb..3cb5480023 100644 --- a/core/crates/ffi/src/statement_at.rs +++ b/core/crates/ffi/src/statement_at.rs @@ -20,7 +20,7 @@ pub(crate) fn statement_at(sql: &str, caret: u32, grammar: SqlGrammar) -> Option } // A unit inside a surrogate pair belongs to the character the pair makes. -fn byte_at_unit(text: &str, unit: usize) -> usize { +pub(crate) fn byte_at_unit(text: &str, unit: usize) -> usize { let mut units = 0; for (byte, c) in text.char_indices() { units += c.len_utf16(); diff --git a/core/crates/ffi/src/syntax.rs b/core/crates/ffi/src/syntax.rs index 9302e0dd52..d80a9b6dde 100644 --- a/core/crates/ffi/src/syntax.rs +++ b/core/crates/ffi/src/syntax.rs @@ -16,6 +16,36 @@ const GRAMMARS: [SqlGrammar; 9] = [ SqlGrammar::Mongo, ]; +// How an editor reads its text, for the calls that take no session; in `SqlGrammar`'s declaration order. +#[derive(Debug, Clone, Copy, PartialEq, Eq, uniffi::Enum)] +pub enum SqlGrammarTag { + PostgreSql, + MySql, + Sqlite, + MsSql, + ClickHouse, + DuckDb, + Cql, + Redis, + Mongo, +} + +impl From for SqlGrammar { + fn from(grammar: SqlGrammarTag) -> Self { + match grammar { + SqlGrammarTag::PostgreSql => Self::PostgreSql, + SqlGrammarTag::MySql => Self::MySql, + SqlGrammarTag::Sqlite => Self::Sqlite, + SqlGrammarTag::MsSql => Self::MsSql, + SqlGrammarTag::ClickHouse => Self::ClickHouse, + SqlGrammarTag::DuckDb => Self::DuckDb, + SqlGrammarTag::Cql => Self::Cql, + SqlGrammarTag::Redis => Self::Redis, + SqlGrammarTag::Mongo => Self::Mongo, + } + } +} + // `None` for an unknown grammar code. pub(crate) fn scan(grammar: u32, backslash_escapes: u32, units: &[u16], out: &mut [TpSyntaxToken]) -> Option { let (text, grammar, settings) = decode(grammar, backslash_escapes, units)?; @@ -82,14 +112,14 @@ fn kind_code(kind: SyntaxKind) -> u32 { } // Byte offsets never go back, so a whole token or statement list maps in one pass over the text. -struct Utf16Offsets<'a> { +pub(crate) struct Utf16Offsets<'a> { text: &'a str, byte: usize, units: usize, } impl<'a> Utf16Offsets<'a> { - fn new(text: &'a str) -> Self { + pub(crate) fn new(text: &'a str) -> Self { Self { text, byte: 0, @@ -97,7 +127,7 @@ impl<'a> Utf16Offsets<'a> { } } - fn at(&mut self, byte: usize) -> usize { + pub(crate) fn at(&mut self, byte: usize) -> usize { let mut next = self.byte; for c in self.text.get(self.byte..).unwrap_or_default().chars() { if next >= byte { diff --git a/core/crates/ffi/tests/sqlite_path.rs b/core/crates/ffi/tests/sqlite_path.rs index a854190712..47b1a4160d 100644 --- a/core/crates/ffi/tests/sqlite_path.rs +++ b/core/crates/ffi/tests/sqlite_path.rs @@ -13,9 +13,9 @@ use std::time::{Duration, Instant}; use secrecy::SecretString; use tablepro_ffi::window_abi::{TpCursor, TpWindow, tablepro_cursor_fetch, tablepro_cursor_free}; use tablepro_ffi::{ - ColumnSort, Core, CoreError, DatabaseSession, EndpointInfo, FileOpenRequest, HostConfig, HostSecretError, - HostSecrets, ObjectKindTag, ObjectScopeArg, QueryEditor, RunEvent, RunListener, RunOutcome, RunSettings, - SecretItemTag, SecretKindTag, SessionHealthTag, TableName, TransactionStateTag, ValueKind, + ColumnSort, CompletionKindTag, Core, CoreError, DatabaseSession, EndpointInfo, FileOpenRequest, HostConfig, + HostSecretError, HostSecrets, ObjectKindTag, ObjectScopeArg, QueryEditor, RunEvent, RunListener, RunOutcome, + RunSettings, SecretItemTag, SecretKindTag, SessionHealthTag, TableName, TransactionStateTag, ValueKind, }; struct NoSecrets; @@ -599,3 +599,125 @@ async fn an_open_file_is_online_and_a_cancel_ends_the_wait_for_a_change() { assert_eq!(health, SessionHealthTag::Online); assert!(matches!(ended, Ok(Err(CoreError::Cancelled))), "{ended:?}"); } + +fn units(text: &str) -> u32 { + u32::try_from(text.encode_utf16().count()).unwrap() +} + +#[tokio::test] +async fn completion_offers_the_files_tables_and_the_columns_of_an_alias() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session, editor) = fixture(dir.path()).await; + let (outcome, _, _) = run( + &editor, + "CREATE TABLE \"Order Items\" (id INTEGER, quantity INTEGER)", + RunSettings::default(), + ) + .await; + assert!(matches!(outcome, RunOutcome::Completed { .. }), "{outcome:?}"); + // The catalog was read when the file opened, before the tables were made. + session + .refresh_objects(ObjectScopeArg::DefaultSchema, None) + .await + .unwrap(); + + let text = "SELECT * FROM pe"; + let tables = session + .complete(text.to_owned(), units(text), false, None, None) + .await + .unwrap() + .unwrap(); + assert_eq!((tables.word_start(), tables.word()), (14, "pe".to_owned())); + let page = tables.rank("pe".to_owned(), 10); + let first = page.items.first().unwrap(); + assert_eq!((first.label.as_str(), first.kind), ("people", CompletionKindTag::Table)); + assert_eq!( + first.matched.first().map(|span| (span.start, span.length)), + Some((0, 2)) + ); + let quoted = tables.rank("ord".to_owned(), 10); + let item = quoted.items.iter().find(|item| item.label == "Order Items").unwrap(); + assert_eq!( + item.insert_text, "\"Order Items\"", + "a name with a space goes in quoted" + ); + + let columns = session + .complete("SELECT p. FROM people p".to_owned(), 9, false, None, None) + .await + .unwrap() + .unwrap(); + let page = columns.rank(String::new(), 50); + let names: Vec<&str> = page.items.iter().map(|item| item.label.as_str()).collect(); + assert_eq!(names, ["id", "name", "note"], "the columns in the table's order"); + let id = page.items.first().unwrap(); + assert_eq!(id.kind, CompletionKindTag::Column); + assert!(id.data_type.is_some()); + assert_eq!(id.table.as_ref().map(|table| table.name.as_str()), Some("people")); + session.close().await.unwrap(); +} + +#[tokio::test] +async fn completion_waits_for_a_letter_where_anything_goes_and_never_opens_in_a_string() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session, _editor) = fixture(dir.path()).await; + session + .refresh_objects(ObjectScopeArg::DefaultSchema, None) + .await + .unwrap(); + + let text = "SELECT "; + let unasked = session + .complete(text.to_owned(), units(text), false, None, None) + .await + .unwrap(); + let asked = session + .complete(text.to_owned(), units(text), true, None, None) + .await + .unwrap() + .unwrap(); + let text = "SELECT 'pe"; + let in_string = session + .complete(text.to_owned(), units(text), true, None, None) + .await + .unwrap(); + + assert!(unasked.is_none()); + assert!(in_string.is_none()); + let page = asked.rank(String::new(), 1000); + assert!( + page.items + .iter() + .any(|item| item.label == "SELECT" && item.kind == CompletionKindTag::Keyword) + ); + assert!(page.items.iter().any(|item| item.label == "people")); + assert_eq!( + asked + .rank("sel".to_owned(), 1) + .items + .first() + .map(|item| item.insert_text.clone()), + Some("select".to_owned()) + ); + session.close().await.unwrap(); +} + +#[tokio::test] +async fn completion_in_a_database_the_catalog_does_not_describe_offers_none_of_its_tables() { + let dir = tempfile::tempdir().unwrap(); + let (_core, session, _editor) = fixture(dir.path()).await; + + let text = "SELECT * FROM pe"; + let elsewhere = session + .complete(text.to_owned(), units(text), true, Some("other".to_owned()), None) + .await + .unwrap() + .unwrap(); + + assert_eq!( + elsewhere.rank(String::new(), 100).total, + 0, + "a table from another database was offered" + ); + session.close().await.unwrap(); +} diff --git a/core/crates/session/src/completion.rs b/core/crates/session/src/completion.rs new file mode 100644 index 0000000000..4de9ee3c9e --- /dev/null +++ b/core/crates/session/src/completion.rs @@ -0,0 +1,238 @@ +use std::sync::Arc; + +use futures::future::join_all; +use tablepro_core::ColumnInfo; +use tablepro_core::meta::{ObjectInfo, ObjectScope, SchemaInfo, TableRef}; +use tablepro_core::sql_syntax::completion::{CatalogAnswer, CatalogHome, CompletionScope}; + +use crate::{OperationError, Session}; + +// The catalog shares each read with every other view of it and keeps what it read, so typing does not reach the server +// again; a list that cannot be read is left out. +pub async fn read_catalog(session: &Session, scope: CompletionScope, home: CatalogHome) -> CatalogAnswer { + let defaults = match &home { + CatalogHome::Connection => listed(session.objects(ObjectScope::DefaultSchema).await), + CatalogHome::Schema(name) => listed(session.objects(ObjectScope::Schema(name.clone())).await), + CatalogHome::Elsewhere(_) => return CatalogAnswer::default(), + }; + let home = Home { home, defaults }; + match scope { + CompletionScope::Objects { schema: None } => CatalogAnswer { + schemas: Some(listed(session.schemas().await)), + objects: Some(home.defaults.clone()), + columns: Vec::new(), + }, + CompletionScope::Objects { schema: Some(name) } => { + // `users.` typed before the FROM that names users. + let table = relation_named(&home.defaults, &name); + CatalogAnswer { + schemas: None, + objects: schema_objects(session, &home, &name).await, + columns: columns_of(session, table.into_iter().collect()).await, + } + } + CompletionScope::Qualified { qualifier, name } => { + let table = TableRef::new(Some(qualifier), name.clone()); + match known_table(session, &home, &table).await { + Some(table) => CatalogAnswer { + columns: columns_of(session, vec![table]).await, + ..CatalogAnswer::default() + }, + None => CatalogAnswer { + objects: schema_objects(session, &home, &name).await, + ..CatalogAnswer::default() + }, + } + } + CompletionScope::Columns { tables } => CatalogAnswer { + columns: columns_of(session, known_tables(session, &home, &tables).await).await, + ..CatalogAnswer::default() + }, + CompletionScope::General { tables_in_statement } => CatalogAnswer { + schemas: None, + columns: columns_of(session, known_tables(session, &home, &tables_in_statement).await).await, + objects: Some(home.defaults), + }, + } +} + +struct Home { + home: CatalogHome, + defaults: Arc<[ObjectInfo]>, +} + +impl Home { + fn is(&self, schema: &SchemaInfo) -> bool { + match &self.home { + CatalogHome::Connection => schema.is_default, + CatalogHome::Schema(name) => schema.name == *name, + CatalogHome::Elsewhere(_) => false, + } + } + + // MySQL's catalog lists only the connection's database as a schema. Exact, since `Analytics` and `analytics` can be + // two databases. + fn named(&self, name: &str) -> bool { + matches!(&self.home, CatalogHome::Schema(home) if home == name) + } +} + +async fn columns_of(session: &Session, tables: Vec) -> Vec<(TableRef, Arc<[ColumnInfo]>)> { + let reads = tables.into_iter().map(|table| async move { + let columns = listed(session.columns(table.clone()).await); + (table, columns) + }); + join_all(reads).await +} + +fn listed(read: Result, Arc>) -> Arc<[T]> { + read.unwrap_or_else(|error| { + tracing::debug!(%error, "completion left out a list it could not read"); + Arc::from([]) + }) +} + +async fn schema_objects(session: &Session, home: &Home, name: &str) -> Option> { + if home.named(name) { + return Some(home.defaults.clone()); + } + let schemas = listed(session.schemas().await); + let schema = schema_named(&schemas, name)?; + Some(listed(session.objects(ObjectScope::Schema(schema.name.clone())).await)) +} + +// A name the catalog does not hold, such as a CTE's, is left out: reading it could only fail, and a failed read is +// asked again every time. +async fn known_tables(session: &Session, home: &Home, tables: &[TableRef]) -> Vec { + let mut known = Vec::new(); + for table in tables { + if let Some(table) = known_table(session, home, table).await + && !known.contains(&table) + { + known.push(table); + } + } + known +} + +// The catalog's own name for `table`, so the read is the one the table's other views share. +async fn known_table(session: &Session, home: &Home, table: &TableRef) -> Option { + let Some(schema) = &table.schema else { + return relation_named(&home.defaults, &table.name); + }; + if home.named(schema) { + return relation_named(&home.defaults, &table.name); + } + let schemas = listed(session.schemas().await); + let schema = schema_named(&schemas, schema)?; + if home.is(schema) { + return relation_named(&home.defaults, &table.name); + } + let objects = listed(session.objects(ObjectScope::Schema(schema.name.clone())).await); + relation_named(&objects, &table.name) +} + +fn schema_named<'a>(schemas: &'a [SchemaInfo], name: &str) -> Option<&'a SchemaInfo> { + named(schemas, name, |schema| Some(schema.name.as_str())) +} + +fn relation_named(objects: &[ObjectInfo], name: &str) -> Option { + named(objects, name, |object| { + object.kind.is_browsable().then_some(object.name.as_str()) + }) + .map(|object| TableRef::new(object.schema.clone(), object.name.clone())) +} + +// As written first, then ignoring case, the way an unquoted name is read. +fn named<'a, T>(items: &'a [T], name: &str, name_of: impl Fn(&T) -> Option<&str>) -> Option<&'a T> { + let folded = name.to_lowercase(); + items.iter().find(|item| name_of(item) == Some(name)).or_else(|| { + items + .iter() + .find(|item| name_of(item).is_some_and(|own| own.to_lowercase() == folded)) + }) +} + +#[cfg(test)] +mod tests { + use tablepro_core::meta::ObjectKind; + + use super::*; + + fn object(name: &str, kind: ObjectKind) -> ObjectInfo { + ObjectInfo { + schema: Some("public".to_owned()), + name: name.to_owned(), + kind, + parent_table: None, + } + } + + // A tab moved to `analytics` reads `shop.orders` from `shop`, the connection's default schema, not from its own. + #[test] + fn only_the_tabs_own_database_is_read_from_its_home() { + let shop = SchemaInfo { + name: "shop".to_owned(), + is_default: true, + is_system: false, + }; + let moved = Home { + home: CatalogHome::Schema("analytics".to_owned()), + defaults: Arc::from([]), + }; + assert!(!moved.is(&shop)); + assert!(moved.named("analytics")); + let staying = Home { + home: CatalogHome::Connection, + defaults: Arc::from([]), + }; + assert!(staying.is(&shop)); + assert!(!staying.named("shop"), "the catalog lists the connection's own"); + } + + #[test] + fn an_unqualified_table_takes_the_catalogs_schema() { + let defaults = [object("users", ObjectKind::Table), object("Orders", ObjectKind::Table)]; + + assert_eq!( + relation_named(&defaults, "users"), + Some(TableRef::new(Some("public".to_owned()), "users")) + ); + assert_eq!( + relation_named(&defaults, "orders"), + Some(TableRef::new(Some("public".to_owned()), "Orders")) + ); + assert_eq!(relation_named(&defaults, "missing"), None); + } + + #[test] + fn a_function_is_not_a_relation() { + let defaults = [object("refresh", ObjectKind::Function)]; + + assert_eq!(relation_named(&defaults, "refresh"), None); + } + + #[test] + fn a_schema_is_matched_as_written_then_ignoring_case() { + let schema = |name: &str| SchemaInfo { + name: name.to_owned(), + is_default: false, + is_system: false, + }; + let schemas = [schema("sales"), schema("Audit"), schema("audit")]; + + assert_eq!( + schema_named(&schemas, "SALES").map(|schema| schema.name.as_str()), + Some("sales") + ); + assert_eq!( + schema_named(&schemas, "Audit").map(|schema| schema.name.as_str()), + Some("Audit") + ); + assert_eq!( + schema_named(&schemas, "audit").map(|schema| schema.name.as_str()), + Some("audit") + ); + assert!(schema_named(&schemas, "hr").is_none()); + } +} diff --git a/core/crates/session/src/lib.rs b/core/crates/session/src/lib.rs index 67fcb3f2e1..74af795124 100644 --- a/core/crates/session/src/lib.rs +++ b/core/crates/session/src/lib.rs @@ -6,6 +6,7 @@ //! adopts these on the GTK thread; nothing here needs an entered //! runtime to be created. +pub mod completion; pub mod connect; #[cfg(any(test, feature = "fakes"))] pub mod fakes; diff --git a/windows/AGENTS.md b/windows/AGENTS.md index 674d3a54e7..f4fcda09d4 100644 --- a/windows/AGENTS.md +++ b/windows/AGENTS.md @@ -54,6 +54,7 @@ This is the guide for work under `windows/`. The root `AGENTS.md` holds the rule - `TableProCore.GetForeignSourcesAsync` lists the other apps found under `ForeignAppDirs.ForCurrentUser()`, `ReadForeignAsync` gives a `ForeignImportReview` whose `Choices()` follow each check, and `ApplyAsync` writes the connections, their passwords into Credential Manager and the saved queries once; DataGrip's KeePass key opens with the user's DPAPI (core 0033), and DBX's key comes from Credential Manager or its DPAPI copy (core 0039). - `DatabaseSession.Layout` says how the server's databases stand to the catalog, `GetDatabasesAsync` lists them, and `OpenEditorAsync(database)` opens an editor that runs in one (opened there or moved there; one that cannot reach it throws). `QueryEditor.Database` follows a typed USE. `TableProCore.ConnectAsync(database:)` opens a saved connection in another of its databases, and `OpenKey` reads a key-value engine's key a window at a time (`KeyReader`). - `SqlSyntax.Tokenize` and `SqlSyntax.Statements` read SQL as the core does, from the editor's UTF-16 text with no copy into a UniFFI string; `Statements` splits as `StatementAt` and the drivers do. A failed run's `CoreError.Server.Position` counts characters from 1 from the start of the SQL the run was given. +- `DatabaseSession.CompleteAsync` gives a `SqlCompletion` for a caret in an editor's text, in the tab's database, from the catalog reads the object tree shares; its `Rank` narrows it per word typed and takes milliseconds on a large catalog, so a UI thread calls it from the pool. `SqlFormatter.Format` and `FormatSelection` reformat through the core, keeping a statement as typed where the formatter would change its tokens (`docs/decisions/0002-sql-editor-control.md`, Completion and format). - `QueryEvent.Finished.Transaction` is the transaction the editor's connection is left in. A session's `Health` is waited on with `WaitForHealthChangeAsync`, which returns once it differs from the one passed in. - The app starts the core with `TableProCore.CreateWithSystemSecrets`, so the core keeps secrets in Credential Manager itself. `ISecretStore` is for tests and hosts with a store of their own; the core calls it on a thread of its own. - `TableProCore.Library` reads and writes the saved connections in the storage directory the configuration names. A list the app shows is rebuilt from `GetSnapshotAsync` after each change. diff --git a/windows/docs/decisions/0002-sql-editor-control.md b/windows/docs/decisions/0002-sql-editor-control.md index be160802eb..9a4ff23228 100644 --- a/windows/docs/decisions/0002-sql-editor-control.md +++ b/windows/docs/decisions/0002-sql-editor-control.md @@ -63,6 +63,8 @@ | Ctrl+/, Tab, Shift+Tab, Ctrl+], Ctrl+[ | Line comment, indent, outdent | | Ctrl+F, F3, Shift+F3 | Find, next, previous | | Ctrl+Shift+[, Ctrl+Shift+] | Fold the innermost region at the caret, unfold the one on its line | +| Ctrl+Space | Completion, wherever a name or a keyword can go | +| Ctrl+Shift+F | Format the selection, or the whole text without one, as on Linux; Query > Filter rows has the same keys only in a table tab, which holds no editor | - Typing closes brackets and quotes and steps over a closer already there; Enter keeps indentation and opens a bracket pair onto its own lines; Backspace in indentation goes back one tab stop. - Mouse: click, Shift+click, double-click word, triple-click line, drag to select with auto-scroll, drag and drop of text (a move inside the editor is one undo step), and a context menu. @@ -82,6 +84,14 @@ - The band behind the statement the caret is in is the host's to turn on, as the macOS editor's is. It is the statement Run Statement sends: the one the primary caret is in, or the last one before it, and it steps aside for a selection. - The bracket beside the caret, before it first as the macOS editor reads it, and its pair get a box, as VS Code and Visual Studio mark them on Windows. Only a bracket the lexer gives as punctuation or an operator pairs, so one inside a string, a comment or a quoted name never does. A text the lexer gives no tokens for, such as a MongoDB query, pairs plain characters. +### Completion and format + +- The core decides what completes and how it is written (`DatabaseSession.CompleteAsync`, over `tablepro_core::sql_syntax::completion` and `tablepro_session::completion`, the code the Linux editor runs): the scope at the caret, the catalog reads the object tree shares, keyword case, quoting and ranking. A tab in another of the server's databases reads that database where the catalog lists it as a schema (MySQL), and offers only keywords where it does not. The editor shows the list and inserts the choice. +- As in the macOS editor, the list opens 50 ms after a letter, and after a space, comma, dot or parenthesis where the place alone takes a name (after FROM, after an alias's dot); Ctrl+Space opens it anywhere. Typing narrows it without reading the catalog again, ranked off the UI thread, and it closes when nothing matches, when the caret leaves the word, on focus loss or on Escape. +- A WinUI `Popup` over the editor holds a `ListView` whose rows are made once and filled again for each word. It never takes focus, so the editor keeps the keys, the caret and the IME, whose composition closes it. Up and Down stop at the ends, Page Up and Page Down move a page, and Tab and a double click accept. Enter accepts only a row that changes what is typed, once the rows are ranked for the word as typed, as VS Code's smart Enter does; otherwise it is the line break, so typing `FROM Track` and Enter starts a new line; with Vim on, these keys reach the list before Vim, and Escape closes it and leaves Insert mode too. The choice replaces the whole word the caret is in, as on Linux, as one undo step. +- Each row has the object tree's symbol for its kind, the matched characters in semibold, and the kind or a column's type. The list is in the editor's `ControlledPeers`, and the editor raises a UIA notification for the selected row ("Track, Table, 1 of 3"). +- Format query (the Query menu, the editor's menu, Ctrl+Shift+F) runs the core's formatter off the UI thread (`SqlFormatter`, over `tablepro_core::sql_syntax::format`). A statement whose tokens the formatter would change is kept as typed; a selection is formatted only where the whole text keeps its tokens, so one inside a comment or a string stays as it is. Only the part that changed is replaced, as one undo step; the caret keeps the characters around it, or goes after a formatted selection, as the macOS editor leaves it. Text typed while the format runs wins. + ### Color schemes - The editor half of each built-in macOS theme is a scheme (`windows/scripts/editor-schemes.py` writes `ColorSchemes.Themes.cs` from `apple/TablePro/Resources/Themes`). The host picks one for light and one for dark; none keeps the Windows colors. Names the user wrote, punctuation and quoted names draw as text, as the macOS editor colors them. High contrast draws the system colors whatever the scheme. @@ -107,7 +117,7 @@ - Text input, hit testing and accessibility are ours to keep right; RichEditBox's own behaviors no longer come for free. - A transitory TSF context gives up reconversion and IME features that read the text around the caret. - The composition is drawn at the primary caret only, and committed text goes to every caret. -- Seams for the later slices: the lexer is an `ISyntaxLexer`; the model's commands and `SqlEditor.HandleKey` are public; `FramePresented` reports every frame. Completion, hover, diagnostics and formatting are not built; Vim mode is [0017](0017-vim-mode.md). +- Seams for the later slices: the lexer is an `ISyntaxLexer`; the model's commands and `SqlEditor.HandleKey` are public; `FramePresented` reports every frame. Hover and diagnostics are not built; Vim mode is [0017](0017-vim-mode.md). Completion offers no snippets and no saved query keywords yet. - A new width lays out every line again for word wrap, which on a 5 MB script costs a pass over its text per column the width gains or loses. - The fold chevrons have no UIA element of their own; the keys and the menu reach every fold. diff --git a/windows/docs/decisions/0004-app-shell.md b/windows/docs/decisions/0004-app-shell.md index 1c529163f6..3736826ecd 100644 --- a/windows/docs/decisions/0004-app-shell.md +++ b/windows/docs/decisions/0004-app-shell.md @@ -79,6 +79,7 @@ On 2026-11-14 the roadmap's Phase 2 added a production cue (W-08), a database sw | Ctrl+B, Ctrl+P | Show or hide the object tree, filter objects | | Ctrl+K | The database menu of the object pane (Database, Switch database) | | Ctrl+F, Ctrl+H, F3 | Find, Find and Replace, next match (Edit menu, the editor's own keys) | +| Ctrl+Shift+F, Ctrl+Space | Format query (Query menu) and completion, the editor's own keys ([0002](0002-sql-editor-control.md)); in a table tab Ctrl+Shift+F is Filter rows | | Ctrl+Shift+H | The Connections page | | F1 | Documentation | | Enter, F2, Delete on the Connections page | Connect; rename the row in place; delete, confirmed (every checked connection in selection mode) | diff --git a/windows/docs/decisions/0007-browsing-a-connection.md b/windows/docs/decisions/0007-browsing-a-connection.md index ffd1a22dc6..f3117ddf3e 100644 --- a/windows/docs/decisions/0007-browsing-a-connection.md +++ b/windows/docs/decisions/0007-browsing-a-connection.md @@ -47,7 +47,6 @@ Each one works as the macOS app's does, with its words in Windows sentence case, ## Consequences -- No completion in the SQL editor yet: the caret analysis lives in the Linux app and moves to the core first, then the editor needs its popup. - MongoDB's typed filter document is not offered; its rules are. - No CSV options and no history retention setting yet: the core's CSV defaults and the store's cap apply. - The history keeps no database, so a run reopened from it runs where the connection is. diff --git a/windows/docs/decisions/0011-saved-queries-csv-import-and-connection-copies.md b/windows/docs/decisions/0011-saved-queries-csv-import-and-connection-copies.md index b4a2fa4668..97a135eebc 100644 --- a/windows/docs/decisions/0011-saved-queries-csv-import-and-connection-copies.md +++ b/windows/docs/decisions/0011-saved-queries-csv-import-and-connection-copies.md @@ -16,7 +16,7 @@ Each works as the macOS app's does, with its words in Windows sentence case, ove - Query > Save query... and the editor's menu keep a query tab's whole text under a name, and the history's entry menu keeps a run's. A dialog asks for the name, prefilled from the text as macOS names a favorite, and a folder once there are folders. A name the connection already uses asks before it replaces that query, which keeps its place unless another folder was picked. - View > Show saved queries opens a pane in the place the history pane takes, one of the two at a time. A tree lists folders, then queries, each in its stored order. Invoking a query inserts it in the editor, as macOS does: it replaces an empty query tab's text, follows a tab's text after a blank line, and opens a new query tab when none is in front. Its menu has Insert in editor, Open in new tab, Copy query, Edit..., Move to and Delete; a folder's has New subfolder, Rename and Delete folder, whose folders go with it and whose queries move to the top level. - Saved queries belong to a saved connection. A file opened without saving gets a new id each time, so it keeps none. Deleting a connection deletes its saved queries, folders and favourites, as the Linux app does. -- Keywords are kept by the core but not edited here: macOS expands one through completion, which the Windows editor does not have yet. +- Keywords are kept by the core but not edited here: macOS expands one through completion, whose list on Windows offers no saved queries yet. ### History pins diff --git a/windows/src/TablePro.App/MainWindow.xaml b/windows/src/TablePro.App/MainWindow.xaml index 5ee0866f56..c701245705 100644 --- a/windows/src/TablePro.App/MainWindow.xaml +++ b/windows/src/TablePro.App/MainWindow.xaml @@ -208,6 +208,8 @@ + + diff --git a/windows/src/TablePro.App/MainWindow.xaml.cs b/windows/src/TablePro.App/MainWindow.xaml.cs index 3fc8321493..e690252a29 100644 --- a/windows/src/TablePro.App/MainWindow.xaml.cs +++ b/windows/src/TablePro.App/MainWindow.xaml.cs @@ -1635,6 +1635,7 @@ private void UpdateCommands() // A definition tab reads its text with Reload, not Run, and has nothing to stop. MenuRun.IsEnabled = tab is QueryTabView { IsRunning: false } or TableTabView { IsRunning: false }; MenuRunStatement.IsEnabled = tab is QueryTabView { IsRunning: false }; + MenuFormatQuery.IsEnabled = tab?.Editor is { CanFormat: true }; MenuStop.IsEnabled = tab is QueryTabView { IsRunning: true } or TableTabView { IsRunning: true } or KeyTabView { IsRunning: true }; var editor = tab?.Editor is not null; @@ -1893,6 +1894,15 @@ private async void OnDisconnect(object sender, RoutedEventArgs e) private void OnStop(object sender, RoutedEventArgs e) => Workspace?.ActiveTab?.Stop(); + private async void OnFormatQuery(object sender, RoutedEventArgs e) + { + if (Workspace?.ActiveTab?.Editor is { } editor) + { + editor.Focus(FocusState.Programmatic); + await editor.FormatAsync().ConfigureAwait(true); + } + } + // A table tab saves its pending changes, a query tab its .sql file, through Save As when it has none. private async void OnSaveChanges(object sender, RoutedEventArgs e) { diff --git a/windows/src/TablePro.App/Services/SessionCompletion.cs b/windows/src/TablePro.App/Services/SessionCompletion.cs new file mode 100644 index 0000000000..6d92e47641 --- /dev/null +++ b/windows/src/TablePro.App/Services/SessionCompletion.cs @@ -0,0 +1,11 @@ +using TablePro.Editor; +using TablePro.Interop; + +namespace TablePro.App.Services; + +// A query tab completes from its connection's catalog, the reads the object tree shares, in the database it runs in. +internal sealed class SessionCompletion(DatabaseSession session, Func database) : ICompletionSource +{ + public Task CompleteAsync(string text, int caret, bool asked, CancellationToken cancellationToken) => + session.CompleteAsync(text, caret, asked, database(), cancellationToken); +} diff --git a/windows/src/TablePro.App/Strings/en-US/Resources.resw b/windows/src/TablePro.App/Strings/en-US/Resources.resw index 5aace09b53..68839337c7 100644 --- a/windows/src/TablePro.App/Strings/en-US/Resources.resw +++ b/windows/src/TablePro.App/Strings/en-US/Resources.resw @@ -5778,6 +5778,55 @@ New fingerprint: {2} Ctrl+/ The keys the editor takes for this item, as Windows words them in this language. + + Format Query + + + Ctrl+Shift+F + The keys the editor takes for this item, as Windows words them in this language. + + + Format query + + + Ctrl+Shift+F + The keys the editor takes for this item, as Windows words them in this language. + + + M + + + Completions + + + Keyword + + + Schema + + + Table + + + View + + + Materialized view + + + Column + + + Function + + + {0}, {1} + {0} is the name a completion writes, {1} what it is: Table, View, Keyword, or a column's type. + + + {0}, {1:N0} of {2:N0} + What Narrator reads for the selected completion. {0} is its name and kind, {1} its place in the list, {2} how many the list holds. + Fold diff --git a/windows/src/TablePro.App/Views/QueryTabView.xaml.cs b/windows/src/TablePro.App/Views/QueryTabView.xaml.cs index d8ed643746..17fd41f421 100644 --- a/windows/src/TablePro.App/Views/QueryTabView.xaml.cs +++ b/windows/src/TablePro.App/Views/QueryTabView.xaml.cs @@ -82,6 +82,7 @@ internal QueryTabView( customTitle: customTitle, wrapText: wrapText); Results.WrapsText = _model.WrapsText; + SqlText.CompletionSource = new SessionCompletion(share.Session, () => _model.Database); AutomationProperties.SetName(SqlText, Strings.Format("EditorName", _model.Title)); ShowDatabase(); _model.StateChanged += (_, _) => OnModelStateChanged(); diff --git a/windows/src/TablePro.Editor.Model/EditorModel.Typing.cs b/windows/src/TablePro.Editor.Model/EditorModel.Typing.cs index 7363fa61e2..90d5167f99 100644 --- a/windows/src/TablePro.Editor.Model/EditorModel.Typing.cs +++ b/windows/src/TablePro.Editor.Model/EditorModel.Typing.cs @@ -224,6 +224,54 @@ public void ReplaceRange(int start, int end, string text) Apply(EditKind.Other, [Edit.Insert(Clamp(start), Clamp(end), text)], 0); } + // Only the part that differs is replaced, so the lines a format leaves keep their line states and folds. One undo + // step, after which `after` holds, in offsets of the text once replaced. + public void ReplaceRange(int start, int end, string text, Selection after) + { + ArgumentNullException.ThrowIfNull(text); + text = TextDocument.Normalize(text); + start = Clamp(start); + end = Math.Max(start, Clamp(end)); + var old = Document.GetText(start, end - start); + var limit = Math.Min(old.Length, text.Length); + var prefix = 0; + while (prefix < limit && old[prefix] == text[prefix]) + { + prefix++; + } + var suffix = 0; + while (suffix < limit - prefix && old[^(suffix + 1)] == text[^(suffix + 1)]) + { + suffix++; + } + // A surrogate pair is never split between the kept and the replaced part. + if (prefix > 0 && char.IsHighSurrogate(text[prefix - 1])) + { + prefix--; + } + if (suffix > 0 && char.IsLowSurrogate(text[^suffix])) + { + suffix--; + } + var length = Document.Length - old.Length + text.Length; + var selection = new Selection(Math.Clamp(after.Anchor, 0, length), Math.Clamp(after.Active, 0, length)); + Document.Undo.Begin(EditKind.Other, _selections.ToList()); + _applying++; + try + { + if (old.Length != text.Length || prefix < old.Length) + { + Document.Replace(start + prefix, old.Length - prefix - suffix, text[prefix..^suffix]); + } + SetSelections([selection]); + } + finally + { + _applying--; + Document.Undo.End(_selections.ToList()); + } + } + // One undo step for all of them; the primary selection moves with the text and the other carets go. public void ReplaceRanges(IReadOnlyList<(int Start, int Length, string Text)> ranges) { diff --git a/windows/src/TablePro.Editor/Automation/SqlEditorAutomationPeer.cs b/windows/src/TablePro.Editor/Automation/SqlEditorAutomationPeer.cs index ad913aed7a..ad41d0b07e 100644 --- a/windows/src/TablePro.Editor/Automation/SqlEditorAutomationPeer.cs +++ b/windows/src/TablePro.Editor/Automation/SqlEditorAutomationPeer.cs @@ -57,7 +57,20 @@ internal void RaiseSelectionChanged() } } - protected override object GetPatternCore(PatternInterface patternInterface) => + // The completion list never takes focus, so the editor speaks for the row it selects. + internal void Announce(string text) + { + if (text.Length > 0) + { + RaiseNotificationEvent( + AutomationNotificationKind.Other, + AutomationNotificationProcessing.MostRecent, + text, + "TableProCompletion"); + } + } + + protected override object GetPatternCore(PatternInterface patternInterface) => patternInterface is PatternInterface.Text or PatternInterface.Text2 ? this : base.GetPatternCore(patternInterface); protected override AutomationControlType GetAutomationControlTypeCore() => AutomationControlType.Edit; diff --git a/windows/src/TablePro.Editor/CompletionPopup.cs b/windows/src/TablePro.Editor/CompletionPopup.cs new file mode 100644 index 0000000000..2a59eed489 --- /dev/null +++ b/windows/src/TablePro.Editor/CompletionPopup.cs @@ -0,0 +1,272 @@ +using Microsoft.UI.Text; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Automation; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Controls.Primitives; +using Microsoft.UI.Xaml.Documents; +using Microsoft.UI.Xaml.Input; +using Microsoft.UI.Xaml.Markup; +using TablePro.Graphics; +using TablePro.Interop; +using Windows.Foundation; + +namespace TablePro.Editor; + +// The rows a completion offers. It never takes focus, so the editor keeps the keys, the caret and the IME. +internal sealed class CompletionPopup +{ + public const double RowHeight = 28; + + private const double IconWidth = 20; + private const double RowPadding = 10; + private const double ColumnSpacing = 10; + + // Only markup can name theme resources, so the frame and its styles are markup and the rows are made in code. + private const string Markup = """ + + + + + + + """; + + private readonly Popup _popup = new() { IsLightDismissEnabled = false }; + private readonly Border _frame; + private readonly ListView _list; + private readonly Style? _detailStyle; + private readonly List _rows = []; + private IReadOnlyList _items = []; + private int _total; + private bool _filling; + + public CompletionPopup(Panel host) + { + _frame = (Border)XamlReader.Load(Markup); + _list = (ListView)_frame.Child; + _detailStyle = _frame.Resources.TryGetValue("CompletionDetail", out var style) ? style as Style : null; + AutomationProperties.SetAutomationId(_list, "CompletionList"); + AutomationProperties.SetName(_list, HostStrings.Get("CompletionListName")); + _popup.Child = _frame; + _popup.HorizontalAlignment = HorizontalAlignment.Left; + _popup.VerticalAlignment = VerticalAlignment.Top; + host.Children.Add(_popup); + _list.SelectionChanged += (_, _) => + { + if (!_filling) + { + SelectionChanged?.Invoke(this, EventArgs.Empty); + } + }; + _list.DoubleTapped += OnDoubleTapped; + } + + public event EventHandler? SelectionChanged; + + public event EventHandler? Accepted; + + public bool IsOpen => _popup.IsOpen; + + public UIElement List => _list; + + // The word the rows were ranked for. + public string Word { get; private set; } = string.Empty; + + public CompletionItem? Selected => _list.SelectedIndex is var index and >= 0 && index < _items.Count + ? _items[index] + : null; + + public int PageRows => Math.Max(1, (int)(_list.MaxHeight / RowHeight) - 1); + + // The rows are made once and filled again for each word, so typing does not build a list of controls per key. + public void Show(CompletionPage page, string word, ElementTheme theme) + { + Word = word; + _total = page.Total; + _items = page.Items; + _frame.RequestedTheme = theme; + _filling = true; + try + { + while (_list.Items.Count > _items.Count) + { + _list.Items.RemoveAt(_list.Items.Count - 1); + } + for (var i = 0; i < _items.Count; i++) + { + if (i == _rows.Count) + { + _rows.Add(NewRow()); + } + Fill(_rows[i], _items[i]); + if (i == _list.Items.Count) + { + _list.Items.Add(_rows[i].Container); + } + } + _popup.IsOpen = true; + _list.SelectedIndex = _items.Count > 0 ? 0 : -1; + } + finally + { + _filling = false; + } + if (_items.Count > 0) + { + _list.ScrollIntoView(_list.Items[0]); + } + SelectionChanged?.Invoke(this, EventArgs.Empty); + } + + public void Close() + { + _popup.IsOpen = false; + _items = []; + _total = 0; + Word = string.Empty; + } + + // Up and Down stop at the ends, as the macOS list does; a page moves by the rows in view. + public void Move(int rows) + { + if (_items.Count == 0) + { + return; + } + var index = Math.Clamp(_list.SelectedIndex + rows, 0, _items.Count - 1); + _list.SelectedIndex = index; + _list.ScrollIntoView(_list.Items[index]); + } + + // The label lines up with the word typed, below the caret's line, or above it when only that fits. + public void Place(Rect wordStart, Rect caret, double viewWidth, double viewHeight) + { + // The rows are laid out on the next pass, so the height comes from their count. + var width = Math.Max(_frame.MinWidth, _frame.ActualWidth); + var padding = _frame.Padding; + var border = _frame.BorderThickness; + var height = Math.Min(_items.Count * RowHeight, _list.MaxHeight) + + padding.Top + padding.Bottom + border.Top + border.Bottom; + var inset = RowPadding + IconWidth + ColumnSpacing; + var x = Math.Clamp(wordStart.X - inset, 0, Math.Max(0, viewWidth - width)); + var below = caret.Bottom + 2; + var above = caret.Top - height - 2; + _popup.HorizontalOffset = x; + _popup.VerticalOffset = below + height <= viewHeight || above < 0 ? below : above; + } + + // What Narrator reads for the selected row: its name, its kind and its place in the list. + public string Announcement => Selected is { } item + ? HostStrings.Format("CompletionAnnouncement", Name(item), _list.SelectedIndex + 1, _total) + : string.Empty; + + private static string Name(CompletionItem item) => + HostStrings.Format("CompletionRowName", item.Label, Describe(item)); + + private static string Describe(CompletionItem item) => item.Kind switch + { + CompletionKind.Column => item.DataType ?? HostStrings.Get("CompletionColumn"), + CompletionKind.Schema => HostStrings.Get("CompletionSchema"), + CompletionKind.Table => HostStrings.Get("CompletionTable"), + CompletionKind.View => HostStrings.Get("CompletionView"), + CompletionKind.MaterializedView => HostStrings.Get("CompletionMaterializedView"), + CompletionKind.Function => HostStrings.Get("CompletionFunction"), + _ => HostStrings.Get("CompletionKeyword"), + }; + + // The object tree's symbols for the same kinds. + private static Symbol Icon(CompletionKind kind) => kind switch + { + CompletionKind.Table => Symbol.ViewAll, + CompletionKind.View or CompletionKind.MaterializedView => Symbol.View, + CompletionKind.Function => Symbol.Calculator, + CompletionKind.Schema => Symbol.Folder, + CompletionKind.Column => Symbol.List, + _ => Symbol.Font, + }; + + private Row NewRow() + { + var icon = new SymbolIcon { VerticalAlignment = VerticalAlignment.Center }; + var label = new TextBlock + { + VerticalAlignment = VerticalAlignment.Center, + TextTrimming = TextTrimming.CharacterEllipsis, + }; + var detail = new TextBlock { Style = _detailStyle }; + Grid.SetColumn(label, 1); + Grid.SetColumn(detail, 2); + var grid = new Grid { ColumnSpacing = ColumnSpacing, Children = { icon, label, detail } }; + grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(IconWidth) }); + grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); + grid.ColumnDefinitions.Add(new ColumnDefinition { Width = GridLength.Auto }); + var container = new ListViewItem + { + Content = grid, + MinHeight = RowHeight, + Height = RowHeight, + Padding = new Thickness(RowPadding, 0, 12, 0), + HorizontalContentAlignment = HorizontalAlignment.Stretch, + IsTabStop = false, + AllowFocusOnInteraction = false, + }; + return new Row(container, icon, label, detail); + } + + private static void Fill(Row row, CompletionItem item) + { + row.Icon.Symbol = Icon(item.Kind); + FillLabel(row.Label, item); + row.Detail.Text = Describe(item); + AutomationProperties.SetName(row.Container, Name(item)); + } + + // The characters the word matched are semibold, as Windows search marks them. + private static void FillLabel(TextBlock label, CompletionItem item) + { + label.Inlines.Clear(); + var text = item.Label; + var at = 0; + foreach (var span in item.Matched) + { + var start = Math.Clamp(span.Start, at, text.Length); + var end = Math.Clamp(span.Start + span.Length, start, text.Length); + if (start > at) + { + label.Inlines.Add(new Run { Text = text[at..start] }); + } + if (end > start) + { + label.Inlines.Add(new Run { Text = text[start..end], FontWeight = FontWeights.SemiBold }); + } + at = end; + } + if (at < text.Length) + { + label.Inlines.Add(new Run { Text = text[at..] }); + } + } + + private void OnDoubleTapped(object sender, DoubleTappedRoutedEventArgs e) + { + if (Selected is not null) + { + e.Handled = true; + Accepted?.Invoke(this, EventArgs.Empty); + } + } + + private sealed record Row(ListViewItem Container, SymbolIcon Icon, TextBlock Label, TextBlock Detail); +} diff --git a/windows/src/TablePro.Editor/ICompletionSource.cs b/windows/src/TablePro.Editor/ICompletionSource.cs new file mode 100644 index 0000000000..4ac185e1ca --- /dev/null +++ b/windows/src/TablePro.Editor/ICompletionSource.cs @@ -0,0 +1,10 @@ +using TablePro.Interop; + +namespace TablePro.Editor; + +// What the editor completes from: a connection's catalog, through DatabaseSession.CompleteAsync. +public interface ICompletionSource +{ + // Caret is in UTF-16 units of text. Null where nothing completes. + Task CompleteAsync(string text, int caret, bool asked, CancellationToken cancellationToken); +} diff --git a/windows/src/TablePro.Editor/SqlEditor.Completion.cs b/windows/src/TablePro.Editor/SqlEditor.Completion.cs new file mode 100644 index 0000000000..200c85c5ba --- /dev/null +++ b/windows/src/TablePro.Editor/SqlEditor.Completion.cs @@ -0,0 +1,460 @@ +using Microsoft.UI.Xaml.Automation; +using TablePro.Editor.Model; +using TablePro.Editor.Model.Vim; +using TablePro.Interop; +using Windows.System; + +namespace TablePro.Editor; + +// The core decides what completes and how it is written; the editor shows the list and inserts the choice, as the macOS +// editor's list behaves (windows/docs/decisions/0002-sql-editor-control.md, Completion and format). +public sealed partial class SqlEditor +{ + private const int MaxShown = 100; + private const int LongestWord = 512; + + // A script of megabytes is not copied and lexed on each key: a completion reads the lines around the caret. + private const int ContextBefore = 128 * 1024; + private const int ContextAfter = 32 * 1024; + + private static readonly TimeSpan CompletionDelay = TimeSpan.FromMilliseconds(50); + + private ICompletionSource? _completionSource; + private CompletionPopup? _completionPopup; + private SqlCompletion? _completion; + private CancellationTokenSource? _completionRequest; + private Microsoft.UI.Dispatching.DispatcherQueueTimer? _completionTimer; + private int _completionStart = -1; + private (int Start, string Word)? _unmatched; + private int _rankGeneration; + private (double WordX, double CaretX, double CaretY)? _placedAt; + private bool _completionHooked; + + // Null leaves the editor without completion. + public ICompletionSource? CompletionSource + { + get => _completionSource; + set + { + CloseCompletion(); + _completionSource = value; + HookCompletion(); + } + } + + public bool IsCompletionOpen => _completionPopup?.IsOpen == true; + + // Ctrl+Space: anywhere a name or a keyword can go, with or without a word typed. + public void ShowCompletion() => RequestCompletion(asked: true); + + public void CloseCompletion() + { + _completionTimer?.Stop(); + CancelCompletionRequest(); + _rankGeneration++; + _completion?.Dispose(); + _completion = null; + _completionStart = -1; + _placedAt = null; + if (_completionPopup is { IsOpen: true } popup) + { + popup.Close(); + AutomationProperties.GetControlledPeers(this).Remove(popup.List); + } + } + + private void CancelCompletionRequest() + { + _completionRequest?.Cancel(); + _completionRequest = null; + } + + private void HookCompletion() + { + if (_completionHooked) + { + return; + } + _completionHooked = true; + _model.SelectionsChanged += (_, _) => + { + _completionTimer?.Stop(); + CancelCompletionRequest(); + FollowCaret(); + }; + Document.Changed += (_, change) => + { + CancelCompletionRequest(); + if (_unmatched is { } unmatched && change.Offset < unmatched.Start) + { + _unmatched = null; + } + if (_completionStart >= 0 && change.Offset < _completionStart) + { + CloseCompletion(); + } + }; + Document.Reset += (_, _) => + { + _unmatched = null; + CloseCompletion(); + }; + Unloaded += (_, _) => CloseCompletion(); + LostFocus += (_, _) => CloseCompletion(); + ActualThemeChanged += (_, _) => CloseCompletion(); + FramePresented += (_, _) => PlaceCompletion(); + } + + // The keys the list takes while it shows, ahead of Vim; another chord closes it and goes on to the editor. + private bool HandleCompletionKey(VirtualKey key, bool control, bool shift, bool alt) + { + if (IsModifier(key)) + { + return false; + } + if (_completionPopup is not { IsOpen: true } popup) + { + // A list still being read never opens after Escape, which still goes on to the editor. + if (key == VirtualKey.Escape) + { + CloseCompletion(); + } + return false; + } + if (control || alt) + { + CloseCompletion(); + return false; + } + switch (key) + { + case VirtualKey.Up: + popup.Move(-1); + return true; + case VirtualKey.Down: + popup.Move(1); + return true; + case VirtualKey.PageUp: + popup.Move(-popup.PageRows); + return true; + case VirtualKey.PageDown: + popup.Move(popup.PageRows); + return true; + // As VS Code's smart Enter: a row that changes the word as typed, else the line break, so `FROM Track` then + // Enter starts a new line. + case VirtualKey.Enter when !shift: + var typed = WordFrom(_completionStart); + if (typed is null || typed != popup.Word || popup.Selected?.InsertText == typed) + { + CloseCompletion(); + return false; + } + AcceptCompletion(); + return true; + case VirtualKey.Tab when !shift: + AcceptCompletion(); + return true; + case VirtualKey.Escape: + CloseCompletion(); + // Vim's Escape also leaves Insert mode, as it does with Vim's own list. + return _vim is null; + default: + return false; + } + } + + private static bool IsModifier(VirtualKey key) => key is VirtualKey.Shift or VirtualKey.Control or VirtualKey.Menu + or VirtualKey.LeftShift or VirtualKey.RightShift or VirtualKey.LeftControl or VirtualKey.RightControl + or VirtualKey.LeftMenu or VirtualKey.RightMenu or VirtualKey.LeftWindows or VirtualKey.RightWindows; + + // Typing that put text in the document; Vim's keys outside Insert mode never open a list. + private void AfterTyped(string text) + { + if (_completionSource is null || text.Length == 0 || (_vim is { } vim && !vim.Mode.IsInsert())) + { + return; + } + if (text.All(TextUnits.IsWordChar)) + { + // An open list narrows as the caret moves, and a longer word than one that matched nothing matches nothing. + if (IsCompletionOpen || _completionRequest is not null || ExtendsUnmatched()) + { + return; + } + ScheduleCompletion(); + return; + } + _unmatched = null; + // A place such as FROM or `alias.` asks for a name before one is typed. + if (text is "." or " " or "," or "(") + { + ScheduleCompletion(); + } + } + + private void ScheduleCompletion() + { + if (_completionTimer is null) + { + _completionTimer = DispatcherQueue.CreateTimer(); + _completionTimer.Interval = CompletionDelay; + _completionTimer.IsRepeating = false; + _completionTimer.Tick += (_, _) => RequestCompletion(asked: false); + } + _completionTimer.Stop(); + _completionTimer.Start(); + } + + private async void RequestCompletion(bool asked) + { + _completionTimer?.Stop(); + if (_completionSource is not { } source || _disposed || _model.HasMultipleSelections || !_model.Primary.IsEmpty + || IsComposing || (_vim is { } vim && !vim.Mode.IsInsert())) + { + return; + } + _completionRequest?.Cancel(); + var request = new CancellationTokenSource(); + _completionRequest = request; + var caret = _model.Primary.Active; + var (from, text) = CompletionContext(caret); + SqlCompletion? completion; + try + { + completion = await source.CompleteAsync(text, caret - from, asked, request.Token).ConfigureAwait(true); + } + catch (OperationCanceledException) + { + return; + } + catch (TableProException) + { + // A catalog the connection cannot read now offers nothing. + completion = null; + } + catch (ObjectDisposedException) + { + // The tab closed under the read. + completion = null; + } + finally + { + if (ReferenceEquals(_completionRequest, request)) + { + _completionRequest = null; + } + request.Dispose(); + } + if (request.IsCancellationRequested || IsComposing || _disposed) + { + completion?.Dispose(); + return; + } + if (completion is null) + { + if (asked) + { + CloseCompletion(); + } + return; + } + // The answer still has to be for the word at the caret. + var start = from + completion.WordStart; + if (WordFrom(start) is not { } word || word != completion.Word) + { + completion.Dispose(); + return; + } + CloseCompletion(); + _completion = completion; + _completionStart = start; + Narrow(word); + } + + private void FollowCaret() + { + if (_completionStart < 0) + { + return; + } + if (WordFrom(_completionStart) is { } word) + { + Narrow(word); + } + else + { + CloseCompletion(); + } + } + + // The word from start to the caret, or null once the caret has left it. + private string? WordFrom(int start) + { + if (_model.HasMultipleSelections || !_model.Primary.IsEmpty) + { + return null; + } + var caret = _model.Primary.Active; + if (start < 0 || caret < start || caret - start > LongestWord) + { + return null; + } + var word = Document.GetText(start, caret - start); + return word.All(TextUnits.IsWordChar) ? word : null; + } + + private bool ExtendsUnmatched() + { + if (_unmatched is not { } unmatched || WordFrom(unmatched.Start) is not { } word) + { + return false; + } + return word.StartsWith(unmatched.Word, StringComparison.Ordinal) + && WordStartBefore(_model.Primary.Active) == unmatched.Start; + } + + private int WordStartBefore(int caret) + { + var start = caret; + while (start > 0 && caret - start < LongestWord && TextUnits.IsWordChar(Document.CharAt(start - 1))) + { + start--; + } + return start; + } + + // Ranking 50,000 names takes milliseconds, so it runs off the UI thread and only the last word's ranking lands. + private async void Narrow(string word) + { + if (_completion is not { } completion) + { + return; + } + var generation = ++_rankGeneration; + CompletionPage page; + try + { + page = await Task.Run(() => completion.Rank(word, MaxShown)).ConfigureAwait(true); + } + catch (ObjectDisposedException) + { + return; + } + if (generation != _rankGeneration || !ReferenceEquals(completion, _completion) || IsComposing || _disposed) + { + return; + } + if (page.Total == 0) + { + _unmatched = (_completionStart, word); + CloseCompletion(); + return; + } + if (_completionPopup is null) + { + _completionPopup = new CompletionPopup(_root); + _completionPopup.SelectionChanged += (_, _) => AnnounceCompletion(); + _completionPopup.Accepted += (_, _) => AcceptCompletion(); + } + var popup = _completionPopup; + var opening = !popup.IsOpen; + popup.Show(page, word, ActualTheme); + if (opening) + { + AutomationProperties.GetControlledPeers(this).Add(popup.List); + } + _placedAt = null; + PlaceCompletion(); + } + + // The choice replaces the whole word the caret is in, as one undo step. + private async void AcceptCompletion() + { + if (_completionPopup is not { IsOpen: true } popup || _completion is not { } completion) + { + return; + } + var start = _completionStart; + if (WordFrom(start) is not { } word) + { + CloseCompletion(); + return; + } + var item = popup.Selected; + // A key typed since the rows were ranked: the closest match for the word as it is now, ranked off the UI + // thread too, and dropped if the text or the list moved on meanwhile. + if (word != popup.Word) + { + var version = Document.Version; + try + { + var closest = await Task.Run(() => completion.Rank(word, 1)).ConfigureAwait(true); + item = closest.Items.Count > 0 ? closest.Items[0] : null; + } + catch (ObjectDisposedException) + { + return; + } + if (Document.Version != version || !ReferenceEquals(completion, _completion) || WordFrom(start) != word) + { + return; + } + } + var caret = _model.Primary.Active; + CloseCompletion(); + if (item is null) + { + return; + } + var end = caret; + while (end < Document.Length && end - caret < LongestWord && TextUnits.IsWordChar(Document.CharAt(end))) + { + end++; + } + var text = item.InsertText; + Edit(() => _model.ReplaceRange(start, end, text)); + } + + private (int From, string Text) CompletionContext(int caret) + { + var line = Document.GetLineFromOffset(Math.Max(0, caret - ContextBefore)); + // From a line outside any token, so a long comment or string is never read as SQL. + while (line > 0 && (_highlighter is null || !_highlighter.IsLineClean(line))) + { + line--; + } + var from = Document.GetLineStart(line); + var to = Math.Min(Document.Length, caret + ContextAfter); + return (from, Document.GetText(from, to - from)); + } + + private void PlaceCompletion() + { + if (_completionPopup is not { IsOpen: true } popup || _completionStart < 0) + { + return; + } + var caret = CaretRect(_model.Primary.Active); + if (caret.Bottom < 0 || caret.Top > ViewHeight) + { + CloseCompletion(); + return; + } + var word = CaretRect(_completionStart); + var at = (word.X, caret.X, caret.Y); + if (at == _placedAt) + { + return; + } + _placedAt = at; + popup.Place(word, caret, ViewWidth, ViewHeight); + } + + private void AnnounceCompletion() + { + if (_completionPopup is { IsOpen: true } popup) + { + _peer?.Announce(popup.Announcement); + } + } +} diff --git a/windows/src/TablePro.Editor/SqlEditor.Format.cs b/windows/src/TablePro.Editor/SqlEditor.Format.cs new file mode 100644 index 0000000000..beaf14e3a8 --- /dev/null +++ b/windows/src/TablePro.Editor/SqlEditor.Format.cs @@ -0,0 +1,60 @@ +using TablePro.Editor.Model; +using TablePro.Interop; + +namespace TablePro.Editor; + +// Format Query reflows the selection, or the whole text without one, as the macOS editor's does. +public sealed partial class SqlEditor +{ + private bool _formatting; + + // The formatter knows only SQL's layout. + public bool CanFormat => _lexer is CoreSqlLexer { Grammar: not (SqlGrammar.Redis or SqlGrammar.Mongo) }; + + // One undo step. The caret keeps the characters around it; a formatted selection leaves the caret after it. + public async Task FormatAsync() + { + if (_lexer is not CoreSqlLexer lexer || !CanFormat || _formatting) + { + return; + } + CloseCompletion(); + _model.CollapseToPrimary(); + var selection = _model.Primary; + var whole = Document.Snapshot(); + var (start, end) = selection.IsEmpty ? (0, whole.Length) : (selection.Start, selection.End); + if (string.IsNullOrWhiteSpace(whole[start..end])) + { + return; + } + var version = Document.Version; + var grammar = lexer.Grammar; + var caret = selection.Active; + FormattedSql formatted; + _formatting = true; + try + { + formatted = await Task.Run(() => selection.IsEmpty + ? SqlFormatter.Format(grammar, whole, caret) + : new FormattedSql(SqlFormatter.FormatSelection(grammar, whole, start, end), 0)) + .ConfigureAwait(true); + } + catch (TableProException) + { + return; + } + finally + { + _formatting = false; + } + // Text typed while the format ran wins over the format. + if (_disposed || Document.Version != version || formatted.Text == whole[start..end]) + { + return; + } + var after = selection.IsEmpty + ? Selection.Caret(formatted.Caret) + : Selection.Caret(start + formatted.Text.Length); + Edit(() => _model.ReplaceRange(start, end, formatted.Text, after)); + } +} diff --git a/windows/src/TablePro.Editor/SqlEditor.Ime.cs b/windows/src/TablePro.Editor/SqlEditor.Ime.cs index d3d485b537..fe7a6b2d6c 100644 --- a/windows/src/TablePro.Editor/SqlEditor.Ime.cs +++ b/windows/src/TablePro.Editor/SqlEditor.Ime.cs @@ -73,11 +73,16 @@ private sealed class TsfHost(SqlEditor editor) : ITsfHost public void Commit(string text) { editor._composition = string.Empty; + var version = editor.Document.Version; // Outside Insert mode an input method's text is Vim's keys, not text for the document. if (!editor.RouteVimText(text, shift: false)) { editor.TypeText(text); } + if (editor.Document.Version != version) + { + editor.AfterTyped(text); + } editor.CompositionCommitted?.Invoke(editor, text); } @@ -88,6 +93,11 @@ public void ShowComposition(string text, int caret, IReadOnlyList 0) + { + editor.CloseCompletion(); + } editor._composition = text; editor._compositionCaret = caret; editor._compositionRuns = runs; diff --git a/windows/src/TablePro.Editor/SqlEditor.Keyboard.cs b/windows/src/TablePro.Editor/SqlEditor.Keyboard.cs index 304c156021..ddd3f5e4fa 100644 --- a/windows/src/TablePro.Editor/SqlEditor.Keyboard.cs +++ b/windows/src/TablePro.Editor/SqlEditor.Keyboard.cs @@ -31,6 +31,12 @@ protected override void OnKeyDown(KeyRoutedEventArgs e) var control = Down(VirtualKey.Control); var shift = Down(VirtualKey.Shift); var alt = Down(VirtualKey.Menu); + // An open completion list takes its keys first, Insert mode's Enter and Tab included. + if (HandleCompletionKey(e.Key, control, shift, alt)) + { + e.Handled = true; + return; + } // Vim sees a key before the editor's own commands, and the app's accelerators come after both. e.Handled = _vim is null ? HandleKey(e.Key, control, shift, alt) : HandleVimKey(e.Key, control, shift, alt); if (!e.Handled) @@ -149,9 +155,16 @@ public bool HandleKey(VirtualKey key, bool control, bool shift, bool alt) case VirtualKey.D: Edit(_model.SelectNextOccurrence); return true; + // As on Linux. Query > Filter rows has the same keys only in a table tab, which holds no editor. + case VirtualKey.F when shift: + _ = FormatAsync(); + return true; case VirtualKey.F: OpenFind(); return true; + case VirtualKey.Space when !shift: + ShowCompletion(); + return true; // Ctrl+Shift+H is the app's Show Connections. case VirtualKey.H when !shift: OpenReplace(); @@ -208,10 +221,15 @@ private void OnCharacterReceived(Microsoft.UI.Xaml.UIElement sender, CharacterRe } var text = char.IsLowSurrogate(c) && _pendingHighSurrogate != 0 ? new string([_pendingHighSurrogate, c]) : c.ToString(); _pendingHighSurrogate = '\0'; + var version = Document.Version; if (!RouteVimText(text, Down(VirtualKey.Shift))) { TypeText(text); } + if (Document.Version != version) + { + AfterTyped(text); + } e.Handled = true; } diff --git a/windows/src/TablePro.Editor/SqlEditor.Pointer.cs b/windows/src/TablePro.Editor/SqlEditor.Pointer.cs index a36ddfe272..9067ffc1fe 100644 --- a/windows/src/TablePro.Editor/SqlEditor.Pointer.cs +++ b/windows/src/TablePro.Editor/SqlEditor.Pointer.cs @@ -406,6 +406,7 @@ MenuFlyoutItem Add(string name, Action action) menu.Items.Add(new MenuFlyoutSeparator()); Add("EditorMenuSelectAll", () => _model.SelectAll()); Add("EditorMenuToggleComment", () => Edit(_model.ToggleLineComment)); + var format = Add("EditorMenuFormat", () => _ = FormatAsync()); var foldSeparator = new MenuFlyoutSeparator(); menu.Items.Add(foldSeparator); var fold = Add("EditorMenuFold", () => FoldAtCaret()); @@ -419,6 +420,7 @@ MenuFlyoutItem Add(string name, Action action) { menu.Items.RemoveAt(menu.Items.Count - 1); } + format.Visibility = CanFormat ? Visibility.Visible : Visibility.Collapsed; // Fold reads the caret's line, as Ctrl+Shift+[ does; Unfold opens the fold on it. var regions = RegionsNow(); var folding = _foldingEnabled && regions.Count > 0; diff --git a/windows/src/TablePro.Editor/SqlEditor.cs b/windows/src/TablePro.Editor/SqlEditor.cs index 8810bfb8ac..c95be7098c 100644 --- a/windows/src/TablePro.Editor/SqlEditor.cs +++ b/windows/src/TablePro.Editor/SqlEditor.cs @@ -261,6 +261,7 @@ public void Dispose() { return; } + CloseCompletion(); _disposed = true; Unhook(); _highlighter?.Dispose(); diff --git a/windows/src/TablePro.Interop/DatabaseSession.cs b/windows/src/TablePro.Interop/DatabaseSession.cs index b895fd5dfe..d9f9bf7387 100644 --- a/windows/src/TablePro.Interop/DatabaseSession.cs +++ b/windows/src/TablePro.Interop/DatabaseSession.cs @@ -225,6 +225,25 @@ public RunGate GateRun(string sql) : null; } + // What completes at caret for an editor in database, null for the connection's own. Null where nothing does, and + // where nothing is typed yet in a place that takes anything unless asked. The catalog reads are the object tree's. + public async Task CompleteAsync( + string text, + int caret, + bool asked, + string? database = null, + CancellationToken cancellationToken = default) + { + ArgumentNullException.ThrowIfNull(text); + ArgumentOutOfRangeException.ThrowIfNegative(caret); + ArgumentOutOfRangeException.ThrowIfGreaterThan(caret, text.Length); + var list = await CoreCall.RunAsync( + token => _session.Complete(text, checked((uint)caret), asked, database, token), + cancellationToken) + .ConfigureAwait(false); + return list is null ? null : new SqlCompletion(list); + } + // A level saved while the session is open applies to it at once. False when it crosses Read only, which only a // new connection can do; the session keeps its level until then. public bool SetSafeMode(SafeMode level) => _session.SetSafeMode(Mapping.Level(level)); diff --git a/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs b/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs index 7eda6ba664..e58880874d 100644 --- a/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs +++ b/windows/src/TablePro.Interop/Generated/tablepro_ffi.cs @@ -1425,6 +1425,20 @@ public struct UniffiVTableCallbackInterfaceRunListener + + + + + + + + + + + + + + @@ -1884,6 +1898,61 @@ public static extern RustBuffer uniffi_tablepro_ffi_fn_method_catalogchange_statements(ulong @ptr,ref UniffiRustCallStatus _uniffi_out_err ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ulong uniffi_tablepro_ffi_fn_clone_completionlist(ulong @handle,ref UniffiRustCallStatus _uniffi_out_err + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + void uniffi_tablepro_ffi_fn_free_completionlist(ulong @handle,ref UniffiRustCallStatus _uniffi_out_err + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + RustBuffer uniffi_tablepro_ffi_fn_method_completionlist_rank(ulong @ptr,RustBuffer @word,uint @limit,ref UniffiRustCallStatus _uniffi_out_err + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + RustBuffer uniffi_tablepro_ffi_fn_method_completionlist_word(ulong @ptr,ref UniffiRustCallStatus _uniffi_out_err + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + uint uniffi_tablepro_ffi_fn_method_completionlist_word_start(ulong @ptr,ref UniffiRustCallStatus _uniffi_out_err + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -3776,6 +3845,17 @@ public static extern ulong uniffi_tablepro_ffi_fn_method_databasesession_columns(ulong @ptr,RustBuffer @table,RustBuffer @cancel ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ulong uniffi_tablepro_ffi_fn_method_databasesession_complete(ulong @ptr,RustBuffer @text,uint @caret,sbyte @asked,RustBuffer @database,RustBuffer @cancel + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -4359,6 +4439,28 @@ public static extern RustBuffer uniffi_tablepro_ffi_fn_func_parse_app_link(RustBuffer @link,ref UniffiRustCallStatus _uniffi_out_err ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + RustBuffer uniffi_tablepro_ffi_fn_func_format_sql(RustBuffer @text,RustBuffer @grammar,uint @caret,ref UniffiRustCallStatus _uniffi_out_err + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + RustBuffer uniffi_tablepro_ffi_fn_func_format_sql_selection(RustBuffer @text,RustBuffer @grammar,uint @start,uint @end,ref UniffiRustCallStatus _uniffi_out_err + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -5074,6 +5176,28 @@ public static extern ushort uniffi_tablepro_ffi_checksum_func_parse_app_link( ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_func_format_sql( + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_func_format_sql_selection( + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -5437,6 +5561,39 @@ public static extern ushort uniffi_tablepro_ffi_checksum_method_catalogchange_statements( ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_completionlist_rank( + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_completionlist_word( + ); + + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_completionlist_word_start( + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -6922,6 +7079,17 @@ public static extern ushort uniffi_tablepro_ffi_checksum_method_databasesession_columns( ); + #if NET8_0_OR_GREATER + [LibraryImport("tablepro_ffi")] + [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] + public static partial +#else + [DllImport("tablepro_ffi", CallingConvention = CallingConvention.Cdecl)] + public static extern +#endif + ushort uniffi_tablepro_ffi_checksum_method_databasesession_complete( + ); + #if NET8_0_OR_GREATER [LibraryImport("tablepro_ffi")] [UnmanagedCallConv(CallConvs = new[] { typeof(CallConvCdecl) })] @@ -7595,6 +7763,18 @@ static void uniffiCheckApiChecksums() { throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_func_parse_app_link` checksum `17722`, library returned `{checksum}`"); } } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_func_format_sql(); + if (checksum != 59571) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_func_format_sql` checksum `59571`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_func_format_sql_selection(); + if (checksum != 33116) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_func_format_sql_selection` checksum `33116`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_func_parse_connection_url(); if (checksum != 48047) { @@ -7793,6 +7973,24 @@ static void uniffiCheckApiChecksums() { throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_catalogchange_statements` checksum `34813`, library returned `{checksum}`"); } } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_completionlist_rank(); + if (checksum != 212) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_completionlist_rank` checksum `212`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_completionlist_word(); + if (checksum != 64307) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_completionlist_word` checksum `64307`, library returned `{checksum}`"); + } + } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_completionlist_word_start(); + if (checksum != 50621) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_completionlist_word_start` checksum `50621`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_core_open_account(); if (checksum != 36884) { @@ -8603,6 +8801,12 @@ static void uniffiCheckApiChecksums() { throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_columns` checksum `27131`, library returned `{checksum}`"); } } + { + var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_complete(); + if (checksum != 48089) { + throw new UniffiContractChecksumException($"TablePro.Interop.Native: uniffi bindings expected function `uniffi_tablepro_ffi_checksum_method_databasesession_complete` checksum `48089`, library returned `{checksum}`"); + } + } { var checksum = _UniFFILib.uniffi_tablepro_ffi_checksum_method_databasesession_database(); if (checksum != 46099) { @@ -10234,7 +10438,155 @@ public async Task Run(bool @confirmed, CancelToken? @cance public string[] Statements() { return CallWithPointer(thisPtr => FfiConverterSequenceString.INSTANCE.Lift( _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => - _UniFFILib.uniffi_tablepro_ffi_fn_method_catalogchange_statements(thisPtr, ref _status) + _UniFFILib.uniffi_tablepro_ffi_fn_method_catalogchange_statements(thisPtr, ref _status) +))); + } + + + + +} +class FfiConverterTypeCatalogChange: FfiConverter { + public static FfiConverterTypeCatalogChange INSTANCE = new FfiConverterTypeCatalogChange(); + + + public override ulong Lower(CatalogChange value) { + return value.CallWithPointer(thisPtr => thisPtr); + } + + public override CatalogChange Lift(ulong value) { + return new CatalogChange(value); + } + + public override CatalogChange Read(BigEndianStream stream) { + return Lift(stream.ReadULong()); + } + + public override int AllocationSize(CatalogChange value) { + return 8; + } + + public override void Write(CatalogChange value, BigEndianStream stream) { + stream.WriteULong(Lower(value)); + } +} + + + +internal interface ICompletionList { + CompletionPage Rank(string @word, uint @limit); + string Word(); + uint WordStart(); +} +internal class CompletionList : ICompletionList, IDisposable { + protected ulong pointer; + private int _wasDestroyed = 0; + private long _callCounter = 1; + + public CompletionList(ulong pointer) { + this.pointer = pointer; + } + + ~CompletionList() { + Destroy(); + } + + protected void FreeRustArcPtr() { + _UniffiHelpers.RustCall((ref UniffiRustCallStatus status) => { + _UniFFILib.uniffi_tablepro_ffi_fn_free_completionlist(this.pointer, ref status); + }); + } + + protected ulong CloneRustArcPtr() { + return _UniffiHelpers.RustCall((ref UniffiRustCallStatus status) => { + return _UniFFILib.uniffi_tablepro_ffi_fn_clone_completionlist(this.pointer, ref status); + }); + } + + public void Destroy() + { + // Only allow a single call to this method. + if (Interlocked.CompareExchange(ref _wasDestroyed, 1, 0) == 0) + { + // This decrement always matches the initial count of 1 given at creation time. + if (Interlocked.Decrement(ref _callCounter) == 0) + { + FreeRustArcPtr(); + } + } + } + + public void Dispose() + { + Destroy(); + GC.SuppressFinalize(this); // Suppress finalization to avoid unnecessary GC overhead. + } + + private void IncrementCallCounter() + { + // Check and increment the call counter, to keep the object alive. + // This needs a compare-and-set retry loop in case of concurrent updates. + long count; + do + { + count = Interlocked.Read(ref _callCounter); + if (count == 0L) throw new System.ObjectDisposedException(String.Format("'{0}' object has already been destroyed", this.GetType().Name)); + if (count == long.MaxValue) throw new System.OverflowException(String.Format("'{0}' call counter would overflow", this.GetType().Name)); + + } while (Interlocked.CompareExchange(ref _callCounter, count + 1, count) != count); + } + + private void DecrementCallCounter() + { + // This decrement always matches the increment we performed above. + if (Interlocked.Decrement(ref _callCounter) == 0) { + FreeRustArcPtr(); + } + } + + internal void CallWithPointer(Action action) + { + IncrementCallCounter(); + try { + action(CloneRustArcPtr()); + } + finally { + DecrementCallCounter(); + } + } + + internal T CallWithPointer(Func func) + { + IncrementCallCounter(); + try { + return func(CloneRustArcPtr()); + } + finally { + DecrementCallCounter(); + } + } + + + public CompletionPage Rank(string @word, uint @limit) { + return CallWithPointer(thisPtr => FfiConverterTypeCompletionPage.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_method_completionlist_rank(thisPtr, FfiConverterString.INSTANCE.Lower(@word), FfiConverterUInt32.INSTANCE.Lower(@limit), ref _status) +))); + } + + + public string Word() { + return CallWithPointer(thisPtr => FfiConverterString.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_method_completionlist_word(thisPtr, ref _status) +))); + } + + + public uint WordStart() { + return CallWithPointer(thisPtr => FfiConverterUInt32.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_method_completionlist_word_start(thisPtr, ref _status) ))); } @@ -10242,27 +10594,27 @@ public string[] Statements() { } -class FfiConverterTypeCatalogChange: FfiConverter { - public static FfiConverterTypeCatalogChange INSTANCE = new FfiConverterTypeCatalogChange(); +class FfiConverterTypeCompletionList: FfiConverter { + public static FfiConverterTypeCompletionList INSTANCE = new FfiConverterTypeCompletionList(); - public override ulong Lower(CatalogChange value) { + public override ulong Lower(CompletionList value) { return value.CallWithPointer(thisPtr => thisPtr); } - public override CatalogChange Lift(ulong value) { - return new CatalogChange(value); + public override CompletionList Lift(ulong value) { + return new CompletionList(value); } - public override CatalogChange Read(BigEndianStream stream) { + public override CompletionList Read(BigEndianStream stream) { return Lift(stream.ReadULong()); } - public override int AllocationSize(CatalogChange value) { + public override int AllocationSize(CompletionList value) { return 8; } - public override void Write(CatalogChange value, BigEndianStream stream) { + public override void Write(CompletionList value, BigEndianStream stream) { stream.WriteULong(Lower(value)); } } @@ -11454,6 +11806,8 @@ internal interface IDatabaseSession { Task Close(); /// Task Columns(TableName @table, CancelToken? @cancel); + /// + Task Complete(string @text, uint @caret, bool @asked, string? @database, CancelToken? @cancel); string? Database(); DatabaseLayoutTag DatabaseLayout(); /// @@ -11793,6 +12147,28 @@ public async Task Columns(TableName @table, CancelToken? @cancel) ); } + /// + public async Task Complete(string @text, uint @caret, bool @asked, string? @database, CancelToken? @cancel) { + return await _UniFFIAsync.UniffiRustCallAsync( + // Get rust future + CallWithPointer(thisPtr => { + return _UniFFILib.uniffi_tablepro_ffi_fn_method_databasesession_complete(thisPtr, FfiConverterString.INSTANCE.Lower(@text), FfiConverterUInt32.INSTANCE.Lower(@caret), FfiConverterBoolean.INSTANCE.Lower(@asked), FfiConverterOptionalString.INSTANCE.Lower(@database), FfiConverterOptionalTypeCancelToken.INSTANCE.Lower(@cancel)); + }), + // Poll + (ulong future, IntPtr continuation, ulong data) => _UniFFILib.ffi_tablepro_ffi_rust_future_poll_rust_buffer(future, continuation, data), + // Complete + (ulong future, ref UniffiRustCallStatus status) => { + return _UniFFILib.ffi_tablepro_ffi_rust_future_complete_rust_buffer(future, ref status); + }, + // Free + (ulong future) => _UniFFILib.ffi_tablepro_ffi_rust_future_free_rust_buffer(future), + // Lift + (result) => FfiConverterOptionalTypeCompletionList.INSTANCE.Lift(result), + // Error + FfiConverterTypeCoreError.INSTANCE + ); + } + public string? Database() { return CallWithPointer(thisPtr => FfiConverterOptionalString.INSTANCE.Lift( _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => @@ -16276,6 +16652,82 @@ public override void Write(ColumnSort value, BigEndianStream stream) { +internal record CompletionItem ( + string Label, + string InsertText, + CompletionKindTag Kind, + string? DataType, + TableName? Table, + MatchedSpan[] Matched +) { +} + +class FfiConverterTypeCompletionItem: FfiConverterRustBuffer { + public static FfiConverterTypeCompletionItem INSTANCE = new FfiConverterTypeCompletionItem(); + + public override CompletionItem Read(BigEndianStream stream) { + return new CompletionItem( + Label: FfiConverterString.INSTANCE.Read(stream), + InsertText: FfiConverterString.INSTANCE.Read(stream), + Kind: FfiConverterTypeCompletionKindTag.INSTANCE.Read(stream), + DataType: FfiConverterOptionalString.INSTANCE.Read(stream), + Table: FfiConverterOptionalTypeTableName.INSTANCE.Read(stream), + Matched: FfiConverterSequenceTypeMatchedSpan.INSTANCE.Read(stream) + ); + } + + public override int AllocationSize(CompletionItem value) { + return 0 + + FfiConverterString.INSTANCE.AllocationSize(value.Label) + + FfiConverterString.INSTANCE.AllocationSize(value.InsertText) + + FfiConverterTypeCompletionKindTag.INSTANCE.AllocationSize(value.Kind) + + FfiConverterOptionalString.INSTANCE.AllocationSize(value.DataType) + + FfiConverterOptionalTypeTableName.INSTANCE.AllocationSize(value.Table) + + FfiConverterSequenceTypeMatchedSpan.INSTANCE.AllocationSize(value.Matched); + } + + public override void Write(CompletionItem value, BigEndianStream stream) { + FfiConverterString.INSTANCE.Write(value.Label, stream); + FfiConverterString.INSTANCE.Write(value.InsertText, stream); + FfiConverterTypeCompletionKindTag.INSTANCE.Write(value.Kind, stream); + FfiConverterOptionalString.INSTANCE.Write(value.DataType, stream); + FfiConverterOptionalTypeTableName.INSTANCE.Write(value.Table, stream); + FfiConverterSequenceTypeMatchedSpan.INSTANCE.Write(value.Matched, stream); + } +} + + + +internal record CompletionPage ( + uint Total, + CompletionItem[] Items +) { +} + +class FfiConverterTypeCompletionPage: FfiConverterRustBuffer { + public static FfiConverterTypeCompletionPage INSTANCE = new FfiConverterTypeCompletionPage(); + + public override CompletionPage Read(BigEndianStream stream) { + return new CompletionPage( + Total: FfiConverterUInt32.INSTANCE.Read(stream), + Items: FfiConverterSequenceTypeCompletionItem.INSTANCE.Read(stream) + ); + } + + public override int AllocationSize(CompletionPage value) { + return 0 + + FfiConverterUInt32.INSTANCE.AllocationSize(value.Total) + + FfiConverterSequenceTypeCompletionItem.INSTANCE.AllocationSize(value.Items); + } + + public override void Write(CompletionPage value, BigEndianStream stream) { + FfiConverterUInt32.INSTANCE.Write(value.Total, stream); + FfiConverterSequenceTypeCompletionItem.INSTANCE.Write(value.Items, stream); + } +} + + + internal record ConnectionRecord ( string Id, string Name, @@ -17656,6 +18108,36 @@ public override void Write(ForeignSourceInfo value, BigEndianStream stream) { +internal record FormattedSql ( + string Text, + uint Caret +) { +} + +class FfiConverterTypeFormattedSql: FfiConverterRustBuffer { + public static FfiConverterTypeFormattedSql INSTANCE = new FfiConverterTypeFormattedSql(); + + public override FormattedSql Read(BigEndianStream stream) { + return new FormattedSql( + Text: FfiConverterString.INSTANCE.Read(stream), + Caret: FfiConverterUInt32.INSTANCE.Read(stream) + ); + } + + public override int AllocationSize(FormattedSql value) { + return 0 + + FfiConverterString.INSTANCE.AllocationSize(value.Text) + + FfiConverterUInt32.INSTANCE.AllocationSize(value.Caret); + } + + public override void Write(FormattedSql value, BigEndianStream stream) { + FfiConverterString.INSTANCE.Write(value.Text, stream); + FfiConverterUInt32.INSTANCE.Write(value.Caret, stream); + } +} + + + internal record GroupRecord ( string Id, string Name, @@ -18237,6 +18719,36 @@ public override void Write(MaskSuggestionInfo value, BigEndianStream stream) { +internal record MatchedSpan ( + uint Start, + uint Length +) { +} + +class FfiConverterTypeMatchedSpan: FfiConverterRustBuffer { + public static FfiConverterTypeMatchedSpan INSTANCE = new FfiConverterTypeMatchedSpan(); + + public override MatchedSpan Read(BigEndianStream stream) { + return new MatchedSpan( + Start: FfiConverterUInt32.INSTANCE.Read(stream), + Length: FfiConverterUInt32.INSTANCE.Read(stream) + ); + } + + public override int AllocationSize(MatchedSpan value) { + return 0 + + FfiConverterUInt32.INSTANCE.AllocationSize(value.Start) + + FfiConverterUInt32.INSTANCE.AllocationSize(value.Length); + } + + public override void Write(MatchedSpan value, BigEndianStream stream) { + FfiConverterUInt32.INSTANCE.Write(value.Start, stream); + FfiConverterUInt32.INSTANCE.Write(value.Length, stream); + } +} + + + internal record McpWriteRequest ( ulong Id, string ConnectionId, @@ -20859,6 +21371,57 @@ public override void Write(CombinatorTag value, BigEndianStream stream) { +internal enum CompletionKindTag: int { + Keyword, + Schema, + Table, + View, + MaterializedView, + Column, + Function +} + +class FfiConverterTypeCompletionKindTag: FfiConverterRustBuffer { + public static FfiConverterTypeCompletionKindTag INSTANCE = new FfiConverterTypeCompletionKindTag(); + + public override CompletionKindTag Read(BigEndianStream stream) { + var value = stream.ReadInt(); + switch (value) { + case 1: return CompletionKindTag.Keyword; + case 2: return CompletionKindTag.Schema; + case 3: return CompletionKindTag.Table; + case 4: return CompletionKindTag.View; + case 5: return CompletionKindTag.MaterializedView; + case 6: return CompletionKindTag.Column; + case 7: return CompletionKindTag.Function; + default: throw new InternalException(String.Format("invalid enum value '{0}' in FfiConverterTypeCompletionKindTag.Read()", value)); + } + } + + public override int AllocationSize(CompletionKindTag value) { + return 4; + } + + public override void Write(CompletionKindTag value, BigEndianStream stream) { + switch (value) { + case CompletionKindTag.Keyword: stream.WriteInt(1); break; + case CompletionKindTag.Schema: stream.WriteInt(2); break; + case CompletionKindTag.Table: stream.WriteInt(3); break; + case CompletionKindTag.View: stream.WriteInt(4); break; + case CompletionKindTag.MaterializedView: stream.WriteInt(5); break; + case CompletionKindTag.Column: stream.WriteInt(6); break; + case CompletionKindTag.Function: stream.WriteInt(7); break; + default: throw new InternalException(String.Format("invalid enum value '{0}' in FfiConverterTypeCompletionKindTag.Write()", value)); + } + } +} + + + + + + + /// /// Why a call produced no value. A cancel is its own variant, so a /// binding can turn it into its language's cancellation. @@ -25265,6 +25828,63 @@ public override void Write(ShareRefusal value, BigEndianStream stream) { +internal enum SqlGrammarTag: int { + PostgreSql, + MySql, + Sqlite, + MsSql, + ClickHouse, + DuckDb, + Cql, + Redis, + Mongo +} + +class FfiConverterTypeSqlGrammarTag: FfiConverterRustBuffer { + public static FfiConverterTypeSqlGrammarTag INSTANCE = new FfiConverterTypeSqlGrammarTag(); + + public override SqlGrammarTag Read(BigEndianStream stream) { + var value = stream.ReadInt(); + switch (value) { + case 1: return SqlGrammarTag.PostgreSql; + case 2: return SqlGrammarTag.MySql; + case 3: return SqlGrammarTag.Sqlite; + case 4: return SqlGrammarTag.MsSql; + case 5: return SqlGrammarTag.ClickHouse; + case 6: return SqlGrammarTag.DuckDb; + case 7: return SqlGrammarTag.Cql; + case 8: return SqlGrammarTag.Redis; + case 9: return SqlGrammarTag.Mongo; + default: throw new InternalException(String.Format("invalid enum value '{0}' in FfiConverterTypeSqlGrammarTag.Read()", value)); + } + } + + public override int AllocationSize(SqlGrammarTag value) { + return 4; + } + + public override void Write(SqlGrammarTag value, BigEndianStream stream) { + switch (value) { + case SqlGrammarTag.PostgreSql: stream.WriteInt(1); break; + case SqlGrammarTag.MySql: stream.WriteInt(2); break; + case SqlGrammarTag.Sqlite: stream.WriteInt(3); break; + case SqlGrammarTag.MsSql: stream.WriteInt(4); break; + case SqlGrammarTag.ClickHouse: stream.WriteInt(5); break; + case SqlGrammarTag.DuckDb: stream.WriteInt(6); break; + case SqlGrammarTag.Cql: stream.WriteInt(7); break; + case SqlGrammarTag.Redis: stream.WriteInt(8); break; + case SqlGrammarTag.Mongo: stream.WriteInt(9); break; + default: throw new InternalException(String.Format("invalid enum value '{0}' in FfiConverterTypeSqlGrammarTag.Write()", value)); + } + } +} + + + + + + + internal record SshAuthTag { public record Agent: SshAuthTag {} @@ -27151,6 +27771,37 @@ public override void Write(CancelToken? value, BigEndianStream stream) { +class FfiConverterOptionalTypeCompletionList: FfiConverterRustBuffer { + public static FfiConverterOptionalTypeCompletionList INSTANCE = new FfiConverterOptionalTypeCompletionList(); + + public override CompletionList? Read(BigEndianStream stream) { + if (stream.ReadByte() == 0) { + return null; + } + return FfiConverterTypeCompletionList.INSTANCE.Read(stream); + } + + public override int AllocationSize(CompletionList? value) { + if (value == null) { + return 1; + } else { + return 1 + FfiConverterTypeCompletionList.INSTANCE.AllocationSize((CompletionList)value); + } + } + + public override void Write(CompletionList? value, BigEndianStream stream) { + if (value == null) { + stream.WriteByte(0); + } else { + stream.WriteByte(1); + FfiConverterTypeCompletionList.INSTANCE.Write((CompletionList)value, stream); + } + } +} + + + + class FfiConverterOptionalTypeExportProgress: FfiConverterRustBuffer { public static FfiConverterOptionalTypeExportProgress INSTANCE = new FfiConverterOptionalTypeExportProgress(); @@ -28789,6 +29440,52 @@ public override void Write(ColumnEntry[] value, BigEndianStream stream) { +class FfiConverterSequenceTypeCompletionItem: FfiConverterRustBuffer { + public static FfiConverterSequenceTypeCompletionItem INSTANCE = new FfiConverterSequenceTypeCompletionItem(); + + public override CompletionItem[] Read(BigEndianStream stream) { + var length = stream.ReadInt(); + if (length == 0) { + return []; + } + + var result = new CompletionItem[length]; + var readFn = FfiConverterTypeCompletionItem.INSTANCE.Read; + for (int i = 0; i < length; i++) { + result[i] = readFn(stream); + } + return result; + } + + public override int AllocationSize(CompletionItem[] value) { + var sizeForLength = 4; + + // details/1-empty-list-as-default-method-parameter.md + if (value == null) { + return sizeForLength; + } + + var allocationSizeFn = FfiConverterTypeCompletionItem.INSTANCE.AllocationSize; + var sizeForItems = value.Sum(item => allocationSizeFn(item)); + return sizeForLength + sizeForItems; + } + + public override void Write(CompletionItem[] value, BigEndianStream stream) { + // details/1-empty-list-as-default-method-parameter.md + if (value == null) { + stream.WriteInt(0); + return; + } + + stream.WriteInt(value.Length); + var writerFn = FfiConverterTypeCompletionItem.INSTANCE.Write; + value.ForEach(item => writerFn(item, stream)); + } +} + + + + class FfiConverterSequenceTypeConnectionRecord: FfiConverterRustBuffer { public static FfiConverterSequenceTypeConnectionRecord INSTANCE = new FfiConverterSequenceTypeConnectionRecord(); @@ -29617,6 +30314,52 @@ public override void Write(LicenseInfo[] value, BigEndianStream stream) { +class FfiConverterSequenceTypeMatchedSpan: FfiConverterRustBuffer { + public static FfiConverterSequenceTypeMatchedSpan INSTANCE = new FfiConverterSequenceTypeMatchedSpan(); + + public override MatchedSpan[] Read(BigEndianStream stream) { + var length = stream.ReadInt(); + if (length == 0) { + return []; + } + + var result = new MatchedSpan[length]; + var readFn = FfiConverterTypeMatchedSpan.INSTANCE.Read; + for (int i = 0; i < length; i++) { + result[i] = readFn(stream); + } + return result; + } + + public override int AllocationSize(MatchedSpan[] value) { + var sizeForLength = 4; + + // details/1-empty-list-as-default-method-parameter.md + if (value == null) { + return sizeForLength; + } + + var allocationSizeFn = FfiConverterTypeMatchedSpan.INSTANCE.AllocationSize; + var sizeForItems = value.Sum(item => allocationSizeFn(item)); + return sizeForLength + sizeForItems; + } + + public override void Write(MatchedSpan[] value, BigEndianStream stream) { + // details/1-empty-list-as-default-method-parameter.md + if (value == null) { + stream.WriteInt(0); + return; + } + + stream.WriteInt(value.Length); + var writerFn = FfiConverterTypeMatchedSpan.INSTANCE.Write; + value.ForEach(item => writerFn(item, stream)); + } +} + + + + class FfiConverterSequenceTypeMembershipInfo: FfiConverterRustBuffer { public static FfiConverterSequenceTypeMembershipInfo INSTANCE = new FfiConverterSequenceTypeMembershipInfo(); @@ -31393,6 +32136,22 @@ public static AppLinkInfo ParseAppLink(string @link) { } + public static FormattedSql FormatSql(string @text, SqlGrammarTag @grammar, uint @caret) { + return FfiConverterTypeFormattedSql.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_func_format_sql(FfiConverterString.INSTANCE.Lower(@text), FfiConverterTypeSqlGrammarTag.INSTANCE.Lower(@grammar), FfiConverterUInt32.INSTANCE.Lower(@caret), ref _status) +)); + } + + + public static string FormatSqlSelection(string @text, SqlGrammarTag @grammar, uint @start, uint @end) { + return FfiConverterString.INSTANCE.Lift( + _UniffiHelpers.RustCall( (ref UniffiRustCallStatus _status) => + _UniFFILib.uniffi_tablepro_ffi_fn_func_format_sql_selection(FfiConverterString.INSTANCE.Lower(@text), FfiConverterTypeSqlGrammarTag.INSTANCE.Lower(@grammar), FfiConverterUInt32.INSTANCE.Lower(@start), FfiConverterUInt32.INSTANCE.Lower(@end), ref _status) +)); + } + + /// public static ParsedConnectionUrl ParseConnectionUrl(SecretString @url) { return FfiConverterTypeParsedConnectionUrl.INSTANCE.Lift( diff --git a/windows/src/TablePro.Interop/Mapping.Completion.cs b/windows/src/TablePro.Interop/Mapping.Completion.cs new file mode 100644 index 0000000000..9bdf96c6af --- /dev/null +++ b/windows/src/TablePro.Interop/Mapping.Completion.cs @@ -0,0 +1,37 @@ +using Native = TablePro.Interop.Native; + +namespace TablePro.Interop; + +internal static partial class Mapping +{ + public static Native.SqlGrammarTag Grammar(SqlGrammar grammar) => grammar switch + { + SqlGrammar.PostgreSql => Native.SqlGrammarTag.PostgreSql, + SqlGrammar.MySql => Native.SqlGrammarTag.MySql, + SqlGrammar.Sqlite => Native.SqlGrammarTag.Sqlite, + SqlGrammar.MsSql => Native.SqlGrammarTag.MsSql, + SqlGrammar.ClickHouse => Native.SqlGrammarTag.ClickHouse, + SqlGrammar.DuckDb => Native.SqlGrammarTag.DuckDb, + SqlGrammar.Cql => Native.SqlGrammarTag.Cql, + SqlGrammar.Redis => Native.SqlGrammarTag.Redis, + SqlGrammar.Mongo => Native.SqlGrammarTag.Mongo, + _ => throw new ArgumentOutOfRangeException(nameof(grammar), grammar, "Not a grammar the core knows."), + }; + + public static CompletionItem CompletionItem(Native.CompletionItem item) => new( + item.Label, + item.InsertText, + item.Kind switch + { + Native.CompletionKindTag.Schema => CompletionKind.Schema, + Native.CompletionKindTag.Table => CompletionKind.Table, + Native.CompletionKindTag.View => CompletionKind.View, + Native.CompletionKindTag.MaterializedView => CompletionKind.MaterializedView, + Native.CompletionKindTag.Column => CompletionKind.Column, + Native.CompletionKindTag.Function => CompletionKind.Function, + _ => CompletionKind.Keyword, + }, + item.DataType, + item.Table is { } table ? new TableName(table.Schema, table.Name) : null, + [.. item.Matched.Select(span => new MatchedSpan(checked((int)span.Start), checked((int)span.Length)))]); +} diff --git a/windows/src/TablePro.Interop/SqlCompletion.cs b/windows/src/TablePro.Interop/SqlCompletion.cs new file mode 100644 index 0000000000..18139d29c2 --- /dev/null +++ b/windows/src/TablePro.Interop/SqlCompletion.cs @@ -0,0 +1,89 @@ +using Native = TablePro.Interop.Native; + +namespace TablePro.Interop; + +public enum CompletionKind +{ + Keyword, + Schema, + Table, + View, + MaterializedView, + Column, + Function, +} + +// UTF-16 units of the label. +public readonly record struct MatchedSpan(int Start, int Length); + +// Label and InsertText are cased for the word ranked. DataType and Table are a column's. +public sealed record CompletionItem( + string Label, + string InsertText, + CompletionKind Kind, + string? DataType, + TableName? Table, + IReadOnlyList Matched); + +// Total counts every match; Items holds the closest of them. +public sealed record CompletionPage(int Total, IReadOnlyList Items); + +// What one completion offers, ranked again for each word typed without reading the catalog again. +public sealed class SqlCompletion : IDisposable +{ + private readonly Native.CompletionList _list; + + internal SqlCompletion(Native.CompletionList list) + { + _list = list; + WordStart = checked((int)list.WordStart()); + Word = list.Word(); + } + + // In UTF-16 units of the text the completion was asked for. + public int WordStart { get; } + + public string Word { get; } + + // Ranking a catalog of 50,000 names takes milliseconds, so a UI thread calls this from the pool. + public CompletionPage Rank(string word, int limit) + { + ArgumentNullException.ThrowIfNull(word); + ArgumentOutOfRangeException.ThrowIfNegative(limit); + var page = _list.Rank(word, checked((uint)limit)); + return new CompletionPage(checked((int)page.Total), [.. page.Items.Select(Mapping.CompletionItem)]); + } + + public void Dispose() => _list.Dispose(); +} + +// UTF-16 units, as the editor counts them. +public sealed record FormattedSql(string Text, int Caret); + +// A statement the formatter would change the meaning of is kept as typed, so a format never changes what runs. +public static class SqlFormatter +{ + public static FormattedSql Format(SqlGrammar grammar, string text, int caret) + { + ArgumentNullException.ThrowIfNull(text); + ArgumentOutOfRangeException.ThrowIfNegative(caret); + ArgumentOutOfRangeException.ThrowIfGreaterThan(caret, text.Length); + var formatted = CoreCall.Run( + () => Native.CoreMethods.FormatSql(text, Mapping.Grammar(grammar), checked((uint)caret))); + return new FormattedSql(formatted.Text, checked((int)formatted.Caret)); + } + + // The text between start and end, formatted where the whole text keeps its meaning, as it is elsewhere. + public static string FormatSelection(SqlGrammar grammar, string text, int start, int end) + { + ArgumentNullException.ThrowIfNull(text); + ArgumentOutOfRangeException.ThrowIfNegative(start); + ArgumentOutOfRangeException.ThrowIfLessThan(end, start); + ArgumentOutOfRangeException.ThrowIfGreaterThan(end, text.Length); + return CoreCall.Run(() => Native.CoreMethods.FormatSqlSelection( + text, + Mapping.Grammar(grammar), + checked((uint)start), + checked((uint)end))); + } +} diff --git a/windows/tests/TablePro.App.UITests/CompletionAndFormatTests.cs b/windows/tests/TablePro.App.UITests/CompletionAndFormatTests.cs new file mode 100644 index 0000000000..e15b50c125 --- /dev/null +++ b/windows/tests/TablePro.App.UITests/CompletionAndFormatTests.cs @@ -0,0 +1,117 @@ +using System.Collections.Concurrent; +using FlaUI.Core.AutomationElements; +using FlaUI.Core.Definitions; +using FlaUI.Core.Tools; +using FlaUI.Core.WindowsAPI; + +namespace TablePro.App.UITests; + +// The SQL editor's completion and Format query, in the same class so they run one at a time with the rest. +public sealed partial class ShellTests +{ + [Theory] + [MemberData(nameof(Themes))] + public void CompletionOffersTheCatalogAndTakesTheChosenName(string theme) + { + using var app = AppSession.Launch(theme); + OpenSample(app); + var editor = app.FindEditor("Query 1"); + var spoken = new ConcurrentQueue(); + using var listener = editor.RegisterNotificationEvent( + TreeScope.Element, + (_, _, _, text, _) => spoken.Enqueue(text)); + + app.TypeInto("Query 1", "SELECT * FROM Tr"); + app.FindByNamePrefix("Track, ", app.Find("CompletionList")); + var heard = Retry.WhileFalse( + () => spoken.Any(text => text.StartsWith("Track, ", StringComparison.Ordinal)), + AppSession.Wait, + throwOnTimeout: false).Success; + Assert.True(heard, "Narrator heard no row: " + string.Join(" | ", spoken)); + app.Screenshot("188-completion-tables"); + app.Chord(() => EditorLines(app) == "SELECT * FROM Track", VirtualKeyShort.RETURN); + Assert.True(CompletionClosed(app)); + + app.TypeInto("Query 1", "SELECT * FROM Track t WHERE t."); + app.FindByNamePrefix("TrackId, ", app.Find("CompletionList")); + app.Screenshot("189-completion-columns"); + app.Chord(() => SelectedCompletion(app).StartsWith("Name, ", StringComparison.Ordinal), VirtualKeyShort.DOWN); + app.Chord(() => EditorLines(app) == "SELECT * FROM Track t WHERE t.Name", VirtualKeyShort.TAB); + + const string Partial = "SELECT * FROM Track t WHERE t.Mi"; + app.TypeInto("Query 1", Partial); + app.FindByNamePrefix("Milliseconds, ", app.Find("CompletionList")); + app.Chord(() => CompletionClosed(app), VirtualKeyShort.ESCAPE); + Assert.Equal(Partial, EditorLines(app)); + + app.Chord( + () => app.TryFind("CompletionList") is { IsOffscreen: false }, + VirtualKeyShort.CONTROL, + VirtualKeyShort.SPACE); + app.FindByNamePrefix("Milliseconds, ", app.Find("CompletionList")); + } + + [Theory] + [MemberData(nameof(Themes))] + public void FormatQueryReflowsTheTextAsOneUndoStep(string theme) + { + using var app = AppSession.Launch(theme); + OpenSample(app); + const string Typed = "select TrackId, Name from Track where Milliseconds > 1000 order by Name"; + + app.TypeInto("Query 1", Typed); + app.Chord( + () => EditorLines(app).Contains('\n', StringComparison.Ordinal), + VirtualKeyShort.CONTROL, + VirtualKeyShort.SHIFT, + VirtualKeyShort.KEY_F); + + var formatted = EditorLines(app); + Assert.StartsWith("select", formatted, StringComparison.Ordinal); + Assert.Equal(Typed.Where(c => !char.IsWhiteSpace(c)), formatted.Where(c => !char.IsWhiteSpace(c))); + app.Screenshot("190-format-query"); + app.Chord(() => EditorLines(app) == Typed, VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_Z); + } + + [Theory] + [MemberData(nameof(Themes))] + public void FormatQueryReflowsTheSelectionAloneFromTheQueryMenu(string theme) + { + using var app = AppSession.Launch(theme); + OpenSample(app); + const string Kept = "-- keep this line\r"; + const string Selected = "select TrackId, Name from Track"; + app.TypeInto("Query 1", Kept + Selected); + var editor = app.FindEditor("Query 1"); + app.Chord( + () => editor.Patterns.Text.Pattern.GetSelection()[0].GetText(-1) == Selected, + VirtualKeyShort.SHIFT, + VirtualKeyShort.HOME); + + app.Click(app.Find("QueryMenu"), () => app.TryFindMenuItem("Format query") is { IsEnabled: true }); + app.FindMenuItem("Format query").Patterns.Invoke.Pattern.Invoke(); + + var reflowed = Retry.WhileFalse( + () => EditorLines(app).StartsWith("-- keep this line\nselect\n", StringComparison.Ordinal), + AppSession.Wait, + throwOnTimeout: false).Success; + Assert.True(reflowed, EditorLines(app)); + Assert.Equal( + Selected.Where(c => !char.IsWhiteSpace(c)), + EditorLines(app)["-- keep this line\n".Length..].Where(c => !char.IsWhiteSpace(c))); + app.Screenshot("191-format-selection"); + } + + // The text pattern's line breaks are the document's own, which the tests compare as '\n'. + private static string EditorLines(AppSession app) => + AppSession.EditorText(app.FindEditor("Query 1")).Replace("\r\n", "\n", StringComparison.Ordinal).TrimEnd('\n'); + + private static string SelectedCompletion(AppSession app) => + app.TryFind("CompletionList")?.AsListBox().SelectedItem?.Properties.Name.ValueOrDefault ?? string.Empty; + + private static bool CompletionClosed(AppSession app) => + Retry.WhileFalse( + () => app.TryFind("CompletionList") is null or { IsOffscreen: true }, + TimeSpan.FromSeconds(5), + throwOnTimeout: false).Success; +} diff --git a/windows/tests/TablePro.Editor.Model.Tests/ReplaceTests.cs b/windows/tests/TablePro.Editor.Model.Tests/ReplaceTests.cs new file mode 100644 index 0000000000..06c293d438 --- /dev/null +++ b/windows/tests/TablePro.Editor.Model.Tests/ReplaceTests.cs @@ -0,0 +1,70 @@ +namespace TablePro.Editor.Model.Tests; + +// What a format applies: the text it wrote in place of a range, as one undo step. +public sealed class ReplaceTests +{ + [Fact] + public void AReplaceIsOneUndoStepThatLeavesTheGivenSelection() + { + var model = Carets.Model("select a,b from t|"); + model.Type(" "); + const string Formatted = "select\n a,\n b\nfrom\n t "; + + model.ReplaceRange(0, model.Document.Length, Formatted, Selection.Caret(Formatted.Length)); + + Assert.Equal(Formatted + "|", Carets.Show(model)); + model.Undo(); + Assert.Equal("select a,b from t |", Carets.Show(model)); + model.Redo(); + Assert.Equal(Formatted + "|", Carets.Show(model)); + } + + [Fact] + public void OnlyThePartThatDiffersIsReplaced() + { + var model = Carets.Model("-- keep\nselect a,b|\n-- keep"); + var changes = new List(); + model.Document.Changed += (_, change) => changes.Add(change); + + model.ReplaceRange(0, model.Document.Length, "-- keep\nselect a, b\n-- keep", Selection.Caret(19)); + + var change = Assert.Single(changes); + Assert.Equal((17, string.Empty, " "), (change.Offset, change.RemovedText, change.InsertedText)); + Assert.Equal("-- keep\nselect a, b|\n-- keep", Carets.Show(model)); + } + + [Fact] + public void AReplacedSelectionLeavesTheCaretAfterItsText() + { + var model = Carets.Model("x; select a,b; y"); + model.SetSelection(3, 13); + + model.ReplaceRange(3, 13, "select\n a,\n b", Selection.Caret(22)); + + Assert.Equal("x; select\n a,\n b|; y", Carets.Show(model)); + } + + [Fact] + public void TheSameTextLeavesNoUndoStep() + { + var model = Carets.Model("select 1|"); + + model.ReplaceRange(0, model.Document.Length, "select 1", Selection.Caret(3)); + + Assert.False(model.Document.Undo.CanUndo); + Assert.Equal("sel|ect 1", Carets.Show(model)); + } + + [Fact] + public void ASurrogatePairIsNeverSplit() + { + var model = Carets.Model("'😀'|"); + var changes = new List(); + model.Document.Changed += (_, change) => changes.Add(change); + + model.ReplaceRange(0, model.Document.Length, "'😁'", Selection.Caret(4)); + + var change = Assert.Single(changes); + Assert.Equal(("😀", "😁"), (change.RemovedText, change.InsertedText)); + } +} diff --git a/windows/tests/TablePro.Interop.Tests/CompletionTests.cs b/windows/tests/TablePro.Interop.Tests/CompletionTests.cs new file mode 100644 index 0000000000..6f1ba95105 --- /dev/null +++ b/windows/tests/TablePro.Interop.Tests/CompletionTests.cs @@ -0,0 +1,129 @@ +namespace TablePro.Interop.Tests; + +public sealed class CompletionTests +{ + [Fact] + public async Task ATablePlaceOffersTheFilesTablesQuotedWhereABareNameWouldNotRead() + { + await using var database = await SqliteDatabase.CreateAsync(); + await database.ExecuteAsync("CREATE TABLE \"Order Items\" (id INTEGER, quantity INTEGER)"); + var cancellationToken = TestContext.Current.CancellationToken; + // The catalog was read when the file opened, before the table was made. + await database.Session.RefreshObjectsAsync(ObjectScope.DefaultSchema, cancellationToken); + const string Text = "SELECT * FROM pe"; + + using var completion = await database.Session.CompleteAsync( + Text, Text.Length, asked: false, cancellationToken: cancellationToken); + + Assert.NotNull(completion); + Assert.Equal((14, "pe"), (completion.WordStart, completion.Word)); + var first = completion.Rank("pe", 10).Items[0]; + Assert.Equal(("people", CompletionKind.Table), (first.Label, first.Kind)); + Assert.Equal([new MatchedSpan(0, 2)], first.Matched); + var quoted = Assert.Single(completion.Rank("ord", 10).Items, item => item.Label == "Order Items"); + Assert.Equal("\"Order Items\"", quoted.InsertText); + } + + [Fact] + public async Task AnAliasDotOffersItsTablesColumnsInTheTablesOrder() + { + await using var database = await SqliteDatabase.CreateAsync(); + var cancellationToken = TestContext.Current.CancellationToken; + await database.Session.RefreshObjectsAsync(ObjectScope.DefaultSchema, cancellationToken); + + using var completion = await database.Session.CompleteAsync( + "SELECT p. FROM people p", "SELECT p.".Length, asked: false, cancellationToken: cancellationToken); + + Assert.NotNull(completion); + var page = completion.Rank(string.Empty, 50); + Assert.Equal(["id", "name", "note"], page.Items.Select(item => item.Label)); + Assert.All(page.Items, item => Assert.Equal(CompletionKind.Column, item.Kind)); + Assert.Equal("people", page.Items[0].Table?.Name); + Assert.False(string.IsNullOrEmpty(page.Items[0].DataType)); + } + + [Fact] + public async Task AKeywordFollowsTheCaseTypedAndOpensOnlyWhenAskedWithNothingTyped() + { + await using var database = await SqliteDatabase.CreateAsync(); + var cancellationToken = TestContext.Current.CancellationToken; + await database.Session.RefreshObjectsAsync(ObjectScope.DefaultSchema, cancellationToken); + + var unasked = await database.Session.CompleteAsync( + "SELECT ", 7, asked: false, cancellationToken: cancellationToken); + using var asked = await database.Session.CompleteAsync( + "SELECT ", 7, asked: true, cancellationToken: cancellationToken); + var inString = await database.Session.CompleteAsync( + "SELECT 'pe", 10, asked: true, cancellationToken: cancellationToken); + + Assert.Null(unasked); + Assert.Null(inString); + Assert.NotNull(asked); + Assert.Equal("select", asked.Rank("sel", 1).Items[0].InsertText); + Assert.Equal("SELECT", asked.Rank("SEL", 1).Items[0].InsertText); + var page = asked.Rank(string.Empty, 1000); + Assert.Contains(page.Items, item => item.Label == "people" && item.Kind == CompletionKind.Table); + Assert.True(page.Total >= page.Items.Count); + } + + [Fact] + public async Task ADecomposedNameReplacesItsWholePrefix() + { + await using var database = await SqliteDatabase.CreateAsync(); + await database.ExecuteAsync("CREATE TABLE \"cafés\" (id INTEGER)"); + var cancellationToken = TestContext.Current.CancellationToken; + await database.Session.RefreshObjectsAsync(ObjectScope.DefaultSchema, cancellationToken); + const string Text = "SELECT * FROM café"; + + using var completion = await database.Session.CompleteAsync( + Text, Text.Length, asked: true, cancellationToken: cancellationToken); + + Assert.NotNull(completion); + Assert.Equal((14, "café"), (completion.WordStart, completion.Word)); + Assert.Equal("cafés", Assert.Single(completion.Rank(completion.Word, 10).Items).Label); + } + + [Fact] + public void AFormatReflowsAStatementAndKeepsTheCaretInUtf16Units() + { + const string Text = "select '😀' as a, b from t where a = 1"; + + var formatted = SqlFormatter.Format(SqlGrammar.Sqlite, Text, Text.IndexOf("b from", StringComparison.Ordinal)); + + Assert.Contains('\n', formatted.Text); + Assert.StartsWith("b", formatted.Text[formatted.Caret..], StringComparison.Ordinal); + Assert.Equal(Text.Where(c => !char.IsWhiteSpace(c)), formatted.Text.Where(c => !char.IsWhiteSpace(c))); + } + + [Fact] + public void AStatementTheFormatterWouldChangeIsKeptAsTyped() + { + const string Text = "SET @x += 1"; + + Assert.Equal(new FormattedSql(Text, 3), SqlFormatter.Format(SqlGrammar.MsSql, Text, 3)); + } + + [Theory] + [InlineData("-- select a,b from t", 3)] + [InlineData("select 'select a,b from t'", 8)] + [InlineData("/* select a,b from t */", 3)] + public void ASelectionInsideACommentOrStringKeepsItsMeaning(string text, int start) + { + const string Selected = "select a,b from t"; + + Assert.Equal(Selected, SqlFormatter.FormatSelection(SqlGrammar.Sqlite, text, start, start + Selected.Length)); + } + + [Fact] + public void ASelectedStatementIsReflowedWithoutItsNeighbours() + { + const string Selected = "select a,b from t"; + const string Before = "select '😀'; "; + + var result = SqlFormatter.FormatSelection( + SqlGrammar.Sqlite, Before + Selected + "; select 2", Before.Length, Before.Length + Selected.Length); + + Assert.Contains('\n', result); + Assert.Equal(Selected.Where(c => !char.IsWhiteSpace(c)), result.Where(c => !char.IsWhiteSpace(c))); + } +}