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
.codeInsight
.navigation
;
19 import com
.intellij
.ide
.structureView
.StructureViewBuilder
;
20 import com
.intellij
.ide
.structureView
.StructureViewModel
;
21 import com
.intellij
.ide
.structureView
.StructureViewTreeElement
;
22 import com
.intellij
.ide
.structureView
.TreeBasedStructureViewBuilder
;
23 import com
.intellij
.ide
.util
.treeView
.smartTree
.TreeElement
;
24 import com
.intellij
.lang
.LanguageStructureViewBuilder
;
25 import com
.intellij
.openapi
.extensions
.Extensions
;
26 import com
.intellij
.psi
.PsiElement
;
27 import com
.intellij
.psi
.PsiFile
;
28 import gnu
.trove
.THashSet
;
29 import gnu
.trove
.TIntArrayList
;
30 import org
.jetbrains
.annotations
.NotNull
;
32 import java
.util
.Collection
;
34 public class MethodUpDownUtil
{
35 private MethodUpDownUtil() {
38 public static int[] getNavigationOffsets(PsiFile file
, final int caretOffset
) {
39 for(MethodNavigationOffsetProvider provider
: Extensions
.getExtensions(MethodNavigationOffsetProvider
.EP_NAME
)) {
40 final int[] offsets
= provider
.getMethodNavigationOffsets(file
, caretOffset
);
41 if (offsets
!= null && offsets
.length
> 0) {
46 Collection
<PsiElement
> array
= new THashSet
<PsiElement
>();
47 addNavigationElements(array
, file
);
48 return offsetsFromElements(array
);
51 public static int[] offsetsFromElements(final Collection
<PsiElement
> array
) {
52 TIntArrayList offsets
= new TIntArrayList(array
.size());
53 for (PsiElement element
: array
) {
54 offsets
.add(element
.getTextOffset());
57 return offsets
.toNativeArray();
60 private static void addNavigationElements(Collection
<PsiElement
> array
, PsiFile element
) {
61 StructureViewBuilder structureViewBuilder
= LanguageStructureViewBuilder
.INSTANCE
.getStructureViewBuilder(element
);
62 if (structureViewBuilder
instanceof TreeBasedStructureViewBuilder
) {
63 TreeBasedStructureViewBuilder builder
= (TreeBasedStructureViewBuilder
) structureViewBuilder
;
64 StructureViewModel model
= builder
.createStructureViewModel();
65 addStructureViewElements(model
.getRoot(), array
, element
);
69 private static void addStructureViewElements(final TreeElement parent
, final Collection
<PsiElement
> array
, @NotNull PsiFile file
) {
70 for(TreeElement treeElement
: parent
.getChildren()) {
71 Object value
= ((StructureViewTreeElement
)treeElement
).getValue();
72 if (value
instanceof PsiElement
) {
73 PsiElement element
= (PsiElement
)value
;
74 if (array
.contains(element
) || !file
.equals(element
.getContainingFile())) return;
77 addStructureViewElements(treeElement
, array
, file
);