Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Console] Avoid duplicate suggestions in autocomplete #201766

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
11 changes: 8 additions & 3 deletions src/plugins/console/public/lib/autocomplete/engine.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,19 +119,24 @@ export function populateContext(tokenPath, context, editor, includeAutoComplete,
editor
);
if (includeAutoComplete) {
let autoCompleteSet = [];
let autoCompleteSet = new Map();
_.each(walkStates, function (ws) {
const contextForState = passThroughContext(context, ws.contextExtensionList);
_.each(ws.components, function (component) {
_.each(component.getTerms(contextForState, editor), function (term) {
if (!_.isObject(term)) {
term = { name: term };
}
autoCompleteSet.push(term);

// Add the term to the autoCompleteSet if it doesn't already exist
if (!autoCompleteSet.has(term.name)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Any reason to avoid using a Set instead of a Map? This comparation won't be needed

Copy link
Contributor

Choose a reason for hiding this comment

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

Nevermind, I just read [this] explanation that you give to Matt in this same PR. It makes sense. Approving.

autoCompleteSet.set(term.name, term);
}
});
});
});
autoCompleteSet = _.uniq(autoCompleteSet);
// Convert Map values to an array of objects
autoCompleteSet = Array.from(autoCompleteSet.values());
context.autoCompleteSet = autoCompleteSet;
}

Expand Down
22 changes: 22 additions & 0 deletions test/functional/apps/console/_autocomplete.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,28 @@ export default function ({ getService, getPageObjects }: FtrProviderContext) {
expect(PageObjects.console.isAutocompleteVisible()).to.be.eql(true);
});

it('should not show duplicate suggestions', async () => {
await PageObjects.console.enterText(`POST _ingest/pipeline/_simulate
{
"pipeline": {
"processors": [
{
"script": {`);
await PageObjects.console.pressEnter();
await PageObjects.console.sleepForDebouncePeriod();
await PageObjects.console.enterText(`"`);
expect(PageObjects.console.isAutocompleteVisible()).to.be.eql(true);

// Iterate on the first 10 suggestions (the ones that are only visible without scrolling)
const suggestions = [];
for (let i = 0; i < 10; i++) {
suggestions.push(await PageObjects.console.getAutocompleteSuggestion(i));
}

// and expect the array to not have duplicates
expect(suggestions).to.eql(_.uniq(suggestions));
});

describe('Autocomplete behavior', () => {
beforeEach(async () => {
await PageObjects.console.clearEditorText();
Expand Down