Skip to content

Commit

Permalink
Added debugging console logs
Browse files Browse the repository at this point in the history
The debugging logs (captured in the tech doc initial-auto-sort-and-notifications.md are more complex than I thought)
  • Loading branch information
SebastianMC committed Sep 24, 2024
1 parent f5611ed commit e21fd5e
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 3 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ data.json
# Exclude macOS Finder (System Explorer) View States
.DS_Store
/yarn.lock
/deploy-to-LR-US-vault.sh
67 changes: 67 additions & 0 deletions docs/tech/initial-auto-sort-and-notifications.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,70 @@ Additional remarks:
- lazy view?
- plugin load time?
- ...

---
Log

Things turn out to be trickier than I expected.
Sample logs generated by current log-decorated version:

Scenario: File Explorer hidden on close, automatically hidden on start:
On end: File Explorer invisible, ribbon indicated 'sorting not applied'
c-s ep: (b pre-1) entered patchFileExplorerFolder()
c-s ep: (a pre-1) entered checkFileExplorerIsAvailableAndPatchable()
c-s ep: (a pre-2) this.getFileExplorer ok
c-s ep: (a) failed
c-s ep: (b) failed
c-s ep: (c) app metadataCache populated by Obsidian
c-s ep: (d pre-1) entered readAndParseSortingSpec()
c-s ep: (d) notify: Parsing custom sorting specification SUCCEEDED!
c-s ep: (a pre-1) entered checkFileExplorerIsAvailableAndPatchable()
c-s ep: (a pre-2) this.getFileExplorer ok
c-s ep: (a) failed

Scenario: File Explorer visible and custom-sorted on close, for some reason hidden on start (a different view self-activates)
On end: File Explorer invisible, ribbon indicated 'sorting applied correctly'

c-s ep: (b pre-1) entered patchFileExplorerFolder()
c-s ep: (a pre-1) entered checkFileExplorerIsAvailableAndPatchable()
c-s ep: (a pre-2) this.getFileExplorer ok
c-s ep: (a pre-3) has requestSort()
c-s ep: (a) 1.6.0+ and has getSortedFolderItems()
c-s ep: (f pre-1) patched getSortedFolderItems factory!
c-s ep: (b) 1.6.0+ and patched getSortedFolderItems()
c-s ep: (c) app metadataCache populated by Obsidian
c-s ep: (d pre-1) entered readAndParseSortingSpec()
c-s ep: (d) notify: Parsing custom sorting specification SUCCEEDED!
c-s ep: (a pre-1) entered checkFileExplorerIsAvailableAndPatchable()
c-s ep: (a pre-2) this.getFileExplorer ok
c-s ep: (a pre-3) has requestSort()
c-s ep: (a) 1.6.0+ and has getSortedFolderItems()

Then after showing File Explorer:

(151x) c-s ep: (f) patched getSortedFolderItems invoked!
c-s ep: (f) patched getSortedFolderItems invoked!

Scenario: fully valid end-to-end: FE visible and custom-sorted, reload
On end: File Explorer visible and sorted, ribbon indicated 'sorting applied correctly'

c-s ep: (b pre-1) entered patchFileExplorerFolder()
c-s ep: (a pre-1) entered checkFileExplorerIsAvailableAndPatchable()
c-s ep: (a pre-2) this.getFileExplorer ok
c-s ep: (a pre-3) has requestSort()
c-s ep: (a) 1.6.0+ and has getSortedFolderItems()
c-s ep: (f pre-1) patched getSortedFolderItems factory!
c-s ep: (b) 1.6.0+ and patched getSortedFolderItems()
c-s ep: (c) app metadataCache populated by Obsidian
c-s ep: (d pre-1) entered readAndParseSortingSpec()
c-s ep: (d) notify: Parsing custom sorting specification SUCCEEDED!
c-s ep: (a pre-1) entered checkFileExplorerIsAvailableAndPatchable()
c-s ep: (a pre-2) this.getFileExplorer ok
c-s ep: (a pre-3) has requestSort()
c-s ep: (a) 1.6.0+ and has getSortedFolderItems()
(151z) c-s ep: (f) patched getSortedFolderItems invoked!
c-s ep: (f) patched getSortedFolderItems invoked!
(151x) c-s ep: (f) patched getSortedFolderItems invoked!
c-s ep: (f) patched getSortedFolderItems invoked!


2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "custom-sort",
"name": "Custom File Explorer sorting",
"version": "2.1.14",
"version": "2.2.0-beta",
"minAppVersion": "0.16.2",
"description": "Allows for manual and automatic, config-driven reordering and sorting of files and folders in File Explorer",
"author": "SebastianMC",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-custom-sort",
"version": "2.1.14",
"version": "2.2.0-beta",
"description": "Custom Sort plugin for Obsidian (https://obsidian.md)",
"main": "main.js",
"scripts": {
Expand Down
23 changes: 23 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ type MonkeyAroundUninstaller = () => void

type ContextMenuProvider = (item: MenuItem) => void

const cl = (executionPointName: string, comment?: string) => {
console.log(`c-s ep: (${executionPointName}) ${comment ? comment : ''}`)
}

export default class CustomSortPlugin
extends Plugin
implements CustomSortPluginAPI
Expand All @@ -105,6 +109,7 @@ export default class CustomSortPlugin
}

readAndParseSortingSpec() {
cl('d pre-1', 'entered readAndParseSortingSpec()')
const mCache: MetadataCache = this.app.metadataCache
let failed: boolean = false
let anySortingSpecFound: boolean = false
Expand Down Expand Up @@ -167,28 +172,35 @@ export default class CustomSortPlugin

if (this.sortSpecCache) {
this.showNotice(`Parsing custom sorting specification SUCCEEDED!`)
cl('d', 'notify: Parsing custom sorting specification SUCCEEDED!')
} else {
if (anySortingSpecFound) {
errorMessage = errorMessage ? errorMessage : `No valid '${SORTINGSPEC_YAML_KEY}:' key(s) in YAML front matter or multiline YAML indentation error or general YAML syntax error`
} else {
errorMessage = `No custom sorting specification found or only empty specification(s)`
}
cl('d', 'failed')
this.showNotice(`Parsing custom sorting specification FAILED. Suspending the plugin.\n${errorMessage}`, ERROR_NOTICE_TIMEOUT)
this.settings.suspended = true
this.saveSettings()
}
}

checkFileExplorerIsAvailableAndPatchable(logWarning: boolean = true): FileExplorerView | undefined {
cl('a pre-1', 'entered checkFileExplorerIsAvailableAndPatchable()')
let fileExplorerView: FileExplorerView | undefined = this.getFileExplorer()
cl('a pre-2', `this.getFileExplorer ${fileExplorerView ? 'ok' : 'undefined'}`)
if (fileExplorerView && typeof fileExplorerView.requestSort === 'function') {
cl('a pre-3', 'has requestSort()')
// The plugin integration points changed with Obsidian 1.6.0 hence the patchability-check should also be Obsidian version aware
if (requireApiVersion && requireApiVersion("1.6.0")) {
if (typeof fileExplorerView.getSortedFolderItems === 'function') {
cl('a', '1.6.0+ and has getSortedFolderItems()')
return fileExplorerView
}
} else { // Obsidian versions prior to 1.6.0
if (typeof fileExplorerView.createFolderDom === 'function') {
cl('a', '<1.6.0 and has createFolderDom()')
return fileExplorerView
}
}
Expand All @@ -197,6 +209,7 @@ export default class CustomSortPlugin
if (logWarning) {
this.logWarningFileExplorerNotAvailable()
}
cl('a', 'failed')
return undefined
}

Expand Down Expand Up @@ -321,6 +334,7 @@ export default class CustomSortPlugin
this.registerEvent(
// Keep in mind: this event is triggered once after app starts and then after each modification of _any_ metadata
plugin.app.metadataCache.on("resolved", () => {
cl('c', 'app metadataCache populated by Obsidian')
if (!this.settings.suspended) {
if (!this.initialAutoOrManualSortingTriggered) {
this.readAndParseSortingSpec()
Expand Down Expand Up @@ -634,6 +648,8 @@ export default class CustomSortPlugin
}
}

cl('b pre-1', 'entered patchFileExplorerFolder()')

// patching file explorer might fail here because of various non-error reasons.
// That's why not showing and not logging error message here
patchableFileExplorer = patchableFileExplorer ?? this.checkFileExplorerIsAvailableAndPatchable(false)
Expand All @@ -642,7 +658,9 @@ export default class CustomSortPlugin
// Starting from Obsidian 1.6.0 the sorting mechanics has been significantly refactored internally in Obsidian
const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(patchableFileExplorer.constructor.prototype, {
getSortedFolderItems(old: any) {
cl('f pre-1', 'patched getSortedFolderItems factory!')
return function (...args: any[]) {
cl('f', 'patched getSortedFolderItems invoked!')
// quick check for plugin status
if (plugin.settings.suspended) {
return old.call(this, ...args);
Expand All @@ -662,6 +680,7 @@ export default class CustomSortPlugin
}
})
this.register(requestStandardObsidianSortAfter(uninstallerOfFolderSortFunctionWrapper))
cl('b', '1.6.0+ and patched getSortedFolderItems()')
return true
} else {
// Up to Obsidian 1.6.0
Expand All @@ -670,7 +689,9 @@ export default class CustomSortPlugin
let Folder = patchableFileExplorer.createFolderDom(tmpFolder).constructor;
const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(Folder.prototype, {
sort(old: any) {
cl('f pre-1', 'patched sort factory!')
return function (...args: any[]) {
cl('f', 'patched sort invoked!')
// quick check for plugin status
if (plugin.settings.suspended) {
return old.call(this, ...args);
Expand All @@ -690,9 +711,11 @@ export default class CustomSortPlugin
}
})
this.register(requestStandardObsidianSortAfter(uninstallerOfFolderSortFunctionWrapper))
cl('b', '<1.6.0 and patched sort() on Folder thanks to createFolderDom()')
return true
}
} else {
cl('b', 'failed')
return false
}
}
Expand Down
3 changes: 2 additions & 1 deletion versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@
"2.1.11": "0.16.2",
"2.1.12": "0.16.2",
"2.1.13": "0.16.2",
"2.1.14": "0.16.2"
"2.1.14": "0.16.2",
"2.2.0-beta": "0.16.2"
}

0 comments on commit e21fd5e

Please sign in to comment.