Skip to content

Commit ec499b9

Browse files
committed
Implement Mathematica libraries
With this commit it is possible to attach source-libraries to a Mathematica project. This helps to resolve symbols from other packages.
1 parent 914efff commit ec499b9

9 files changed

Lines changed: 431 additions & 40 deletions

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ task wrapper(type: Wrapper) {
7575
version '3.0pre1'
7676

7777
intellij {
78-
version = '2017.2.5'
78+
// version = '2017.2.5'
7979
downloadSources = true
8080
pluginName = 'Mathematica-IntelliJ-Plugin'
8181
updateSinceUntilBuild = false

resources/META-INF/plugin.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,11 @@
113113
<errorHandler implementation="de.halirutan.mathematica.errorreporting.GitHubErrorReporter"/>
114114
<referencesSearch implementation="de.halirutan.mathematica.lang.search.MathematicaReferenceSearch"/>
115115

116+
<!-- Support for libraries -->
117+
<library.type implementation="de.halirutan.mathematica.library.MathematicaLibraryType"/>
118+
<orderRootType id="MATHEMATICA_LIBRARY" order="FIRST" implementation="de.halirutan.mathematica.library.MathematicaLibraryRootType"/>
119+
<OrderRootTypeUI key="MATHEMATICA_LIBRARY" implementationClass="de.halirutan.mathematica.library.MathematicaLibraryRootTypeUIFactory"/>
120+
116121
</extensions>
117122

118123
<actions>

resources/de/halirutan/mathematica/MathematicaBundle.properties

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,24 @@
11
#
2-
# Copyright (c) 2014 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:
2+
# Copyright (c) 2017 Patrick Scheibe
93
#
10-
# The above copyright notice and this permission notice shall be included in
11-
# all copies or substantial portions of the Software.
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
12+
# all 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
20+
# THE SOFTWARE.
1221
#
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.
2022
#
2123

2224
language.name=Mathematica
@@ -47,4 +49,6 @@ structureview.grouper.by.symbol.name.text=Group by Name
4749
structureview.grouper.by.type.description=Groups all assignments by their type
4850
structureview.grouper.by.type.text=Group by Assignment Type
4951
structureview.sorter.by.appearance.text=Sort by Appearance
50-
structureview.sorter.by.appearance.description=Sorts the entries by appearance in the source file.
52+
structureview.sorter.by.appearance.description=Sorts the entries by appearance in the source file.
53+
library.new=Mathematica Library
54+
library.description=A Mathematica package directory that acts as library
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
/*
2+
* Copyright (c) 2017 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
12+
* all 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
20+
* THE SOFTWARE.
21+
*
22+
*/
23+
24+
package de.halirutan.mathematica.library;
25+
26+
import com.intellij.openapi.progress.ProgressIndicator;
27+
import com.intellij.openapi.roots.OrderRootType;
28+
import com.intellij.openapi.roots.libraries.ui.RootDetector;
29+
import com.intellij.openapi.vfs.JarFileSystem;
30+
import com.intellij.openapi.vfs.VfsUtil;
31+
import com.intellij.openapi.vfs.VirtualFile;
32+
import com.intellij.util.CommonProcessors;
33+
import org.jetbrains.annotations.NotNull;
34+
35+
import java.util.ArrayList;
36+
import java.util.Collection;
37+
import java.util.Collections;
38+
import java.util.List;
39+
40+
/**
41+
* Provides functionality to detect the root directories of valid Mathematica source libraries
42+
*
43+
* @author patrick (25.11.17).
44+
*/
45+
public class MathematicaLibraryRootDetector extends RootDetector {
46+
MathematicaLibraryRootDetector(OrderRootType rootType, boolean jarDirectory, String presentableRootTypeName) {
47+
super(rootType, jarDirectory, presentableRootTypeName);
48+
}
49+
50+
/**
51+
* Provides a way to select directories, starting from some root, that are likely to be the parent-directory of
52+
* package sources. We test for each directory if either there is a PacletInfo.m in it or if it contains a package
53+
* file that has the same name as the directory itself.
54+
* <p>
55+
* If the user selects a directory that contains several packages, we collect all valid package dirs.
56+
*
57+
* @param dir Starting directory for the package search. In the best case, it is the root of the package itself
58+
*
59+
* @return A list of found packages.
60+
*/
61+
private static Collection<VirtualFile> collectPackageDirs(final VirtualFile dir) {
62+
if (!dir.isDirectory()) {
63+
return Collections.emptyList();
64+
}
65+
66+
CommonProcessors.CollectProcessor<VirtualFile> paclets = new CommonProcessors.CollectProcessor<VirtualFile>() {
67+
@Override
68+
public boolean accept(VirtualFile virtualFile) {
69+
if (virtualFile.isDirectory()) {
70+
final String dirName = virtualFile.getName();
71+
final VirtualFile[] children = virtualFile.getChildren();
72+
for (VirtualFile child : children) {
73+
if (!child.isDirectory() && ("PacletInfo.m".equals(child.getName()) ||
74+
(dirName.equals(child.getNameWithoutExtension()) && "m".equals(child.getExtension())))) {
75+
return true;
76+
}
77+
}
78+
}
79+
return false;
80+
}
81+
};
82+
VfsUtil.processFilesRecursively(dir, paclets);
83+
return paclets.getResults();
84+
}
85+
86+
@NotNull
87+
@Override
88+
public Collection<VirtualFile> detectRoots(@NotNull VirtualFile rootCandidate, @NotNull ProgressIndicator progressIndicator) {
89+
final List<VirtualFile> result = new ArrayList<>();
90+
final Collection<VirtualFile> packageDirectories = collectPackageDirs(rootCandidate);
91+
if (rootCandidate.getFileSystem() instanceof JarFileSystem || packageDirectories.isEmpty()) {
92+
return result;
93+
}
94+
result.addAll(packageDirectories);
95+
return result;
96+
}
97+
98+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright (c) 2017 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
12+
* all 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
20+
* THE SOFTWARE.
21+
*
22+
*/
23+
24+
package de.halirutan.mathematica.library;
25+
26+
import com.intellij.openapi.roots.PersistentOrderRootType;
27+
import org.jetbrains.annotations.NotNull;
28+
29+
/**
30+
* I'm not sure if I'm doing this right, but it seems to work
31+
*
32+
* @author patrick (25.11.17).
33+
*/
34+
public class MathematicaLibraryRootType extends PersistentOrderRootType {
35+
protected MathematicaLibraryRootType() {
36+
super("MATHEMATICA_LIBRARY", null, null, null);
37+
}
38+
39+
@NotNull
40+
public static MathematicaLibraryRootType getInstance() {
41+
return getOrderRootType(MathematicaLibraryRootType.class);
42+
}
43+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright (c) 2017 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
12+
* all 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
20+
* THE SOFTWARE.
21+
*
22+
*/
23+
24+
package de.halirutan.mathematica.library;
25+
26+
import com.intellij.openapi.fileChooser.FileChooserDescriptorFactory;
27+
import com.intellij.openapi.projectRoots.Sdk;
28+
import com.intellij.openapi.projectRoots.ui.SdkPathEditor;
29+
import com.intellij.openapi.roots.ui.OrderRootTypeUIFactory;
30+
import de.halirutan.mathematica.util.MathematicaIcons;
31+
import org.jetbrains.annotations.Nullable;
32+
33+
import javax.swing.*;
34+
35+
/**
36+
* When I understood it correctly, then this is the class that provides the UI inside the settings dialog that is
37+
* opened after the user has selected a library dir. Then, the user can (de)select some of the found library roots.
38+
* As far as I can tell, this is the UI for it.
39+
*
40+
* @author patrick (25.11.17).
41+
*/
42+
public class MathematicaLibraryRootTypeUIFactory implements OrderRootTypeUIFactory {
43+
44+
@Nullable
45+
@Override
46+
public SdkPathEditor createPathEditor(Sdk sdk) {
47+
return new SdkPathEditor(getNodeText(), MathematicaLibraryRootType.getInstance(),
48+
FileChooserDescriptorFactory.createSingleLocalFileDescriptor());
49+
}
50+
51+
@Override
52+
public Icon getIcon() {
53+
return MathematicaIcons.FILE_ICON;
54+
}
55+
56+
@Override
57+
public String getNodeText() {
58+
return "Package Files";
59+
}
60+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Copyright (c) 2017 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
12+
* all 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
20+
* THE SOFTWARE.
21+
*
22+
*/
23+
24+
package de.halirutan.mathematica.library;
25+
26+
import com.google.common.collect.Lists;
27+
import com.intellij.openapi.roots.OrderRootType;
28+
import com.intellij.openapi.roots.libraries.ui.RootDetector;
29+
import com.intellij.openapi.roots.ui.configuration.libraryEditor.DefaultLibraryRootsComponentDescriptor;
30+
import org.jetbrains.annotations.NotNull;
31+
32+
import java.util.List;
33+
34+
/**
35+
* Wrapper class for the library root detection
36+
*
37+
* @author patrick (25.11.17).
38+
*/
39+
public class MathematicaLibraryRootsComponentDescriptor extends DefaultLibraryRootsComponentDescriptor {
40+
41+
@NotNull
42+
@Override
43+
public List<? extends RootDetector> getRootDetectors() {
44+
return Lists.newArrayList(new MathematicaLibraryRootDetector(OrderRootType.SOURCES, false, "Mathematica Library"));
45+
}
46+
47+
}

0 commit comments

Comments
 (0)