Skip to content

Commit e21fd5e

Browse files
committed
Added debugging console logs
The debugging logs (captured in the tech doc initial-auto-sort-and-notifications.md are more complex than I thought)
1 parent f5611ed commit e21fd5e

File tree

6 files changed

+95
-3
lines changed

6 files changed

+95
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ data.json
2121
# Exclude macOS Finder (System Explorer) View States
2222
.DS_Store
2323
/yarn.lock
24+
/deploy-to-LR-US-vault.sh

docs/tech/initial-auto-sort-and-notifications.md

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,70 @@ Additional remarks:
4545
- lazy view?
4646
- plugin load time?
4747
- ...
48+
49+
---
50+
Log
51+
52+
Things turn out to be trickier than I expected.
53+
Sample logs generated by current log-decorated version:
54+
55+
Scenario: File Explorer hidden on close, automatically hidden on start:
56+
On end: File Explorer invisible, ribbon indicated 'sorting not applied'
57+
c-s ep: (b pre-1) entered patchFileExplorerFolder()
58+
c-s ep: (a pre-1) entered checkFileExplorerIsAvailableAndPatchable()
59+
c-s ep: (a pre-2) this.getFileExplorer ok
60+
c-s ep: (a) failed
61+
c-s ep: (b) failed
62+
c-s ep: (c) app metadataCache populated by Obsidian
63+
c-s ep: (d pre-1) entered readAndParseSortingSpec()
64+
c-s ep: (d) notify: Parsing custom sorting specification SUCCEEDED!
65+
c-s ep: (a pre-1) entered checkFileExplorerIsAvailableAndPatchable()
66+
c-s ep: (a pre-2) this.getFileExplorer ok
67+
c-s ep: (a) failed
68+
69+
Scenario: File Explorer visible and custom-sorted on close, for some reason hidden on start (a different view self-activates)
70+
On end: File Explorer invisible, ribbon indicated 'sorting applied correctly'
71+
72+
c-s ep: (b pre-1) entered patchFileExplorerFolder()
73+
c-s ep: (a pre-1) entered checkFileExplorerIsAvailableAndPatchable()
74+
c-s ep: (a pre-2) this.getFileExplorer ok
75+
c-s ep: (a pre-3) has requestSort()
76+
c-s ep: (a) 1.6.0+ and has getSortedFolderItems()
77+
c-s ep: (f pre-1) patched getSortedFolderItems factory!
78+
c-s ep: (b) 1.6.0+ and patched getSortedFolderItems()
79+
c-s ep: (c) app metadataCache populated by Obsidian
80+
c-s ep: (d pre-1) entered readAndParseSortingSpec()
81+
c-s ep: (d) notify: Parsing custom sorting specification SUCCEEDED!
82+
c-s ep: (a pre-1) entered checkFileExplorerIsAvailableAndPatchable()
83+
c-s ep: (a pre-2) this.getFileExplorer ok
84+
c-s ep: (a pre-3) has requestSort()
85+
c-s ep: (a) 1.6.0+ and has getSortedFolderItems()
86+
87+
Then after showing File Explorer:
88+
89+
(151x) c-s ep: (f) patched getSortedFolderItems invoked!
90+
c-s ep: (f) patched getSortedFolderItems invoked!
91+
92+
Scenario: fully valid end-to-end: FE visible and custom-sorted, reload
93+
On end: File Explorer visible and sorted, ribbon indicated 'sorting applied correctly'
94+
95+
c-s ep: (b pre-1) entered patchFileExplorerFolder()
96+
c-s ep: (a pre-1) entered checkFileExplorerIsAvailableAndPatchable()
97+
c-s ep: (a pre-2) this.getFileExplorer ok
98+
c-s ep: (a pre-3) has requestSort()
99+
c-s ep: (a) 1.6.0+ and has getSortedFolderItems()
100+
c-s ep: (f pre-1) patched getSortedFolderItems factory!
101+
c-s ep: (b) 1.6.0+ and patched getSortedFolderItems()
102+
c-s ep: (c) app metadataCache populated by Obsidian
103+
c-s ep: (d pre-1) entered readAndParseSortingSpec()
104+
c-s ep: (d) notify: Parsing custom sorting specification SUCCEEDED!
105+
c-s ep: (a pre-1) entered checkFileExplorerIsAvailableAndPatchable()
106+
c-s ep: (a pre-2) this.getFileExplorer ok
107+
c-s ep: (a pre-3) has requestSort()
108+
c-s ep: (a) 1.6.0+ and has getSortedFolderItems()
109+
(151z) c-s ep: (f) patched getSortedFolderItems invoked!
110+
c-s ep: (f) patched getSortedFolderItems invoked!
111+
(151x) c-s ep: (f) patched getSortedFolderItems invoked!
112+
c-s ep: (f) patched getSortedFolderItems invoked!
113+
114+

manifest.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"id": "custom-sort",
33
"name": "Custom File Explorer sorting",
4-
"version": "2.1.14",
4+
"version": "2.2.0-beta",
55
"minAppVersion": "0.16.2",
66
"description": "Allows for manual and automatic, config-driven reordering and sorting of files and folders in File Explorer",
77
"author": "SebastianMC",

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "obsidian-custom-sort",
3-
"version": "2.1.14",
3+
"version": "2.2.0-beta",
44
"description": "Custom Sort plugin for Obsidian (https://obsidian.md)",
55
"main": "main.js",
66
"scripts": {

src/main.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ type MonkeyAroundUninstaller = () => void
8181

8282
type ContextMenuProvider = (item: MenuItem) => void
8383

84+
const cl = (executionPointName: string, comment?: string) => {
85+
console.log(`c-s ep: (${executionPointName}) ${comment ? comment : ''}`)
86+
}
87+
8488
export default class CustomSortPlugin
8589
extends Plugin
8690
implements CustomSortPluginAPI
@@ -105,6 +109,7 @@ export default class CustomSortPlugin
105109
}
106110

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

168173
if (this.sortSpecCache) {
169174
this.showNotice(`Parsing custom sorting specification SUCCEEDED!`)
175+
cl('d', 'notify: Parsing custom sorting specification SUCCEEDED!')
170176
} else {
171177
if (anySortingSpecFound) {
172178
errorMessage = errorMessage ? errorMessage : `No valid '${SORTINGSPEC_YAML_KEY}:' key(s) in YAML front matter or multiline YAML indentation error or general YAML syntax error`
173179
} else {
174180
errorMessage = `No custom sorting specification found or only empty specification(s)`
175181
}
182+
cl('d', 'failed')
176183
this.showNotice(`Parsing custom sorting specification FAILED. Suspending the plugin.\n${errorMessage}`, ERROR_NOTICE_TIMEOUT)
177184
this.settings.suspended = true
178185
this.saveSettings()
179186
}
180187
}
181188

182189
checkFileExplorerIsAvailableAndPatchable(logWarning: boolean = true): FileExplorerView | undefined {
190+
cl('a pre-1', 'entered checkFileExplorerIsAvailableAndPatchable()')
183191
let fileExplorerView: FileExplorerView | undefined = this.getFileExplorer()
192+
cl('a pre-2', `this.getFileExplorer ${fileExplorerView ? 'ok' : 'undefined'}`)
184193
if (fileExplorerView && typeof fileExplorerView.requestSort === 'function') {
194+
cl('a pre-3', 'has requestSort()')
185195
// The plugin integration points changed with Obsidian 1.6.0 hence the patchability-check should also be Obsidian version aware
186196
if (requireApiVersion && requireApiVersion("1.6.0")) {
187197
if (typeof fileExplorerView.getSortedFolderItems === 'function') {
198+
cl('a', '1.6.0+ and has getSortedFolderItems()')
188199
return fileExplorerView
189200
}
190201
} else { // Obsidian versions prior to 1.6.0
191202
if (typeof fileExplorerView.createFolderDom === 'function') {
203+
cl('a', '<1.6.0 and has createFolderDom()')
192204
return fileExplorerView
193205
}
194206
}
@@ -197,6 +209,7 @@ export default class CustomSortPlugin
197209
if (logWarning) {
198210
this.logWarningFileExplorerNotAvailable()
199211
}
212+
cl('a', 'failed')
200213
return undefined
201214
}
202215

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

651+
cl('b pre-1', 'entered patchFileExplorerFolder()')
652+
637653
// patching file explorer might fail here because of various non-error reasons.
638654
// That's why not showing and not logging error message here
639655
patchableFileExplorer = patchableFileExplorer ?? this.checkFileExplorerIsAvailableAndPatchable(false)
@@ -642,7 +658,9 @@ export default class CustomSortPlugin
642658
// Starting from Obsidian 1.6.0 the sorting mechanics has been significantly refactored internally in Obsidian
643659
const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(patchableFileExplorer.constructor.prototype, {
644660
getSortedFolderItems(old: any) {
661+
cl('f pre-1', 'patched getSortedFolderItems factory!')
645662
return function (...args: any[]) {
663+
cl('f', 'patched getSortedFolderItems invoked!')
646664
// quick check for plugin status
647665
if (plugin.settings.suspended) {
648666
return old.call(this, ...args);
@@ -662,6 +680,7 @@ export default class CustomSortPlugin
662680
}
663681
})
664682
this.register(requestStandardObsidianSortAfter(uninstallerOfFolderSortFunctionWrapper))
683+
cl('b', '1.6.0+ and patched getSortedFolderItems()')
665684
return true
666685
} else {
667686
// Up to Obsidian 1.6.0
@@ -670,7 +689,9 @@ export default class CustomSortPlugin
670689
let Folder = patchableFileExplorer.createFolderDom(tmpFolder).constructor;
671690
const uninstallerOfFolderSortFunctionWrapper: MonkeyAroundUninstaller = around(Folder.prototype, {
672691
sort(old: any) {
692+
cl('f pre-1', 'patched sort factory!')
673693
return function (...args: any[]) {
694+
cl('f', 'patched sort invoked!')
674695
// quick check for plugin status
675696
if (plugin.settings.suspended) {
676697
return old.call(this, ...args);
@@ -690,9 +711,11 @@ export default class CustomSortPlugin
690711
}
691712
})
692713
this.register(requestStandardObsidianSortAfter(uninstallerOfFolderSortFunctionWrapper))
714+
cl('b', '<1.6.0 and patched sort() on Folder thanks to createFolderDom()')
693715
return true
694716
}
695717
} else {
718+
cl('b', 'failed')
696719
return false
697720
}
698721
}

versions.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,6 @@
4747
"2.1.11": "0.16.2",
4848
"2.1.12": "0.16.2",
4949
"2.1.13": "0.16.2",
50-
"2.1.14": "0.16.2"
50+
"2.1.14": "0.16.2",
51+
"2.2.0-beta": "0.16.2"
5152
}

0 commit comments

Comments
 (0)