update copyright
[fedora-idea.git] / java / java-impl / src / com / intellij / psi / impl / source / resolve / reference / impl / providers / JavaClassListReferenceProvider.java
blobb81ea04b9a9610f0c30e20d20866e2e688c488d5
1 /*
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.
16 package com.intellij.psi.impl.source.resolve.reference.impl.providers;
18 import com.intellij.openapi.progress.ProgressManager;
19 import com.intellij.openapi.project.Project;
20 import com.intellij.openapi.util.NotNullLazyValue;
21 import com.intellij.psi.PsiElement;
22 import com.intellij.psi.PsiPackage;
23 import com.intellij.psi.PsiReference;
24 import com.intellij.psi.xml.XmlTag;
25 import com.intellij.psi.search.GlobalSearchScope;
26 import org.jetbrains.annotations.NotNull;
28 import java.util.*;
30 /**
31 * Created by IntelliJ IDEA.
32 * User: ik
33 * Date: 05.06.2003
34 * Time: 20:27:59
35 * To change this template use Options | File Templates.
37 public class JavaClassListReferenceProvider extends JavaClassReferenceProvider {
39 public JavaClassListReferenceProvider(final Project project) {
40 super(GlobalSearchScope.allScope(project), project);
41 setOption(ADVANCED_RESOLVE, Boolean.TRUE);
44 @NotNull
45 public PsiReference[] getReferencesByString(String str, PsiElement position, int offsetInPosition){
46 if (position instanceof XmlTag && ((XmlTag)position).getValue().getTextElements().length == 0) {
47 return PsiReference.EMPTY_ARRAY;
50 if (str.length() < 2) {
51 return PsiReference.EMPTY_ARRAY;
53 NotNullLazyValue<Set<String>> topLevelPackages = new NotNullLazyValue<Set<String>>() {
54 @NotNull
55 @Override
56 protected Set<String> compute() {
57 final Set<String> knownTopLevelPackages = new HashSet<String>();
58 final List<PsiElement> defaultPackages = getDefaultPackages();
59 for (final PsiElement pack : defaultPackages) {
60 if (pack instanceof PsiPackage) {
61 knownTopLevelPackages.add(((PsiPackage)pack).getName());
64 return knownTopLevelPackages;
67 final List<PsiReference> results = new ArrayList<PsiReference>();
69 for(int dot = str.indexOf('.'); dot > 0; dot = str.indexOf('.', dot + 1)) {
70 int start = dot;
71 while (start > 0 && Character.isLetterOrDigit(str.charAt(start - 1))) start--;
72 if (dot == start) {
73 continue;
75 String candidate = str.substring(start, dot);
76 if (topLevelPackages.getValue().contains(candidate)) {
77 int end = dot;
78 while (end < str.length() - 1) {
79 end++;
80 char ch = str.charAt(end);
81 if (ch != '.' && !Character.isJavaIdentifierPart(ch)) {
82 break;
85 String s = str.substring(start, end + 1);
86 results.addAll(Arrays.asList(new JavaClassReferenceSet(s, position, offsetInPosition + start, false, this){
87 public boolean isSoft(){
88 return true;
90 }.getAllReferences()));
91 ProgressManager.getInstance().checkCanceled();
94 return results.toArray(new PsiReference[results.size()]);