mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
[vcs-log] introduce VcsUserUtil
This commit is contained in:
@@ -15,13 +15,9 @@
|
||||
*/
|
||||
package com.intellij.vcs.log.impl;
|
||||
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.vcs.log.VcsUser;
|
||||
import com.intellij.vcs.log.util.VcsUserUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import java.util.Objects;
|
||||
|
||||
@@ -29,8 +25,6 @@ import java.util.Objects;
|
||||
* Note: users are considered equal if they have the same name and email. Emails are converted to lower case in constructor.
|
||||
*/
|
||||
public class VcsUserImpl implements VcsUser {
|
||||
@NotNull private static final Pattern NAME_WITH_DOT = Pattern.compile("(\\w*)\\.(\\w*)");
|
||||
@NotNull private static final Pattern NAME_WITH_SPACE = Pattern.compile("(\\w*) (\\w*)");
|
||||
|
||||
@NotNull private final String myName;
|
||||
@NotNull private final String myEmail;
|
||||
@@ -72,32 +66,6 @@ public class VcsUserImpl implements VcsUser {
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return myName + "<" + myEmail + ">";
|
||||
}
|
||||
|
||||
public static boolean isSamePerson(@NotNull VcsUser user1, @NotNull VcsUser user2) {
|
||||
return getNameInStandardForm(user1.getName()).equals(getNameInStandardForm(user2.getName()));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String getNameInStandardForm(@NotNull String name) {
|
||||
Pair<String, String> firstAndLastName = getFirstAndLastName(name);
|
||||
if (firstAndLastName != null) {
|
||||
return firstAndLastName.first.toLowerCase() + " " + firstAndLastName.second.toLowerCase();
|
||||
}
|
||||
return name.toLowerCase();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Pair<String, String> getFirstAndLastName(@NotNull String name) {
|
||||
Matcher nameWithDotMatcher = NAME_WITH_DOT.matcher(name);
|
||||
if (nameWithDotMatcher.matches()) {
|
||||
return Pair.create(nameWithDotMatcher.group(1), nameWithDotMatcher.group(2));
|
||||
}
|
||||
Matcher nameWithSpaceMatcher = NAME_WITH_SPACE.matcher(name);
|
||||
if (nameWithSpaceMatcher.matches()) {
|
||||
return Pair.create(nameWithSpaceMatcher.group(1), nameWithSpaceMatcher.group(2));
|
||||
}
|
||||
return null;
|
||||
return VcsUserUtil.toExactString(this);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +19,8 @@ import com.intellij.util.NotNullFunction;
|
||||
import com.intellij.util.containers.ContainerUtil;
|
||||
import com.intellij.vcs.log.*;
|
||||
import com.intellij.vcs.log.data.VcsLogDataManager;
|
||||
import com.intellij.vcs.log.impl.VcsUserImpl;
|
||||
import com.intellij.vcs.log.ui.filter.VcsLogUserFilterImpl;
|
||||
import com.intellij.vcs.log.util.VcsUserUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Collection;
|
||||
@@ -43,7 +43,7 @@ public class MyCommitsHighlighter implements VcsLogHighlighter {
|
||||
if (!myLogUi.isHighlighterEnabled(Factory.ID)) return VcsCommitStyle.DEFAULT;
|
||||
if (myShouldHighlightUser) {
|
||||
VcsUser currentUser = myDataManager.getCurrentUser().get(details.getRoot());
|
||||
if (currentUser != null && VcsUserImpl.isSamePerson(currentUser, details.getAuthor())) {
|
||||
if (currentUser != null && VcsUserUtil.isSamePerson(currentUser, details.getAuthor())) {
|
||||
return VcsCommitStyleFactory.bold();
|
||||
}
|
||||
}
|
||||
@@ -61,7 +61,7 @@ public class MyCommitsHighlighter implements VcsLogHighlighter {
|
||||
@NotNull
|
||||
@Override
|
||||
public String fun(VcsUser user) {
|
||||
return VcsUserImpl.getNameInStandardForm(user.getName());
|
||||
return VcsUserUtil.getNameInStandardForm(user.getName());
|
||||
}
|
||||
};
|
||||
Set<String> allUserNames = ContainerUtil.newHashSet(ContainerUtil.map(myDataManager.getAllUsers(), nameToString));
|
||||
|
||||
@@ -9,9 +9,8 @@ import com.intellij.util.containers.MultiMap;
|
||||
import com.intellij.vcs.log.VcsCommitMetadata;
|
||||
import com.intellij.vcs.log.VcsLogUserFilter;
|
||||
import com.intellij.vcs.log.VcsUser;
|
||||
import com.intellij.vcs.log.impl.VcsUserImpl;
|
||||
import com.intellij.vcs.log.util.VcsUserUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
@@ -36,23 +35,13 @@ public class VcsLogUserFilterImpl implements VcsLogUserFilter {
|
||||
myAllUsersByNames.putValue(name.toLowerCase(), user);
|
||||
}
|
||||
String email = user.getEmail();
|
||||
String emailNamePart = getEmailNamePart(email);
|
||||
if (emailNamePart != null) {
|
||||
myAllUsersByEmails.putValue(emailNamePart.toLowerCase(), user);
|
||||
String nameFromEmail = VcsUserUtil.getNameFromEmail(email);
|
||||
if (nameFromEmail != null) {
|
||||
myAllUsersByEmails.putValue(nameFromEmail.toLowerCase(), user);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static String getEmailNamePart(@NotNull String email) {
|
||||
int at = email.indexOf('@');
|
||||
String emailNamePart = null;
|
||||
if (at > 0) {
|
||||
emailNamePart = email.substring(0, at);
|
||||
}
|
||||
return emailNamePart;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
@Override
|
||||
public Collection<String> getUserNames(@NotNull VirtualFile root) {
|
||||
@@ -63,7 +52,7 @@ public class VcsLogUserFilterImpl implements VcsLogUserFilter {
|
||||
result.addAll(ContainerUtil.map(users, new Function<VcsUser, String>() {
|
||||
@Override
|
||||
public String fun(VcsUser user) {
|
||||
return userToString(user);
|
||||
return VcsUserUtil.toExactString(user);
|
||||
}
|
||||
}));
|
||||
}
|
||||
@@ -81,7 +70,7 @@ public class VcsLogUserFilterImpl implements VcsLogUserFilter {
|
||||
VcsUser vcsUser = myData.get(root);
|
||||
if (vcsUser != null) {
|
||||
users.addAll(getUsers(vcsUser.getName())); // do not just add vcsUser, also add synonyms
|
||||
String emailNamePart = getEmailNamePart(vcsUser.getEmail());
|
||||
String emailNamePart = VcsUserUtil.getNameFromEmail(vcsUser.getEmail());
|
||||
if (emailNamePart != null) {
|
||||
users.addAll(getUsers(emailNamePart));
|
||||
}
|
||||
@@ -131,7 +120,7 @@ public class VcsLogUserFilterImpl implements VcsLogUserFilter {
|
||||
|
||||
@NotNull
|
||||
private static List<String> getSynonyms(@NotNull String name) {
|
||||
Pair<String, String> firstAndLastName = VcsUserImpl.getFirstAndLastName(name);
|
||||
Pair<String, String> firstAndLastName = VcsUserUtil.getFirstAndLastName(name);
|
||||
if (firstAndLastName != null) {
|
||||
return Arrays.asList(firstAndLastName.first + " " + firstAndLastName.second,
|
||||
firstAndLastName.first + "." + firstAndLastName.second,
|
||||
@@ -139,24 +128,4 @@ public class VcsLogUserFilterImpl implements VcsLogUserFilter {
|
||||
}
|
||||
return Collections.singletonList(name);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String userToString(@NotNull VcsUser user) {
|
||||
String name = user.getName();
|
||||
String email = user.getEmail();
|
||||
|
||||
String result = name;
|
||||
if (!email.isEmpty()) {
|
||||
if (!name.isEmpty()) {
|
||||
result += " <";
|
||||
}
|
||||
result += email;
|
||||
|
||||
if (!name.isEmpty()) {
|
||||
result += ">";
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,85 @@
|
||||
/*
|
||||
* Copyright 2000-2016 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.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package com.intellij.vcs.log.util;
|
||||
|
||||
import com.intellij.openapi.util.Pair;
|
||||
import com.intellij.vcs.log.VcsUser;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
public class VcsUserUtil {
|
||||
@NotNull private static final Pattern NAME_WITH_DOT = Pattern.compile("(\\w*)\\.(\\w*)");
|
||||
@NotNull private static final Pattern NAME_WITH_SPACE = Pattern.compile("(\\w*) (\\w*)");
|
||||
|
||||
@NotNull
|
||||
public static String toExactString(@NotNull VcsUser user) {
|
||||
return getString(user.getName(), user.getEmail());
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private static String getString(@NotNull String name, @NotNull String email) {
|
||||
if (name.isEmpty()) return email;
|
||||
if (email.isEmpty()) return name;
|
||||
return name + " <" + email + ">";
|
||||
}
|
||||
|
||||
public static boolean isSamePerson(@NotNull VcsUser user1, @NotNull VcsUser user2) {
|
||||
return getNameInStandardForm(getName(user1)).equals(getNameInStandardForm(getName(user2)));
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String getName(@NotNull VcsUser user) {
|
||||
if (!user.getName().isEmpty()) return user.getName();
|
||||
String emailNamePart = getNameFromEmail(user.getEmail());
|
||||
if (emailNamePart != null) return emailNamePart;
|
||||
return user.getEmail();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static String getNameFromEmail(@NotNull String email) {
|
||||
int at = email.indexOf('@');
|
||||
String emailNamePart = null;
|
||||
if (at > 0) {
|
||||
emailNamePart = email.substring(0, at);
|
||||
}
|
||||
return emailNamePart;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String getNameInStandardForm(@NotNull String name) {
|
||||
Pair<String, String> firstAndLastName = getFirstAndLastName(name);
|
||||
if (firstAndLastName != null) {
|
||||
return firstAndLastName.first.toLowerCase() + " " + firstAndLastName.second.toLowerCase();
|
||||
}
|
||||
return name.toLowerCase();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
public static Pair<String, String> getFirstAndLastName(@NotNull String name) {
|
||||
Matcher nameWithDotMatcher = NAME_WITH_DOT.matcher(name);
|
||||
if (nameWithDotMatcher.matches()) {
|
||||
return Pair.create(nameWithDotMatcher.group(1), nameWithDotMatcher.group(2));
|
||||
}
|
||||
Matcher nameWithSpaceMatcher = NAME_WITH_SPACE.matcher(name);
|
||||
if (nameWithSpaceMatcher.matches()) {
|
||||
return Pair.create(nameWithSpaceMatcher.group(1), nameWithSpaceMatcher.group(2));
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user