11/*
2- * Copyright (c) 2017 Patrick Scheibe
2+ * Copyright (c) 2018 Patrick Scheibe
33 *
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:
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:
1010 *
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.
11+ * The above copyright notice and this permission notice shall be included in all
12+ * copies or substantial portions of the Software.
2113 *
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.
2221 */
2322
2423package de .halirutan .mathematica .codeinsight .completion .providers ;
2726import com .intellij .codeInsight .lookup .LookupElementBuilder ;
2827import com .intellij .openapi .module .Module ;
2928import com .intellij .openapi .module .ModuleUtilCore ;
29+ import com .intellij .openapi .progress .ProgressManager ;
3030import com .intellij .openapi .project .Project ;
31- import com .intellij .patterns .PlatformPatterns ;
3231import com .intellij .patterns .PsiElementPattern .Capture ;
3332import com .intellij .psi .PsiElement ;
3433import com .intellij .psi .PsiFile ;
3534import com .intellij .psi .search .GlobalSearchScope ;
36- import com .intellij .psi .search .ProjectScope ;
3735import com .intellij .util .ProcessingContext ;
3836import com .intellij .util .indexing .FileBasedIndex ;
37+ import com .intellij .util .indexing .IdFilter ;
3938import de .halirutan .mathematica .index .packageexport .MathematicaPackageExportIndex ;
39+ import de .halirutan .mathematica .lang .parsing .MathematicaElementTypes ;
4040import de .halirutan .mathematica .lang .psi .api .Symbol ;
4141import org .jetbrains .annotations .NotNull ;
4242
4343import java .util .Objects ;
4444
45+ import static com .intellij .patterns .PlatformPatterns .psiElement ;
4546import static de .halirutan .mathematica .codeinsight .completion .MathematicaCompletionContributor .IMPORT_VARIABLE_PRIORITY ;
4647
4748
@@ -52,7 +53,7 @@ public class ImportedSymbolCompletion extends MathematicaCompletionProvider {
5253
5354 @ Override
5455 public void addTo (CompletionContributor contributor ) {
55- final Capture <PsiElement > symbolPattern = PlatformPatterns . psiElement ().withParent ( Symbol . class );
56+ final Capture <PsiElement > symbolPattern = psiElement ().withElementType ( MathematicaElementTypes . IDENTIFIER );
5657 contributor .extend (CompletionType .BASIC , symbolPattern , this );
5758 }
5859
@@ -62,26 +63,32 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
6263 final Project project = callingSymbol .getProject ();
6364
6465 String prefix = findCurrentText (parameters , parameters .getPosition ());
66+ if (parameters .getInvocationCount () == 0 && prefix .isEmpty ()) {
67+ return ;
68+ }
6569 final PsiFile originalFile = parameters .getOriginalFile ();
6670 final Module module =
6771 ModuleUtilCore .findModuleForFile (originalFile .getVirtualFile (), project );
6872 if (module != null ) {
6973 if (!parameters .isExtendedCompletion () || !(prefix .isEmpty () || Character .isDigit (prefix .charAt (0 )))) {
70- final GlobalSearchScope moduleScope = GlobalSearchScope . moduleWithDependenciesAndLibrariesScope ( module )
71- . union ( ProjectScope . getLibrariesScope ( project ) );
74+
75+ final GlobalSearchScope moduleScope = module . getModuleWithDependenciesAndLibrariesScope ( true );
7276 final FileBasedIndex index = FileBasedIndex .getInstance ();
77+
7378 index .processAllKeys (
7479 MathematicaPackageExportIndex .INDEX_ID ,
7580 key -> {
76- if (key .isExported () && !Objects .equals (key .getFileName (), originalFile .getName ())) {
81+ ProgressManager .checkCanceled ();
82+ if (key .isExported () && !Objects .equals (key .getFileName (), originalFile .getName ()) &&
83+ !index .getContainingFiles (MathematicaPackageExportIndex .INDEX_ID , key , moduleScope ).isEmpty ()) {
7784 result .addElement (PrioritizedLookupElement .withPriority (
7885 LookupElementBuilder .create (key .getSymbol ()).withTypeText ("(" + key .getFileName () + ")" , true ),
7986 IMPORT_VARIABLE_PRIORITY ));
8087 }
8188 return true ;
8289 },
8390 moduleScope ,
84- null
91+ IdFilter . getProjectIdFilter ( project , false )
8592 );
8693 }
8794 }
0 commit comments