From d91f591e8eca7d5afa16548b32744b07e6a6bb45 Mon Sep 17 00:00:00 2001 From: Paul Sebastian Date: Mon, 2 Dec 2024 18:49:37 +0000 Subject: [PATCH] beginning of updating combined results properly Signed-off-by: Paul Sebastian --- src/plugins/data/public/antlr/shared/types.ts | 2 +- src/plugins/data/public/antlr/shared/utils.ts | 58 ++++++++++++++----- 2 files changed, 43 insertions(+), 17 deletions(-) diff --git a/src/plugins/data/public/antlr/shared/types.ts b/src/plugins/data/public/antlr/shared/types.ts index 8fca07c0ac4b..e47432e640e8 100644 --- a/src/plugins/data/public/antlr/shared/types.ts +++ b/src/plugins/data/public/antlr/shared/types.ts @@ -156,5 +156,5 @@ export interface ParsingSubject { query: string; cursor: CursorPosition; context?: ParserRuleContext; - previousResultKeywords?: KeywordSuggestion[]; + previousResult?: A; } diff --git a/src/plugins/data/public/antlr/shared/utils.ts b/src/plugins/data/public/antlr/shared/utils.ts index db8477e5cecc..939c54fb9414 100644 --- a/src/plugins/data/public/antlr/shared/utils.ts +++ b/src/plugins/data/public/antlr/shared/utils.ts @@ -193,7 +193,7 @@ export const parseQuery = < query, cursor, context, - previousResultKeywords, + previousResult, }: ParsingSubject): AutocompleteResultBase => { const parser = createParser(Lexer, Parser, query); const { tokenStream } = parser; @@ -268,22 +268,48 @@ export const parseQuery = < ); // combine previous and current result - if (previousResultKeywords) { - // only need to modify initial results if there are context keywords - if (!currentResult?.suggestKeywords) { - // set initial keywords to be context keywords - currentResult.suggestKeywords = previousResultKeywords; - } else { - // merge initial and context keywords - const combined = [...currentResult.suggestKeywords, ...previousResultKeywords]; - - // ES6 magic to filter out duplicate objects based on id field - currentResult.suggestKeywords = combined.filter( - (item, index, self) => index === self.findIndex((other) => other.id === item.id) - ); - } + const combinedResult: A = {}; + + // look at every field in both results then combine inside of combinedResult + + if (previousResult) { + Object.keys({ ...currentResult, ...previousResult }).forEach((key) => { + const currentField = currentResult[key as keyof A]; + const previousField = previousResult[key as keyof A]; + + if (currentField && previousField) { + combinedResult[key as keyof A] = { + ...currentField, + suggestKeywords: [ + ...(previousField.suggestKeywords ?? []), + ...(currentField.suggestKeywords ?? []), + ], + }; + } else if (currentField) { + combinedResult[key as keyof A] = currentField; + } else if (previousField) { + combinedResult[key as keyof A] = previousField; + } + }); } + // // combine previous and current result + // if (previousResultKeywords) { + // // only need to modify initial results if there are context keywords + // if (!currentResult?.suggestKeywords) { + // // set initial keywords to be context keywords + // currentResult.suggestKeywords = previousResultKeywords; + // } else { + // // merge initial and context keywords + // const combined = [...currentResult.suggestKeywords, ...previousResultKeywords]; + + // // ES6 magic to filter out duplicate objects based on id field + // currentResult.suggestKeywords = combined.filter( + // (item, index, self) => index === self.findIndex((other) => other.id === item.id) + // ); + // } + // } + // return when we don't have any more rerun and combine rules if (!currentResult?.rerunWithoutRules || currentResult.rerunWithoutRules.length === 0) { return currentResult; @@ -324,6 +350,6 @@ export const parseQuery = < query, cursor, context, - previousResultKeywords: currentResult.suggestKeywords, + previousResultKeywords: currentResult, }); };