Customize branch names in Tasks

已回答

I'm writing a plugin for PhpStorm related with Tasks tool, I want to solve some problems related with this.

In the case in question, I would like that given the name of a Task (Changelist), the branch that was created had a certain format (PascalCase for example). However, it is not possible to configure the format of the created branch.

I've been looking at the repository(https://github.com/JetBrains/intellij-community/blob/master/plugins/tasks/tasks-core/src/com/intellij/tasks/actions/vcs/VcsOpenTaskPanel.java#L182) and there doesn't seem to be a way to override the getBranchName method.

Is there any way, through events, overwriting, overloading... to perform this action?

1

Hi mr.cone

Sorry for the long reply. Yes, we do have com.intellij.tasks.impl.TaskManagerImpl.Config#branchNameFormat template for the branch name field. So you can customize it using velocity templates by setting this field's value somewhere in your plugin. Or, you can use a more flexible way to achieve this.

- Implement the com.intellij.tasks.CommitPlaceholderProvider interface in a similar way:

class CustomNameCommitPlaceholder : CommitPlaceholderProvider {

	override fun getPlaceholders(repository: TaskRepository?): Array<String> {
		return arrayOf(MY_CUSTOM_BRANCH_NAME_PLACEHOLDER)
	}

	override fun getPlaceholderValue(task: LocalTask, placeholder: String): String? {
		if (placeholder == MY_CUSTOM_BRANCH_NAME_PLACEHOLDER) {
			return task.summary.uppercase(Locale.getDefault()) //here you can use all task properties to form name that you need
		}
		return null
	}

	override fun getPlaceholderDescription(placeholder: String): String? = null

	companion object {
		const val MY_CUSTOM_BRANCH_NAME_PLACEHOLDER = "CUSTOM_BRANCH_NAME"
	}
}


 

- Register tasks.commitPlaceholderProvider in your plugin.xml under the com.intellij namespace.

<extensions defaultExtensionNs="com.intellij">
	<tasks.commitPlaceholderProvider implementation="com.intellij.tasks.CustomNameCommitPlaceholder"/>
	...

 

- Manually replace ${id} placeholder with your own (we need to tell IDE to use our placeholder instead of the default one).

myTaskManager.getState().branchNameFormat = "${" + CustomNameCommitPlaceholder.MY_CUSTOM_BRANCH_NAME_PLACEHOLDER + "}"; 

You can do it on IDE startup or in other places.

And that's it. Now you can override branch naming rules as per your wish.

1

请先登录再写评论。