3

I faced with the problem of writing my vcs current branch name each time I have written 'todo' comment.

Recently I learned about Intellij's 'Live Templates' which is quite comfortable to use. I tried to apply it to my problem but there's no templates to take out a branch name.

So the question is could I actually take out the name of my branch to code comments somehow?

Lelay
  • 31
  • 2
  • Could you describe the problem more specifically? I can not say that I fully understood it. – Feedforward Dec 22 '18 at 16:52
  • @AxelP well, I must to write my current branch name in 'todo' comment (like 'todo [feature-xxx]: ') each time I want to leave a comment somewhere in a code. And it's actually a bit boring so I want to generate 'todo' template automatically (as I understood Live Templates, you only start writing a special key word and then full template will be generated). In search of acceptable template I didn't find any way to do that (I couldn't found any function to find out which branch is currently used). – Lelay Dec 22 '18 at 17:10

2 Answers2

6

It is possible to use the groovyScript predefined function and a script to extract the branch name. For example create the following live template:

$COMMENT$ todo [$BRANCH$]: $END$

with abbreviation "todo" and description "Inserts todo comment with branch name". Click Edit variables and give the variables the following definitions:

COMMENT:

lineCommentStart()

BRANCH (updated for 2020.2 and newer)

groovyScript("com.intellij.dvcs.repo.VcsRepositoryManager.getInstance(_editor.project).getRepositoryForFileQuick(com.intellij.openapi.fileEditor.FileDocumentManager.getInstance().getFile(_editor.document)).getCurrentBranchName()")

Skip if defined checked for both variables. The Groovy script is (unfortunately) all one line. Set applicable contexts to Everywhere.

With this live template it is now possible to type todoTab somewhere in a source file and a line comment with the branch name will be inserted. This will insert the proper line comment depending on the language of the file, or nothing in case of languages without a line comment like HTML. And should extract the branch name no matter the type of version control used (I tested with Git).

Bas Leijdekkers
  • 23,709
  • 4
  • 70
  • 68
1

For live templates you can use predefined functions. Unfortunately there is no function to detect the current VCS branch.
But you can create a template to make work a little easier:

// TODO [$branch_name$]: $comment$

With this template, you still have to fill branch name, but you should not type symbols like [ and caret will be placed automatically.

You can also create a feature request for a new predefined function.

Feedforward
  • 4,521
  • 4
  • 22
  • 34