-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathExtensionManager.ts
106 lines (89 loc) · 3.18 KB
/
ExtensionManager.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { commands, ExtensionContext, languages, Range, TextDocument } from 'vscode';
import { ExtensionConfig } from './config/ExtensionConfig';
import { AIServiceFactory } from './services/AIServiceFactory';
import { WriteAssistAI } from './WriteAssistAI';
import { ACCEPT_REPHRASE_CMD, OPEN_AI_API_KEY_CMD, REJECT_REPHRASE_CMD } from './constants';
export class ExtensionManager {
private config: ExtensionConfig;
private aiServiceFactory: AIServiceFactory;
constructor(private context: ExtensionContext) {
this.config = new ExtensionConfig(
context,
this.handleCommandsConfigChange.bind(this)
);
this.aiServiceFactory = new AIServiceFactory(this.config);
this.registerCommandsAndActions();
}
private handleCommandsConfigChange() {
// Dispose all the existing subscriptions
while (this.context.subscriptions.length) {
this.context.subscriptions.pop()?.dispose();
}
// Re-register commands and actions
this.registerCommandsAndActions();
}
private registerCodeActionsProviderAndCmds(actionsProvider: WriteAssistAI, supportedLanguages: string[], supportedCommands: string[]) {
// Register the code action provider for supported languages
const provider = languages.registerCodeActionsProvider(
supportedLanguages,
actionsProvider,
{
providedCodeActionKinds: WriteAssistAI.providedCodeActionKinds,
}
);
this.context.subscriptions.push(provider);
// Register all the commands for the code actions
for (const command of supportedCommands) {
this.context.subscriptions.push(
commands.registerCommand(command, (...args: [string, TextDocument, Range]) => {
actionsProvider.handleAction(...args);
})
);
}
}
private registerCodeLensProviderAndCmds(codelensProvider: WriteAssistAI, supportedLanguages: string[], supportedCommands: string[]) {
// Register the code lens provider for supported languages
const provider = languages.registerCodeLensProvider(
supportedLanguages,
codelensProvider
);
this.context.subscriptions.push(provider);
// Register the commands for accept/reject buttons
this.context.subscriptions.push(
commands.registerCommand(ACCEPT_REPHRASE_CMD,
() => {
codelensProvider.acceptRephrase();
}
)
);
this.context.subscriptions.push(
commands.registerCommand(REJECT_REPHRASE_CMD,
() => {
codelensProvider.rejectRephrase();
}
)
);
}
private registerCommandsAndActions() {
const writeAssist: WriteAssistAI = new WriteAssistAI(
this.config,
this.aiServiceFactory
);
const supportedLanguages = [
'markdown',
'plaintext',
'tex',
'latex',
'bibtex',
'quarto',
];
this.registerCodeActionsProviderAndCmds(writeAssist, supportedLanguages, writeAssist.commands);
this.registerCodeLensProviderAndCmds(writeAssist, supportedLanguages, writeAssist.commands);
// Add the command for OpenAI API Key configuration
this.context.subscriptions.push(
commands.registerCommand(OPEN_AI_API_KEY_CMD, () =>
this.config.promptUserForApiKey()
)
);
}
}