Skip to content

Commit 8d72eba

Browse files
committed
Implement GotoSymbol contributor and update build settings
1 parent db66d29 commit 8d72eba

9 files changed

Lines changed: 226 additions & 28 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
## Todo
44

5-
- Better rendering for "Go to Declaration" targets
65
- Inspection for using the same variable in the e.g. Module definition list several times
76
- Quick fix for adding usages and other messages
87
- Update to Mathematica version 11.2
@@ -21,10 +20,12 @@ in a later version of Mathematica basing on the module settings in the project s
2120
can now be used for completion and navigation. The highlighter shows which functions come from a different file.
2221
- Support for project-wide navigation and resolving of symbols. Now you can refactor, navigate and complete functions
2322
that are located in a different package file.
24-
- Smart completion inside comments to insert symbols from file.
23+
- Completion inside comments to insert symbols from file.
2524
- Implementation of better local variable support. Especially, "With" supports now several declaration lists.
2625
- Fixing "Go to related symbol". Finds all usages of a symbol and gives file, line and a snip of the code there. You can
2726
directly navigate to any of these places.
2827
- Implemented "Go to Declaration" so that the user can navigate to all targets where a function is given a definition (
2928
I know that go to _declaration_ is misleading for Mathematica, but it is the easier short-cut and widely used).
30-
- Heavy reimplementation of the core algorithms for resolving references.
29+
- Heavy reimplementation of the core algorithms for resolving references.
30+
- Goto Declaration now shows all places, where a functions gets some value. Be it several patterns, usage messages, options, or others.
31+
- Now there is a "Goto Symbol" function that lets you search for symbols that have a usage messages throughout your project

build.gradle

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ task wrapper(type: Wrapper) {
6565
// Information about the plugin
6666

6767
// Plugin version number
68-
version '3.0pre13'
68+
version '3.0pre14'
6969

7070
intellij {
71-
version = '2018.1.2'
71+
version = '2018.1.3'
7272
downloadSources = true
7373
pluginName = 'Mathematica-IntelliJ-Plugin'
74+
sameSinceUntilBuild = true
7475
updateSinceUntilBuild = true
7576
}
7677

@@ -80,4 +81,5 @@ def htmlFixer = {htmlFile -> file(htmlFile).text.replace('<html>', '').replace('
8081
patchPluginXml {
8182
changeNotes = htmlFixer('resources/META-INF/change-notes.html')
8283
pluginDescription = htmlFixer('resources/META-INF/description.html')
84+
sinceBuild = '181'
8385
}

resources/META-INF/plugin.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
<name>Mathematica Support</name>
2727
<category>Custom Language</category>
2828
<vendor url="http://mathematicaplugin.halirutan.de">Patrick Scheibe</vendor>
29-
<idea-version since-build="172"/>
29+
<idea-version since-build="182"/>
3030
<depends>com.intellij.modules.lang</depends>
3131

3232
<extensions defaultExtensionNs="com.intellij">
@@ -51,6 +51,7 @@
5151
<fileBasedIndex implementation="de.halirutan.mathematica.index.packageexport.MathematicaPackageExportIndex"/>
5252
<lang.refactoringSupport language="Mathematica" implementationClass="de.halirutan.mathematica.refactoring.MathematicaRefactoringSupport"/>
5353
<lang.namesValidator language="Mathematica" implementationClass="de.halirutan.mathematica.refactoring.MathematicaNamesValidator"/>
54+
<gotoSymbolContributor implementation="de.halirutan.mathematica.codeinsight.navigation.GotoSymbolContributor"/>
5455
<gotoRelatedProvider implementation="de.halirutan.mathematica.codeinsight.navigation.MathematicaGotoRelatedProvider"/>
5556
<lang.foldingBuilder language="Mathematica" implementationClass="de.halirutan.mathematica.codeinsight.folding.MathematicaExpressionFoldingBuilder"/>
5657

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030

3131
<appender name="DIALOG" class="com.intellij.diagnostic.DialogAppender">
3232
<filter class="org.apache.log4j.varia.LevelRangeFilter">
33-
<param name="LevelMin" value="INFO"/>
33+
<param name="LevelMin" value="DEBUG"/>
3434
</filter>
3535
</appender>
3636

@@ -52,9 +52,10 @@
5252
</appender>
5353

5454
<root>
55-
<priority value="INFO"/>
55+
<priority value="DEBUG"/>
5656
<appender-ref ref="DIALOG"/>
5757
<appender-ref ref="CONSOLE-WARN"/>
58+
<appender-ref ref="CONSOLE-DEBUG"/>
5859
<appender-ref ref="FILE"/>
5960
</root>
6061
</log4j:configuration>
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
/*
2+
* Copyright (c) 2018 Patrick Scheibe
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package de.halirutan.mathematica.codeinsight.navigation
24+
25+
import com.intellij.navigation.ChooseByNameContributor
26+
import com.intellij.navigation.NavigationItem
27+
import com.intellij.openapi.diagnostic.Logger
28+
import com.intellij.openapi.project.Project
29+
import com.intellij.openapi.vfs.VirtualFile
30+
import com.intellij.psi.PsiElement
31+
import com.intellij.psi.search.FilenameIndex
32+
import com.intellij.psi.search.GlobalSearchScope
33+
import com.intellij.util.indexing.FileBasedIndex
34+
import de.halirutan.mathematica.index.packageexport.MathematicaPackageExportIndex
35+
import de.halirutan.mathematica.index.packageexport.PackageExportSymbol
36+
import de.halirutan.mathematica.lang.psi.api.Symbol
37+
import de.halirutan.mathematica.lang.resolve.GlobalDefinitionCollector
38+
39+
/**
40+
* Provides GotoSymbol for all file level definitions that have a usage message.
41+
* @author patrick (10.05.18).
42+
*/
43+
class GotoSymbolContributor : ChooseByNameContributor {
44+
45+
private val packageIndex = MathematicaPackageExportIndex.INDEX_ID
46+
private val logger = Logger.getInstance("#de.halirutan.mathematica.codeinsight.navigation.GotoSymbolContributor")
47+
48+
override fun getItemsByName(name: String?, pattern: String?, project: Project?, includeNonProjectItems: Boolean): Array<NavigationItem> {
49+
val project1 = project ?: return emptyArray()
50+
val scope = GlobalSearchScope.projectScope(project1)
51+
val name1 = name ?: return emptyArray()
52+
val keys = getPackageExportKeys(project1, includeNonProjectItems)
53+
val result = keys.filter { k -> k.symbol == name1 }.fold(ArrayList<NavigationItem>(), { accumulator, key ->
54+
val filesByName = FilenameIndex.getFilesByName(project1, key.fileName, scope)
55+
filesByName.forEach {
56+
it?.let {
57+
val symbol = it.findElementAt(key.offset)?.parent
58+
if (symbol is Symbol && symbol.symbolName == key.symbol) {
59+
val c = GlobalDefinitionCollector(symbol.getContainingFile())
60+
if (c.assignments.containsKey(symbol.symbolName)) {
61+
c.assignments[symbol.symbolName]?.forEach { assignmentProperty ->
62+
if (assignmentProperty.myAssignmentSymbol is Symbol && assignmentProperty.myLhsOfAssignment is PsiElement) {
63+
accumulator.add(SymbolNavigationItem(assignmentProperty.myAssignmentSymbol, assignmentProperty.myLhsOfAssignment.text))
64+
}
65+
}
66+
}
67+
}
68+
}
69+
}
70+
accumulator
71+
})
72+
return result.toTypedArray()
73+
}
74+
75+
override fun getNames(project: Project?, includeNonProjectItems: Boolean): Array<String> {
76+
val nameArray = MathematicaPackageExportIndex.getSymbolNames(project).toTypedArray()
77+
logger.debug("found ${nameArray.size} Mathematica symbols")
78+
return nameArray
79+
}
80+
81+
private fun getPackageExportKeys(project: Project, includeNonProjectItems: Boolean): ArrayList<PackageExportSymbol> {
82+
val fileIndex = FileBasedIndex.getInstance() ?: return arrayListOf()
83+
val scope = if (includeNonProjectItems) GlobalSearchScope.projectScope(project) else GlobalSearchScope.allScope(project)
84+
val result: ArrayList<PackageExportSymbol> = arrayListOf()
85+
fileIndex.getAllKeys(packageIndex, project).forEach {
86+
it?.let {
87+
val fileForKey = arrayListOf<VirtualFile>()
88+
val inScope = !fileIndex.processValues(
89+
packageIndex,
90+
it,
91+
null,
92+
{ file, _ -> fileForKey.add(file); false },
93+
scope
94+
)
95+
96+
if (fileForKey.isNotEmpty() && inScope) {
97+
result.add(it)
98+
}
99+
}
100+
}
101+
return result
102+
}
103+
}

src/de/halirutan/mathematica/codeinsight/navigation/MathematicaGotoRelatedProvider.kt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
/*
2-
* Copyright (c) 2017 Patrick Scheibe
2+
* Copyright (c) 2018 Patrick Scheibe
3+
*
34
* Permission is hereby granted, free of charge, to any person obtaining a copy
45
* of this software and associated documentation files (the "Software"), to deal
56
* in the Software without restriction, including without limitation the rights
67
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
78
* copies of the Software, and to permit persons to whom the Software is
89
* furnished to do so, subject to the following conditions:
910
*
10-
* The above copyright notice and this permission notice shall be included in
11-
* all copies or substantial portions of the Software.
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
1213
*
1314
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1415
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1516
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1617
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1718
* 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.
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
2021
*/
2122

2223
package de.halirutan.mathematica.codeinsight.navigation
@@ -50,7 +51,7 @@ class MathematicaGotoRelatedProvider : GotoRelatedProvider() {
5051
psiElement is LeafPsiElement && psiElement.elementType == MathematicaElementTypes.IDENTIFIER -> psiElement.parent
5152
else -> psiElement
5253
}
53-
val declarations = SortedList(Comparator.comparingInt<GotoSymbolItem>({ it.lineNumber }))
54+
val declarations = SortedList(Comparator.comparingInt<MathematicaGotoSymbolItem>({ it.lineNumber }))
5455
if (symbol is Symbol) {
5556
val containingFile = symbol.getContainingFile()
5657
val resolve = symbol.resolve() ?: return declarations
@@ -74,7 +75,7 @@ class MathematicaGotoRelatedProvider : GotoRelatedProvider() {
7475
var textToShow = document.getText(TextRange.create(lineStartOffset, lineEndOffset)).trim { it <= ' ' }
7576
textToShow = if (textToShow.length > 80) textToShow.substring(0, 80) else textToShow
7677

77-
val item = GotoSymbolItem(
78+
val item = MathematicaGotoSymbolItem(
7879
usageElement,
7980
textToShow,
8081
"(line ${lineNumber + 1} in $fileName)",

src/de/halirutan/mathematica/codeinsight/navigation/GotoSymbolItem.java renamed to src/de/halirutan/mathematica/codeinsight/navigation/MathematicaGotoSymbolItem.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
/*
2-
* Copyright (c) 2017 Patrick Scheibe
2+
* Copyright (c) 2018 Patrick Scheibe
3+
*
34
* Permission is hereby granted, free of charge, to any person obtaining a copy
45
* of this software and associated documentation files (the "Software"), to deal
56
* in the Software without restriction, including without limitation the rights
67
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
78
* copies of the Software, and to permit persons to whom the Software is
89
* furnished to do so, subject to the following conditions:
910
*
10-
* The above copyright notice and this permission notice shall be included in
11-
* all copies or substantial portions of the Software.
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
1213
*
1314
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1415
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1516
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
1617
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
1718
* 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.
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
2021
*/
2122

2223
package de.halirutan.mathematica.codeinsight.navigation;
@@ -34,20 +35,21 @@
3435
*
3536
* @author patrick (28.12.16).
3637
*/
37-
class GotoSymbolItem extends GotoRelatedItem {
38+
class MathematicaGotoSymbolItem extends GotoRelatedItem {
3839

3940
private final String myDescription;
4041
private final String myContext;
4142
private final int myLineNumber;
4243

4344
/**
4445
* Create an entry with description text
45-
* @param element {@link PsiElement} to navigate to
46+
*
47+
* @param element {@link PsiElement} to navigate to
4648
* @param description descriptive text that appears in the info box
4749
* @param contextInfo additional information like line number and context
48-
* @param lineNumber used for sorting the entries
50+
* @param lineNumber used for sorting the entries
4951
*/
50-
GotoSymbolItem(@NotNull PsiElement element, @NotNull final String description, @NotNull final String contextInfo, final int lineNumber) {
52+
MathematicaGotoSymbolItem(@NotNull PsiElement element, @NotNull final String description, @NotNull final String contextInfo, final int lineNumber) {
5153
super(element);
5254
myDescription = description;
5355
myContext = contextInfo;
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (c) 2018 Patrick Scheibe
3+
*
4+
* Permission is hereby granted, free of charge, to any person obtaining a copy
5+
* of this software and associated documentation files (the "Software"), to deal
6+
* in the Software without restriction, including without limitation the rights
7+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8+
* copies of the Software, and to permit persons to whom the Software is
9+
* furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all
12+
* copies or substantial portions of the Software.
13+
*
14+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20+
* SOFTWARE.
21+
*/
22+
23+
package de.halirutan.mathematica.codeinsight.navigation
24+
25+
import com.intellij.navigation.ItemPresentation
26+
import com.intellij.navigation.NavigationItem
27+
import de.halirutan.mathematica.lang.psi.api.Symbol
28+
import de.halirutan.mathematica.util.MathematicaIcons
29+
import javax.swing.Icon
30+
31+
/**
32+
* A simple navigation item that is used in the GotoSymbol contributor
33+
* @author patrick (10.05.18).
34+
*/
35+
36+
class SymbolNavigationItem(val symbol: Symbol, val context: String) : NavigationItem, ItemPresentation {
37+
override fun navigate(requestFocus: Boolean) {
38+
(symbol as NavigationItem).navigate(requestFocus)
39+
}
40+
41+
override fun getPresentation(): ItemPresentation? {
42+
return object : ItemPresentation {
43+
override fun getLocationString(): String? {
44+
val file = symbol.containingFile
45+
val fileName = file?.let { "(${it.name})" } ?: ""
46+
return "$context $fileName"
47+
}
48+
49+
override fun getIcon(unused: Boolean): Icon? {
50+
return MathematicaIcons.FILE_ICON
51+
}
52+
53+
override fun getPresentableText(): String? {
54+
return name
55+
}
56+
}
57+
}
58+
59+
override fun canNavigate(): Boolean {
60+
return true
61+
}
62+
63+
override fun getName(): String? {
64+
return symbol.symbolName
65+
}
66+
67+
override fun canNavigateToSource(): Boolean {
68+
return true
69+
}
70+
71+
override fun getLocationString(): String? {
72+
return symbol.mathematicaContext
73+
}
74+
75+
override fun getIcon(unused: Boolean): Icon? {
76+
return MathematicaIcons.FILE_ICON
77+
}
78+
79+
override fun getPresentableText(): String? {
80+
return name
81+
}
82+
}

0 commit comments

Comments
 (0)