Skip to content

Commit 67e9431

Browse files
committed
feat: provide completion for Resource Bundle keys
Signed-off-by: Fred Bricon <fbricon@gmail.com>
1 parent 81fe76b commit 67e9431

10 files changed

Lines changed: 1442 additions & 1 deletion

File tree

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/contentassist/ResourceBundleCompletionProposal.java

Lines changed: 929 additions & 0 deletions
Large diffs are not rendered by default.

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/handlers/CompletionHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.eclipse.jdt.ls.core.internal.contentassist.CompletionProposalRequestor;
4949
import org.eclipse.jdt.ls.core.internal.contentassist.CompletionProposalUtils;
5050
import org.eclipse.jdt.ls.core.internal.contentassist.JavadocCompletionProposal;
51+
import org.eclipse.jdt.ls.core.internal.contentassist.ResourceBundleCompletionProposal;
5152
import org.eclipse.jdt.ls.core.internal.contentassist.SnippetCompletionProposal;
5253
import org.eclipse.jdt.ls.core.internal.contentassist.SortTextHelper;
5354
import org.eclipse.jdt.ls.core.internal.preferences.PreferenceManager;
@@ -298,6 +299,9 @@ public boolean isCanceled() {
298299
proposals.addAll(SnippetCompletionProposal.getSnippets(unit, collector, subMonitor));
299300
}
300301
proposals.addAll(new JavadocCompletionProposal().getProposals(unit, offset, collector, subMonitor));
302+
if (manager.getPreferences().isResourceBundleCompletionEnabled()) {
303+
proposals.addAll(new ResourceBundleCompletionProposal().getProposals(unit, offset, collector, subMonitor));
304+
}
301305
} catch (OperationCanceledException e) {
302306
monitor.setCanceled(true);
303307
}
@@ -307,6 +311,7 @@ public boolean isCanceled() {
307311
List<CompletionItem> tempProposals = proposals.stream().filter(prop -> prop.getKind() == CompletionItemKind.Keyword || prop.getKind() == CompletionItemKind.Snippet).collect(Collectors.toList());
308312
tempProposals.sort(LABEL_COMPARATOR);
309313
int newSortText = SortTextHelper.CEILING;
314+
boolean foundMatch = false;
310315
for (int i = 0; i < tempProposals.size() - 1; i++) {
311316
CompletionItem currentItem = tempProposals.get(i);
312317
CompletionItem nextItem = tempProposals.get(i + 1);
@@ -319,10 +324,11 @@ public boolean isCanceled() {
319324
}
320325
if (tempSortText < newSortText) {
321326
newSortText = tempSortText;
327+
foundMatch = true;
322328
}
323329
}
324330
}
325-
if (newSortText != -1) {
331+
if (foundMatch) {
326332
String finalSortText = Integer.toString(newSortText);
327333
tempProposals.stream().filter(prop -> prop.getKind() == CompletionItemKind.Snippet).forEach(p -> p.setSortText(finalSortText));
328334
}

org.eclipse.jdt.ls.core/src/org/eclipse/jdt/ls/core/internal/preferences/Preferences.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,11 @@ public class Preferences {
539539
*/
540540
public static final String CHAIN_COMPLETION_KEY = "java.completion.chain.enabled";
541541

542+
/**
543+
* Preference key to enable/disable resource bundle completion.
544+
*/
545+
public static final String RESOURCE_BUNDLE_COMPLETION_KEY = "java.completion.resourceBundle.enabled";
546+
542547
/**
543548
* Preference key to set the scope value to use when searching java code. Allowed value are
544549
* <ul>
@@ -717,6 +722,7 @@ public class Preferences {
717722
private boolean telemetryEnabled;
718723
private boolean validateAllOpenBuffersOnChanges;
719724
private boolean chainCompletionEnabled;
725+
private boolean resourceBundleCompletionEnabled;
720726
private List<String> diagnosticFilter;
721727
private SearchScope searchScope;
722728
private boolean inlayHintsSuppressedWhenSameNameNumberedParameter;
@@ -977,6 +983,7 @@ public Preferences() {
977983
extractInterfaceReplaceEnabled = false;
978984
telemetryEnabled = false;
979985
validateAllOpenBuffersOnChanges = true;
986+
resourceBundleCompletionEnabled = true;
980987
diagnosticFilter = new ArrayList<>();
981988
searchScope = SearchScope.all;
982989
}
@@ -1139,6 +1146,7 @@ public Preferences clone() {
11391146
prefs.telemetryEnabled = this.telemetryEnabled;
11401147
prefs.validateAllOpenBuffersOnChanges = this.validateAllOpenBuffersOnChanges;
11411148
prefs.chainCompletionEnabled = this.chainCompletionEnabled;
1149+
prefs.resourceBundleCompletionEnabled = this.resourceBundleCompletionEnabled;
11421150
prefs.searchScope = this.searchScope;
11431151

11441152
// Deep copy collections
@@ -1806,6 +1814,11 @@ public static Preferences updateFrom(Preferences existing, Map<String, Object> c
18061814
prefs.setChainCompletionEnabled(chainCompletionEnabled);
18071815
}
18081816

1817+
if (getValue(configuration, RESOURCE_BUNDLE_COMPLETION_KEY) != null) {
1818+
boolean resourceBundleCompletionEnabled = getBoolean(configuration, RESOURCE_BUNDLE_COMPLETION_KEY, existing.resourceBundleCompletionEnabled);
1819+
prefs.setResourceBundleCompletionEnabled(resourceBundleCompletionEnabled);
1820+
}
1821+
18091822
if (getValue(configuration, JAVA_DIAGNOSTIC_FILER) != null) {
18101823
List<String> diagnosticFilter = getList(configuration, JAVA_DIAGNOSTIC_FILER, existing.diagnosticFilter);
18111824
prefs.setDiagnosticFilter(diagnosticFilter);
@@ -2907,6 +2920,14 @@ public boolean isChainCompletionEnabled() {
29072920
return this.chainCompletionEnabled;
29082921
}
29092922

2923+
public void setResourceBundleCompletionEnabled(boolean resourceBundleCompletionEnabled) {
2924+
this.resourceBundleCompletionEnabled = resourceBundleCompletionEnabled;
2925+
}
2926+
2927+
public boolean isResourceBundleCompletionEnabled() {
2928+
return this.resourceBundleCompletionEnabled;
2929+
}
2930+
29102931
/**
29112932
* update the null analysis options of all projects based on the null analysis mode
29122933
* Returns the list of enabled clean ups.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.8"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>
7+
8+
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>resourcebundle</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
18+
19+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.sample;
2+
3+
import java.util.ResourceBundle;
4+
5+
public class ResourceBundleTest {
6+
private ResourceBundle bundle;
7+
8+
public void testResourceBundle() {
9+
bundle = ResourceBundle.getBundle("resources.messages");
10+
String value = bundle.getString("");
11+
}
12+
}
13+
14+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Resource bundle messages
2+
greeting.hello=Hello
3+
greeting.goodbye=Goodbye
4+
error.notfound=Not found
5+
error.unauthorized=Unauthorized access
6+
info.loading=Loading...
7+
info.success=Success
8+
user.name=Name
9+
user.email=Email
10+
user.phone=Phone
11+
app.title=Application Title
12+
app.version=Version
13+
message.multiline=This is a multiline message.\nIt has multiple lines.\nEach line should be displayed separately.
14+
15+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# English resource bundle messages
2+
greeting.hello=Hello
3+
greeting.goodbye=Goodbye
4+
error.notfound=Not found
5+
error.unauthorized=Unauthorized access
6+
7+
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Validation messages
2+
validation.required=This field is required
3+
validation.email=Invalid email address
4+
validation.phone=Invalid phone number
5+
validation.minlength=Minimum length is {0}
6+
validation.maxlength=Maximum length is {0}
7+
8+

0 commit comments

Comments
 (0)