Skip to content

Commit 76cc422

Browse files
committed
Move triggerpoints to resource file
1 parent 4d21c8f commit 76cc422

File tree

4 files changed

+110
-55
lines changed

4 files changed

+110
-55
lines changed

code4me-jetbrains-plugin/src/main/java/me/code4me/plugin/listeners/Code4MeDocumentListener.java

Lines changed: 5 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -9,67 +9,17 @@
99
import com.intellij.openapi.project.Project;
1010
import com.intellij.openapi.vfs.VirtualFile;
1111
import me.code4me.plugin.completions.Code4MeCompletionContributor;
12+
import me.code4me.plugin.services.Code4MeTriggerPointsService;
1213
import org.jetbrains.annotations.NotNull;
1314

14-
import java.util.Map;
15-
import static java.util.Map.entry;
16-
1715
public class Code4MeDocumentListener implements DocumentListener {
1816

19-
private static final Map<String, Boolean> TRIGGER_POINT_MAP = Map.ofEntries(
20-
entry(".", false),
21-
entry("await", true),
22-
entry("assert", true),
23-
entry("raise", true),
24-
entry("del", true),
25-
entry("lambda", true),
26-
entry("yield", true),
27-
entry("return", true),
28-
entry("while", true),
29-
entry("for", true),
30-
entry("if", true),
31-
entry("elif", true),
32-
entry("else", true),
33-
entry("global", true),
34-
entry("in", true),
35-
entry("and", true),
36-
entry("not", true),
37-
entry("or", true),
38-
entry("is", true),
39-
entry("+", false),
40-
entry("-", false),
41-
entry("*", false),
42-
entry("/", false),
43-
entry("%", false),
44-
entry("**", false),
45-
entry("<<", false),
46-
entry(">>", false),
47-
entry("&", false),
48-
entry("|", false),
49-
entry("^", false),
50-
entry("=", true),
51-
entry("==", false),
52-
entry("!=", false),
53-
entry("<=", false),
54-
entry(">=", false),
55-
entry("with", true),
56-
entry(";", false),
57-
entry(",", false),
58-
entry("[", false),
59-
entry("(", false),
60-
entry("{", false),
61-
entry("~", false)
62-
);
63-
64-
private static final int MAX_TRIGGER_WORD_LENGTH = TRIGGER_POINT_MAP.keySet().stream()
65-
.mapToInt(String::length)
66-
.max()
67-
.orElse(0);
68-
6917
private final Project project;
18+
private final Code4MeTriggerPointsService triggerPointsService;
7019

7120
public Code4MeDocumentListener(Project project) {
7221
this.project = project;
22+
this.triggerPointsService = project.getService(Code4MeTriggerPointsService.class);
7323
}
7424

7525

@@ -91,7 +41,7 @@ public void documentChanged(@NotNull DocumentEvent event) {
9141
int offsetPlusOne = offset + 1;
9242
if (offsetPlusOne < text.length() && text.charAt(offsetPlusOne) != '\n') return;
9343

94-
char[] word = new char[MAX_TRIGGER_WORD_LENGTH];
44+
char[] word = new char[triggerPointsService.getMaxTriggerPointLength()];
9545
int i = 0;
9646
boolean initSpaces = text.charAt(offset) == ' ';
9747
int spaces = 0;
@@ -110,7 +60,7 @@ public void documentChanged(@NotNull DocumentEvent event) {
11060

11161
word[word.length - 1 - i] = c;
11262
String triggerPoint = new String(word).trim();
113-
Boolean trailingSpace = TRIGGER_POINT_MAP.get(triggerPoint);
63+
Boolean trailingSpace = triggerPointsService.getTriggerPoint(triggerPoint);
11464
if (trailingSpace != null && (!trailingSpace || spaces > 0)) {
11565
Code4MeCompletionContributor.suggestCompletionFromParts(project, editor, doc, offsetPlusOne, new String[] {
11666
text.substring(0, offsetPlusOne),
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package me.code4me.plugin.services;
2+
3+
import com.google.gson.Gson;
4+
5+
import java.io.IOException;
6+
import java.io.InputStream;
7+
import java.io.InputStreamReader;
8+
import java.util.Arrays;
9+
import java.util.HashMap;
10+
import java.util.Map;
11+
import java.util.Objects;
12+
13+
public class Code4MeTriggerPointsService {
14+
15+
private static final Gson gson = new Gson();
16+
private final Map<String, Boolean> triggerPointMap = new HashMap<>();
17+
private final int maxTriggerPointLength;
18+
19+
public Code4MeTriggerPointsService() throws IOException {
20+
int maxTriggerPointLength;
21+
try (
22+
InputStream in = getClass().getResourceAsStream("/triggerPoints.json");
23+
InputStreamReader reader = new InputStreamReader(Objects.requireNonNull(in))
24+
) {
25+
TriggerPoints points = gson.fromJson(reader, TriggerPoints.class);
26+
Arrays.stream(points.enforceSpace).forEach(keyword -> triggerPointMap.put(keyword, true));
27+
Arrays.stream(points.noSpace).forEach(keyword -> triggerPointMap.put(keyword, false));
28+
maxTriggerPointLength = triggerPointMap.keySet().stream()
29+
.mapToInt(String::length)
30+
.max()
31+
.orElse(0);
32+
}
33+
this.maxTriggerPointLength = maxTriggerPointLength;
34+
}
35+
36+
public Boolean getTriggerPoint(String keyword) {
37+
return triggerPointMap.get(keyword);
38+
}
39+
40+
public int getMaxTriggerPointLength() {
41+
return maxTriggerPointLength;
42+
}
43+
44+
private static class TriggerPoints {
45+
46+
private String[] enforceSpace;
47+
private String[] noSpace;
48+
49+
private TriggerPoints(String[] enforceSpace, String[] noSpace) {
50+
this.enforceSpace = enforceSpace;
51+
this.noSpace = noSpace;
52+
}
53+
}
54+
}

code4me-jetbrains-plugin/src/main/resources/META-INF/plugin.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
<extensions defaultExtensionNs="com.intellij">
1010
<projectService serviceImplementation="me.code4me.plugin.services.Code4MeApiService"/>
1111
<projectService serviceImplementation="me.code4me.plugin.services.Code4MeSettingsService"/>
12+
<projectService serviceImplementation="me.code4me.plugin.services.Code4MeTriggerPointsService"/>
1213
<notificationGroup id="Code4Me Notifications" displayType="BALLOON"/>
1314
<completion.contributor language="any"
1415
implementationClass="me.code4me.plugin.completions.Code4MeCompletionContributor"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
{
2+
"enforceSpace": [
3+
"await",
4+
"assert",
5+
"raise",
6+
"del",
7+
"lambda",
8+
"yield",
9+
"return",
10+
"while",
11+
"for",
12+
"if",
13+
"elif",
14+
"else",
15+
"global",
16+
"in",
17+
"and",
18+
"not",
19+
"or",
20+
"is",
21+
"=",
22+
"<",
23+
">",
24+
"with"
25+
],
26+
"noSpace": [
27+
".",
28+
"+",
29+
"-",
30+
"*",
31+
"/",
32+
"%",
33+
"**",
34+
"<<",
35+
">>",
36+
"&",
37+
"|",
38+
"^",
39+
"==",
40+
"!=",
41+
"<=",
42+
">=",
43+
";",
44+
",",
45+
"[",
46+
"(",
47+
"{",
48+
"~"
49+
]
50+
}

0 commit comments

Comments
 (0)