@@ -4,7 +4,7 @@ import { Formatter } from "./formatter";
44import { fmt } from "./i18n" ;
55import { getCurrentVersion , getDefaultSettings , migrate } from "./model" ;
66import { SettingsTab } from "./setting" ;
7- import { withPerfNotice } from "./utils/common" ;
7+ import { logger , showNotice , withPerfNotice } from "./utils/common" ;
88
99import type { Data } from "./model" ;
1010import type { Command , EventRef , TFile } from "obsidian" ;
@@ -19,16 +19,45 @@ export default class PrettierPlugin extends Plugin {
1919 private originalSaveCallback : Command [ "checkCallback" ] ;
2020
2121 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+ }
2330
2431 this . formatter = new Formatter ( this ) ;
2532
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+ }
2946
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 ( ) ;
3057 this . registerMenu ( ) ;
3158
59+ logger ( "Plugin loaded." ) ;
60+
3261 this . addSettingTab ( new SettingsTab ( this ) ) ;
3362 }
3463
@@ -54,15 +83,15 @@ export default class PrettierPlugin extends Plugin {
5483 }
5584
5685 private registerCommands ( ) {
57- this . addCommand ( {
86+ const formatContentCommand = this . addCommand ( {
5887 id : "format-content" ,
5988 name : fmt ( "command:format-content-name" ) ,
6089 editorCallback : async ( editor , view ) => {
6190 await withPerfNotice ( ( ) => this . formatter . formatContent ( editor , view . file ) ) ;
6291 } ,
6392 } ) ;
6493
65- this . addCommand ( {
94+ const formatSelectionCommand = this . addCommand ( {
6695 id : "format-selection" ,
6796 name : fmt ( "command:format-selection-name" ) ,
6897 editorCheckCallback : ( checking , editor , view ) => {
@@ -73,6 +102,8 @@ export default class PrettierPlugin extends Plugin {
73102 return editor . somethingSelected ( ) ;
74103 } ,
75104 } ) ;
105+
106+ return { formatContentCommand, formatSelectionCommand } ;
76107 }
77108
78109 private registerEvents ( ) {
0 commit comments