Skip to content

Commit e1188dc

Browse files
committed
Merge branch 'feat-PackageExportIndexer' into ReworkingCoreFeaturesAndParsing
# Conflicts: # resources/META-INF/plugin.xml # src/de/halirutan/mathematica/codeinsight/completion/SmartContextAwareCompletion.java # src/de/halirutan/mathematica/codeinsight/completion/VariableNameCompletion.java # src/de/halirutan/mathematica/codeinsight/highlighting/MathematicaHighlightingAnnotator.java # src/de/halirutan/mathematica/codeinsight/inspections/bugs/UnsupportedVersion.java # src/de/halirutan/mathematica/documentation/MathematicaDocumentationProvider.java # src/de/halirutan/mathematica/lang/psi/util/MathematicaPsiUtilities.java # src/de/halirutan/mathematica/parsing/psi/impl/SymbolImpl.java
2 parents ec03c7e + 5176dfd commit e1188dc

11 files changed

Lines changed: 650 additions & 6 deletions

File tree

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 = {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
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.openapi.actionSystem.AnAction;
25+
import com.intellij.openapi.actionSystem.AnActionEvent;
26+
import com.intellij.openapi.actionSystem.DataKeys;
27+
import com.intellij.openapi.project.Project;
28+
import com.intellij.openapi.vfs.VirtualFile;
29+
import com.intellij.psi.search.GlobalSearchScope;
30+
import com.intellij.util.indexing.FileBasedIndex;
31+
import com.intellij.util.indexing.ID;
32+
import de.halirutan.mathematica.index.packageexport.MathematicaPackageExportIndex;
33+
import de.halirutan.mathematica.index.packageexport.MathematicaPackageExportIndex.FileKey;
34+
import de.halirutan.mathematica.index.packageexport.MathematicaPackageExportIndex.Key;
35+
import de.halirutan.mathematica.index.packageexport.PackageExportSymbol;
36+
37+
import java.util.Collection;
38+
import java.util.List;
39+
40+
/**
41+
* @author patrick (08.11.16).
42+
*/
43+
public class PackageExportChecker extends AnAction {
44+
45+
@Override
46+
public void actionPerformed(AnActionEvent e) {
47+
48+
final Project project = e.getProject();
49+
if (project == null) {
50+
return;
51+
}
52+
53+
final VirtualFile currentFile = e.getDataContext().getData(DataKeys.VIRTUAL_FILE);
54+
final FileBasedIndex fileBasedIndex = FileBasedIndex.getInstance();
55+
final ID<Key, List<PackageExportSymbol>> indexId = MathematicaPackageExportIndex.INDEX_ID;
56+
final Collection<Key> allKeys = fileBasedIndex.getAllKeys(indexId,project);
57+
58+
for (Key next : allKeys) {
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 +")");
65+
}
66+
}
67+
}
68+
}
69+
}

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void handleInsert(InsertionContext context) {
9999
final int currentPosition = context.getTailOffset();
100100
document.insertString(currentPosition, myInfo.getCallPattern());
101101
document.insertString(context.getTailOffset(), Character.toString(CLOSING_BRACKET));
102-
final int endOffset = getFirstArgumentRange(myInfo).getEndOffset() + currentPosition;
102+
final int endOffset = getFirstArgumentRange(myInfo) + currentPosition;
103103
editor.getSelectionModel().setSelection(currentPosition, endOffset);
104104
editor.getCaretModel().moveToOffset(endOffset);
105105

@@ -155,12 +155,12 @@ public LookupElement[] calculateLookupItems(ExpressionContext context) {
155155
PsiDocumentManager.getInstance(project).commitDocument(document);
156156
}
157157

158-
private TextRange getFirstArgumentRange(SymbolInformation info) {
158+
private int getFirstArgumentRange(SymbolInformation info) {
159159
final String callPattern = info.getCallPattern();
160160
final int firstComma = callPattern.indexOf(',');
161161
if (firstComma == -1) {
162-
return new TextRange(1, callPattern.length());
162+
return callPattern.length();
163163
}
164-
return new TextRange(1, firstComma);
164+
return firstComma;
165165
}
166166
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
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.completion;
23+
24+
import com.intellij.psi.PsiElement;
25+
import com.intellij.psi.PsiFile;
26+
import de.halirutan.mathematica.parsing.psi.MathematicaVisitor;
27+
import de.halirutan.mathematica.parsing.psi.api.CompoundExpression;
28+
import de.halirutan.mathematica.parsing.psi.api.FunctionCall;
29+
import de.halirutan.mathematica.parsing.psi.api.string.MString;
30+
31+
import java.util.HashSet;
32+
33+
/**
34+
* @author patrick (19.12.16).
35+
*/
36+
public class ImportedContextVisitor extends MathematicaVisitor {
37+
38+
private final HashSet<String > myImportedContexts;
39+
40+
public ImportedContextVisitor() {
41+
myImportedContexts = new HashSet<String>();
42+
}
43+
44+
public HashSet<String> getImportedContexts() {
45+
return myImportedContexts;
46+
}
47+
48+
@Override
49+
public void visitFile(PsiFile file) {
50+
file.acceptChildren(this);
51+
}
52+
53+
@Override
54+
public void visitCompoundExpression(CompoundExpression compoundExpression) {
55+
compoundExpression.acceptChildren(this);
56+
}
57+
58+
@Override
59+
public void visitFunctionCall(FunctionCall functionCall) {
60+
if (functionCall.matchesHead("Needs")) {
61+
final PsiElement context = functionCall.getArgument(1);
62+
if (context instanceof MString) {
63+
final String text = context.getText();
64+
myImportedContexts.add(text.substring(1, text.length() - 1));
65+
}
66+
}
67+
}
68+
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,18 @@
3030
import com.intellij.psi.PsiFile;
3131
import com.intellij.psi.PsiRecursiveElementVisitor;
3232
import com.intellij.psi.ResolveState;
33+
import com.intellij.psi.search.GlobalSearchScope;
3334
import com.intellij.psi.util.PsiTreeUtil;
3435
import com.intellij.util.ProcessingContext;
3536
import com.intellij.util.containers.hash.HashSet;
37+
import com.intellij.util.indexing.FileBasedIndex;
38+
import de.halirutan.mathematica.index.packageexport.MathematicaPackageExportIndex;
39+
import de.halirutan.mathematica.index.packageexport.MathematicaPackageExportIndex.Key;
40+
import de.halirutan.mathematica.index.packageexport.PackageExportSymbol;
3641
import de.halirutan.mathematica.lang.psi.api.Symbol;
3742
import org.jetbrains.annotations.NotNull;
3843

44+
import java.util.Collection;
3945
import java.util.List;
4046
import java.util.Set;
4147

@@ -75,6 +81,21 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
7581
}
7682
}
7783

84+
ImportedContextVisitor importVisitor = new ImportedContextVisitor();
85+
callingSymbol.getContainingFile().accept(importVisitor);
86+
final java.util.HashSet<String> importedContexts = importVisitor.getImportedContexts();
87+
final FileBasedIndex index = FileBasedIndex.getInstance();
88+
final Collection<Key> allKeys = index.getAllKeys(MathematicaPackageExportIndex.INDEX_ID, callingSymbol.getProject());
89+
for (Key key : allKeys) {
90+
final List<List<PackageExportSymbol>> values = index.getValues(MathematicaPackageExportIndex.INDEX_ID, key, GlobalSearchScope.allScope(callingSymbol.getProject()));
91+
for (List<PackageExportSymbol> value : values) {
92+
for (PackageExportSymbol packageExportSymbol : value) {
93+
//TODO: Implement adding completions
94+
// if(packageExportSymbol.nameSpace.equals())
95+
}
96+
}
97+
}
98+
7899

79100
final LocalDefinitionCompletionProvider processor = new LocalDefinitionCompletionProvider(callingSymbol);
80101
PsiTreeUtil.treeWalkUp(processor, callingSymbol, containingFile, ResolveState.initial());
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+
}

0 commit comments

Comments
 (0)