Skip to content

Commit 290da60

Browse files
committed
Made completetion for cross-file references
1 parent add69be commit 290da60

6 files changed

Lines changed: 245 additions & 221 deletions

File tree

Lines changed: 69 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +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-
}
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/VariableNameCompletion.java

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import com.google.common.collect.Lists;
2525
import com.intellij.codeInsight.completion.*;
2626
import com.intellij.codeInsight.lookup.LookupElementBuilder;
27+
import com.intellij.openapi.project.Project;
28+
import com.intellij.openapi.vfs.VirtualFile;
2729
import com.intellij.patterns.PlatformPatterns;
2830
import com.intellij.patterns.PsiElementPattern.Capture;
2931
import com.intellij.psi.PsiElement;
@@ -36,7 +38,6 @@
3638
import com.intellij.util.containers.hash.HashSet;
3739
import com.intellij.util.indexing.FileBasedIndex;
3840
import de.halirutan.mathematica.index.packageexport.MathematicaPackageExportIndex;
39-
import de.halirutan.mathematica.index.packageexport.MathematicaPackageExportIndex.Key;
4041
import de.halirutan.mathematica.index.packageexport.PackageExportSymbol;
4142
import de.halirutan.mathematica.lang.psi.api.Symbol;
4243
import org.jetbrains.annotations.NotNull;
@@ -67,6 +68,7 @@ void addTo(CompletionContributor contributor) {
6768
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
6869
final Symbol callingSymbol = (Symbol) parameters.getPosition().getParent();
6970
final String callingSymbolName = callingSymbol.getFullSymbolName();
71+
final Project project = callingSymbol.getProject();
7072

7173
if (!parameters.isExtendedCompletion()) {
7274
final PsiFile containingFile = parameters.getOriginalFile();
@@ -85,16 +87,21 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
8587
callingSymbol.getContainingFile().accept(importVisitor);
8688
final java.util.HashSet<String> importedContexts = importVisitor.getImportedContexts();
8789
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-
}
90+
final Collection<PackageExportSymbol> allKeys = index.getAllKeys(MathematicaPackageExportIndex.INDEX_ID, project);
91+
for (PackageExportSymbol key : allKeys) {
92+
if (key.isExported()) {
93+
result.addElement(PrioritizedLookupElement.withPriority(LookupElementBuilder.create(key.getSymbol()).appendTailText("(" + key.getFileName() + ")",true), GLOBAL_VARIABLE_PRIORITY));
9694
}
9795
}
96+
// for (String key : allKeys) {
97+
// final List<List<PackageExportSymbol>> values = index.getValues(MathematicaPackageExportIndex.INDEX_ID, key, GlobalSearchScope.allScope(callingSymbol.getProject()));
98+
// for (List<PackageExportSymbol> value : values) {
99+
// for (PackageExportSymbol packageExportSymbol : value) {
100+
// //TODO: Implement adding completions
101+
//// if(packageExportSymbol.nameSpace.equals())
102+
// }
103+
// }
104+
// }
98105

99106

100107
final LocalDefinitionCompletionProvider processor = new LocalDefinitionCompletionProvider(callingSymbol);

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import com.intellij.lang.annotation.Annotation;
2727
import com.intellij.lang.annotation.AnnotationHolder;
2828
import com.intellij.lang.annotation.Annotator;
29+
import com.intellij.openapi.editor.colors.CodeInsightColors;
2930
import com.intellij.openapi.editor.colors.EditorColorsManager;
3031
import com.intellij.openapi.editor.colors.TextAttributesKey;
3132
import com.intellij.openapi.editor.markup.TextAttributes;
@@ -95,7 +96,7 @@ public void visitSymbol(final Symbol symbol) {
9596
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.IDENTIFIER);
9697
break;
9798
case NULL:
98-
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.BAD_CHARACTER);
99+
setHighlighting(symbol, myHolder, CodeInsightColors.WRONG_REFERENCES_ATTRIBUTES);
99100
break;
100101
case BUILT_IN:
101102
setHighlighting(symbol, myHolder, MathematicaSyntaxHighlighterColors.BUILTIN_FUNCTION);

src/de/halirutan/mathematica/index/packageexport/MathematicaPackageExportIndex.java

Lines changed: 21 additions & 105 deletions
Original file line numberDiff line numberDiff line change
@@ -21,39 +21,40 @@
2121

2222
package de.halirutan.mathematica.index.packageexport;
2323

24+
import com.intellij.openapi.fileTypes.LanguageFileType;
2425
import com.intellij.openapi.vfs.VirtualFile;
2526
import com.intellij.psi.PsiFile;
2627
import com.intellij.util.containers.HashMap;
2728
import com.intellij.util.indexing.*;
2829
import com.intellij.util.indexing.FileBasedIndex.InputFilter;
29-
import com.intellij.util.io.DataExternalizer;
30+
import com.intellij.util.io.EnumeratorStringDescriptor;
3031
import com.intellij.util.io.IOUtil;
3132
import com.intellij.util.io.KeyDescriptor;
32-
import de.halirutan.mathematica.index.packageexport.MathematicaPackageExportIndex.Key;
33+
import de.halirutan.mathematica.lang.MathematicaLanguage;
3334
import org.jetbrains.annotations.NotNull;
3435

3536
import java.io.DataInput;
3637
import java.io.DataOutput;
3738
import java.io.IOException;
38-
import java.util.ArrayList;
39-
import java.util.List;
39+
import java.util.Collection;
4040
import java.util.Map;
4141

4242
/**
4343
* @author patrick (01.11.16).
4444
*/
45-
public class MathematicaPackageExportIndex extends FileBasedIndexExtension<Key, List<PackageExportSymbol>> {
45+
public class MathematicaPackageExportIndex extends ScalarIndexExtension<PackageExportSymbol> {
4646

47-
public static final ID<Key,List<PackageExportSymbol>> INDEX_ID = ID.create("Mathematica.fileExports");
48-
private static final int BASE_VERSION = 4;
47+
public static final ID<PackageExportSymbol, Void> INDEX_ID = ID.create("Mathematica.fileExports");
48+
private static final int BASE_VERSION = 7;
4949

5050
@NotNull
5151
@Override
5252
public InputFilter getInputFilter() {
5353
return new InputFilter() {
5454
@Override
5555
public boolean acceptInput(@NotNull VirtualFile file) {
56-
return "m".equals(file.getExtension());
56+
final LanguageFileType fileType = MathematicaLanguage.INSTANCE.getAssociatedFileType();
57+
return file.getFileType().equals(fileType);
5758
}
5859
};
5960
}
@@ -70,125 +71,40 @@ public boolean dependsOnFileContent() {
7071

7172
@NotNull
7273
@Override
73-
public ID<Key, List<PackageExportSymbol>> getName() {
74+
public ID<PackageExportSymbol, Void> getName() {
7475
return INDEX_ID;
7576
}
7677

7778
@NotNull
7879
@Override
79-
public DataIndexer<Key, List<PackageExportSymbol>, FileContent> getIndexer() {
80-
return new DataIndexer<Key, List<PackageExportSymbol>, FileContent>() {
80+
public DataIndexer<PackageExportSymbol, Void, FileContent> getIndexer() {
81+
return new DataIndexer<PackageExportSymbol, Void, FileContent>() {
8182
@NotNull
8283
@Override
83-
public Map<Key, List<PackageExportSymbol>> map(@NotNull FileContent inputData) {
84-
final Map<Key, List<PackageExportSymbol>> map = new HashMap<Key, List<PackageExportSymbol>>();
85-
if (!"m".equals(inputData.getFile().getExtension())) return map;
84+
public Map<PackageExportSymbol, Void> map(@NotNull FileContent inputData) {
85+
final Map<PackageExportSymbol , Void> map = new HashMap<>();
8686
final PsiFile psiFile = inputData.getPsiFile();
8787
PackageClassifier visitor = new PackageClassifier();
8888
psiFile.accept(visitor);
89-
map.put(new FileKey(inputData.getFile()), new ArrayList<PackageExportSymbol>(visitor.getListOfExportSymbols()));
90-
return map;
91-
}
92-
};
93-
}
94-
95-
@NotNull
96-
@Override
97-
public KeyDescriptor<Key> getKeyDescriptor() {
98-
//noinspection AnonymousInnerClassWithTooManyMethods,OverlyComplexAnonymousInnerClass
99-
return new KeyDescriptor<Key>() {
100-
@Override
101-
public int getHashCode(Key value) {
102-
return value.hashCode();
103-
}
104-
105-
@Override
106-
public boolean isEqual(Key val1, Key val2) {
107-
return val1.equals(val2);
108-
}
89+
final Collection<PackageExportSymbol> listOfExportSymbols = visitor.getListOfExportSymbols();
10990

110-
@Override
111-
public void save(@NotNull DataOutput out, Key value) throws IOException {
112-
value.writeValue(out);
113-
}
114-
115-
@Override
116-
public Key read(@NotNull DataInput in) throws IOException {
117-
return new FileKey(in.readInt());
91+
for (PackageExportSymbol symbol : listOfExportSymbols) {
92+
map.putIfAbsent(symbol, null);
93+
}
94+
return map;
11895
}
11996
};
12097
}
12198

12299
@NotNull
123100
@Override
124-
public DataExternalizer<List<PackageExportSymbol>> getValueExternalizer() {
125-
return new ListDataExternalizer();
101+
public KeyDescriptor<PackageExportSymbol> getKeyDescriptor() {
102+
return PackageExportSymbol.INSTANCE;
126103
}
127104

128105
@Override
129106
public int getVersion() {
130107
return BASE_VERSION;
131108
}
132109

133-
public interface Key {
134-
void writeValue(DataOutput out) throws IOException;
135-
}
136-
137-
public static class FileKey implements Key {
138-
private final int myFileId;
139-
private String myFileName = "";
140-
141-
private FileKey(int fileId) {
142-
myFileId = fileId;
143-
}
144-
145-
private FileKey(VirtualFile file) {
146-
myFileId = FileBasedIndex.getFileId(file);
147-
myFileName = file.getName();
148-
}
149-
150-
public String getFileName() {
151-
return myFileName;
152-
}
153-
154-
public int getFileId() {
155-
return myFileId;
156-
}
157-
158-
@Override
159-
public void writeValue(DataOutput out) throws IOException {
160-
out.writeInt(myFileId);
161-
}
162-
163-
@Override
164-
public int hashCode() {
165-
return myFileId;
166-
}
167-
168-
@Override
169-
public boolean equals(Object obj) {
170-
return obj instanceof FileKey && ((FileKey)obj).myFileId == myFileId;
171-
}
172-
}
173-
174-
private static class ListDataExternalizer implements DataExternalizer<List<PackageExportSymbol>> {
175-
@Override
176-
public void save(@NotNull DataOutput out, List<PackageExportSymbol> value) throws IOException {
177-
out.writeInt(value.size());
178-
for (PackageExportSymbol info : value) {
179-
IOUtil.writeUTF(out, info.nameSpace);
180-
IOUtil.writeUTF(out, info.symbol);
181-
}
182-
}
183-
184-
@Override
185-
public List<PackageExportSymbol> read(@NotNull DataInput in) throws IOException {
186-
int size = in.readInt();
187-
ArrayList<PackageExportSymbol> info = new ArrayList<PackageExportSymbol>(size);
188-
for (int i = 0; i < size; i++) {
189-
info.add(new PackageExportSymbol(IOUtil.readUTF(in), IOUtil.readUTF(in)));
190-
}
191-
return info;
192-
}
193-
}
194110
}

0 commit comments

Comments
 (0)