Skip to content

Commit

Permalink
Feature/prefix params (#2)
Browse files Browse the repository at this point in the history
* Верстка с дополнительными опциями

* визуальный рефакторинг

* Сохранение доп параметров

* регистровая логика

* Актуализировал получение сервиса.

* обновил тест и описание

Co-authored-by: d.molchanov <[email protected]>
  • Loading branch information
MolchanovDmitry and d.molchanov authored Nov 20, 2021
1 parent 53d5af2 commit 411542a
Show file tree
Hide file tree
Showing 16 changed files with 287 additions and 100 deletions.
1 change: 0 additions & 1 deletion .idea/.name

This file was deleted.

2 changes: 1 addition & 1 deletion .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 0 additions & 8 deletions .idea/modules.xml

This file was deleted.

33 changes: 27 additions & 6 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,26 +1,47 @@
plugins {
id 'java'
id 'org.jetbrains.intellij' version '0.7.2'
id 'org.jetbrains.kotlin.jvm' version '1.4.31'
id 'org.jetbrains.kotlin.jvm' version '1.5.31'
}

group 'dmitriy.molchanov'
version '1.3'
version '1.4'

repositories {
mavenCentral()
}

java {
sourceCompatibility = JavaVersion.VERSION_11
targetCompatibility = JavaVersion.VERSION_11
}

dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib"
implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.6.0'
}

// See https://github.com/JetBrains/gradle-intellij-plugin/
intellij {
version '2020.3.3'
version '2020.3.2'
plugins = ['Git4Idea']
}
patchPluginXml {
changeNotes """
Add change notes here.<br>
<em>most HTML tags may be used</em>"""
<b>1.4</b><br>
<ul>
<li>Added the ability to add additional text around the result regex value.</li>
<li>Added the ability change text register.</li>
</ul>
"""
}
test {
useJUnitPlatform()
}

runPluginVerifier {
ideVersions = ["IC-2021.2"]
}

patchPluginXml {
untilBuild null
}
41 changes: 30 additions & 11 deletions src/main/kotlin/dmitriy/molchanov/GitMessageTagCheckerProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import com.intellij.openapi.project.Project
import com.intellij.openapi.vcs.changes.LocalChangeList
import com.intellij.openapi.vcs.changes.ui.CommitMessageProvider
import dmitriy.molchanov.data.Repository
import dmitriy.molchanov.model.Rule
import git4idea.repo.GitRepository
import git4idea.repo.GitRepositoryManager

Expand All @@ -14,30 +15,48 @@ class GitMessageTagCheckerProvider : CommitMessageProvider {
val gitRepositoryManager = GitRepositoryManager.getInstance(project)
val currentRepository = gitRepositoryManager.repositories.firstOrNull()
val branchName = currentRepository?.currentBranch?.name ?: return lastComment
val regex = getRegexForRepository(currentRepository) ?: return lastComment
val rule = getRuleForRepository(currentRepository)
val regex = rule?.regexPrefix?.let(::Regex) ?: return lastComment
val match = regex.find(branchName)
return match?.value
?.let { taskPrefix -> getConcatenatedMessage(lastComment, taskPrefix, regex) }
?.let { taskPrefix ->
getConcatenatedMessage(
lastComment = lastComment,
taskPrefix = taskPrefix,
startWith = rule.startWith,
endWith = rule.endWith,
isUpperCase = rule.isUpperCase
)
}
?: lastComment
}

private fun getRegexForRepository(repository: GitRepository): Regex? {
private fun getRuleForRepository(repository: GitRepository): Rule? {
val remoteUrl = repository
.info.remotes.firstOrNull()
?.firstUrl
?: return null
return Repository.instance
.getRules()
.firstOrNull { it.gitRepo.trim() == remoteUrl.trim() }
?.regexPrefix
?.let(::Regex)
}

private fun getConcatenatedMessage(lastComment: String?, taskPrefix: String, regex: Regex): String {
if (lastComment.isNullOrEmpty()) return "$taskPrefix "
val currentPrefix = regex.find(lastComment)?.value
return currentPrefix
?.let { lastComment.replace(currentPrefix, taskPrefix) }
?: "$taskPrefix $lastComment"
private fun getConcatenatedMessage(
lastComment: String?,
taskPrefix: String,
startWith: String,
endWith: String,
isUpperCase: Boolean?
): String {
val registeredPrefix = when (isUpperCase) {
false -> taskPrefix.lowercase()
true -> taskPrefix.uppercase()
else -> taskPrefix
}
val fullPrefix = "$startWith$registeredPrefix$endWith"
return when {
lastComment?.contains(fullPrefix) == true -> lastComment
else -> fullPrefix
}
}
}
11 changes: 10 additions & 1 deletion src/main/kotlin/dmitriy/molchanov/Strings.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package dmitriy.molchanov

object Strings {
const val EMPTY = ""
const val DISABLE_CLEAR_INITIAL_OPTION = "For the CommitMessage plugin to work, you must disable the \"Clear initial commit message\" option <a href=\"enable\"> Disable </a>"
const val DISABLE_CLEAR_INITIAL_OPTION =
"For the CommitMessage plugin to work, you must disable the \"Clear initial commit message\" option <a href=\"enable\"> Disable </a>"
const val SUCCESS_DISABLE = "Option has been disabled"
const val FILL_FIELDS = "Fill in the fields"
const val ADD_RULE = "Add rule"
const val GIT_REPO = "Git repo:"
const val REGEX_PREFIX = "Regex prefix:"
const val CHECK_BRANCH = "Check branch:"
const val REGISTER = "Register:"
const val START_WITH = "Start with:"
const val END_WITH = "End with:"
const val GIT_REPO_WARNING = "Git repo is empty"
const val REGEX_PREFIX_WARNING = "Regex prefix is empty"
const val CHECK_BRANCH_WARNING = "Check string is empty"
Expand All @@ -20,4 +24,9 @@ object Strings {
const val LIKE_THIS_PLUGIN = "Like this plugin? Please "
const val STAR_ON_GITHUB = "star on github."
const val GITHUB_URL = "https://github.com/MolchanovDmitry/CommitPrefixIdeaPlugin"
const val RESULT = "Result:"
const val COMMIT_MESSAGE = "your commit message.\n"
const val REGISTER_NONE = "None"
const val REGISTER_LOWER_CASE = "Lower case"
const val REGISTER_UPPER_CASE = "Upper case"
}
17 changes: 7 additions & 10 deletions src/main/kotlin/dmitriy/molchanov/data/Repository.kt
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package dmitriy.molchanov.data

import com.intellij.openapi.application.ApplicationManager
import com.intellij.openapi.components.PersistentStateComponent
import com.intellij.openapi.components.ServiceManager
import com.intellij.openapi.components.State
import com.intellij.openapi.components.Storage
import dmitriy.molchanov.model.Rule


@State(name = "RuleServiceData", storages = [Storage("ruleServiceData.xml")])
class Repository : PersistentStateComponent<Repository> {

Expand All @@ -21,12 +22,8 @@ class Repository : PersistentStateComponent<Repository> {

fun removeRule(rule: Rule) {
rules
.firstOrNull { it.gitRepo == rule.gitRepo }
?.let(rules::remove)
}

fun removeRule(rules: List<Rule>) {
rules.forEach(::removeRule)
.firstOrNull { it.gitRepo == rule.gitRepo }
?.let(rules::remove)
}

override fun getState(): Repository {
Expand All @@ -36,13 +33,13 @@ class Repository : PersistentStateComponent<Repository> {

override fun loadState(stateLoadedFromPersistence: Repository) {
stateLoadedFromPersistence.serializedRules
?.let { Serializer.deserialize<Rule>(it) }
?.let { rules = it }
?.let { Serializer.deserialize<Rule>(it) }
?.let { rules = it }
}

companion object {
var rules: ArrayList<Rule> = ArrayList()
val instance: Repository
get() = ServiceManager.getService(Repository::class.java)
get() = ApplicationManager.getApplication().getService(Repository::class.java)
}
}
20 changes: 12 additions & 8 deletions src/main/kotlin/dmitriy/molchanov/domain/Presenter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ class Presenter : SettingsDialog.OnSettingsDialogListener {
private val repository = Repository.instance
private val repositoryManagers = ProjectManager.getInstance().openProjects
.map(GitRepositoryManager::getInstance)
private lateinit var settingsDialog: SettingsDialog
private val settingsDialog by lazy {
SettingsDialog(this).apply {
addRules(repository.getRules())
}
}

fun showMain() {
val rules = repository.getRules()
settingsDialog = SettingsDialog(this)
settingsDialog.addRules(rules)
settingsDialog.show()
}

Expand All @@ -30,13 +31,16 @@ class Presenter : SettingsDialog.OnSettingsDialogListener {
}

override fun onRemoveClick() {
val rules = settingsDialog.getSelectedRules()
repository.removeRule(rules)
val selectedReps = settingsDialog.getSelectedReps()
repository.getRules()
.filter { rule -> selectedReps.contains(rule.gitRepo) }
.forEach(repository::removeRule)
updateSettingsDialogTable()
}

override fun onEditClick() {
val rule = settingsDialog.getSelectedRules().firstOrNull() ?: return
val selectedRep = settingsDialog.getSelectedReps().firstOrNull() ?: return
val rule = repository.getRules().firstOrNull { it.gitRepo == selectedRep } ?: return
showAddRuleDialog(rule) { newRule ->
repository.removeRule(rule)
repository.addRule(newRule)
Expand Down Expand Up @@ -74,7 +78,7 @@ class Presenter : SettingsDialog.OnSettingsDialogListener {
repositoryManagers.forEach { gitRepManager ->
gitRepManager.repositories
.mapNotNull { gitRep -> gitRep.info.remotes.firstOrNull()?.firstUrl }
.forEach { url -> gitRepUrls.add(url) }
.forEach(gitRepUrls::add)
}
return gitRepUrls
}
Expand Down
31 changes: 28 additions & 3 deletions src/main/kotlin/dmitriy/molchanov/model/Rule.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,48 @@ package dmitriy.molchanov.model
import dmitriy.molchanov.Strings
import java.io.Serializable

/**
* Правило для сохранения.
*
* @property gitRepo репозиторий, для которого актуально правило
* @property regexPrefix регулярное выражение
* @property checkString строка для проверки
* @property startWith строка, которая будет подставляться в начало нашего префикса
* @property endWith строка, которая будет подставляться в конец нашего префикса
* @property isUpperCase перевести префикс в:
* true -> верхний регистр, false - нижний регистр, null -> не переводить.
*/
class Rule : Serializable {
var gitRepo: String
var regexPrefix: String
var checkString: String
var startWith: String
var endWith: String
var isUpperCase: Boolean?

@Suppress("unused") //serialization fix
constructor() {
gitRepo = Strings.EMPTY
regexPrefix = Strings.EMPTY
checkString = Strings.EMPTY
startWith = Strings.EMPTY
endWith = Strings.EMPTY
isUpperCase = null
}

constructor(gitRepo: String,
regexPrefix: String,
checkString: String) {
constructor(
gitRepo: String,
regexPrefix: String,
checkString: String,
startWith: String,
endWith: String,
isUpperCase: Boolean?
) {
this.gitRepo = gitRepo
this.regexPrefix = regexPrefix
this.checkString = checkString
this.startWith = startWith
this.endWith = endWith
this.isUpperCase = isUpperCase
}
}
Loading

0 comments on commit 411542a

Please sign in to comment.