Skip to content

Move typeahead per-position suggestion logic from ProseMirror-based TypeaheadPopover to CQL-based Typeahead#99

Open
jonathonherbert wants to merge 16 commits intojsh/correct-mappings-like-really-correctfrom
jsh/pos-based-suggestions
Open

Move typeahead per-position suggestion logic from ProseMirror-based TypeaheadPopover to CQL-based Typeahead#99
jonathonherbert wants to merge 16 commits intojsh/correct-mappings-like-really-correctfrom
jsh/pos-based-suggestions

Conversation

@jonathonherbert
Copy link
Collaborator

@jonathonherbert jonathonherbert commented Nov 15, 2025

What's changed?

Prior to this PR, the typeahead API didn't reason about the document position — it provides all the possible typeahead suggestions for a query, and it was up to the UI to figure out whether a suggestion applies given the state of the UI.

This is less efficient than fetching the given suggestions for a position, but was an easy way to provide reasonable typeahead behaviour when the mapping from query to document, and vice-versa, was less than perfect.

After #103, we can map from document position to query position with confidence, and rely entirely on the Typeahead class to provide the correct suggestions for the given position, which improves separation of concerns (no more reasoning about document positions in TypeaheadPopover.

NB: TypeaheadPopover does couple itself to the ProseMirror view, and we'll need to remove that dependency to share it with a hypothetical non-ProseMirror based UI.

How to test

  • This is a no-op — everything should continue to work as before.
  • The tests should pass. A great many exercise typeahead, so we should be confident that nothing is broken.

How can we measure success?

@jonathonherbert jonathonherbert changed the title Enable Add configurable typeahead suggestions for queryStr Nov 15, 2025
@github-actions
Copy link
Contributor

github-actions bot commented Nov 15, 2025

@github-actions
Copy link
Contributor

github-actions bot commented Nov 15, 2025

@jonathonherbert jonathonherbert force-pushed the jsh/pos-based-suggestions branch from 3dbbcb6 to 7f5af43 Compare November 16, 2025 14:28
@jonathonherbert jonathonherbert changed the base branch from main to jsh/improve-debug-positioning November 16, 2025 14:36
@jonathonherbert jonathonherbert force-pushed the jsh/pos-based-suggestions branch from 7f5af43 to 184d17b Compare November 16, 2025 14:37
@jonathonherbert jonathonherbert changed the title Add configurable typeahead suggestions for queryStr Move typeahead per-position suggestion logic from ProseMirror-based TypeaheadPopover to CQL-based Typeahead Nov 16, 2025
@jonathonherbert jonathonherbert force-pushed the jsh/improve-debug-positioning branch from f4621d8 to 5e7291f Compare November 16, 2025 15:22
@jonathonherbert jonathonherbert force-pushed the jsh/pos-based-suggestions branch from 184d17b to 8b2f271 Compare November 16, 2025 15:23
@jonathonherbert jonathonherbert force-pushed the jsh/improve-debug-positioning branch 2 times, most recently from 414d013 to 8781a4a Compare November 16, 2025 15:37
@jonathonherbert jonathonherbert force-pushed the jsh/pos-based-suggestions branch 2 times, most recently from c6140ae to 33bc62c Compare November 16, 2025 15:40
…gestion for a query from the typeahead helper

which breaks the current plugin behaviour, todo
…selection position either side of a character to perform correctly), and adjust debug visualisation
to make them simpler and more comprehensible ... but although the tests pass, they are not reversible, which stretches my mental model a bit ... I need to understand better why this wasn't a problem before
…which makes the behaviour clearer

I was testing an inverted mapping, making changes unintuitive D:<

Having said that, I'm not quite sure why getFieldValueRanges requires a negative oldSize rather than a positive toSize for its chipValue end, which is something I arrived at via experimentation rather than a complete understanding ... dislike
cleaning up the ranges to remove the odd inversion, and expanding the property-based tests to verify they are correct
@jonathonherbert jonathonherbert force-pushed the jsh/pos-based-suggestions branch from 33bc62c to 1dc8435 Compare November 16, 2025 15:44
@jonathonherbert jonathonherbert force-pushed the jsh/improve-debug-positioning branch from 8781a4a to 509320c Compare November 16, 2025 15:45

/**
* These diff functions are vendored to ignore attributes and markup, as we only care about markup.
* These diff functions are vendored to ignore attributes, as we only care about markup.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment didn't make sense, tidied up on the fly

createCqlEditor("example +tag:");

await moveCaretToQueryPos(queryStr.length - 1);
await moveCaretToQueryPos(queryStr.length);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of these positions were revealed to be slightly inaccurate by the mapping changes, so they've been amended here.

const newDoc = tokensToDoc(mapResult(parser(queryBeforeParse)).tokens);
const queryAfterParse = docToCqlStr(newDoc);
const result = parser(queryAfterParse);
const { mapping, queryAst, tokens, error } = mapResult(result);
Copy link
Collaborator Author

@jonathonherbert jonathonherbert Nov 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parsing twice here eliminates an issue that came up in the tests where the cql query derived by a document would not be exactly the same as the query that produced it. For example, the following could occur:

k:v -> <chip><chipKey>k</chipKey><chipValue>v</chipValue></chip> -> +k:v

Re-parsing the string derived from the new document, which may differ from the original, ensures that we get the correct tokens and mapping.

}

this.currentSuggestion = suggestionThatCoversSelection;
this.currentSuggestion = typeaheadSuggestion;
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic to find suggestions here has been moved to the Typeahead class

@jonathonherbert jonathonherbert changed the base branch from jsh/improve-debug-positioning to jsh/correct-mappings-like-really-correct November 22, 2025 13:30
@jonathonherbert jonathonherbert added the feature Departmental tracking: work on a new feature label Nov 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feature Departmental tracking: work on a new feature

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant