Skip to content

Commit

Permalink
beginning of updating combined results properly
Browse files Browse the repository at this point in the history
Signed-off-by: Paul Sebastian <[email protected]>
  • Loading branch information
paulstn committed Jan 8, 2025
1 parent 131ed09 commit d91f591
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/plugins/data/public/antlr/shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,5 +156,5 @@ export interface ParsingSubject<A extends AutocompleteResultBase, L, P> {
query: string;
cursor: CursorPosition;
context?: ParserRuleContext;
previousResultKeywords?: KeywordSuggestion[];
previousResult?: A;
}
58 changes: 42 additions & 16 deletions src/plugins/data/public/antlr/shared/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ export const parseQuery = <
query,
cursor,
context,
previousResultKeywords,
previousResult,
}: ParsingSubject<A, L, P>): AutocompleteResultBase => {
const parser = createParser(Lexer, Parser, query);
const { tokenStream } = parser;
Expand Down Expand Up @@ -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];

Check warning on line 278 in src/plugins/data/public/antlr/shared/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/shared/utils.ts#L276-L278

Added lines #L276 - L278 were not covered by tests

if (currentField && previousField) {
combinedResult[key as keyof A] = {

Check warning on line 281 in src/plugins/data/public/antlr/shared/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/shared/utils.ts#L281

Added line #L281 was not covered by tests
...currentField,
suggestKeywords: [
...(previousField.suggestKeywords ?? []),
...(currentField.suggestKeywords ?? []),
],
};
} else if (currentField) {
combinedResult[key as keyof A] = currentField;

Check warning on line 289 in src/plugins/data/public/antlr/shared/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/shared/utils.ts#L289

Added line #L289 was not covered by tests
} else if (previousField) {
combinedResult[key as keyof A] = previousField;

Check warning on line 291 in src/plugins/data/public/antlr/shared/utils.ts

View check run for this annotation

Codecov / codecov/patch

src/plugins/data/public/antlr/shared/utils.ts#L291

Added line #L291 was not covered by tests
}
});
}

// // 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;
Expand Down Expand Up @@ -324,6 +350,6 @@ export const parseQuery = <
query,
cursor,
context,
previousResultKeywords: currentResult.suggestKeywords,
previousResultKeywords: currentResult,
});
};

0 comments on commit d91f591

Please sign in to comment.