From 48a2f03a92ca0bfc6bcf007081b1799f11a8b3ce Mon Sep 17 00:00:00 2001 From: Ross Keenan Date: Sat, 21 Aug 2021 10:31:08 +0200 Subject: [PATCH] feat: :sparkles: Much better debugging. Now grouped by function --- src/MatrixView.ts | 13 ++++++++-- src/main.ts | 59 ++++++++++++++++++++++++++++-------------- src/sharedFunctions.ts | 52 ++++++++++++++++++++++++++++++------- 3 files changed, 94 insertions(+), 30 deletions(-) diff --git a/src/MatrixView.ts b/src/MatrixView.ts index c1fd494d..18bf8680 100644 --- a/src/MatrixView.ts +++ b/src/MatrixView.ts @@ -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"; @@ -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(); @@ -403,5 +411,6 @@ export default class MatrixView extends ItemView { }, }); } + debugGroupEnd(settings, "debugMode"); } } diff --git a/src/main.ts b/src/main.ts index 8c364490..1c9aae56 100644 --- a/src/main.ts +++ b/src/main.ts @@ -27,6 +27,8 @@ import MatrixView from "src/MatrixView"; import { closeImpliedLinks, debug, + debugGroupEnd, + debugGroupStart, getAllGsInDir, getDVMetadataCache, getNeighbourObjArr, @@ -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; @@ -324,7 +327,7 @@ export default class BreadcrumbsPlugin extends Plugin { } }); - console.log({ differences }); + debug(settings, { differences }); const posFilteredDifferences = differences .filter((diff) => diff !== 0) @@ -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); } @@ -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) { @@ -415,27 +418,29 @@ export default class BreadcrumbsPlugin extends Plugin { } async initGraphs(): Promise { - 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 @@ -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: [], @@ -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] @@ -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; } @@ -630,17 +638,26 @@ export default class BreadcrumbsPlugin extends Plugin { } async drawTrail(): Promise { - 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); @@ -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 ${ @@ -672,6 +692,7 @@ export default class BreadcrumbsPlugin extends Plugin { if (sortedTrails.length === 0) { trailDiv.innerText = settings.noPathMessage; + debugGroupEnd(settings, "debugMode"); return; } diff --git a/src/sharedFunctions.ts b/src/sharedFunctions.ts index 93ed6563..6ceb9404 100644 --- a/src/sharedFunctions.ts +++ b/src/sharedFunctions.ts @@ -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) => { @@ -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; } @@ -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 = @@ -91,7 +113,9 @@ export function getObsMetadataCache( } }); + debugGroupEnd(settings, "superDebugMode"); debug(settings, { fileFrontmatterArr }); + debugGroupEnd(settings, "debugMode"); return fileFrontmatterArr; } @@ -109,6 +133,9 @@ export async function getJugglLinks( app: App, settings: BreadcrumbsSettings ): Promise { + debugGroupStart(settings, "debugMode", "getJugglLinks"); + debug(settings, "Using Juggl"); + const files = app.vault.getMarkdownFiles(); const { userHierarchies } = settings; @@ -190,6 +217,7 @@ export async function getJugglLinks( (jugglLink) => jugglLink.links.length ); debug(settings, { filteredLinks }); + debugGroupEnd(settings, "debugMode"); return filteredLinks; } @@ -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; @@ -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: { @@ -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 ); }); }); @@ -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; }