Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 43 additions & 8 deletions crates/data/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@ mod complete;
#[cfg(test)]
mod legacy;

pub use complete::{Call, Completion, FieldValue, FieldValues, Place, VALUES_READ, complete_at, field_values, literal};
pub use complete::{
Call, Completion, FieldValue, FieldValues, Follows, Place, VALUE_CHARS, VALUES_READ, brackets, complete_at, field_values, follows,
literal,
};

#[derive(Clone, Debug, PartialEq)]
pub enum Expr {
Expand Down Expand Up @@ -649,6 +652,12 @@ struct Lexed {
/// `@` or `$` that is the last character, ends it instead of being an error. Anything else that
/// can't be read is an error either way.
fn lex_spans(s: &str, tolerant: bool) -> Result<Lexed, String> {
lex_first(s, tolerant, usize::MAX)
}

/// As [`lex_spans`], reading no more than the first `most` tokens: what comes after them, which
/// may not read (`'a'#`), isn't looked at.
fn lex_first(s: &str, tolerant: bool, most: usize) -> Result<Lexed, String> {
if s.chars().count() > MAX_LEN {
return Err("the expression is too long".into());
}
Expand All @@ -664,7 +673,9 @@ fn lex_spans(s: &str, tolerant: bool) -> Result<Lexed, String> {
}
cs.get(st..*i).map(|s| s.iter().collect()).unwrap_or_default()
};
while let Some(&c) = cs.get(i) {
while let Some(&c) = cs.get(i)
&& out.len() < most
{
let start = i;
let before = out.len();
match c {
Expand Down Expand Up @@ -821,6 +832,11 @@ struct Probe {
field: Option<usize>,
/// Whether that value is a `LIKE` / `ILIKE` pattern (`name LIKE ▮`).
pattern: bool,
/// Whether the value at the end is compared, whatever the left side is (`upper(name) = ▮`).
compared: bool,
/// Where the last compared value starts (a token), with `field` and `pattern` for it: a
/// bracket opened there compares the value just inside it too (`name = (▮`).
compared_from: Option<(usize, Option<usize>, bool)>,
}

/// How a token is written, for the symbols [`Probe`] notes.
Expand Down Expand Up @@ -864,17 +880,35 @@ impl Parser<'_> {
what(&mut p);
}
}
/// Notes that a value at the end would be compared with `l`, when `l` is a field.
/// Notes that a value at the end would be compared with `l`, and with which field when `l` is
/// one.
fn note_compared(&self, l: &Expr) {
self.note_compared_as(l, false);
}
/// As [`note_compared`](Self::note_compared), the value a `LIKE` pattern when `pattern`.
fn note_compared_as(&self, l: &Expr, pattern: bool) {
if let Expr::Field(f) = l {
self.note(self.pos, |p| {
p.field = Some(*f);
p.pattern = pattern;
});
let field = if let Expr::Field(f) = l { Some(*f) } else { None };
self.note_compared_at(self.pos, field, pattern);
}
/// Notes that the value starting at token `i` is compared, with `field` when one.
fn note_compared_at(&self, i: usize, field: Option<usize>, pattern: bool) {
if let Some(probe) = &self.probe {
probe.borrow_mut().compared_from = Some((i, field, pattern));
}
self.note(i, |p| {
p.compared = true;
p.field = field;
p.pattern = pattern;
});
}
/// Notes, for a bracket at token `open` where a compared value starts, that the value just
/// inside it is compared as well (`name = (▮`, `pop > ((▮`).
fn note_compared_within(&self, open: usize) {
let from = self.probe.as_ref().and_then(|p| p.borrow().compared_from);
if let Some((at, field, pattern)) = from
&& at == open
{
self.note_compared_at(open + 1, field, pattern);
}
}
fn probing(&self) -> bool {
Expand Down Expand Up @@ -1254,6 +1288,7 @@ impl Parser<'_> {
self.note(self.pos, |p| p.operand = true);
match self.next() {
Some(Tok::LParen) => {
self.note_compared_within(self.pos.saturating_sub(1));
let e = self.or()?;
self.note(self.pos, |p| p.symbols.push(")"));
if self.next() != Some(Tok::RParen) {
Expand Down
211 changes: 190 additions & 21 deletions crates/data/src/query/complete.rs

Large diffs are not rendered by default.

15 changes: 10 additions & 5 deletions crates/ui-egui/src/completion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -878,15 +878,18 @@ struct MenuState {
/// and what it says (`describe`) given to a screen reader with each. The same from the keyboard:
/// Tab to the button, Enter (or Space) opens it, and Tab on to another widget closes it. Returns
/// the button, and the item accepted.
pub(crate) fn menu(
pub(crate) fn menu<L>(
ui: &mut Ui,
label: impl Into<egui::WidgetText>,
items: impl FnOnce() -> Vec<String>,
items: impl FnOnce() -> L,
icon: impl Fn(&str) -> Option<&'static str>,
describe: impl Fn(usize) -> Option<String>,
has_help: bool,
help: impl FnOnce(&mut Ui, &str, f32),
) -> (egui::Response, Option<String>) {
) -> (egui::Response, Option<String>)
where
L: std::ops::Deref<Target = [String]>,
{
let button = ui.button(label);
let id = button.id.with("menu");
let pass = ui.ctx().cumulative_pass_nr();
Expand All @@ -910,7 +913,9 @@ pub(crate) fn menu(
});
}
s.pass = pass;
let items = if s.open { items() } else { Vec::new() };
// Lent, not copied: a field's values can run to thousands (`fieldvalues`).
let held = s.open.then(items);
let items: &[String] = held.as_deref().unwrap_or_default();
let n = items.len();
// While it's open the arrows and Escape are the list's, not egui's: they'd move the focus off
// the button (to another button, which Enter would then press) or drop it. A focused row's
Expand Down Expand Up @@ -980,7 +985,7 @@ pub(crate) fn menu(
let scroll = &mut s.scroll;
let mut got = Rows::default();
let drawn = popup(ui, id, corner, room, |ui| {
got = rows(ui, &items, selected, pointer_leads, scroll, follow, has_help, icon, describe, help)
got = rows(ui, items, selected, pointer_leads, scroll, follow, has_help, icon, describe, help)
});
// The arrows move the highlight with the focus on the button: the highlighted row is the
// button's active descendant, read as a box's list's is (`List::show`).
Expand Down
Loading
Loading