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
.codeInsight
.daemon
.impl
;
18 import com
.intellij
.codeInsight
.daemon
.ReferenceImporter
;
19 import com
.intellij
.codeInsight
.daemon
.impl
.quickfix
.ImportClassFix
;
20 import com
.intellij
.openapi
.editor
.Editor
;
21 import com
.intellij
.openapi
.editor
.Document
;
22 import com
.intellij
.psi
.PsiFile
;
23 import com
.intellij
.psi
.PsiElement
;
24 import com
.intellij
.psi
.PsiJavaCodeReferenceElement
;
25 import com
.intellij
.lang
.StdLanguages
;
26 import org
.jetbrains
.annotations
.NotNull
;
28 import java
.util
.List
;
33 public class JavaReferenceImporter
implements ReferenceImporter
{
34 public boolean autoImportReferenceAtCursor(@NotNull final Editor editor
, @NotNull final PsiFile file
) {
35 return autoImportReferenceAtCursor(editor
, file
, false);
38 public static boolean autoImportReferenceAtCursor(@NotNull Editor editor
, @NotNull PsiFile file
, final boolean allowCaretNearRef
) {
39 if (!file
.getViewProvider().getLanguages().contains(StdLanguages
.JAVA
)) return false;
40 int caretOffset
= editor
.getCaretModel().getOffset();
41 Document document
= editor
.getDocument();
42 int lineNumber
= document
.getLineNumber(caretOffset
);
43 int startOffset
= document
.getLineStartOffset(lineNumber
);
44 int endOffset
= document
.getLineEndOffset(lineNumber
);
46 List
<PsiElement
> elements
= CollectHighlightsUtil
.getElementsInRange(file
, startOffset
, endOffset
);
47 for (PsiElement element
: elements
) {
48 if (element
instanceof PsiJavaCodeReferenceElement
) {
49 PsiJavaCodeReferenceElement ref
= (PsiJavaCodeReferenceElement
)element
;
50 if (ref
.multiResolve(true).length
== 0) {
51 new ImportClassFix(ref
).doFix(editor
, false, allowCaretNearRef
);