Skip to content

Commit 487b38a

Browse files
committed
Investigated why renaming doesn't rename all instances. Implemented an additional reference provider for symbols. UsageProvider seems to be ignored when Idea can use references instead.
1 parent ec6d9b1 commit 487b38a

12 files changed

Lines changed: 278 additions & 6 deletions

resources/META-INF/plugin.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,11 @@
167167

168168
<psi.referenceContributor language="Mathematica" implementation="de.halirutan.mathematica.parsing.psi.MathematicaReferenceContributor"/>
169169

170+
<lang.findUsagesProvider language="Mathematica"
171+
implementationClass="de.halirutan.mathematica.find.MathematicaFindUsageProvider"/>
172+
173+
174+
170175
<!--<applicationService serviceInterface="de.halirutan.mathematica.MathematicaSettings" serviceImplementation="de.halirutan.mathematica.MathematicaSettings"/>-->
171176
<!--<applicationConfigurable groupId="language" displayName="Mathematica" id="preferences.Mathematica"-->
172177
<!--instance="de.halirutan.mathematica.MathematicaSettingsConfigurable"/>-->
@@ -205,6 +210,8 @@
205210
description="Collapse all named characters into their utf8 counterpart.">
206211
</action>
207212

213+
<action id="Mathematica.RenameReferenenceResolve" class="de.halirutan.mathematica.actions.RenameReferenceResolve"
214+
text="Rename Referenence Resolve"/>
208215
<!--<separator/>-->
209216

210217
<!--<action id="Mathematica.ShowFormattingBlocks" class="de.halirutan.mathematica.actions.ShowFormattingBlocks"-->
@@ -214,5 +221,6 @@
214221

215222
<add-to-group group-id="MainMenu" anchor="after" relative-to-action="ToolsMenu"/>
216223
</group>
224+
217225
</actions>
218226
</idea-plugin>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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.actions;
23+
24+
import com.intellij.codeInsight.highlighting.HighlightManager;
25+
import com.intellij.openapi.actionSystem.AnAction;
26+
import com.intellij.openapi.actionSystem.AnActionEvent;
27+
import com.intellij.openapi.actionSystem.DataKeys;
28+
import com.intellij.openapi.editor.Editor;
29+
import com.intellij.openapi.editor.EditorGutter;
30+
import com.intellij.openapi.editor.HighlighterColors;
31+
import com.intellij.openapi.editor.SelectionModel;
32+
import com.intellij.openapi.editor.colors.EditorColors;
33+
import com.intellij.openapi.editor.colors.EditorColorsManager;
34+
import com.intellij.openapi.editor.colors.EditorColorsScheme;
35+
import com.intellij.openapi.editor.colors.TextAttributesKey;
36+
import com.intellij.openapi.editor.markup.EffectType;
37+
import com.intellij.openapi.editor.markup.HighlighterTargetArea;
38+
import com.intellij.openapi.editor.markup.MarkupModel;
39+
import com.intellij.openapi.editor.markup.TextAttributes;
40+
import com.intellij.openapi.project.Project;
41+
import com.intellij.openapi.ui.Messages;
42+
import com.intellij.psi.PsiElement;
43+
import com.intellij.psi.PsiReference;
44+
import com.intellij.psi.search.searches.ReferencesSearch;
45+
46+
import java.awt.*;
47+
import java.util.*;
48+
import java.util.List;
49+
50+
/**
51+
* @author patrick (21.12.16).
52+
*/
53+
public class RenameReferenceResolve extends AnAction {
54+
55+
@Override
56+
public void actionPerformed(AnActionEvent e) {
57+
final Project project = e.getProject();
58+
final PsiElement element = e.getData(DataKeys.PSI_ELEMENT);
59+
60+
final Editor editor = e.getData(DataKeys.EDITOR);
61+
62+
final HighlightManager highlightManager = HighlightManager.getInstance(project);
63+
final EditorColorsManager editorColorsManager =
64+
EditorColorsManager.getInstance();
65+
final EditorColorsScheme globalScheme =
66+
editorColorsManager.getGlobalScheme();
67+
final TextAttributes textattributes =
68+
globalScheme.getAttributes(
69+
EditorColors.SEARCH_RESULT_ATTRIBUTES);
70+
71+
72+
if (element != null && editor != null) {
73+
final List<PsiElement> usages = new ArrayList<PsiElement>();
74+
usages.add(element);
75+
76+
final Collection<PsiReference> refs = ReferencesSearch.search(element).findAll();
77+
for (PsiReference ref : refs) {
78+
usages.add(ref.getElement());
79+
}
80+
highlightManager.addOccurrenceHighlights(
81+
editor, usages.toArray(new PsiElement[usages.size()]), textattributes, false, null);
82+
} else {
83+
Messages.showErrorDialog("No element under cursor.", "Error");
84+
}
85+
}
86+
}

src/de/halirutan/mathematica/codeinsight/completion/MathematicaCharFilter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public class MathematicaCharFilter extends CharFilter {
3535
@Nullable
3636
@Override
3737
public Result acceptChar(final char c, final int prefixLength, final Lookup lookup) {
38-
if (!lookup.isFocused()) return null;
38+
// if (!lookup.isFocused()) return null;
3939
final PsiFile psiFile = lookup.getPsiFile();
4040

4141
if (psiFile != null && !psiFile.getViewProvider().getLanguages().contains(MathematicaLanguage.INSTANCE))
@@ -47,6 +47,8 @@ public Result acceptChar(final char c, final int prefixLength, final Lookup look
4747
case '{':
4848
((LookupImpl) lookup).finishLookup('\n');
4949
return Result.SELECT_ITEM_AND_FINISH_LOOKUP;
50+
case '`':
51+
return Result.ADD_TO_PREFIX;
5052
default:
5153
return null;
5254
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
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.find;
23+
24+
import com.intellij.lang.cacheBuilder.DefaultWordsScanner;
25+
import com.intellij.lang.cacheBuilder.WordsScanner;
26+
import com.intellij.lang.findUsages.FindUsagesProvider;
27+
import com.intellij.psi.PsiElement;
28+
import com.intellij.psi.PsiNamedElement;
29+
import com.intellij.psi.tree.TokenSet;
30+
import de.halirutan.mathematica.lexer.MathematicaLexer;
31+
import de.halirutan.mathematica.parsing.MathematicaElementTypes;
32+
import de.halirutan.mathematica.parsing.psi.api.Symbol;
33+
import de.halirutan.mathematica.parsing.psi.api.string.MString;
34+
import org.jetbrains.annotations.NotNull;
35+
import org.jetbrains.annotations.Nullable;
36+
37+
/**
38+
* @author patrick (21.12.16).
39+
*/
40+
public class MathematicaFindUsageProvider implements FindUsagesProvider{
41+
@Nullable
42+
@Override
43+
public WordsScanner getWordsScanner() {
44+
return new DefaultWordsScanner(new MathematicaLexer(),
45+
TokenSet.create(MathematicaElementTypes.IDENTIFIER),
46+
MathematicaElementTypes.COMMENTS,
47+
TokenSet.EMPTY
48+
);
49+
}
50+
51+
@Override
52+
public boolean canFindUsagesFor(@NotNull PsiElement psiElement) {
53+
return psiElement instanceof Symbol;
54+
}
55+
56+
@Nullable
57+
@Override
58+
public String getHelpId(@NotNull PsiElement psiElement) {
59+
return null;
60+
}
61+
62+
@NotNull
63+
@Override
64+
public String getType(@NotNull PsiElement element) {
65+
if (element instanceof Symbol) {
66+
return "Symbol";
67+
} else if (element instanceof MString) {
68+
return "String";
69+
}
70+
return "";
71+
}
72+
73+
@NotNull
74+
@Override
75+
public String getDescriptiveName(@NotNull PsiElement element) {
76+
if (element instanceof Symbol) {
77+
return ((Symbol) element).getSymbolName();
78+
} else {
79+
return "";
80+
}
81+
}
82+
83+
@NotNull
84+
@Override
85+
public String getNodeText(@NotNull PsiElement element, boolean useFullName) {
86+
if (element instanceof Symbol) {
87+
return ((Symbol) element).getSymbolName();
88+
} else {
89+
return "";
90+
}
91+
}
92+
}

src/de/halirutan/mathematica/parsing/psi/MathematicaReferenceContributor.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.intellij.patterns.PlatformPatterns;
2525
import com.intellij.psi.PsiReferenceContributor;
2626
import com.intellij.psi.PsiReferenceRegistrar;
27+
import de.halirutan.mathematica.parsing.psi.api.Symbol;
2728
import de.halirutan.mathematica.parsing.psi.api.string.MString;
2829
import org.jetbrains.annotations.NotNull;
2930

@@ -40,5 +41,7 @@ public class MathematicaReferenceContributor extends PsiReferenceContributor {
4041
public void registerReferenceProviders(@NotNull PsiReferenceRegistrar registrar) {
4142
registrar.registerReferenceProvider(PlatformPatterns.psiElement(MString.class),
4243
new MathematicaStringReferenceProvider());
44+
// registrar.registerReferenceProvider(PlatformPatterns.psiElement(Symbol.class),
45+
// new MathematicaSymbolReferenceProvider());
4346
}
4447
}

src/de/halirutan/mathematica/parsing/psi/MathematicaStringReferenceProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,14 @@ public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNu
6969
final Expression symbol = ((MessageName) messageElement).getSymbol();
7070
if (symbol instanceof Symbol) {
7171
String usageText = element.getText();
72-
final String symbolName = ((Symbol) symbol).getSymbolName();
72+
final String symbolName = Matcher.quoteReplacement(((Symbol) symbol).getSymbolName());
7373
Pattern symbolNamePattern = StringUsageReference.getSymbolPattern(symbolName);
7474
final Matcher matcher = symbolNamePattern.matcher(usageText);
7575
while (matcher.find()) {
7676
final int start = matcher.start(2);
7777
final int end = matcher.end(2);
7878
result.add(
79-
new StringUsageReference((MString) element, TextRange.create(start, end), symbolName, (Symbol) symbol)
79+
new StringUsageReference((MString) element, TextRange.create(start, end), symbolName, (Symbol) symbol.getReference().resolve())
8080
);
8181
}
8282
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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.parsing.psi;
23+
24+
import com.intellij.psi.PsiElement;
25+
import com.intellij.psi.PsiReference;
26+
import com.intellij.psi.PsiReferenceProvider;
27+
import com.intellij.util.ProcessingContext;
28+
import de.halirutan.mathematica.parsing.psi.api.Symbol;
29+
import de.halirutan.mathematica.parsing.psi.api.string.MString;
30+
import de.halirutan.mathematica.parsing.psi.impl.SymbolPsiReference;
31+
import org.jetbrains.annotations.NotNull;
32+
33+
import java.util.ArrayList;
34+
35+
/**
36+
* @author patrick (21.12.16).
37+
*/
38+
public class MathematicaSymbolReferenceProvider extends PsiReferenceProvider {
39+
@NotNull
40+
@Override
41+
public PsiReference[] getReferencesByElement(@NotNull PsiElement element, @NotNull ProcessingContext context) {
42+
if (!(element instanceof Symbol)) {
43+
return new PsiReference[0];
44+
}
45+
ArrayList<PsiReference> result = new ArrayList<PsiReference>();
46+
47+
Symbol symbol = (Symbol) element;
48+
final SymbolPsiReference reference = (SymbolPsiReference) symbol.getReference();
49+
final PsiElement resolve;
50+
if (reference != null) {
51+
result.add(reference);
52+
resolve = reference.resolve();
53+
if (resolve instanceof Symbol) {
54+
final PsiElement[] elemsReferencingToMe = ((Symbol) resolve).getElementsReferencingToMe();
55+
for (PsiElement psiElement : elemsReferencingToMe) {
56+
result.add(psiElement.getReference());
57+
}
58+
}
59+
}
60+
return result.toArray(new PsiReference[result.size()]);
61+
}
62+
}

src/de/halirutan/mathematica/parsing/psi/api/Symbol.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
import com.intellij.psi.PsiElement;
2525
import com.intellij.psi.PsiNameIdentifierOwner;
26+
import com.intellij.psi.PsiReference;
2627
import de.halirutan.mathematica.parsing.psi.util.LocalizationConstruct;
2728

2829
/**
@@ -83,5 +84,7 @@ public interface Symbol extends PsiNameIdentifierOwner {
8384

8485
void addElementReferencingToMe(Symbol reference);
8586

87+
PsiElement[] getElementsReferencingToMe();
88+
8689
public void subtreeChanged();
8790
}

src/de/halirutan/mathematica/parsing/psi/impl/StringUsageReference.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public Object[] getVariants() {
6565
public PsiElement handleElementRename(String newElementName) throws IncorrectOperationException {
6666
final String text = myElement1.getText();
6767
Matcher matcher = getSymbolPattern(mySymbolNameInside).matcher(text);
68-
String newContent = matcher.replaceAll("$1" + newElementName + "$3");
68+
String newContent = matcher.replaceAll("$1" + Matcher.quoteReplacement(newElementName) + "$3");
6969
myElement1 = (MString) myElement1.setName(newContent);
7070
return myElement1;
7171
}

src/de/halirutan/mathematica/parsing/psi/impl/SymbolImpl.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,10 @@
2626
import com.intellij.psi.PsiElementVisitor;
2727
import com.intellij.psi.PsiFileFactory;
2828
import com.intellij.psi.PsiReference;
29+
import com.intellij.psi.impl.source.resolve.reference.ReferenceProvidersRegistry;
2930
import de.halirutan.mathematica.filetypes.MathematicaFileType;
3031
import de.halirutan.mathematica.parsing.MathematicaElementTypes;
32+
import de.halirutan.mathematica.parsing.psi.MathematicaSymbolReferenceProvider;
3133
import de.halirutan.mathematica.parsing.psi.MathematicaVisitor;
3234
import de.halirutan.mathematica.parsing.psi.api.Symbol;
3335
import de.halirutan.mathematica.parsing.psi.util.LocalizationConstruct.ConstructType;
@@ -160,6 +162,11 @@ public void addElementReferencingToMe(Symbol reference) {
160162
if (!reference.equals(this)) myReferringElements.add(reference);
161163
}
162164

165+
@Override
166+
public PsiElement[] getElementsReferencingToMe() {
167+
return myReferringElements.toArray(new Symbol[myReferringElements.size()]);
168+
}
169+
163170
@Override
164171
public void accept(@NotNull PsiElementVisitor visitor) {
165172
if (visitor instanceof MathematicaVisitor) {
@@ -168,4 +175,12 @@ public void accept(@NotNull PsiElementVisitor visitor) {
168175
super.accept(visitor);
169176
}
170177
}
178+
179+
@NotNull
180+
@Override
181+
public PsiReference[] getReferences() {
182+
return super.getReferences();
183+
// return ReferenceProvidersRegistry.getReferencesFromProviders(this);
184+
}
185+
171186
}

0 commit comments

Comments
 (0)