Skip to content

Commit

Permalink
Clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-liadniou-epam committed Jul 24, 2023
1 parent 22c05f9 commit 2bb738f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 31 deletions.
24 changes: 24 additions & 0 deletions src/context-tree/dedupe/dedupe-matches.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { ContextTree, SectionWithMatch } from "../../types";
import { isSamePosition } from "../../metadata-cache-util/position";

export function dedupeMatches(tree: ContextTree) {
tree.sectionsWithMatches = dedupe(tree.sectionsWithMatches);

tree.branches = tree.branches.map((branch) => dedupeMatches(branch));

return tree;
}

function areMatchesInSameSection(a: SectionWithMatch, b: SectionWithMatch) {
return (
a.text === b.text && isSamePosition(a.cache.position, b.cache.position)
);
}

function dedupe(matches: SectionWithMatch[]) {
return matches.filter(
(match: SectionWithMatch, index: number, array: SectionWithMatch[]) =>
index ===
array.findIndex((inner) => areMatchesInSameSection(inner, match))
);
}
9 changes: 9 additions & 0 deletions src/disposer-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ export class DisposerRegistry {
private contextDom: any;
private domToDisposers: WeakMap<any, Array<Disposer>> = new WeakMap();

/**
* We assume here that match results are going to be added synchronously to the same dom, on which onAddResult is
* called
* @param searchResultDom
*/
onAddResult(searchResultDom: any) {
this.contextDom = searchResultDom;
if (!this.domToDisposers.has(this)) {
Expand All @@ -20,6 +25,10 @@ export class DisposerRegistry {
this.domToDisposers.get(this.contextDom)?.push(fn);
}

/**
* This may be called before any results are added
* @param searchResultDom
*/
onEmptyResults(searchResultDom: any) {
this.domToDisposers.get(searchResultDom)?.forEach((disposer) => disposer());
this.domToDisposers.set(searchResultDom, []);
Expand Down
34 changes: 3 additions & 31 deletions src/patcher.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,13 @@
import { Component, Notice } from "obsidian";
import { around } from "monkey-around";
import {
createPositionFromOffsets,
isSamePosition,
} from "./metadata-cache-util/position";
import { createPositionFromOffsets } from "./metadata-cache-util/position";
import { createContextTree } from "./context-tree/create/create-context-tree";
import { renderContextTree } from "./ui/solid/render-context-tree";
import BetterSearchViewsPlugin from "./plugin";
import { wikiLinkBrackets } from "./patterns";
import { ContextTree, SectionWithMatch } from "./types";
import { produce } from "immer";
import { DisposerRegistry } from "./disposer-registry";
import { dedupeMatches } from "./context-tree/dedupe/dedupe-matches";

const errorTimeout = 10000;

Expand Down Expand Up @@ -176,12 +173,11 @@ export class Patcher {

contextTree.text = "";

const dedupedTree = produce(contextTree, dedupeMatchesRecursively);
const dedupedTree = produce(contextTree, dedupeMatches);

const mountPoint = createDiv();

// todo: remove the hack for file names

const dispose = renderContextTree({
highlights,
contextTrees: [dedupedTree],
Expand All @@ -195,27 +191,3 @@ export class Patcher {
match.el = mountPoint;
}
}

function areMatchesInSameSection(a: SectionWithMatch, b: SectionWithMatch) {
return (
a.text === b.text && isSamePosition(a.cache.position, b.cache.position)
);
}

function dedupe(matches: SectionWithMatch[]) {
return matches.filter(
(match: SectionWithMatch, index: number, array: SectionWithMatch[]) =>
index ===
array.findIndex((inner) => areMatchesInSameSection(inner, match))
);
}

function dedupeMatchesRecursively(tree: ContextTree) {
tree.sectionsWithMatches = dedupe(tree.sectionsWithMatches);

tree.branches = tree.branches.map((branch) =>
dedupeMatchesRecursively(branch)
);

return tree;
}

0 comments on commit 2bb738f

Please sign in to comment.