Skip to content

Commit 40c7fbb

Browse files
committed
Improved completion of built-in symbols by removing the importance weighting. Fixed some smaller bugs in the completion engine
1 parent 14db0ed commit 40c7fbb

8 files changed

Lines changed: 52 additions & 32 deletions

File tree

.idea/dictionaries/patrick.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

resources/META-INF/plugin.xml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
1+
<!--
2+
~ Copyright (c) 2017 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+
122
<idea-plugin url="http://mathematicaplugin.halirutan.de" version="2">
223
<id>de.halirutan.mathematica</id>
324
<name>Mathematica Support</name>
@@ -174,7 +195,6 @@
174195
instance="de.halirutan.mathematica.settings.MathematicaSettingsConfigurable"/>
175196
<gotoRelatedProvider
176197
implementation="de.halirutan.mathematica.codeinsight.navigation.MathematicaGotoRelatedProvider"/>
177-
178198
</extensions>
179199

180200
<actions>

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013 Patrick Scheibe
2+
* Copyright (c) 2017 Patrick Scheibe
33
* Permission is hereby granted, free of charge, to any person obtaining a copy
44
* of this software and associated documentation files (the "Software"), to deal
55
* in the Software without restriction, including without limitation the rights
@@ -21,9 +21,12 @@
2121

2222
package de.halirutan.mathematica.codeinsight.completion;
2323

24-
import com.intellij.codeInsight.completion.*;
24+
import com.intellij.codeInsight.completion.CompletionContributor;
25+
import com.intellij.codeInsight.completion.CompletionParameters;
26+
import com.intellij.codeInsight.completion.CompletionResultSet;
27+
import com.intellij.codeInsight.completion.CompletionType;
28+
import com.intellij.codeInsight.completion.impl.BetterPrefixMatcher;
2529
import com.intellij.codeInsight.completion.impl.CamelHumpMatcher;
26-
import com.intellij.codeInsight.lookup.LookupElementBuilder;
2730
import com.intellij.patterns.PsiElementPattern.Capture;
2831
import com.intellij.psi.PsiElement;
2932
import com.intellij.util.ProcessingContext;
@@ -57,13 +60,12 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
5760
}
5861

5962
String prefix = findCurrentText(parameters, parameters.getPosition());
60-
CamelHumpMatcher matcher = new CamelHumpMatcher(prefix, true);
61-
63+
BetterPrefixMatcher matcher = new BetterPrefixMatcher(new CamelHumpMatcher(prefix, true), 200);
6264
CompletionResultSet result2 = result.withPrefixMatcher(matcher);
6365

6466
for (SymbolInformation info : symbols.values()) {
6567
BuiltinSymbolLookupElement lookup = new BuiltinSymbolLookupElement(info);
66-
result2.addElement(PrioritizedLookupElement.withPriority(lookup, info.importance));
68+
result2.addElement(lookup);
6769
}
6870
}
6971
}

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2016 Patrick Scheibe
2+
* Copyright (c) 2017 Patrick Scheibe
33
* Permission is hereby granted, free of charge, to any person obtaining a copy
44
* of this software and associated documentation files (the "Software"), to deal
55
* in the Software without restriction, including without limitation the rights
@@ -41,6 +41,8 @@
4141
import org.jetbrains.annotations.Nullable;
4242

4343
/**
44+
* One particular lookup element of a built-in function or symbol. It handles the insert and lets users inserts
45+
* completions ins various ways.
4446
* @author patrick (30.11.16).
4547
*/
4648
@SuppressWarnings("AnonymousClassVariableHidesContainingMethodVariable")
@@ -132,8 +134,7 @@ public LookupElement[] calculateLookupItems(ExpressionContext context) {
132134
}
133135
});
134136
}
135-
Template template = builder.buildInlineTemplate();
136-
TemplateManager.getInstance(context.getProject()).startTemplate(context.getEditor(), template);
137+
builder.run(context.getEditor(), true);
137138

138139
}
139140

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

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015 Patrick Scheibe
2+
* Copyright (c) 2017 Patrick Scheibe
33
* Permission is hereby granted, free of charge, to any person obtaining a copy
44
* of this software and associated documentation files (the "Software"), to deal
55
* in the Software without restriction, including without limitation the rights
@@ -24,19 +24,13 @@
2424
import com.intellij.codeInsight.completion.*;
2525
import com.intellij.codeInsight.completion.impl.CamelHumpMatcher;
2626
import com.intellij.codeInsight.lookup.LookupElementBuilder;
27-
import com.intellij.patterns.PsiElementPattern;
2827
import com.intellij.patterns.PsiElementPattern.Capture;
2928
import com.intellij.psi.PsiComment;
30-
import com.intellij.psi.PsiElement;
3129
import com.intellij.util.ProcessingContext;
3230
import de.halirutan.mathematica.MathematicaLanguage;
33-
import de.halirutan.mathematica.parsing.MathematicaElementTypes;
3431
import org.jetbrains.annotations.NotNull;
3532

36-
import java.util.HashMap;
37-
3833
import static com.intellij.patterns.PlatformPatterns.psiComment;
39-
import static com.intellij.patterns.PlatformPatterns.psiElement;
4034

4135
/**
4236
* @author patrick (4/2/13)

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2014 Patrick Scheibe
2+
* Copyright (c) 2017 Patrick Scheibe
33
* Permission is hereby granted, free of charge, to any person obtaining a copy
44
* of this software and associated documentation files (the "Software"), to deal
55
* in the Software without restriction, including without limitation the rights
@@ -25,21 +25,23 @@
2525
import com.intellij.codeInsight.lookup.Lookup;
2626
import com.intellij.codeInsight.lookup.impl.LookupImpl;
2727
import com.intellij.psi.PsiFile;
28-
import de.halirutan.mathematica.MathematicaLanguage;
28+
import de.halirutan.mathematica.filetypes.MathematicaFileType;
2929
import org.jetbrains.annotations.Nullable;
3030

3131
/**
32+
* This changes the behaviour of what happens with the completion pop-up when you press several chars.
33+
* In Mathematica we want to insert completions with various braces (more than in Java) and additionally,
34+
* we want the pop-up to stay open when we insert ` as it is a valid variable name part.
3235
* @author patrick (7/7/14)
3336
*/
3437
public class MathematicaCharFilter extends CharFilter {
3538
@Nullable
3639
@Override
3740
public Result acceptChar(final char c, final int prefixLength, final Lookup lookup) {
38-
// if (!lookup.isFocused()) return null;
3941
final PsiFile psiFile = lookup.getPsiFile();
40-
41-
if (psiFile != null && !psiFile.getViewProvider().getLanguages().contains(MathematicaLanguage.INSTANCE))
42+
if (psiFile == null || !(psiFile.getFileType() instanceof MathematicaFileType)) {
4243
return null;
44+
}
4345

4446
switch (c) {
4547
case '[':

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

Lines changed: 0 additions & 7 deletions
This file was deleted.

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2013 Patrick Scheibe
2+
* Copyright (c) 2017 Patrick Scheibe
33
* Permission is hereby granted, free of charge, to any person obtaining a copy
44
* of this software and associated documentation files (the "Software"), to deal
55
* in the Software without restriction, including without limitation the rights
@@ -57,7 +57,7 @@ class SmartContextAwareCompletion extends MathematicaCompletionProvider {
5757

5858

5959
private static final HashMap<String, SymbolInformation> ourSymbolInformation = SymbolInformationProvider.getSymbolNames();
60-
private static final HashSet<String> ourOptionsWithSetDelayed = new HashSet<String>(Arrays.asList(new String[]{
60+
private static final HashSet<String> ourOptionsWithSetDelayed = new HashSet<>(Arrays.asList(new String[]{
6161
"EvaluationMonitor", "StepMonitor", "DisplayFunction", "Deinitialization", "DisplayFunction",
6262
"DistributedContexts", "Initialization", "UnsavedVariables", "UntrackedVariables"
6363
}));
@@ -77,6 +77,9 @@ 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+
if (functionContext.equals("") && ourSymbolInformation.containsKey("System`" + functionName)) {
81+
functionContext = "System`";
82+
}
8083
final String key = functionContext + functionName;
8184
if (ourSymbolInformation.containsKey(key) && ourSymbolInformation.get(key).function) {
8285
final SymbolInformation functionInformation = ourSymbolInformation.get(key);
@@ -90,7 +93,7 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
9093
}
9194

9295
if (functionName.equals("Message")) {
93-
final Set<LookupElement> usages = new com.intellij.util.containers.hash.HashSet<LookupElement>();
96+
final Set<LookupElement> usages = new com.intellij.util.containers.hash.HashSet<>();
9497

9598
MathematicaRecursiveVisitor visitor = new MathematicaRecursiveVisitor() {
9699

0 commit comments

Comments
 (0)