2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.intellij
.usageView
;
19 import com
.intellij
.lang
.Language
;
20 import com
.intellij
.lang
.findUsages
.FindUsagesProvider
;
21 import com
.intellij
.lang
.findUsages
.LanguageFindUsages
;
22 import com
.intellij
.openapi
.diagnostic
.Logger
;
23 import com
.intellij
.openapi
.util
.text
.StringUtil
;
24 import com
.intellij
.psi
.ElementDescriptionUtil
;
25 import com
.intellij
.psi
.PsiElement
;
26 import com
.intellij
.psi
.meta
.PsiMetaData
;
27 import com
.intellij
.psi
.meta
.PsiMetaOwner
;
28 import org
.jetbrains
.annotations
.NotNull
;
30 import java
.util
.Arrays
;
31 import java
.util
.LinkedHashSet
;
37 public class UsageViewUtil
{
38 private static final Logger LOG
= Logger
.getInstance("#com.intellij.usageView.UsageViewUtil");
40 private UsageViewUtil() { }
42 public static String
createNodeText(PsiElement element
) {
43 return ElementDescriptionUtil
.getElementDescription(element
, UsageViewNodeTextLocation
.INSTANCE
);
46 public static String
getMetaDataName(final PsiMetaData metaData
) {
47 final String name
= metaData
.getName();
48 return StringUtil
.isEmpty(name
) ?
"''" : name
;
51 public static String
getShortName(final PsiElement psiElement
) {
52 LOG
.assertTrue(psiElement
.isValid());
53 return ElementDescriptionUtil
.getElementDescription(psiElement
, UsageViewShortNameLocation
.INSTANCE
);
56 public static String
getLongName(final PsiElement psiElement
) {
57 LOG
.assertTrue(psiElement
.isValid());
58 return ElementDescriptionUtil
.getElementDescription(psiElement
, UsageViewLongNameLocation
.INSTANCE
);
61 public static String
getType(@NotNull PsiElement psiElement
) {
62 return ElementDescriptionUtil
.getElementDescription(psiElement
, UsageViewTypeLocation
.INSTANCE
);
65 public static String
getDescriptiveName(@NotNull PsiElement psiElement
) {
66 LOG
.assertTrue(psiElement
.isValid());
68 if (psiElement
instanceof PsiMetaOwner
) {
69 final PsiMetaOwner psiMetaOwner
= (PsiMetaOwner
)psiElement
;
70 final PsiMetaData metaData
= psiMetaOwner
.getMetaData();
71 if (metaData
!= null) return getMetaDataName(metaData
);
74 final Language lang
= psiElement
.getLanguage();
75 FindUsagesProvider provider
= LanguageFindUsages
.INSTANCE
.forLanguage(lang
);
76 return provider
.getDescriptiveName(psiElement
);
79 public static boolean hasNonCodeUsages(UsageInfo
[] usages
) {
80 for (UsageInfo usage
: usages
) {
81 if (usage
.isNonCodeUsage
) return true;
86 public static boolean hasReadOnlyUsages(UsageInfo
[] usages
) {
87 for (UsageInfo usage
: usages
) {
88 if (!usage
.isWritable()) return true;
93 public static UsageInfo
[] removeDuplicatedUsages(@NotNull UsageInfo
[] usages
) {
94 Set
<UsageInfo
> set
= new LinkedHashSet
<UsageInfo
>(Arrays
.asList(usages
));
95 return set
.toArray(new UsageInfo
[set
.size()]);