Skip to content

Commit b87b606

Browse files
committed
Implemented a first version of a gotoRelatedProvider
1 parent 0f03579 commit b87b606

4 files changed

Lines changed: 125 additions & 5 deletions

File tree

resources/META-INF/plugin.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,10 +168,12 @@
168168
<psi.referenceContributor language="Mathematica" implementation="de.halirutan.mathematica.parsing.psi.MathematicaReferenceContributor"/>
169169
<renamePsiElementProcessor implementation="de.halirutan.mathematica.refactoring.MathematicaPsiRenameProcessor"/>
170170

171-
172171
<!--<applicationService serviceInterface="de.halirutan.mathematica.MathematicaSettings" serviceImplementation="de.halirutan.mathematica.MathematicaSettings"/>-->
173172
<!--<applicationConfigurable groupId="language" displayName="Mathematica" id="preferences.Mathematica"-->
174173
<!--instance="de.halirutan.mathematica.MathematicaSettingsConfigurable"/>-->
174+
<gotoRelatedProvider
175+
implementation="de.halirutan.mathematica.codeinsight.navigation.MathematicaGotoRelatedProvider"/>
176+
175177
</extensions>
176178

177179
<actions>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.psi.PsiElement;
26+
import org.jetbrains.annotations.NotNull;
27+
import org.jetbrains.annotations.Nullable;
28+
29+
/**
30+
* @author patrick (28.12.16).
31+
*/
32+
public class GotoSymbolItem extends GotoRelatedItem {
33+
private final String myDescription;
34+
GotoSymbolItem(@NotNull PsiElement element, final String description) {
35+
super(element);
36+
myDescription = description;
37+
}
38+
39+
@Nullable
40+
@Override
41+
public String getCustomName() {
42+
return myDescription;
43+
}
44+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
}

src/de/halirutan/mathematica/parsing/psi/util/GlobalDefinitionCollector.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@
4444
*/
4545
public class GlobalDefinitionCollector {
4646

47-
private Map<String, HashSet<AssignmentProperty>> myAssignments;
47+
private final Map<String, HashSet<AssignmentProperty>> myAssignments;
4848

4949
public GlobalDefinitionCollector(PsiFile startElement) {
50-
myAssignments = new HashMap<String, HashSet<AssignmentProperty>>();
50+
myAssignments = new HashMap<>();
5151
final CollectorVisitor myVisitor = new CollectorVisitor();
5252
startElement.accept(myVisitor);
5353
}
@@ -64,7 +64,7 @@ private void addAssignment(Symbol symbol, PsiElement lhs, SymbolAssignmentType t
6464
if (myAssignments.containsKey(key)) {
6565
assignment = myAssignments.get(key);
6666
} else {
67-
assignment = new HashSet<AssignmentProperty>(1);
67+
assignment = new HashSet<>(1);
6868
myAssignments.put(key, assignment);
6969
}
7070
assignment.add(new AssignmentProperty(symbol, lhs, type));
@@ -92,7 +92,8 @@ public void visitSet(final Set set) {
9292
for (Symbol symbol : unboundSymbols) {
9393
PsiElement context = lhs;
9494
if (visitor.getAssignmentType() == ATTRIBUTES_ASSIGNMENT || visitor.getAssignmentType() == OPTIONS_ASSIGNMENT) {
95-
context = set.getLastChild();
95+
// context = set.getLastChild();
96+
context = set.getFirstChild();
9697
}
9798
addAssignment(symbol, context, visitor.getAssignmentType());
9899
}

0 commit comments

Comments
 (0)