diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index ff5ff4eece..7a2850e17f 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -7973,7 +7973,9 @@ offered after it; a line with neither ("hi", "NY", "?") runs nothing. "hide ever Tokyo. "hide everything except flights" (or "all but", "other than", with "turn off", "clear" or "remove") switches off every layer that is on but the ones named, on Enter, or says no other layers are on; with a layer to -keep that is not read, nothing is switched off and only Ask GEV is offered. "remove flights from the +keep that is not read, nothing is switched off and only Ask GEV is offered. +Space Missions and Satellites are kept together, since Space Missions draws +through Satellites. "remove flights from the map" is "remove flights", a doubled verb ("hide hide everything") is read once, and a grid reference after "near" is the place ("flights near 14R PU 2090 4906"). Following needs a callsign or ICAO address (letters and a digit), a @@ -8006,7 +8008,8 @@ layer ("go to Tokyo, north up"). A line with one keyword and no verb beside the rest of a name ("Tilt Cove", "tokyo traffic", "Hurricane, Utah") offers the whole line as a place first and the plan second. When that keyword starts the line with no comma after it and the rest starts with a word for -a kind of place ("Fire Island", "Radio City Music Hall"), it begins the +a kind of place written after a name ("Fire Island", "Radio City Music +Hall"; not "Lake Tahoe" or "Mount Fuji"), it begins the name: the place and the layer are offered, and no plan goes to the rest ("earthquakes japan" keeps its plan). Further places joined with "and" are offered as their own rows. Things loaded on the map that diff --git a/src/ui/omnibarIntent.js b/src/ui/omnibarIntent.js index 7386afa5e7..e9a2ebfd7b 100644 --- a/src/ui/omnibarIntent.js +++ b/src/ui/omnibarIntent.js @@ -522,6 +522,9 @@ export function parseOmnibar( const extraPlaces = []; let previousHadPlace = false; let previousBarePlace = false; + // Whether the clause before was a further place ("near Paris and + // Trinidad"), which a compound name may go on ("and Tobago"). + let previousExtraPlace = false; let previousGo = false; // Whether a request was peeled off a "go to" clause, and how the place // was named: after "go to", after "near"/"in", or bare. @@ -561,6 +564,8 @@ export function parseOmnibar( const afterPlaceComma = previousHadPlace && separator === ','; // "trinidad and tobago": a bare place joined to a bare place by "and". const afterBarePlaceAnd = previousBarePlace && separator === 'and'; + const afterExtraPlace = previousExtraPlace; + previousExtraPlace = false; const continuedLayer = previousLayerEnabled; const afterGo = previousGo; previousGo = false; @@ -607,10 +612,29 @@ export function parseOmnibar( afterPlace && (separator === 'and' || separator === '&') && compoundJoin(place.split(', ').at(-1), value); - if (joinsName) place += `${joinsName}${value}`; - else if (place && bare && afterBarePlaceAnd) place += ` and ${value}`; + if (joinsName) { + // The name is whole: a further "and" names another place ("near + // Trinidad and Tobago and Barbados"). + place += `${joinsName}${value}`; + previousHadPlace = true; + previousBarePlace = false; + return; + } + // "near Paris and Trinidad and Tobago": the further place is whole too. + const joinsExtra = + afterExtraPlace && + (separator === 'and' || separator === '&') && + compoundJoin(extraPlaces.at(-1), value); + if (joinsExtra) { + extraPlaces[extraPlaces.length - 1] += `${joinsExtra}${value}`; + return; + } + if (place && bare && afterBarePlaceAnd) place += ` and ${value}`; else if (place && !afterPlaceComma) { - if (separator === 'and') extraPlaces.push(value); + if (separator === 'and') { + extraPlaces.push(value); + previousExtraPlace = true; + } return; } else if (!place) { place = value; diff --git a/src/ui/omnibarRows.js b/src/ui/omnibarRows.js index 3adeab6a11..8c3839c3a5 100644 --- a/src/ui/omnibarRows.js +++ b/src/ui/omnibarRows.js @@ -153,11 +153,22 @@ const SETTINGS_FOR = const SWITCH = /^(turn on|switch on|enable|show|display|add|turn off|switch off|disable|hide|remove|toggle)(?:\s+(.*))?$/; const SWITCH_OFF = /^(turn off|switch off|disable|hide|remove)\b/; -/** "hide everything except …", "turn off all layers but …". */ -/** A word for a kind of place, which a name goes on with ("Fire Island"). */ +/** + * A word for a kind of place written after the name it ends ("Fire + * Island", "Ship Bottom", "Radio City"), not one written before it ("Lake + * Tahoe", "Mount Fuji", "Tower Bridge", "Park City"). + */ const NAME_TAIL = - /^(?:islands?|isle|city|hall|bottom|lake|river|bay|beach|point|station|park|street|road|mountains?|mount|hills?|valley|falls|springs?|creek|county|town|village|harbou?r|cove|canyon|center|centre|square|tower|bridge|pass|ridge|peak)\b/i; + /^(?:islands?|isle|city|hall|bottom|beach|station|street|road|hills?|valley|falls|springs?|creek|county|town|village|harbou?r|cove|canyon|center|centre|square|bridge|ridge|peak)\b(?!\s+of\b)/i; +/** + * A layer that shows itself through another, which must stay on with it: + * Space Missions draws its missions with Satellites, and switching it off + * puts Satellites back as it was before. + */ +const SHOWN_THROUGH = new Map([['rocket-launches', ['satellites']]]); + +/** "hide everything except …", "turn off all layers but …". */ const EXCEPT = /^(?:turn off|switch off|disable|hide|remove|clear)\s+(?:everything|all(?: the)?(?: other)?(?: layers?)?|every layer)\s+(?:except(?: for)?|but(?: for)?|other than|apart from|besides|save for)\s+(.+)$/; // Layers people reach for first, so "turn on" offers these before the rest. @@ -930,12 +941,6 @@ export function createOmnibar({ layers = [], isEntity = () => false } = {}) { return cache.get(line); } - /** - * "turn on …", "hide …", "toggle …", alone or with a few letters: the - * layers and features that switch that way, never places. Null when the - * line is not such a switch, or names nothing to switch and may be a place - * ("Show Low, AZ"). - */ /** * "hide everything except flights", "turn off all but ships": a plan that * switches off every layer that is on but the ones named. Null when the @@ -953,6 +958,13 @@ export function createOmnibar({ layers = [], isEntity = () => false } = {}) { if (!kept.length) return null; if (!kept.every(named)) return [ask]; const keptIds = new Set(kept.map((layer) => layer.id)); + // Keeping either of a pair keeps both: switching one off would take the + // other with it ("except space missions", "except satellites"). + for (const [layer, through] of SHOWN_THROUGH) { + const on = all().find((entry) => entry.id === layer)?.enabled; + if (keptIds.has(layer) || (on && through.some((id) => keptIds.has(id)))) + for (const id of [layer, ...through]) keptIds.add(id); + } const steps = all() .filter((layer) => layer.enabled && !keptIds.has(layer.id)) .map((layer) => ({ kind: 'layer', layer, enabled: false })); @@ -982,14 +994,22 @@ export function createOmnibar({ layers = [], isEntity = () => false } = {}) { ]; } + /** + * "turn on …", "hide …", "toggle …", alone or with a few letters: the + * layers and features that switch that way, never places. Null when the + * line is not such a switch, or names nothing to switch and may be a place + * ("Show Low, AZ"). + */ function switchRows(line, ask) { const excepted = exceptRows(line, ask); if (excepted) return excepted; - // "remove flights from the map": the map is where every layer is. - line = line.replace( + // "remove flights from the map": the map is where every layer is. Not + // the verb's own "off" ("turn off the screen" is no layer switch). + const bare = line.replace( /\s+(?:from|off) (?:the )?(?:map|globe|screen|view)$/i, '', ); + if (bare !== line && SWITCH.test(bare.toLowerCase())) line = bare; const switching = line.toLowerCase().match(SWITCH); if ( !switching || diff --git a/src/ui/omnibarRows.test.mjs b/src/ui/omnibarRows.test.mjs index b084a6dc50..e4a8f46d78 100644 --- a/src/ui/omnibarRows.test.mjs +++ b/src/ui/omnibarRows.test.mjs @@ -767,3 +767,59 @@ test('a keyword that begins a name offers no plan to the rest of it', () => { ); } }); + +test('"except" keeps a layer and the one it shows through together', () => { + const omnibar = createOmnibar({ + layers: [ + { id: 'flights', name: 'Live Flights', enabled: true }, + { id: 'satellites', name: 'Satellites', enabled: true }, + { id: 'rocket-launches', name: 'Space Missions', enabled: true }, + ], + }); + for (const text of [ + 'hide everything except space missions', + 'hide everything except satellites', + ]) + assert.deepEqual( + omnibar.rows(text, { places: [] }).rows[0].steps.map(stepOutcome), + ['layer(flights,off)'], + text, + ); +}); + +test('"turn off the screen" is no layer switch and no place', () => { + for (const text of ['turn off the screen', 'switch off the map']) + assert.equal(outcome(text, { on: ['flights'] }), 'nothing', text); +}); + +test('a place after a compound country is another place', () => { + const omnibar = createOmnibar({ layers: layers() }); + const places = (text) => + omnibar + .rows(text, { places: [] }) + .rows.filter((row) => row.kind === 'place') + .map((row) => row.place.toLowerCase()); + assert.deepEqual(places('flights near trinidad and tobago and barbados'), [ + 'trinidad and tobago', + 'barbados', + ]); + assert.deepEqual(places('flights near Paris and Trinidad and Tobago'), [ + 'paris', + 'trinidad and tobago', + ]); +}); + +test('a kind of place written before its name keeps the plan', () => { + const omnibar = createOmnibar({ layers: layers() }); + for (const text of [ + 'earthquakes lake tahoe', + 'earthquakes mount fuji', + 'traffic tower bridge', + ]) { + const { rows } = omnibar.rows(text, { places: [] }); + assert.ok( + rows.some((row) => row.kind === 'plan'), + text, + ); + } +});