Refactor ProfileService to enhance event handling and improve code clarity

This commit is contained in:
Ahmet Ildirim 2025-03-27 21:48:01 +01:00
parent 58136cee32
commit 0febf73535
3 changed files with 15 additions and 14 deletions

View file

@ -27,10 +27,6 @@ export default class Inscribe extends Plugin {
await this.setupExtension();
}
async onunload() {
this.profileService.onunload();
}
async setupExtension() {
const extension = inlineSuggestions({
fetchFunc: () => this.completionService.fetchCompletion(),

View file

@ -10,7 +10,6 @@ export class ProfileService {
private activePath: string;
private app: App;
private settings: Settings;
private inlineSuggestionOptions: InlineCompletionOptions = { delayMs: 300, splitStrategy: "sentence" };
private profileChangeCallbacks: ((profile: Profile) => void)[] = [];
constructor(plugin: Inscribe) {
@ -19,10 +18,20 @@ export class ProfileService {
this.settings = this.plugin.settings;
this.update(this.getActiveFilePath());
this.app.workspace.on('file-open', (file) => {
if (!file) return;
this.update(file.path);
});
// Add listener for file open events
this.plugin.registerEvent(
this.app.workspace.on('file-open', (file) => {
if (!file) return;
this.update(file.path);
})
);
// Add listener for file rename events
this.plugin.registerEvent(
this.app.vault.on('rename', (file) => {
this.update(file.path);
})
);
}
getActiveProfile(): Profile {
@ -83,8 +92,4 @@ export class ProfileService {
return [pathMapping, this.settings.profiles[matchedProfile]];
}
onunload() {
this.app.workspace.off('file-open', this.update);
}
}

View file

@ -58,7 +58,7 @@ export default class StatusBarItem {
item.setTitle(`Profile: ${profile.name}`).setIsLabel(true);
});
menu.addItem((item) => {
item.setTitle(`Completion is ${pathCompletionEnabled ? 'On' : 'Off'} for: {${path}]}`)
item.setTitle(`Completion is ${pathCompletionEnabled ? 'On' : 'Off'} for: {${path}}`)
.setChecked(pathCompletionEnabled)
.onClick(() => {
const pathConfig = findPathConfig(this.plugin.settings, path);