From 214f99bc443711961bc933a472877500ff1601d3 Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Wed, 23 Apr 2014 15:08:07 +0400 Subject: [PATCH] IDEA-103836 --- .../lang/properties/PropertiesUtil.java | 69 ++++++++--------- .../lang/properties/ResourceBundleImpl.java | 2 +- .../lang/properties/PropertiesUtilTest.java | 75 +++++++++++++++++++ 3 files changed, 111 insertions(+), 35 deletions(-) create mode 100644 plugins/properties/testSrc/com/intellij/lang/properties/PropertiesUtilTest.java diff --git a/plugins/properties/properties-psi-api/src/com/intellij/lang/properties/PropertiesUtil.java b/plugins/properties/properties-psi-api/src/com/intellij/lang/properties/PropertiesUtil.java index 03cd1ca1fbb5..abc281f7ea82 100644 --- a/plugins/properties/properties-psi-api/src/com/intellij/lang/properties/PropertiesUtil.java +++ b/plugins/properties/properties-psi-api/src/com/intellij/lang/properties/PropertiesUtil.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2014 JetBrains s.r.o. + * Copyright 2000-2009 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. @@ -20,6 +20,7 @@ import com.intellij.openapi.application.ApplicationManager; import com.intellij.openapi.project.Project; import com.intellij.openapi.roots.ProjectRootManager; import com.intellij.openapi.util.NullableComputable; +import com.intellij.openapi.util.io.FileUtil; import com.intellij.openapi.util.text.StringUtil; import com.intellij.openapi.vfs.VirtualFile; import com.intellij.psi.PsiDirectory; @@ -27,14 +28,17 @@ import com.intellij.util.SmartList; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; -import java.util.Arrays; -import java.util.List; -import java.util.Locale; +import java.util.*; +import java.util.regex.MatchResult; +import java.util.regex.Matcher; +import java.util.regex.Pattern; /** * @author cdr */ public class PropertiesUtil { + private final static Pattern LOCALE_PATTERN = Pattern.compile("(_[^\\._]{2}(_[^\\._]+){0,2})\\.[^_]+$"); + private final static Locale DEFAULT_LOCALE = new Locale("", "", ""); public static boolean isPropertyComplete(final Project project, ResourceBundle resourceBundle, String propertyName) { @@ -47,23 +51,24 @@ public class PropertiesUtil { @NotNull public static String getBaseName(@NotNull VirtualFile virtualFile) { - String name = virtualFile.getNameWithoutExtension(); - - List parts = StringUtil.split(name, "_"); - if (parts.size() == 1) return parts.get(0); - if (parts.size() == 0) return ""; - - String baseName = parts.get(0); - for (int i = 1; i< parts.size(); i++) { - String part = parts.get(i); - if (part.length() == 2) { - break; + String name = virtualFile.getName(); + final Matcher matcher = LOCALE_PATTERN.matcher(name); + final String baseNameWithExtension; + if (matcher.find()) { + final MatchResult matchResult = matcher.toMatchResult(); + final String[] splitted = matchResult.group(1).split("_"); + if (splitted.length > 1) { + baseNameWithExtension = name.substring(0, matchResult.start(1)) + name.substring(matchResult.end(1)); } - baseName += "_"; - baseName += part; + else { + baseNameWithExtension = name; + } + } + else { + baseNameWithExtension = name; } - return baseName; + return FileUtil.getNameWithoutExtension(baseNameWithExtension); } /** @@ -108,24 +113,20 @@ public class PropertiesUtil { } @NotNull - public static Locale getLocale(VirtualFile propertiesFile) { - String name = propertiesFile.getNameWithoutExtension(); - String tail = StringUtil.trimStart(name, getBaseName(propertiesFile)); - tail = StringUtil.trimStart(tail, "_"); - String[] parts = tail.split("_"); - String language = parts.length == 0 ? "" : parts[0]; - String country = ""; - String variant = ""; - if (parts.length >= 2 && parts[1].length() == 2) { - country = parts[1]; - for (int i = 2; i < parts.length; i++) { - String part = parts[i]; - if (variant.length() != 0) variant += "_"; - variant += part; + public static Locale getLocale(final VirtualFile propertiesFile) { + String name = propertiesFile.getName(); + final Matcher matcher = LOCALE_PATTERN.matcher(name); + if (matcher.find()) { + String rawLocale = matcher.group(1); + String[] splittedRawLocale = rawLocale.split("_"); + if (splittedRawLocale.length > 1 && splittedRawLocale[1].length() == 2) { + final String language = splittedRawLocale[1]; + final String country = splittedRawLocale.length > 2 ? splittedRawLocale[2] : ""; + final String variant = splittedRawLocale.length > 3 ? splittedRawLocale[3] : ""; + return new Locale(language, country, variant); } } - - return new Locale(language,country,variant); + return DEFAULT_LOCALE; } @NotNull diff --git a/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/ResourceBundleImpl.java b/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/ResourceBundleImpl.java index 1cd45628f498..1ac96a96f6af 100644 --- a/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/ResourceBundleImpl.java +++ b/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/ResourceBundleImpl.java @@ -50,7 +50,7 @@ public class ResourceBundleImpl extends ResourceBundle { List result = new SmartList(); PsiManager psiManager = PsiManager.getInstance(project); for (VirtualFile file : children) { - if (!file.isValid()) continue; + if (!file.isValid() || file.getExtension() == null) continue; if (Comparing.strEqual(PropertiesUtil.getBaseName(file), myBaseName)) { PropertiesFile propertiesFile = PropertiesImplUtil.getPropertiesFile(psiManager.findFile(file)); if (propertiesFile != null) { diff --git a/plugins/properties/testSrc/com/intellij/lang/properties/PropertiesUtilTest.java b/plugins/properties/testSrc/com/intellij/lang/properties/PropertiesUtilTest.java new file mode 100644 index 000000000000..5ebf25d4258f --- /dev/null +++ b/plugins/properties/testSrc/com/intellij/lang/properties/PropertiesUtilTest.java @@ -0,0 +1,75 @@ +/* + * Copyright 2000-2014 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.lang.properties; + +import com.intellij.openapi.vfs.newvfs.impl.StubVirtualFile; +import junit.framework.TestCase; +import org.jetbrains.annotations.NotNull; + +/** + * @author Dmitry Batkovich + */ +public class PropertiesUtilTest extends TestCase { + + public void testBaseNameWithoutLocale() { + assertBaseNameEquals("property-file.properties", "property-file"); + } + + public void testBaseNameWithLocale() { + assertBaseNameEquals("property-file_en._fr.asd_ru.properties", "property-file_en._fr.asd"); + } + + public void testBaseNameWithoutLocaleWithAdditionalExtension() { + assertBaseNameEquals("property-file.utf8.properties", "property-file.utf8"); + } + + public void testBaseNameWithLocaleWithAdditionalExtension() { + assertBaseNameEquals("property-file_fr.file_en.utf8.properties", "property-file_fr.file.utf8"); + } + + public void testBaseNameWithCountryAndVariant() { + assertBaseNameEquals("property-file_fr.file_en_GB_UNIX.utf8.properties", "property-file_fr.file.utf8"); + } + + public void testBaseNameWithCountry() { + assertBaseNameEquals("property-file_fr.file_en_GB.utf8.properties", "property-file_fr.file.utf8"); + } + + public void testBaseName() { + assertBaseNameEquals("Base_Properties.utf8.properties", "Base_Properties.utf8"); + } + + public void test1() { + assertBaseNameEquals("Base_Page_fr.utf8.properties", "Base_Page.utf8"); + } + public void test2() { + assertBaseNameEquals("Base_Page_en.utf8.properties", "Base_Page.utf8"); + } + public void test3() { + assertBaseNameEquals("Base_Page.utf8.properties", "Base_Page.utf8"); + } + + private static void assertBaseNameEquals(final String propertyFileName, final String expectedBaseName) { + final String actualBaseName = PropertiesUtil.getBaseName(new StubVirtualFile() { + @NotNull + @Override + public String getName() { + return propertyFileName; + } + }); + assertEquals(expectedBaseName, actualBaseName); + } +}