@@ -4,7 +4,7 @@ import { Formatter } from "./formatter";
4
4
import { fmt } from "./i18n" ;
5
5
import { getCurrentVersion , getDefaultSettings , migrate } from "./model" ;
6
6
import { SettingsTab } from "./setting" ;
7
- import { withPerfNotice } from "./utils/common" ;
7
+ import { logger , showNotice , withPerfNotice } from "./utils/common" ;
8
8
9
9
import type { Data } from "./model" ;
10
10
import type { Command , EventRef , TFile } from "obsidian" ;
@@ -19,16 +19,45 @@ export default class PrettierPlugin extends Plugin {
19
19
private originalSaveCallback : Command [ "checkCallback" ] ;
20
20
21
21
override async onload ( ) {
22
- await this . loadSettings ( ) ;
22
+ logger ( "Loading plugin..." ) ;
23
+
24
+ try {
25
+ await this . loadSettings ( ) ;
26
+ } catch ( error ) {
27
+ logger ( "Error loading plugin settings:" , error ) ;
28
+ showNotice ( fmt ( "notice:load-settings-error" ) ) ;
29
+ }
23
30
24
31
this . formatter = new Formatter ( this ) ;
25
32
26
- this . registerCommands ( ) ;
27
- this . registerEvents ( ) ;
28
- this . hookSaveCommands ( ) ;
33
+ try {
34
+ try {
35
+ const { formatContentCommand, formatSelectionCommand } = this . registerCommands ( ) ;
36
+ if ( ! formatContentCommand || ! formatSelectionCommand ) {
37
+ const missing = [ ] ;
38
+ if ( ! formatContentCommand ) missing . push ( "format-content" ) ;
39
+ if ( ! formatSelectionCommand ) missing . push ( "format-selection" ) ;
40
+
41
+ throw new Error ( `Failed to register commands: ${ missing . join ( ", " ) } ` ) ;
42
+ }
43
+ } catch ( error ) {
44
+ logger ( "Error registering commands:" , error ) ;
45
+ }
29
46
47
+ try {
48
+ this . hookSaveCommands ( ) ;
49
+ } catch ( error ) {
50
+ logger ( "Error hooking save commands:" , error ) ;
51
+ }
52
+ } catch {
53
+ showNotice ( fmt ( "notice:register-plugin-error" ) ) ;
54
+ }
55
+
56
+ this . registerEvents ( ) ;
30
57
this . registerMenu ( ) ;
31
58
59
+ logger ( "Plugin loaded." ) ;
60
+
32
61
this . addSettingTab ( new SettingsTab ( this ) ) ;
33
62
}
34
63
@@ -54,15 +83,15 @@ export default class PrettierPlugin extends Plugin {
54
83
}
55
84
56
85
private registerCommands ( ) {
57
- this . addCommand ( {
86
+ const formatContentCommand = this . addCommand ( {
58
87
id : "format-content" ,
59
88
name : fmt ( "command:format-content-name" ) ,
60
89
editorCallback : async ( editor , view ) => {
61
90
await withPerfNotice ( ( ) => this . formatter . formatContent ( editor , view . file ) ) ;
62
91
} ,
63
92
} ) ;
64
93
65
- this . addCommand ( {
94
+ const formatSelectionCommand = this . addCommand ( {
66
95
id : "format-selection" ,
67
96
name : fmt ( "command:format-selection-name" ) ,
68
97
editorCheckCallback : ( checking , editor , view ) => {
@@ -73,6 +102,8 @@ export default class PrettierPlugin extends Plugin {
73
102
return editor . somethingSelected ( ) ;
74
103
} ,
75
104
} ) ;
105
+
106
+ return { formatContentCommand, formatSelectionCommand } ;
76
107
}
77
108
78
109
private registerEvents ( ) {
0 commit comments