|
| 1 | +/* |
| 2 | + * Copyright (c) 2016 Patrick Scheibe |
| 3 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | + * of this software and associated documentation files (the "Software"), to deal |
| 5 | + * in the Software without restriction, including without limitation the rights |
| 6 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | + * copies of the Software, and to permit persons to whom the Software is |
| 8 | + * furnished to do so, subject to the following conditions: |
| 9 | + * |
| 10 | + * The above copyright notice and this permission notice shall be included in |
| 11 | + * all copies or substantial portions of the Software. |
| 12 | + * |
| 13 | + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 19 | + * THE SOFTWARE. |
| 20 | + */ |
| 21 | + |
| 22 | +package de.halirutan.mathematica.codeinsight.navigation; |
| 23 | + |
| 24 | +import com.intellij.navigation.GotoRelatedItem; |
| 25 | +import com.intellij.navigation.GotoRelatedProvider; |
| 26 | +import com.intellij.psi.PsiElement; |
| 27 | +import com.intellij.psi.PsiReference; |
| 28 | +import com.intellij.psi.impl.source.tree.LeafPsiElement; |
| 29 | +import de.halirutan.mathematica.parsing.MathematicaElementTypes; |
| 30 | +import de.halirutan.mathematica.parsing.psi.api.Symbol; |
| 31 | +import de.halirutan.mathematica.parsing.psi.util.GlobalDefinitionCollector; |
| 32 | +import de.halirutan.mathematica.parsing.psi.util.GlobalDefinitionCollector.AssignmentProperty; |
| 33 | +import de.halirutan.mathematica.parsing.psi.util.LocalizationConstruct.ConstructType; |
| 34 | +import org.jetbrains.annotations.NotNull; |
| 35 | + |
| 36 | +import java.util.ArrayList; |
| 37 | +import java.util.HashSet; |
| 38 | +import java.util.List; |
| 39 | +import java.util.Map; |
| 40 | + |
| 41 | +/** |
| 42 | + * @author patrick (28.12.16). |
| 43 | + */ |
| 44 | +public class MathematicaGotoRelatedProvider extends GotoRelatedProvider { |
| 45 | + |
| 46 | + @NotNull |
| 47 | + @Override |
| 48 | + public List<? extends GotoRelatedItem> getItems(@NotNull PsiElement psiElement) { |
| 49 | + ArrayList<GotoRelatedItem> declarations = new ArrayList<>(); |
| 50 | + if (psiElement instanceof LeafPsiElement && ((LeafPsiElement) psiElement).getElementType().equals(MathematicaElementTypes.IDENTIFIER)) { |
| 51 | + psiElement = psiElement.getParent(); |
| 52 | + } |
| 53 | + if (psiElement instanceof Symbol) { |
| 54 | + PsiReference ref = psiElement.getReference(); |
| 55 | + if (ref != null) { |
| 56 | + PsiElement resolve = ref.resolve(); |
| 57 | + if (resolve != null) { |
| 58 | + if (resolve instanceof Symbol && ((Symbol) resolve).getLocalizationConstruct().equals(ConstructType.NULL)) { |
| 59 | + GlobalDefinitionCollector collector = new GlobalDefinitionCollector(psiElement.getContainingFile()); |
| 60 | + Map<String, HashSet<AssignmentProperty>> assignments = collector.getAssignments(); |
| 61 | + for (AssignmentProperty property : assignments.get(((Symbol) resolve).getSymbolName())) { |
| 62 | + String text = property.myLhsOfAssignment.getText(); |
| 63 | + GotoRelatedItem item = new GotoSymbolItem(property.myAssignmentSymbol, text.substring(0, Math.min(text.length(),50))); |
| 64 | + declarations.add(item); |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + return declarations; |
| 71 | + } |
| 72 | + |
| 73 | +} |
0 commit comments