diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgBranchReferenceValidator.java b/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgBranchReferenceValidator.java index f78b877a7835..5d50ff9dab5d 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgBranchReferenceValidator.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgBranchReferenceValidator.java @@ -20,22 +20,12 @@ import org.jetbrains.annotations.Nullable; import org.zmlx.hg4idea.repo.HgRepository; import java.util.Collection; -import java.util.regex.Pattern; public class HgBranchReferenceValidator extends HgReferenceValidator { - private static final Pattern DIGITS_ILLEGAL = Pattern.compile("[0-9]*"); // reference names couldn't contain only digits + private final HgRepository myRepository; public HgBranchReferenceValidator(@NotNull HgRepository repository) { - super(repository); - } - - @Override - public boolean checkInput(@Nullable String name) { - if (name != null && DIGITS_ILLEGAL.matcher(name).matches()) { - myErrorText = "Invalid name for branch/tag"; - return false; - } - return super.checkInput(name); + myRepository = repository; } @Override diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgPatchReferenceValidator.java b/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgPatchReferenceValidator.java index f5202f165cb2..a919d83c5b28 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgPatchReferenceValidator.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgPatchReferenceValidator.java @@ -15,13 +15,21 @@ */ package org.zmlx.hg4idea.util; +import com.intellij.openapi.util.text.StringUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; import org.zmlx.hg4idea.repo.HgRepository; public class HgPatchReferenceValidator extends HgReferenceValidator { + private final HgRepository myRepository; + public HgPatchReferenceValidator(@NotNull HgRepository repository) { - super(repository); + myRepository = repository; + } + + @Override + public boolean checkInput(@Nullable String name) { + return !StringUtil.isEmptyOrSpaces(name) && !containsIllegalSymbols(name) && !hasConflictsWithAnotherNames(name); } @Override diff --git a/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgReferenceValidator.java b/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgReferenceValidator.java index eaf63be85ea0..28d39edee7ed 100644 --- a/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgReferenceValidator.java +++ b/plugins/hg4idea/src/org/zmlx/hg4idea/util/HgReferenceValidator.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2015 JetBrains s.r.o. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,31 +17,51 @@ package org.zmlx.hg4idea.util; import com.intellij.openapi.ui.InputValidatorEx; import com.intellij.openapi.util.text.StringUtil; -import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import org.zmlx.hg4idea.repo.HgRepository; + +import java.util.regex.Pattern; import static org.zmlx.hg4idea.util.HgUtil.TIP_REFERENCE; -public abstract class HgReferenceValidator implements InputValidatorEx { - - protected final HgRepository myRepository; +public class HgReferenceValidator implements InputValidatorEx { protected String myErrorText; + private static final HgReferenceValidator INSTANCE = new HgReferenceValidator(); - protected HgReferenceValidator(@NotNull HgRepository repository) { - myRepository = repository; + private static final Pattern DIGITS_ILLEGAL = Pattern.compile("[0-9]*"); // reference names couldn't contain only digits + private static final Pattern ILLEGAL = Pattern.compile( + "[:]" // contains ':' character + ); + + public static HgReferenceValidator getInstance() { + return INSTANCE; + } + + protected HgReferenceValidator() { } @Override - public boolean checkInput(@Nullable String name) { - if (StringUtil.isEmptyOrSpaces(name)) { + public boolean checkInput(String inputString) { + if (StringUtil.isEmptyOrSpaces(inputString)) { return false; } - if (name.contains(":")) { + if (containsIllegalSymbols(inputString)) return false; + return !isReservedWord(inputString) && !onlyDigits(inputString) && !hasConflictsWithAnotherNames(inputString); + } + + protected boolean containsIllegalSymbols(@Nullable String inputString) { + if (inputString != null && ILLEGAL.matcher(inputString).find()) { myErrorText = "Name could not contain colons"; - return false; + return true; } - return !isReservedWord(name) && !hasConflictsWithAnotherNames(name); + return false; + } + + private boolean onlyDigits(@Nullable String inputString) { + if (inputString != null && DIGITS_ILLEGAL.matcher(inputString).matches()) { + myErrorText = "Invalid name for hg reference"; + return true; + } + return false; } @Override @@ -54,7 +74,9 @@ public abstract class HgReferenceValidator implements InputValidatorEx { return myErrorText != null; } - protected abstract boolean hasConflictsWithAnotherNames(@Nullable String name); + protected boolean hasConflictsWithAnotherNames(@Nullable String name) { + return false; + } @Nullable @Override