Skip to content

Commit 24bc657

Browse files
committed
CallPatter insertion by SmartEnter on built-in functions.
1 parent daeeab1 commit 24bc657

5 files changed

Lines changed: 222 additions & 1 deletion

File tree

resources/META-INF/plugin.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,10 @@
166166
implementation="de.halirutan.mathematica.codeinsight.inspections.MathematicaInspectionProvider"/>
167167

168168
<psi.referenceContributor language="Mathematica" implementation="de.halirutan.mathematica.parsing.psi.MathematicaReferenceContributor"/>
169+
170+
<!--<applicationService serviceInterface="de.halirutan.mathematica.MathematicaSettings" serviceImplementation="de.halirutan.mathematica.MathematicaSettings"/>-->
171+
<!--<applicationConfigurable groupId="language" displayName="Mathematica" id="preferences.Mathematica"-->
172+
<!--instance="de.halirutan.mathematica.MathematicaSettingsConfigurable"/>-->
169173
</extensions>
170174

171175
<actions>
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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;/*
23+
* Copyright (c) 2016 Patrick Scheibe
24+
* Permission is hereby granted, free of charge, to any person obtaining a copy
25+
* of this software and associated documentation files (the "Software"), to deal
26+
* in the Software without restriction, including without limitation the rights
27+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
28+
* copies of the Software, and to permit persons to whom the Software is
29+
* furnished to do so, subject to the following conditions:
30+
*
31+
* The above copyright notice and this permission notice shall be included in
32+
* all copies or substantial portions of the Software.
33+
*
34+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
35+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
36+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
37+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
38+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
39+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
40+
* THE SOFTWARE.
41+
*/
42+
43+
import com.intellij.openapi.components.PersistentStateComponent;
44+
import com.intellij.openapi.components.ServiceManager;
45+
import com.intellij.openapi.components.State;
46+
import com.intellij.openapi.components.Storage;
47+
import com.intellij.util.xmlb.XmlSerializerUtil;
48+
import org.jetbrains.annotations.Nullable;
49+
50+
/**
51+
* @author patrick (01.12.16).
52+
*/
53+
@State(name = "MathematicaSettings", storages = @Storage("other.xml"))
54+
public class MathematicaSettings implements PersistentStateComponent<MathematicaSettings> {
55+
56+
public enum SmartEnterResult {
57+
INSERT_BRACES,
58+
INSERT_CALL_PATTERN
59+
}
60+
61+
public SmartEnterResult mySmartEnterResult = SmartEnterResult.INSERT_CALL_PATTERN;
62+
public boolean mySortEntriesLexicographically = false;
63+
64+
public static MathematicaSettings getInstance() {
65+
return ServiceManager.getService(MathematicaSettings.class);
66+
}
67+
68+
@Nullable
69+
@Override
70+
public MathematicaSettings getState() {
71+
return this;
72+
}
73+
74+
@Override
75+
public void loadState(MathematicaSettings state) {
76+
XmlSerializerUtil.copyBean(state, this);
77+
}
78+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="de.halirutan.mathematica.MathematicaSettingsConfigurable">
3+
<grid id="27dc6" binding="myWholePanel" layout-manager="GridLayoutManager" row-count="3" column-count="2" same-size-horizontally="false" same-size-vertically="false" hgap="-1" vgap="-1">
4+
<margin top="0" left="0" bottom="0" right="0"/>
5+
<constraints>
6+
<xy x="20" y="20" width="500" height="400"/>
7+
</constraints>
8+
<properties/>
9+
<border type="none"/>
10+
<children>
11+
<component id="266ff" class="javax.swing.JLabel">
12+
<constraints>
13+
<grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
14+
</constraints>
15+
<properties>
16+
<text value="Completion Settings:"/>
17+
</properties>
18+
</component>
19+
<hspacer id="d5236">
20+
<constraints>
21+
<grid row="0" column="1" row-span="1" col-span="1" vsize-policy="1" hsize-policy="6" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
22+
</constraints>
23+
</hspacer>
24+
<component id="ee435" class="javax.swing.JCheckBox" binding="mySortEntries">
25+
<constraints>
26+
<grid row="1" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
27+
</constraints>
28+
<properties>
29+
<selected value="false"/>
30+
<text value="Sort completion results lexicographically"/>
31+
</properties>
32+
</component>
33+
<component id="57dfb" class="javax.swing.JComboBox" binding="mySmartEnterMethod" custom-create="true">
34+
<constraints>
35+
<grid row="2" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
36+
</constraints>
37+
<properties/>
38+
</component>
39+
</children>
40+
</grid>
41+
</form>
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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;
23+
24+
import com.intellij.openapi.options.Configurable;
25+
import com.intellij.openapi.options.Configurable.NoScroll;
26+
import com.intellij.openapi.options.ConfigurationException;
27+
import com.intellij.openapi.options.SearchableConfigurable;
28+
import org.jetbrains.annotations.Nls;
29+
import org.jetbrains.annotations.NotNull;
30+
import org.jetbrains.annotations.Nullable;
31+
32+
import javax.swing.*;
33+
34+
/**
35+
* @author patrick (01.12.16).
36+
*/
37+
public class MathematicaSettingsConfigurable implements SearchableConfigurable, NoScroll{
38+
private JPanel myWholePanel;
39+
private JCheckBox mySortEntries;
40+
private JComboBox mySmartEnterMethod;
41+
42+
@NotNull
43+
@Override
44+
public String getId() {
45+
return null;
46+
}
47+
48+
@Nls
49+
@Override
50+
public String getDisplayName() {
51+
return null;
52+
}
53+
54+
@Nullable
55+
@Override
56+
public String getHelpTopic() {
57+
return null;
58+
}
59+
60+
@Nullable
61+
@Override
62+
public JComponent createComponent() {
63+
return null;
64+
}
65+
66+
@Override
67+
public boolean isModified() {
68+
return false;
69+
}
70+
71+
@Override
72+
public void apply() throws ConfigurationException {
73+
74+
}
75+
76+
@Override
77+
public void reset() {
78+
79+
}
80+
81+
private void createUIComponents() {
82+
// TODO: place custom component creation code here
83+
}
84+
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import com.intellij.openapi.editor.Document;
2929
import com.intellij.openapi.editor.Editor;
3030
import com.intellij.openapi.project.Project;
31+
import com.intellij.openapi.util.TextRange;
3132
import com.intellij.psi.PsiDocumentManager;
3233
import de.halirutan.mathematica.codeinsight.completion.SymbolInformationProvider.SymbolInformation;
3334
import org.jetbrains.annotations.NotNull;
@@ -79,8 +80,12 @@ public void handleInsert(InsertionContext context) {
7980
if (completionChar == Lookup.COMPLETE_STATEMENT_SELECT_CHAR) {
8081
if (myInfo.function) {
8182
document.insertString(context.getTailOffset(), Character.toString(OPEN_BRACKET));
82-
editor.getCaretModel().moveToOffset(context.getTailOffset());
83+
final int currentPosition = context.getTailOffset();
84+
document.insertString(currentPosition, myInfo.getCallPattern());
8385
document.insertString(context.getTailOffset(), Character.toString(CLOSING_BRACKET));
86+
final int endOffset = getFirstArgumentRange(myInfo).getEndOffset() + currentPosition;
87+
editor.getSelectionModel().setSelection(currentPosition, endOffset);
88+
editor.getCaretModel().moveToOffset(endOffset);
8489
} else {
8590
document.insertString(context.getTailOffset(), " ");
8691
editor.getCaretModel().moveToOffset(context.getTailOffset());
@@ -95,4 +100,13 @@ public void handleInsert(InsertionContext context) {
95100
final Project project = context.getProject();
96101
PsiDocumentManager.getInstance(project).commitDocument(document);
97102
}
103+
104+
private TextRange getFirstArgumentRange(SymbolInformation info) {
105+
final String callPattern = info.getCallPattern();
106+
final int firstComma = callPattern.indexOf(',');
107+
if (firstComma == -1) {
108+
return new TextRange(1, callPattern.length());
109+
}
110+
return new TextRange(1, firstComma);
111+
}
98112
}

0 commit comments

Comments
 (0)