Skip to content

Commit 5968d59

Browse files
committed
File indexer for exported symbols
1 parent 40e8ebf commit 5968d59

16 files changed

Lines changed: 345 additions & 60 deletions

File tree

resources/META-INF/plugin.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@
169169
<!--<applicationService serviceInterface="de.halirutan.mathematica.MathematicaSettings" serviceImplementation="de.halirutan.mathematica.MathematicaSettings"/>-->
170170
<!--<applicationConfigurable groupId="language" displayName="Mathematica" id="preferences.Mathematica"-->
171171
<!--instance="de.halirutan.mathematica.MathematicaSettingsConfigurable"/>-->
172-
<fileBasedIndex implementation="de.halirutan.mathematica.index.MathematicaPackageExportIndex"/>
172+
<fileBasedIndex implementation="de.halirutan.mathematica.index.export.MathematicaPackageExportIndex"/>
173173

174174

175175
</extensions>

resources/mathematica/IDEAPlugin.m

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

2424
Begin["`Private`"];
2525

26-
2726
(* Here we replace Mathematica box expressions with HTML constructs. If we lack of some things we just use a
2827
a string representation like with UnderscriptBox *)
2928
$boxRules = {

src/de/halirutan/mathematica/actions/PackageExportChecker.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,16 @@
2323

2424
import com.intellij.openapi.actionSystem.AnAction;
2525
import com.intellij.openapi.actionSystem.AnActionEvent;
26+
import com.intellij.openapi.actionSystem.DataKeys;
2627
import com.intellij.openapi.project.Project;
28+
import com.intellij.openapi.vfs.VirtualFile;
2729
import com.intellij.psi.search.GlobalSearchScope;
2830
import com.intellij.util.indexing.FileBasedIndex;
2931
import com.intellij.util.indexing.ID;
30-
import de.halirutan.mathematica.index.MathematicaPackageExportIndex;
31-
import de.halirutan.mathematica.index.MathematicaPackageExportIndex.Key;
32-
import de.halirutan.mathematica.index.PackageExportInfo;
32+
import de.halirutan.mathematica.index.export.MathematicaPackageExportIndex;
33+
import de.halirutan.mathematica.index.export.MathematicaPackageExportIndex.FileKey;
34+
import de.halirutan.mathematica.index.export.MathematicaPackageExportIndex.Key;
35+
import de.halirutan.mathematica.index.export.PackageExportSymbol;
3336

3437
import java.util.Collection;
3538
import java.util.List;
@@ -46,15 +49,19 @@ public void actionPerformed(AnActionEvent e) {
4649
if (project == null) {
4750
return;
4851
}
52+
53+
final VirtualFile currentFile = e.getDataContext().getData(DataKeys.VIRTUAL_FILE);
4954
final FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();
50-
final ID<Key, List<PackageExportInfo>> indexId = MathematicaPackageExportIndex.INDEX_ID;
51-
final Collection<Key> allKeys = fileBasedIndex.getAllKeys(indexId, project);
55+
final ID<Key, List<PackageExportSymbol>> indexId = MathematicaPackageExportIndex.INDEX_ID;
56+
final Collection<Key> allKeys = fileBasedIndex.getAllKeys(indexId,project);
5257

5358
for (Key next : allKeys) {
54-
final List<List<PackageExportInfo>> values = fileBasedIndex.getValues(indexId, next, GlobalSearchScope.allScope(project));
55-
for (List<PackageExportInfo> list : values) {
56-
for (PackageExportInfo info : list) {
57-
System.out.println("Info: " + info.symbol + " " + info.nameSpace);
59+
final VirtualFile file = fileBasedIndex.findFileById(project, ((FileKey) next).getFileId());
60+
System.out.printf("\nFILE: " + file.getPresentableName() + "\n-----------------------------------------\n");
61+
final List<List<PackageExportSymbol>> values = fileBasedIndex.getValues(indexId, next, GlobalSearchScope.allScope(project));
62+
for (List<PackageExportSymbol> list : values) {
63+
for (PackageExportSymbol info : list) {
64+
System.out.println(info.symbol + " (" + info.nameSpace +")");
5865
}
5966
}
6067
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void handleInsert(InsertionContext context) {
8383
final int currentPosition = context.getTailOffset();
8484
document.insertString(currentPosition, myInfo.getCallPattern());
8585
document.insertString(context.getTailOffset(), Character.toString(CLOSING_BRACKET));
86-
final int endOffset = getFirstArgumentRange(myInfo).getEndOffset() + currentPosition;
86+
final int endOffset = getFirstArgumentRange(myInfo) + currentPosition;
8787
editor.getSelectionModel().setSelection(currentPosition, endOffset);
8888
editor.getCaretModel().moveToOffset(endOffset);
8989
} else {
@@ -101,12 +101,12 @@ public void handleInsert(InsertionContext context) {
101101
PsiDocumentManager.getInstance(project).commitDocument(document);
102102
}
103103

104-
private TextRange getFirstArgumentRange(SymbolInformation info) {
104+
private int getFirstArgumentRange(SymbolInformation info) {
105105
final String callPattern = info.getCallPattern();
106106
final int firstComma = callPattern.indexOf(',');
107107
if (firstComma == -1) {
108-
return new TextRange(1, callPattern.length());
108+
return callPattern.length();
109109
}
110-
return new TextRange(1, firstComma);
110+
return firstComma;
111111
}
112112
}

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
7777
final Symbol head = (Symbol) ((FunctionCall) function).getHead();
7878
String functionName = head.getSymbolName();
7979
String functionContext = head.getMathematicaContext();
80+
functionContext = "".equals(functionContext) ? "System`" : functionContext;
8081
final String key = functionContext + functionName;
8182
if (ourSymbolInformation.containsKey(key) && ourSymbolInformation.get(key).function) {
8283
final SymbolInformation functionInformation = ourSymbolInformation.get(key);

src/de/halirutan/mathematica/codeinsight/highlighting/MathematicaHighlightingAnnotator.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ public void annotate(@NotNull PsiElement element, @NotNull final AnnotationHolde
8686

8787
@Override
8888
public void visitSymbol(final Symbol symbol) {
89-
if (NAMES.contains(symbol.getMathematicaContext()+symbol.getSymbolName())) {
89+
String context = symbol.getMathematicaContext();
90+
context = "".equals(context) ? "System`" : context;
91+
if (NAMES.contains(context+symbol.getSymbolName())) {
9092
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.BUILTIN_FUNCTION);
9193
return;
9294
}

src/de/halirutan/mathematica/codeinsight/inspections/bugs/UnsupportedVersion.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ public void visitSymbol(Symbol symbol) {
211211
return;
212212
}
213213

214-
String nameWithContext = symbol.getMathematicaContext() + symbolName;
214+
String context = symbol.getMathematicaContext();
215+
context = "".equals(context) ? "System`" : context;
216+
String nameWithContext = context + symbolName;
215217
if (mySymbolVersions.containsKey(nameWithContext)) {
216218
double version = mySymbolVersions.get(nameWithContext);
217219
if (version > myLanguageLevel.getVersionNumber()) {

src/de/halirutan/mathematica/documentation/MathematicaDocumentationProvider.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public String generateDoc(PsiElement element, @Nullable PsiElement originalEleme
7171

7272
if (element instanceof Symbol) {
7373
String context = ((Symbol) element).getMathematicaContext();
74+
context = "".equals(context) ? "System`" : context;
7475
String name = ((Symbol) element).getSymbolName();
7576
if (ALL_SLOT_PATTERN.matcher(name).matches()) {
7677
if (SLOT_PATTERN.matcher(name).matches()) name = "Slot";
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
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.index;
23+
24+
import java.util.List;
25+
import java.util.regex.Matcher;
26+
import java.util.regex.Pattern;
27+
28+
/**
29+
* @author patrick (18.12.16).
30+
*/
31+
public class PackageUtil {
32+
public static final Pattern contextPattern = Pattern.compile("`?(([a-zA-Z$]+[0-9]*)+`)+");
33+
public static final Pattern relativeContextPattern = Pattern.compile("`(([a-zA-Z$]+[0-9]*)+`)+");
34+
public static final Pattern absoluteContextPattern = Pattern.compile("(([a-zA-Z$]+[0-9]*)+`)+");
35+
private PackageUtil(){}
36+
37+
public static String buildContext(List<String> contextStack) {
38+
StringBuilder context = new StringBuilder("Global`");
39+
for (String current : contextStack) {
40+
final Matcher mRelative = relativeContextPattern.matcher(current);
41+
final Matcher mAbsolute = absoluteContextPattern.matcher(current);
42+
if (mRelative.matches()) {
43+
context.append(current.substring(1));
44+
} else if (mAbsolute.matches()) {
45+
context = new StringBuilder(current);
46+
}
47+
}
48+
return context.toString();
49+
}
50+
}

src/de/halirutan/mathematica/index/ExportSymbolVisitor.java renamed to src/de/halirutan/mathematica/index/export/ExportSymbolVisitor.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* THE SOFTWARE.
2020
*/
2121

22-
package de.halirutan.mathematica.index;
22+
package de.halirutan.mathematica.index.export;
2323

2424
import de.halirutan.mathematica.parsing.psi.MathematicaRecursiveVisitor;
2525
import de.halirutan.mathematica.parsing.psi.api.Expression;
@@ -34,13 +34,13 @@
3434
*/
3535
public class ExportSymbolVisitor extends MathematicaRecursiveVisitor {
3636

37-
private List<PackageExportInfo> myInfos;
37+
private List<PackageExportSymbol> myInfos;
3838

3939
public ExportSymbolVisitor() {
40-
myInfos = new ArrayList<PackageExportInfo>();
40+
myInfos = new ArrayList<PackageExportSymbol>();
4141
}
4242

43-
public List<PackageExportInfo> getInfos() {
43+
public List<PackageExportSymbol> getInfos() {
4444
return myInfos;
4545
}
4646

@@ -53,7 +53,7 @@ public void visitMessageName(MessageName messageName) {
5353
if (tag.getText().equals("usage")) {
5454
final Expression symbol = messageName.getSymbol();
5555
if (symbol != null) {
56-
myInfos.add(new PackageExportInfo("Global`", symbol.getName()));
56+
myInfos.add(new PackageExportSymbol("Global`", symbol.getName()));
5757
}
5858

5959
}

0 commit comments

Comments
 (0)