Skip to content

Commit 443c3f4

Browse files
committed
Reworked some of the built-in symbol information and completion functionality
1 parent 7ddfd42 commit 443c3f4

3 files changed

Lines changed: 42 additions & 62 deletions

File tree

resources/mathematica/FunctionInformation.m

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
(* :Keywords: *)
1313
(* :Discussion: *)
1414

15-
BeginPackage["FunctionInformation`"]
15+
BeginPackage["FunctionInformation`"];
1616

1717
CreateCompletionInformation::usage = "CreateCompletionInformation[] returns a list of strings where each element is an \
1818
entry of the .properties file that is used to enable autocompletion in idea.";
1919

2020
Begin["`Private`"] (* Begin Private Context *)
2121

22+
<< JLink`;
23+
2224
(* For good code completion we need an ordering of all possible completions. This is done with the *)
2325
(* function frequency list that comes with Mathematica nowadays. I just assign numbers according to the *)
2426
(* place in this list. The higher the number, the more important and the more like is the completion result. *)
@@ -34,15 +36,11 @@
3436
Rule @@@ Get[file]
3537
];
3638

37-
makeContextNames[context_String] :=
38-
Append[StringReplace[
39-
Names[RegularExpression[context <> "`\$?[A-Z]\\w*"]],
40-
context ~~ "`" ~~ rest__ :> rest], context];
39+
makeContextNames[context_String] := Block[{$ContextPath = {context}},
40+
StringJoin[context, #]& /@ Names[RegularExpression[context <> "\$?[A-Z]\\w*"]]
41+
];
4142

42-
names = Sort[
43-
Join[Names[RegularExpression["\$?[A-Z]\\w*"]],
44-
makeContextNames["JLink"],
45-
Names[RegularExpression["(Developer|Internal)`\$?[A-Z]\\w*"]]]];
43+
names = Sort[Flatten[ makeContextNames /@ {"System`", "Developer`", "Internal`", "JLink`"} ]];
4644

4745
isFunction[str_String] :=
4846
With[{usg =
@@ -92,7 +90,7 @@
9290

9391

9492
createInformation[name_String] := Module[{importance, info, context},
95-
importance = name /. $functionFrequency;
93+
importance = StringReplace[name, "System`" ~~ n__ :> n] /. $functionFrequency;
9694
Check[
9795
context = Context[name];
9896
info = Cases[

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

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,10 @@
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;
27+
import com.intellij.patterns.PsiElementPattern.Capture;
2828
import com.intellij.psi.PsiElement;
2929
import com.intellij.util.ProcessingContext;
30+
import de.halirutan.mathematica.codeinsight.completion.SymbolInformationProvider.SymbolInformation;
3031
import de.halirutan.mathematica.parsing.MathematicaElementTypes;
3132
import org.jetbrains.annotations.NotNull;
3233

@@ -35,19 +36,21 @@
3536
import static com.intellij.patterns.PlatformPatterns.psiElement;
3637

3738
/**
38-
* @author patrick (4/2/13)
39+
* Provides completion for Mathematica built-in symbols. The underlying important file with all information can be
40+
* found in the resource directory de/halirutan/mathematica/codeinsight/completion.
41+
* @author hal (4/2/13)
3942
*/
4043
public class BuiltinFunctionCompletionProvider extends MathematicaCompletionProvider {
4144

4245
@Override
4346
void addTo(CompletionContributor contributor) {
44-
final PsiElementPattern.Capture<PsiElement> psiElementCapture = psiElement().withElementType(MathematicaElementTypes.IDENTIFIER);
47+
final Capture<PsiElement> psiElementCapture = psiElement().withElementType(MathematicaElementTypes.IDENTIFIER);
4548
contributor.extend(CompletionType.BASIC, psiElementCapture, this);
4649
}
4750

4851
@Override
4952
protected void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) {
50-
HashMap<String, SymbolInformationProvider.SymbolInformation> symbols = SymbolInformationProvider.getSymbolNames();
53+
HashMap<String, SymbolInformation> symbols = SymbolInformationProvider.getSymbolNames();
5154

5255
if (Character.isLowerCase(parameters.getPosition().getText().charAt(0))) {
5356
return;
@@ -58,11 +61,11 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
5861

5962
CompletionResultSet result2 = result.withPrefixMatcher(matcher);
6063
for (String name : symbols.keySet()) {
61-
SymbolInformationProvider.SymbolInformation symbol = symbols.get(name);
64+
SymbolInformation symbol = symbols.get(name);
6265
LookupElementBuilder elm = LookupElementBuilder
63-
.create(name)
66+
.create(symbol.context.compareTo("System`") == 0 ? symbol.nameWithoutContext : symbol.name)
6467
.withInsertHandler(MathematicaBracketInsertHandler.getInstance())
65-
.withTypeText(symbol.context + "`")
68+
.withTypeText(symbol.context)
6669
.withPresentableText(symbol.nameWithoutContext);
6770
result2.addElement(PrioritizedLookupElement.withPriority(elm, symbol.importance));
6871
}

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

Lines changed: 24 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,32 @@
2626
import java.util.ResourceBundle;
2727

2828
/**
29-
* @author patrick (4/3/13)
29+
* Loads and extracts all information of Mathematica built-in symbols that are used for completion and intelligent
30+
* code insight.
31+
* @author hal (4/3/13)
3032
*/
3133
public class SymbolInformationProvider {
3234

33-
private static HashMap<String, SymbolInformation> ourSymbols;
35+
private final static String ourSymbolInformationFile = "/de/halirutan/mathematica/codeinsight/completion/symbolInformationV10_3_1";
36+
private final static HashMap<String, SymbolInformation> ourSymbols;
3437

35-
private SymbolInformationProvider() {
36-
}
37-
38-
private static void initialize() {
38+
private SymbolInformationProvider() {}
3939

40-
ResourceBundle info = ResourceBundle.getBundle("/de/halirutan/mathematica/codeinsight/completion/symbolInformationV10_0_2");
40+
static {
4141

42+
ResourceBundle info = ResourceBundle.getBundle(ourSymbolInformationFile);
4243
ourSymbols = new HashMap<String, SymbolInformation>(6000);
4344

4445
Enumeration<String> names = info.getKeys();
4546
while (names.hasMoreElements()) {
4647
String name = names.nextElement();
48+
String context = name.split("`")[0] + "`";
49+
String nameWithoutContext = name.substring(context.length());
4750
String parts[] = info.getString(name).split(";");
4851

4952
boolean isFunction = false;
5053
int importance = 0;
5154
String attributes[] = {};
52-
String shortName = "";
5355
String pattern = "";
5456
String options[] = {};
5557

@@ -67,10 +69,8 @@ private static void initialize() {
6769
attributes = parts[1].trim().split(" ");
6870
}
6971

70-
if (parts.length > 2) {
71-
shortName = parts[2];
72-
73-
}
72+
// part 2 is missing because it was used as "ShortName" until everything was refactored and the short name is
73+
// now extracted from the full context name of a symbol
7474

7575
if (parts.length > 3) {
7676
pattern = parts[3].trim();
@@ -84,58 +84,37 @@ private static void initialize() {
8484
isFunction = true;
8585
}
8686

87-
ourSymbols.put(name, new SymbolInformation(name, shortName, importance, pattern, isFunction, attributes, options));
87+
ourSymbols.put(name, new SymbolInformation(name, nameWithoutContext, context, importance, pattern, isFunction, attributes, options));
8888
}
8989

9090
}
9191

9292
public static HashMap<String, SymbolInformation> getSymbolNames() {
93-
if (ourSymbols == null) {
94-
initialize();
95-
}
9693
return ourSymbols;
9794
}
9895

96+
@SuppressWarnings("InstanceVariableNamingConvention")
9997
public static class SymbolInformation {
10098
public final String name;
10199
public final int importance;
102-
public final String shortName;
103100
public final String callPattern;
104101
public final boolean function;
105102
public final String attributes[];
106103
public final String options[];
107-
public String context = "System";
104+
public String context;
108105
public String nameWithoutContext;
109106

110-
public SymbolInformation(String name, String shortName, int importance, String callPattern, boolean function, String[] attributes, String[] options) {
111-
this.name = name;
112-
this.nameWithoutContext = name;
113-
this.shortName = shortName.length() == 0 ? name : shortName;
114-
this.importance = importance;
115-
this.callPattern = callPattern;
116-
this.function = function;
117-
this.attributes = attributes;
118-
this.options = options;
119-
120-
if (name.contains("`")) {
121-
context = name.split("`")[0];
122-
nameWithoutContext = name.substring(context.length() + 1);
123-
}
124-
125-
107+
public SymbolInformation(String nameIn, String nameWithoutContextIn, String contextIn, int importanceIn, String callPatternIn, boolean functionIn, String[] attributesIn, String[] optionsIn) {
108+
this.name = nameIn;
109+
this.nameWithoutContext = nameWithoutContextIn;
110+
this.context = contextIn;
111+
this.importance = importanceIn;
112+
this.callPattern = callPatternIn;
113+
this.function = functionIn;
114+
this.attributes = attributesIn;
115+
this.options = optionsIn;
126116
}
127117

128-
public SymbolInformation(String name) {
129-
this.name = name;
130-
shortName = name;
131-
importance = 0;
132-
callPattern = "";
133-
function = false;
134-
attributes = new String[0];
135-
options = new String[0];
136-
}
137-
138-
139118
}
140119

141120
}

0 commit comments

Comments
 (0)