Skip to content

Commit 561e7d7

Browse files
committed
Build a better completion lookup. Fixed automatic bracket inserting on the way
1 parent 9e18fa5 commit 561e7d7

5 files changed

Lines changed: 116 additions & 100 deletions

File tree

resources/de/halirutan/mathematica/codeinsight/completion/symbolInformationV11_0_1.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3717,7 +3717,7 @@ System`MandelbrotSetMemberQ=1369;Protected ReadProtected;MandelbrotSetMemberQ;{_
37173717
System`MandelbrotSetPlot=1695;Protected ReadProtected;MandelbrotSetPlot;{Optional[{_, _}], OptionsPattern[]};{AlignmentPoint, AspectRatio, Axes, AxesLabel, AxesOrigin, AxesStyle, Background, BaselinePosition, BaseStyle, ColorFunction, ColorFunctionScaling, ColorOutput, ContentSelectable, CoordinatesToolOptions, DisplayFunction, Epilog, EscapeRadius, FormatType, Frame, FrameLabel, FrameStyle, FrameTicks, FrameTicksStyle, GridLines, GridLinesStyle, ImageMargins, ImagePadding, ImageResolution, ImageSize, ImageSizeRaw, LabelStyle, MaxIterations, Method, PerformanceGoal, PlotLabel, PlotLegends, PlotRange, PlotRangeClipping, PlotRangePadding, PlotRegion, PlotTheme, PreserveImageOptions, Prolog, RotateLabel, Ticks, TicksStyle}
37183718
System`MangoldtLambda=0;Protected ReadProtected;MangoldtLambda;{_}
37193719
System`ManhattanDistance=1462;Protected;ManhattanDistance;{_, _}
3720-
System`Manipulate=3257;Protected ReadProtected;Manipulate;{FunctionInformation`Private`expr_, FunctionInformation`Private`uspec__, OptionsPattern[]};{Alignment, AppearanceElements, AutoAction, AutorunSequencing, BaselinePosition, BaseStyle, Bookmarks, ContentSize, ContinuousAction, ControlAlignment, ControllerLinking, ControllerMethod, ControllerPath, ControlPlacement, ControlType, DefaultBaseStyle, DefaultLabelStyle, Deinitialization, Deployed, Evaluator, Frame, FrameLabel, FrameMargins, ImageMargins, Initialization, InterpolationOrder, LabelStyle, LocalizeVariables, Method, Paneled, PreserveImageOptions, RotateLabel, SaveDefinitions, ShrinkingDelay, SynchronousInitialization, SynchronousUpdating, TouchscreenAutoZoom, TouchscreenControlPlacement, TrackedSymbols, UndoTrackedVariables, UnsavedVariables, UntrackedVariables};{Manipulate, {2, Infinity}, Lexical}
3720+
System`Manipulate=3257;Protected ReadProtected;Manipulate;{_, __, OptionsPattern[]};{Alignment, AppearanceElements, AutoAction, AutorunSequencing, BaselinePosition, BaseStyle, Bookmarks, ContentSize, ContinuousAction, ControlAlignment, ControllerLinking, ControllerMethod, ControllerPath, ControlPlacement, ControlType, DefaultBaseStyle, DefaultLabelStyle, Deinitialization, Deployed, Evaluator, Frame, FrameLabel, FrameMargins, ImageMargins, Initialization, InterpolationOrder, LabelStyle, LocalizeVariables, Method, Paneled, PreserveImageOptions, RotateLabel, SaveDefinitions, ShrinkingDelay, SynchronousInitialization, SynchronousUpdating, TouchscreenAutoZoom, TouchscreenControlPlacement, TrackedSymbols, UndoTrackedVariables, UnsavedVariables, UntrackedVariables};{Manipulate, {2, Infinity}, Lexical}
37213721
System`Manipulator=2608;Protected ReadProtected;Manipulator;{_., Optional[{_, _, _.}], OptionsPattern[]};{AnimationDirection, AnimationRate, AnimationRunning, AnimationTimeIndex, Appearance, AppearanceElements, AutoAction, BaselinePosition, BaseStyle, ContinuousAction, DefaultOptions, Enabled, Exclusions, ImageMargins, ImageSize, LabelStyle, PausedTime}
37223722
System`MannedSpaceMissionData=1956;Protected ReadProtected;MannedSpaceMissionData;{_., _., _.}
37233723
System`MannWhitneyTest=0;Protected ReadProtected;MannWhitneyTest;{_, _., _., OptionsPattern[]};{AlternativeHypothesis, Method, SignificanceLevel, VerifyTestAssumptions, MaxIterations}

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -57,17 +57,13 @@ protected void addCompletions(@NotNull CompletionParameters parameters, Processi
5757
}
5858

5959
String prefix = findCurrentText(parameters, parameters.getPosition());
60-
CamelHumpMatcher matcher = new CamelHumpMatcher(prefix, false);
60+
CamelHumpMatcher matcher = new CamelHumpMatcher(prefix, true);
6161

6262
CompletionResultSet result2 = result.withPrefixMatcher(matcher);
63-
for (String name : symbols.keySet()) {
64-
SymbolInformation symbol = symbols.get(name);
65-
LookupElementBuilder elm = LookupElementBuilder
66-
.create(symbol.context.compareTo("System`") == 0 ? symbol.nameWithoutContext : symbol.name)
67-
.withInsertHandler(MathematicaBracketInsertHandler.getInstance())
68-
.withTypeText(symbol.context)
69-
.withPresentableText(symbol.nameWithoutContext);
70-
result2.addElement(PrioritizedLookupElement.withPriority(elm, symbol.importance));
63+
64+
for (SymbolInformation info : symbols.values()) {
65+
BuiltinSymbolLookupElement lookup = new BuiltinSymbolLookupElement(info);
66+
result2.addElement(PrioritizedLookupElement.withPriority(lookup, info.importance));
7167
}
7268
}
7369
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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.codeInsight.completion.InsertionContext;
25+
import com.intellij.codeInsight.lookup.Lookup;
26+
import com.intellij.codeInsight.lookup.LookupElement;
27+
import com.intellij.codeInsight.lookup.LookupElementPresentation;
28+
import com.intellij.openapi.editor.Document;
29+
import com.intellij.openapi.editor.Editor;
30+
import com.intellij.openapi.project.Project;
31+
import com.intellij.psi.PsiDocumentManager;
32+
import de.halirutan.mathematica.codeinsight.completion.SymbolInformationProvider.SymbolInformation;
33+
import org.jetbrains.annotations.NotNull;
34+
35+
/**
36+
* @author patrick (30.11.16).
37+
*/
38+
public class BuiltinSymbolLookupElement extends LookupElement {
39+
40+
private final SymbolInformation myInfo;
41+
private static final char OPEN_BRACKET = '[';
42+
private static final char CLOSING_BRACKET = ']';
43+
44+
public BuiltinSymbolLookupElement(SymbolInformation info) {
45+
myInfo = info;
46+
}
47+
48+
@NotNull
49+
@Override
50+
public String getLookupString() {
51+
if ("System`".equals(myInfo.context)) {
52+
return myInfo.nameWithoutContext;
53+
}
54+
return myInfo.name;
55+
}
56+
57+
@Override
58+
public void renderElement(LookupElementPresentation presentation) {
59+
presentation.setItemText(getLookupString());
60+
presentation.setItemTextBold(myInfo.function);
61+
presentation.setTailText(myInfo.function ? "[" + myInfo.getCallPattern() + "]" : "", true);
62+
presentation.setTypeText(myInfo.context);
63+
}
64+
65+
@Override
66+
public boolean isCaseSensitive() {
67+
return true;
68+
}
69+
70+
@Override
71+
public void handleInsert(InsertionContext context) {
72+
Editor editor = context.getEditor();
73+
Document document = editor.getDocument();
74+
context.commitDocument();
75+
76+
char completionChar = context.getCompletionChar();
77+
context.setAddCompletionChar(false);
78+
79+
if (completionChar == Lookup.COMPLETE_STATEMENT_SELECT_CHAR) {
80+
if (myInfo.function) {
81+
document.insertString(context.getTailOffset(), Character.toString(OPEN_BRACKET));
82+
editor.getCaretModel().moveToOffset(context.getTailOffset());
83+
document.insertString(context.getTailOffset(), Character.toString(CLOSING_BRACKET));
84+
} else {
85+
document.insertString(context.getTailOffset(), " ");
86+
editor.getCaretModel().moveToOffset(context.getTailOffset());
87+
}
88+
89+
}
90+
91+
if (completionChar == ' ') {
92+
context.setAddCompletionChar(true);
93+
}
94+
95+
final Project project = context.getProject();
96+
PsiDocumentManager.getInstance(project).commitDocument(document);
97+
}
98+
}

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

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

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ public static class SymbolInformation {
104104
public String context;
105105
public String nameWithoutContext;
106106

107-
public SymbolInformation(String nameIn, String nameWithoutContextIn, String contextIn, int importanceIn, String callPatternIn, boolean functionIn, String[] attributesIn, String[] optionsIn) {
107+
SymbolInformation(String nameIn, String nameWithoutContextIn, String contextIn, int importanceIn, String callPatternIn, boolean functionIn, String[] attributesIn, String[] optionsIn) {
108108
this.name = nameIn;
109109
this.nameWithoutContext = nameWithoutContextIn;
110110
this.context = contextIn;
@@ -115,6 +115,17 @@ public SymbolInformation(String nameIn, String nameWithoutContextIn, String cont
115115
this.options = optionsIn;
116116
}
117117

118+
/**
119+
* Removes the braces around the call patter
120+
* @return call pattern without braces
121+
*/
122+
String getCallPattern() {
123+
if (callPattern.length() > 2 && callPattern.matches("\\{.*}")) {
124+
return callPattern.substring(1, callPattern.length() - 1);
125+
}
126+
return "";
127+
}
128+
118129
}
119130

120131
}

0 commit comments

Comments
 (0)