From 3a2a54275f7068241f67ba405aaf3756e9a2a030 Mon Sep 17 00:00:00 2001 From: Dmitry Batkovich Date: Fri, 1 Sep 2017 18:55:23 +0300 Subject: [PATCH] properties: cache default base name (IDEA-178385) --- .../lang/properties/PropertiesUtil.java | 57 ++++++++++++------- .../properties/ResourceBundleManager.java | 14 ++--- 2 files changed, 40 insertions(+), 31 deletions(-) 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 a6c67972057b..ef71c12dccc4 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-2009 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -23,6 +23,9 @@ 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; +import com.intellij.psi.PsiFile; +import com.intellij.psi.util.CachedValueProvider; +import com.intellij.psi.util.CachedValuesManager; import com.intellij.reference.SoftLazyValue; import com.intellij.util.Function; import com.intellij.util.SmartList; @@ -98,32 +101,42 @@ public class PropertiesUtil { } @NotNull - static String getDefaultBaseName(@NotNull final VirtualFile file) { - final String name = file.getName(); + static String getDefaultBaseName(@NotNull final PsiFile file) { + return CachedValuesManager.getCachedValue(file, new CachedValueProvider() { + @NotNull + @Override + public Result compute() { + return Result.create(computeBaseName(), file); + } - if (!StringUtil.containsChar(name, '_')) { - return FileUtil.getNameWithoutExtension(name); - } + private String computeBaseName() { + final String name = file.getName(); - final Matcher matcher = LOCALE_PATTERN.matcher(name); - final String baseNameWithExtension; - - int matchIndex = 0; - while (matcher.find(matchIndex)) { - final MatchResult matchResult = matcher.toMatchResult(); - final String[] splitted = matchResult.group(1).split("_"); - if (splitted.length > 1) { - final String langCode = splitted[1]; - if (!LOCALES_LANGUAGE_CODES.getValue().contains(langCode)) { - matchIndex = matchResult.start(1) + 1; - continue; + if (!StringUtil.containsChar(name, '_')) { + return FileUtil.getNameWithoutExtension(name); } - baseNameWithExtension = name.substring(0, matchResult.start(1)) + name.substring(matchResult.end(1)); + + final Matcher matcher = LOCALE_PATTERN.matcher(name); + final String baseNameWithExtension; + + int matchIndex = 0; + while (matcher.find(matchIndex)) { + final MatchResult matchResult = matcher.toMatchResult(); + final String[] splitted = matchResult.group(1).split("_"); + if (splitted.length > 1) { + final String langCode = splitted[1]; + if (!LOCALES_LANGUAGE_CODES.getValue().contains(langCode)) { + matchIndex = matchResult.start(1) + 1; + continue; + } + baseNameWithExtension = name.substring(0, matchResult.start(1)) + name.substring(matchResult.end(1)); + return FileUtil.getNameWithoutExtension(baseNameWithExtension); + } + } + baseNameWithExtension = name; return FileUtil.getNameWithoutExtension(baseNameWithExtension); } - } - baseNameWithExtension = name; - return FileUtil.getNameWithoutExtension(baseNameWithExtension); + }); } @NotNull diff --git a/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/ResourceBundleManager.java b/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/ResourceBundleManager.java index 40b5fc240e40..50397814c48d 100644 --- a/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/ResourceBundleManager.java +++ b/plugins/properties/properties-psi-impl/src/com/intellij/lang/properties/ResourceBundleManager.java @@ -1,5 +1,5 @@ /* - * Copyright 2000-2016 JetBrains s.r.o. + * Copyright 2000-2017 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. @@ -197,17 +197,13 @@ public class ResourceBundleManager implements PersistentStateComponent