Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wrapping support #10

Merged
merged 3 commits into from
Apr 12, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,18 @@ Plugin can be downloaded whithin IntelliJ or on the Jetbrains [Plugin Site](http
* Automatically change the commit prefix if switching branches
* If intellij is suggesting a (previous) commit message, only the prefix will be updated
* Set your custom delimiter between the issue key and the commit message (Default ": ")
* Wrap the issue key as you like

## Samples
Following prefix for commit messages will automatically be generated:

| Branch name | Commit prefix |
|---------------------------------------- |--------------- |
| master | no action |
| bugfix/ABC-1234-app-not-working | ABC-1234: |
| feature/ABC-1234-app-not-working | ABC-1234: |
| release/ABC-1234-app-not-working | ABC-1234: |
| someOtherType/ABC-1234-app-not-working | ABC-1234: |
| ABC-1234-app-not-working | ABC-1234: |
| ABC-1234 | ABC-1234: |
| Branch name | Commit prefix (with delimiter) | Commit prefix (wrapped)
|---------------------------------------- |--------------- |---------------
| master | no action | no action
| bugfix/ABC-1234-app-not-working | ABC-1234: | [ABC-1234]
| feature/ABC-1234-app-not-working | ABC-1234: | [ABC-1234]
| release/ABC-1234-app-not-working | ABC-1234: | [ABC-1234]
| someOtherType/ABC-1234-app-not-working | ABC-1234: | [ABC-1234]
| ABC-1234-app-not-working | ABC-1234: | [ABC-1234]
| ABC-1234 | ABC-1234: | [ABC-1234]

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ plugins {
}

group 'ch.repnik'
version '1.0.6'
version '1.1.0'

repositories {
mavenCentral()
Expand Down
26 changes: 15 additions & 11 deletions src/main/java/ch/repnik/intellij/CommitPrefixCheckinHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ private String getNewCommitMessage(){
Optional<String> jiraTicketName = getJiraTicketName(branchName);

if (jiraTicketName.isPresent()){
String newMessage = updatePrefix(jiraTicketName.get(), panel.getCommitMessage(), getCommitMessageDelimiter());
String newMessage = updatePrefix(jiraTicketName.get(), panel.getCommitMessage(), getWrapLeft(), getWrapRight());
//Sets the value for the new Panel UI
return newMessage;
}
Expand All @@ -84,25 +84,25 @@ static String rTrim(String input){
return input.substring(0,i+1);
}

static String updatePrefix(String newPrefix, String currentMessage, String commitMessageDelimiter){
static String updatePrefix(String newPrefix, String currentMessage, String wrapLeft, String wrapRight){
if (currentMessage == null || currentMessage.trim().isEmpty()){
return newPrefix + commitMessageDelimiter;
return wrapLeft + newPrefix + wrapRight;
}

//If there is already a commit message with a matching prefix only replace the prefix
Matcher matcher = prefixPattern.matcher(currentMessage);
if (matcher.find() &&
subString(currentMessage,0, matcher.start()).trim().isEmpty() &&
(subString(currentMessage, matcher.end(), matcher.end() + commitMessageDelimiter.length()).equals(commitMessageDelimiter) ||
subString(currentMessage, matcher.end(), matcher.end() + commitMessageDelimiter.length()).equals(rTrim(commitMessageDelimiter)))
subString(currentMessage,0, matcher.start()).trim().equals(wrapLeft) &&
(subString(currentMessage, matcher.end(), matcher.end() + wrapRight.length()).equals(wrapRight) ||
subString(currentMessage, matcher.end(), matcher.end() + wrapRight.length()).equals(rTrim(wrapRight)))
){
String start = subString(currentMessage, 0, matcher.start());
String end = subString(currentMessage, matcher.end() + commitMessageDelimiter.length());
String end = subString(currentMessage, matcher.end() + wrapRight.length());

return start + newPrefix + commitMessageDelimiter + end;
return start + newPrefix + wrapRight + end;
}

return newPrefix + commitMessageDelimiter + currentMessage;
return wrapLeft + newPrefix + wrapRight + currentMessage;
}

static String subString(String string, int start){
Expand All @@ -127,8 +127,12 @@ static String subString(String string, int start, int end){



String getCommitMessageDelimiter() {
return PluginSettings.getInstance().getCommitMessageDelimiter();
String getWrapRight() {
return PluginSettings.getInstance().getWrapRight();
}

String getWrapLeft() {
return PluginSettings.getInstance().getWrapLeft();
}

private String extractBranchName() {
Expand Down
37 changes: 35 additions & 2 deletions src/main/java/ch/repnik/intellij/settings/PluginSettings.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,13 @@ public class PluginSettings {

private static PluginSettings instance;
private static final String SETTING_COMMIT_MESSAGE_DELIMITER = "git.auto.prefix.delimiter";
private static final String SETTING_WRAP_LEFT = "git.auto.prefix.wrap.left";
private static final String SETTING_WRAP_RIGHT = "git.auto.prefix.wrap.right";


private String commitMessageDelimiter;
private String wrapLeft;
private String wrapRight;

private PluginSettings(){}

Expand All @@ -16,10 +21,18 @@ public static PluginSettings getInstance(){
instance = new PluginSettings();

//Set unset Properties
instance.savePropertyIfUnset(SETTING_COMMIT_MESSAGE_DELIMITER, ": ");
PropertiesComponent properties = PropertiesComponent.getInstance();

if (properties.isValueSet(SETTING_COMMIT_MESSAGE_DELIMITER)){
properties.setValue(SETTING_WRAP_RIGHT, properties.getValue(SETTING_COMMIT_MESSAGE_DELIMITER));
properties.unsetValue(SETTING_COMMIT_MESSAGE_DELIMITER);
}

PluginSettings.instance.savePropertyIfUnset(SETTING_WRAP_RIGHT, ": ");
PluginSettings.instance.savePropertyIfUnset(SETTING_WRAP_LEFT, "");

//Load all properties
instance.load();
PluginSettings.instance.load();
}

return instance;
Expand All @@ -34,10 +47,14 @@ private void savePropertyIfUnset(String propertyName, String defaultValue){

private void load(){
this.commitMessageDelimiter = PropertiesComponent.getInstance().getValue(SETTING_COMMIT_MESSAGE_DELIMITER);
this.wrapLeft = PropertiesComponent.getInstance().getValue(SETTING_WRAP_LEFT);
this.wrapRight = PropertiesComponent.getInstance().getValue(SETTING_WRAP_RIGHT);
}

public void save(){
PropertiesComponent.getInstance().setValue(SETTING_COMMIT_MESSAGE_DELIMITER, this.commitMessageDelimiter);
PropertiesComponent.getInstance().setValue(SETTING_WRAP_LEFT, this.wrapLeft);
PropertiesComponent.getInstance().setValue(SETTING_WRAP_RIGHT, this.wrapRight);
}

public String getCommitMessageDelimiter() {
Expand All @@ -48,4 +65,20 @@ public void setCommitMessageDelimiter(String commitMessageDelimiter) {
this.commitMessageDelimiter = commitMessageDelimiter;
}

public String getWrapLeft() {
return wrapLeft;
}

public void setWrapLeft(String wrapLeft) {
this.wrapLeft = wrapLeft;
}

public String getWrapRight() {
return wrapRight;
}

public void setWrapRight(String wrapRight) {
this.wrapRight = wrapRight;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@

import javax.swing.*;
import java.util.Objects;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class PluginSettingsConfigurable implements SearchableConfigurable {

private Pattern allowedCharsPattern = Pattern.compile("[ :\\_\\-/\\|,\\.]+");
private Pattern allowedCharsPattern = Pattern.compile("[ \\[\\]\\(\\)\\{\\}:\\_\\-/\\|,\\.]+");
private PluginSettingsForm settingsForm;


Expand All @@ -36,30 +35,37 @@ public JComponent createComponent() {
@Override
public boolean isModified() {
PluginSettings settings = PluginSettings.getInstance();
String oldValue = settings.getCommitMessageDelimiter();
String newValue = settingsForm.getDelimiter();
return !Objects.equals(oldValue, newValue);
String oldValueRight = settings.getWrapRight();
String newValueRight = settingsForm.getWrapRight();
String oldValueLeft = settings.getWrapLeft();
String newValueLeft = settingsForm.getWrapRight();
return !Objects.equals(oldValueRight, newValueRight) || !Objects.equals(oldValueLeft, newValueLeft);
}

@Override
public void apply() throws ConfigurationException {
if (settingsForm != null) {
if (isModified()) {
validate();
PluginSettings.getInstance().setCommitMessageDelimiter(settingsForm.getDelimiter());
PluginSettings.getInstance().setWrapRight(settingsForm.getWrapRight());
PluginSettings.getInstance().setWrapLeft(settingsForm.getWrapLeft());
PluginSettings.getInstance().save();
}
}
}

private void validate() throws ConfigurationException {

if (settingsForm.getDelimiter().isEmpty()){
throw new ConfigurationException("Delimiter must not be empty", "Validation failed");
if (settingsForm.getWrapRight().isEmpty()){
throw new ConfigurationException("Wrap Right/Delimiter must not be empty", "Validation failed");
}

if (!allowedCharsPattern.matcher(settingsForm.getDelimiter()).matches()){
throw new ConfigurationException("Delimiter can only contain following chars: \" :_-/|,.\"", "Validation failed");
if (!allowedCharsPattern.matcher(settingsForm.getWrapRight()).matches()){
throw new ConfigurationException("Wrap Right/Delimiter can only contain following chars: \" [](){}:_-/|,.\"", "Validation failed");
}

if (!settingsForm.getWrapLeft().isEmpty() && !allowedCharsPattern.matcher(settingsForm.getWrapLeft()).matches()){
throw new ConfigurationException("Wrap Left can only contain following chars: \" [](){}:_-/|,.\"", "Validation failed");
}
}

Expand Down
51 changes: 41 additions & 10 deletions src/main/java/ch/repnik/intellij/settings/PluginSettingsForm.form
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="ch.repnik.intellij.settings.PluginSettingsForm">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="3" column-count="4" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<grid id="27dc6" binding="mainPanel" layout-manager="GridLayoutManager" row-count="3" column-count="5" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
<margin top="0" left="0" bottom="0" right="0"/>
<constraints>
<xy x="20" y="20" width="500" height="400"/>
Expand All @@ -10,15 +10,15 @@
<children>
<component id="46d34" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="0" column="0" row-span="1" col-span="2" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<text value="Delimiter for commit message"/>
<text value="Wrap Left"/>
</properties>
</component>
<component id="fcb65" class="javax.swing.JTextField" binding="txtDelimiter">
<component id="fcb65" class="javax.swing.JTextField" binding="txtWrapLeft">
<constraints>
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false">
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="60" height="-1"/>
</grid>
</constraints>
Expand All @@ -28,17 +28,48 @@
</component>
<hspacer id="936a0">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
<grid row="0" column="4" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
</constraints>
</hspacer>
<vspacer id="4514d">
<component id="8566f" class="javax.swing.JLabel">
<constraints>
<grid row="0" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
</constraints>
<properties>
<font size="10" style="2"/>
<text value="Default: empty"/>
</properties>
</component>
<component id="d413e" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
<preferred-size width="129" height="32"/>
</grid>
</constraints>
<properties>
<text value="Wrap Right/Delimiter"/>
</properties>
</component>
<vspacer id="baff5">
<constraints>
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="6" hsize-policy="1" anchor="0" fill="2" indent="0" use-parent-layout="false"/>
</constraints>
</vspacer>
<component id="8566f" class="javax.swing.JLabel">
<component id="d968b" class="javax.swing.JTextField" binding="txtWrapRight">
<constraints>
<grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
<grid row="1" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="0" fill="1" indent="0" use-parent-layout="false">
<preferred-size width="60" height="32"/>
</grid>
</constraints>
<properties>
<toolTipText value="Only following chars are allowed &quot; :_-/|,.&quot;"/>
</properties>
</component>
<component id="ddc97" class="javax.swing.JLabel">
<constraints>
<grid row="1" column="3" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false">
<preferred-size width="52" height="32"/>
</grid>
</constraints>
<properties>
<font size="10" style="2"/>
Expand Down
23 changes: 17 additions & 6 deletions src/main/java/ch/repnik/intellij/settings/PluginSettingsForm.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@

public class PluginSettingsForm {
private JPanel mainPanel;
private JTextField txtDelimiter;
private JTextField txtWrapLeft;
private JTextField txtWrapRight;

public PluginSettingsForm(){
txtDelimiter.addKeyListener(new KeyAdapter() {
txtWrapLeft.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if (txtDelimiter.getText().length() >= 5 ) // limit textfield to 5 characters
if (txtWrapLeft.getText().length() >= 5 ) // limit textfield to 5 characters
e.consume();
}
});

txtWrapRight.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent e) {
if (txtWrapRight.getText().length() >= 5 ) // limit textfield to 5 characters
e.consume();
}
});
Expand All @@ -21,11 +29,14 @@ public JPanel getPanel(){
return mainPanel;
}

public String getDelimiter(){
return txtDelimiter.getText();
public String getWrapRight(){
return txtWrapRight.getText();
}

public String getWrapLeft() {return txtWrapLeft.getText();}

public void resetEditorFrom(PluginSettings settings){
this.txtDelimiter.setText(settings.getCommitMessageDelimiter());
this.txtWrapLeft.setText(settings.getWrapLeft());
this.txtWrapRight.setText(settings.getWrapRight());
}
}
Loading