Skip to content

Commit 534f78f

Browse files
committed
feat(prompts): option to choose prompt per project
Closes #229
1 parent ba87d76 commit 534f78f

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
## [Unreleased]
44

5+
### Added
6+
7+
- Option to choose prompt per project.
8+
59
## [2.3.1] - 2024-09-11
610

711
### Fixed

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/settings/AppSettingsConfigurable.kt

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ class AppSettingsConfigurable(val project: Project, cs: CoroutineScope) : BoundC
2525
private var isProjectSpecificLLMClientCheckBox = JBCheckBox(message("settings.llmClient.projectSpecific"))
2626
private lateinit var llmClientToolbarDecorator: ToolbarDecorator
2727
private val promptTable = PromptTable(cs)
28+
private val isProjectSpecificPromptCheckBox = JBCheckBox(message("settings.prompt.projectSpecific"))
2829
private lateinit var toolbarDecorator: ToolbarDecorator
2930
private lateinit var promptComboBox: ComboBox<Prompt>
3031

@@ -33,11 +34,13 @@ class AppSettingsConfigurable(val project: Project, cs: CoroutineScope) : BoundC
3334
row {
3435
label(message("settings.llmClient")).widthGroup("labelPrompt")
3536
llmClientConfigurationComboBox = comboBox(AppSettings2.instance.llmClientConfigurations, AICommitsListCellRenderer())
36-
.bindItem(getter = { getActiveLLMClientConfiguration() }, setter = { client: LLMClientConfiguration? -> setActiveLLMClientConfiguration(client) })
37+
.bindItem(getter = { getActiveLLMClientConfiguration() }, setter = { setActiveLLMClientConfiguration(it) })
3738
.widthGroup("input")
3839
.component
3940
cell(isProjectSpecificLLMClientCheckBox)
4041
.bindSelected(project.service<ProjectSettings>()::isProjectSpecificLLMClient)
42+
contextHelp(message("settings.llmClient.projectSpecific.contextHelp"))
43+
.align(AlignX.LEFT)
4144
}
4245
row {
4346
llmClientToolbarDecorator = ToolbarDecorator.createDecorator(llmClientTable.table)
@@ -76,17 +79,21 @@ class AppSettingsConfigurable(val project: Project, cs: CoroutineScope) : BoundC
7679
AICommitsListCellRenderer()
7780
)
7881
.widthGroup("input")
79-
.bindItem(AppSettings2.instance::locale.toNullableProperty())
82+
.bindItem(getter = { getActiveLocale() }, setter = { setActiveLocale(it)} )
8083

8184
browserLink(message("settings.more-prompts"), AICommitsBundle.URL_PROMPTS_DISCUSSION.toString())
8285
.align(AlignX.RIGHT)
8386
}
8487
row {
8588
label(message("settings.prompt")).widthGroup("labelPrompt")
8689
promptComboBox = comboBox(AppSettings2.instance.prompts.values, AICommitsListCellRenderer())
87-
.bindItem(AppSettings2.instance::activePrompt.toNullableProperty())
90+
.bindItem(getter = { getActivePrompt() }, { setActivePrompt(it) })
8891
.widthGroup("input")
8992
.component
93+
cell(isProjectSpecificPromptCheckBox)
94+
.bindSelected(project.service<ProjectSettings>()::isProjectSpecificPrompt)
95+
contextHelp(message("settings.prompt.projectSpecific.contextHelp"))
96+
.align(AlignX.LEFT)
9097
}
9198
row {
9299
toolbarDecorator = ToolbarDecorator.createDecorator(promptTable.table)
@@ -148,6 +155,42 @@ class AppSettingsConfigurable(val project: Project, cs: CoroutineScope) : BoundC
148155
}
149156
}
150157

158+
private fun getActivePrompt(): Prompt? {
159+
return if (isProjectSpecificPromptCheckBox.isSelected) {
160+
project.service<ProjectSettings>().activePrompt
161+
} else {
162+
AppSettings2.instance.activePrompt
163+
}
164+
}
165+
166+
private fun setActivePrompt(prompt: Prompt?) {
167+
prompt?.let {
168+
if (isProjectSpecificPromptCheckBox.isSelected) {
169+
project.service<ProjectSettings>().activePrompt = prompt
170+
} else {
171+
AppSettings2.instance.activePrompt = prompt
172+
}
173+
}
174+
}
175+
176+
private fun getActiveLocale(): Locale {
177+
return if (isProjectSpecificPromptCheckBox.isSelected) {
178+
project.service<ProjectSettings>().locale
179+
} else {
180+
AppSettings2.instance.locale
181+
}
182+
}
183+
184+
private fun setActiveLocale(locale: Locale?) {
185+
locale?.let {
186+
if (isProjectSpecificPromptCheckBox.isSelected) {
187+
project.service<ProjectSettings>().locale = locale
188+
} else {
189+
AppSettings2.instance.locale = locale
190+
}
191+
}
192+
}
193+
151194
private fun updateActionAvailability(action: CommonActionsPanel.Buttons) {
152195
val selectedRow = promptTable.table.selectedRow
153196
val selectedPrompt = promptTable.table.items[selectedRow]

src/main/kotlin/com/github/blarc/ai/commits/intellij/plugin/settings/ProjectSettings.kt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
package com.github.blarc.ai.commits.intellij.plugin.settings
22

33
import com.github.blarc.ai.commits.intellij.plugin.AICommitsUtils
4+
import com.github.blarc.ai.commits.intellij.plugin.settings.AppSettings2.LocaleConverter
45
import com.github.blarc.ai.commits.intellij.plugin.settings.clients.LLMClientConfiguration
6+
import com.github.blarc.ai.commits.intellij.plugin.settings.prompts.Prompt
57
import com.intellij.openapi.components.PersistentStateComponent
68
import com.intellij.openapi.components.Service
79
import com.intellij.openapi.components.State
810
import com.intellij.openapi.components.Storage
911
import com.intellij.util.xmlb.XmlSerializerUtil
1012
import com.intellij.util.xmlb.annotations.Attribute
13+
import com.intellij.util.xmlb.annotations.OptionTag
14+
import java.util.*
1115

1216
@State(
1317
name = ProjectSettings.SERVICE_NAME,
@@ -27,6 +31,12 @@ class ProjectSettings : PersistentStateComponent<ProjectSettings?> {
2731
@Attribute
2832
var isProjectSpecificLLMClient: Boolean = false
2933

34+
var activePrompt: Prompt? = null
35+
@OptionTag(converter = LocaleConverter::class)
36+
var locale: Locale = Locale.ENGLISH
37+
@Attribute
38+
var isProjectSpecificPrompt: Boolean = false
39+
3040
override fun getState() = this
3141

3242
override fun loadState(state: ProjectSettings) {

src/main/resources/messages/AiCommitsBundle.properties

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ actions.add=Add
4949
settings.prompt.edit.title=Edit Prompt
5050
settings.prompt.add.title=Add Prompt
5151
settings.prompt.description=Description
52+
settings.prompt.projectSpecific=Project specific
53+
settings.prompt.projectSpecific.contextHelp=When selected, saves the chosen prompt and locale for this project only.
5254
validation.unique=Value already exists.
5355
settings.loadingModels=Loading OpenAI models...
5456
settings.more-prompts=More prompts
@@ -62,6 +64,7 @@ settings.prompt.hint=Hint
6264
settings.prompt.hint.comment=Note: This field is for testing purposes only. When generating the actual prompt, the {hint} variable will be replaced with content from the commit dialog.
6365
settings.llmClient=LLM Client
6466
settings.llmClient.projectSpecific=Project specific
67+
settings.llmClient.projectSpecific.contextHelp=When selected, saves the chosen LLM client for this project only.
6568
settings.llmClient.name=Name
6669
settings.llmClient.host=Host
6770
settings.llmClient.proxy=Proxy URL

0 commit comments

Comments
 (0)