Skip to content

Commit

Permalink
feat: ✨ Much better debugging. Now grouped by function
Browse files Browse the repository at this point in the history
  • Loading branch information
SkepticMystic committed Aug 21, 2021
1 parent d96bc0b commit 48a2f03
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 30 deletions.
13 changes: 11 additions & 2 deletions src/MatrixView.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@ import type {
SquareProps,
} from "src/interfaces";
import type BreadcrumbsPlugin from "src/main";
import { copy, debug, mergeGs } from "src/sharedFunctions";
import {
copy,
debug,
debugGroupEnd,
debugGroupStart,
mergeGs,
} from "src/sharedFunctions";
import Lists from "./Components/Lists.svelte";
import Matrix from "./Components/Matrix.svelte";

Expand Down Expand Up @@ -240,8 +246,10 @@ export default class MatrixView extends ItemView {
this.contentEl.empty();

const settings = this.plugin.settings;

debugGroupStart(settings, "debugMode", "Draw Matrix/List View");

const hierGs = this.plugin.currGraphs;
debug(settings, { hierGs });
const { userHierarchies } = this.plugin.settings;

const currFile = this.app.workspace.getActiveFile();
Expand Down Expand Up @@ -403,5 +411,6 @@ export default class MatrixView extends ItemView {
},
});
}
debugGroupEnd(settings, "debugMode");
}
}
59 changes: 40 additions & 19 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import MatrixView from "src/MatrixView";
import {
closeImpliedLinks,
debug,
debugGroupEnd,
debugGroupStart,
getAllGsInDir,
getDVMetadataCache,
getNeighbourObjArr,
Expand Down Expand Up @@ -311,6 +313,7 @@ export default class BreadcrumbsPlugin extends Plugin {
}

hierarchyNoteAdjList = (str: string) => {
const settings = this.settings;
const layers = str.split("\n").filter((line) => line);

const depth = (line: string) => line.split("-")[0].length;
Expand All @@ -324,7 +327,7 @@ export default class BreadcrumbsPlugin extends Plugin {
}
});

console.log({ differences });
debug(settings, { differences });

const posFilteredDifferences = differences
.filter((diff) => diff !== 0)
Expand Down Expand Up @@ -353,7 +356,7 @@ export default class BreadcrumbsPlugin extends Plugin {
const noteUp = copy.reverse().find((adjItem, i) => {
return adjItem.depth === currDepth - difference;
});
debug(this.settings, { noteUp });
debug(settings, { noteUp });
if (noteUp) {
hier[hier.indexOf(noteUp)].children.push(currNote);
}
Expand All @@ -374,7 +377,7 @@ export default class BreadcrumbsPlugin extends Plugin {
const nextDepth = depth(nextLine);

if (nextDepth > currDepth) {
debug(this.settings, { currNote, nextNote });
debug(settings, { currNote, nextNote });
hier[lineNo].children.push(nextNote);
pushNoteUp(hier, currNote, currDepth);
} else if (currDepth === 0) {
Expand Down Expand Up @@ -415,27 +418,29 @@ export default class BreadcrumbsPlugin extends Plugin {
}

async initGraphs(): Promise<BCIndex> {
debug(this.settings, "initialising graphs");
const settings = this.settings;
debugGroupStart(settings, "debugMode", "Initialise Graphs");
const files = this.app.vault.getMarkdownFiles();

const dvQ = !!this.app.plugins.plugins.dataview?.api;

const fileFrontmatterArr: dvFrontmatterCache[] = dvQ
? getDVMetadataCache(this.app, this.settings, files)
: getObsMetadataCache(this.app, this.settings, files);
? getDVMetadataCache(this.app, settings, files)
: getObsMetadataCache(this.app, settings, files);

const relObjArr = await getNeighbourObjArr(this, fileFrontmatterArr);

debugGroupStart(settings, "debugMode", "Hierarchy Note Adjacency List");
let hierarchyNotesArr: {
note: string;
depth: number;
children: string[];
}[] = [];
if (this.settings.hierarchyNotes[0] !== "") {
if (settings.hierarchyNotes[0] !== "") {
const currPath = this.app.workspace.getActiveFile().path;
const contentArr = [];

this.settings.hierarchyNotes.forEach(async (note) => {
settings.hierarchyNotes.forEach(async (note) => {
const file = this.app.metadataCache.getFirstLinkpathDest(
note,
currPath
Expand All @@ -453,10 +458,11 @@ export default class BreadcrumbsPlugin extends Plugin {
await Promise.all(contentArr);
console.log({ contentArr });
hierarchyNotesArr = contentArr.map(this.hierarchyNoteAdjList).flat();
debug(this.settings, { hierarchyNotesArr });
debug(settings, { hierarchyNotesArr });
}
debugGroupEnd(settings, "debugMode");

const { userHierarchies } = this.settings;
const { userHierarchies } = settings;

const graphs: BCIndex = {
hierGs: [],
Expand Down Expand Up @@ -492,7 +498,7 @@ export default class BreadcrumbsPlugin extends Plugin {
});

if (hierarchyNotesArr.length) {
const { hierarchyNoteFieldName } = this.settings;
const { hierarchyNoteFieldName } = settings;

const g = graphs.hierGs.find(
(hierG) => hierG.down[hierarchyNoteFieldName]
Expand Down Expand Up @@ -528,8 +534,10 @@ export default class BreadcrumbsPlugin extends Plugin {
}
});

debug(this.settings, "graphs inited");
debug(this.settings, { graphs });
debug(settings, "graphs inited");
debug(settings, { graphs });

debugGroupEnd(settings, "debugMode");

return graphs;
}
Expand Down Expand Up @@ -630,17 +638,26 @@ export default class BreadcrumbsPlugin extends Plugin {
}

async drawTrail(): Promise<void> {
if (!this.settings.showTrail) return;
const settings = this.settings;
debugGroupStart(settings, "debugMode", "Draw Trail");
if (!settings.showTrail) {
debugGroupEnd(settings, "debugMode");
return;
}

const activeMDView = this.app.workspace.getActiveViewOfType(MarkdownView);
if (!activeMDView) return;
if (!activeMDView) {
debugGroupEnd(settings, "debugMode");
return;
}

const currFile = activeMDView.file;
const frontm =
this.app.metadataCache.getFileCache(currFile)?.frontmatter ?? {};
if (frontm["kanban-plugin"]) return;

const settings = this.settings;
if (frontm["kanban-plugin"]) {
debugGroupEnd(settings, "debugMode");
return;
}

const closedUp = this.currGraphs.closedGs.up;
const sortedTrails = this.getBreadcrumbs(closedUp, currFile);
Expand All @@ -653,7 +670,10 @@ export default class BreadcrumbsPlugin extends Plugin {
// Make sure it's empty
previewView.querySelector("div.breadcrumbs-trail")?.remove();

if (sortedTrails.length === 0 && settings.noPathMessage === "") return;
if (sortedTrails.length === 0 && settings.noPathMessage === "") {
debugGroupEnd(settings, "debugMode");
return;
}

const trailDiv = createDiv({
cls: `breadcrumbs-trail ${
Expand All @@ -672,6 +692,7 @@ export default class BreadcrumbsPlugin extends Plugin {

if (sortedTrails.length === 0) {
trailDiv.innerText = settings.noPathMessage;
debugGroupEnd(settings, "debugMode");
return;
}

Expand Down
52 changes: 43 additions & 9 deletions src/sharedFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,32 @@ export function superDebug(settings: BreadcrumbsSettings, log: any): void {
}
}

export function debugGroupStart(
settings: BreadcrumbsSettings,
type: "debugMode" | "superDebugMode",
group: string
) {
if (settings[type]) {
console.groupCollapsed(group);
}
}
export function debugGroupEnd(
settings: BreadcrumbsSettings,
type: "debugMode" | "superDebugMode"
) {
if (settings[type]) {
console.groupEnd();
}
}

export function getDVMetadataCache(
app: App,
settings: BreadcrumbsSettings,
files: TFile[]
) {
debugGroupStart(settings, "debugMode", "getDVMetadataCache");
debug(settings, "Using Dataview");
debugGroupStart(settings, "superDebugMode", "dvCaches");

const fileFrontmatterArr: dvFrontmatterCache[] = [];
files.forEach((file) => {
Expand All @@ -61,12 +81,13 @@ export function getDVMetadataCache(
const dvCache: dvFrontmatterCache = app.plugins.plugins.dataview.api.page(
file.path
);

superDebug(settings, { dvCache });
fileFrontmatterArr.push(dvCache);
});

debugGroupEnd(settings, "superDebugMode");
debug(settings, { fileFrontmatterArr });
debugGroupEnd(settings, "debugMode");
return fileFrontmatterArr;
}

Expand All @@ -75,10 +96,11 @@ export function getObsMetadataCache(
settings: BreadcrumbsSettings,
files: TFile[]
) {
debugGroupStart(settings, "debugMode", "getObsMetadataCache");
debug(settings, "Using Obsidian");
debugGroupStart(settings, "superDebugMode", "obsCaches");

const fileFrontmatterArr: dvFrontmatterCache[] = [];

files.forEach((file) => {
superDebug(settings, `GetObsMetadataCache: ${file.basename}`);
const obs: FrontMatterCache =
Expand All @@ -91,7 +113,9 @@ export function getObsMetadataCache(
}
});

debugGroupEnd(settings, "superDebugMode");
debug(settings, { fileFrontmatterArr });
debugGroupEnd(settings, "debugMode");
return fileFrontmatterArr;
}

Expand All @@ -109,6 +133,9 @@ export async function getJugglLinks(
app: App,
settings: BreadcrumbsSettings
): Promise<JugglLink[]> {
debugGroupStart(settings, "debugMode", "getJugglLinks");
debug(settings, "Using Juggl");

const files = app.vault.getMarkdownFiles();
const { userHierarchies } = settings;

Expand Down Expand Up @@ -190,6 +217,7 @@ export async function getJugglLinks(
(jugglLink) => jugglLink.links.length
);
debug(settings, { filteredLinks });
debugGroupEnd(settings, "debugMode");
return filteredLinks;
}

Expand Down Expand Up @@ -217,7 +245,7 @@ export function getFieldValues(
[rawValuesPreFlat].flat(4);

superDebug(settings, `${field} of: ${frontmatterCache?.file?.path}`);
superDebug(settings, { rawValues });
superDebug(settings, rawValues);

rawValues.forEach((rawItem) => {
if (!rawItem) return;
Expand Down Expand Up @@ -257,16 +285,19 @@ export async function getNeighbourObjArr(
hierarchies: HierarchyFields[];
}[]
> {
const { userHierarchies } = plugin.settings;
const { settings } = plugin;
const { userHierarchies } = settings;

if (settings.debugMode || settings.superDebugMode) {
console.groupCollapsed("getNeighbourObjArr");
}

let jugglLinks: JugglLink[] = [];
if (
plugin.app.plugins.plugins.juggl !== undefined ||
plugin.settings.parseJugglLinksWithoutJuggl
) {
debug(plugin.settings, "Using Juggl");
jugglLinks = await getJugglLinks(plugin.app, plugin.settings);
debug(plugin.settings, { jugglLinks });
}

const neighbourObjArr: {
Expand All @@ -288,13 +319,13 @@ export async function getNeighbourObjArr(
const newHier: HierarchyFields = { up: {}, same: {}, down: {} };

// Add regular metadata links
if (plugin.settings.useAllMetadata) {
if (settings.useAllMetadata) {
DIRECTIONS.forEach((dir, i) => {
fieldsArr[i].forEach((field) => {
newHier[dir][field] = getFieldValues(
fileFrontmatter,
field,
plugin.settings
settings
);
});
});
Expand Down Expand Up @@ -326,7 +357,10 @@ export async function getNeighbourObjArr(
return hierFields;
});

debug(plugin.settings, { neighbourObjArr });
debug(settings, { neighbourObjArr });
if (settings.debugMode || settings.superDebugMode) {
console.groupEnd();
}
return neighbourObjArr;
}

Expand Down

0 comments on commit 48a2f03

Please sign in to comment.