Skip to content

Fix EL ResourceBundle preview panel #8603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -163,9 +163,9 @@ public boolean isValidKey(String bundle, String key) {
*/
private ResourceBundleInfo getBundleForIdentifier(String ident) {
// XXX - do it more efficiently
for (Map.Entry<String, ResourceBundleInfo> entry : getBundlesMap().entrySet()) {
if (ident.equals(entry.getValue().getVarName())) {
return entry.getValue();
for (ResourceBundleInfo rbi : getBundlesMap().values()) {
if (ident.equals(rbi.getVarName())) {
return rbi;
}
}
return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@
import org.netbeans.api.java.source.JavaSource;
import org.netbeans.api.java.source.Task;
import org.netbeans.modules.csl.api.CodeCompletionContext;
import org.netbeans.modules.csl.api.CodeCompletionHandler;
import org.netbeans.modules.csl.api.CodeCompletionHandler2;
import org.netbeans.modules.csl.api.CodeCompletionResult;
import org.netbeans.modules.csl.api.CompletionProposal;
Expand All @@ -72,6 +71,7 @@
import org.netbeans.modules.web.el.ELVariableResolvers;
import org.netbeans.modules.web.el.NodeUtil;
import org.netbeans.modules.web.el.ResourceBundles;
import org.netbeans.modules.web.el.ResourceBundles.Location;
import org.netbeans.modules.web.el.refactoring.RefactoringUtil;
import org.netbeans.modules.web.el.spi.ELPlugin;
import org.netbeans.modules.web.el.spi.ELVariableResolver.VariableInfo;
Expand Down Expand Up @@ -603,11 +603,16 @@ private void proposeBundleKeysInArrayNotation(CodeCompletionContext context,
if (!resourceBundles.canHaveBundles()) {
return;
}
FileObject bundleFile = null;
List<Location> bundleLocations = resourceBundles.getLocationsForBundleIdent(bundleKey);
if (!bundleLocations.isEmpty()) {
bundleFile = bundleLocations.get(0).getFile();
}
for (Map.Entry<String, String> entry : resourceBundles.getEntries(bundleKey).entrySet()) {
if (!prefix.matches(entry.getKey())) {
continue;
}
ELResourceBundleKeyCompletionItem item = new ELResourceBundleKeyCompletionItem(entry.getKey(), entry.getValue(), elElement);
ELResourceBundleKeyCompletionItem item = new ELResourceBundleKeyCompletionItem(entry.getKey(), entry.getValue(), elElement, bundleFile);
item.setSmart(true);
item.setAnchorOffset(context.getCaretOffset() - prefix.length());
proposals.add(item);
Expand All @@ -626,11 +631,16 @@ private void proposeBundleKeysInDotNotation(CodeCompletionContext context,
if (!resourceBundles.canHaveBundles()) {
return;
}
FileObject bundleFile = null;
List<Location> bundleLocations = resourceBundles.getLocationsForBundleIdent(bundleKey);
if (!bundleLocations.isEmpty()) {
bundleFile = bundleLocations.get(0).getFile();
}
for (Map.Entry<String, String> entry : resourceBundles.getEntries(bundleKey).entrySet()) {
if (!prefix.matches(entry.getKey())) {
continue;
}
ELResourceBundleKeyCompletionItem item = new ELResourceBundleKeyCompletionItem(entry.getKey(), entry.getValue(), elElement);
ELResourceBundleKeyCompletionItem item = new ELResourceBundleKeyCompletionItem(entry.getKey(), entry.getValue(), elElement, bundleFile);
item.setSmart(true);
item.setAnchorOffset(context.getCaretOffset() - prefix.length());
proposals.add(item);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.netbeans.modules.web.el.completion;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
Expand Down Expand Up @@ -120,11 +121,10 @@ Documentation document(ParserResult info, Callable<Boolean> cancel) {

Map<String, String> entries = bundles.getEntries(bundle.getVar());
for (Map.Entry<String, String> entry : entries.entrySet()) {
String value = entries.get(entry.getValue());
buf.append(entry.getKey());
buf.append('='); //NOI18N
buf.append("<font color='#ce7b00'>"); //NOI18N
buf.append(value);
buf.append(entry.getValue());
buf.append("</font>"); //NOI18N
buf.append("<br>"); //NOI18N
}
Expand All @@ -133,6 +133,10 @@ Documentation document(ParserResult info, Callable<Boolean> cancel) {

@Override
public FileObject getFileObject() {
List<FileObject> files = bundle.getFiles();
if (!files.isEmpty()) {
return files.get(0);
}
return file;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,13 @@ final class ELResourceBundleKeyCompletionItem extends DefaultCompletionProposal
private final String key;
private final String value;
private final ELElement element;
private final FileObject bundleFile;

public ELResourceBundleKeyCompletionItem(String key, String value, ELElement element) {
public ELResourceBundleKeyCompletionItem(String key, String value, ELElement element, FileObject bundleFile) {
this.key = key;
this.value = value;
this.element = element;
this.bundleFile = bundleFile;
}

@Override
Expand Down Expand Up @@ -92,6 +94,9 @@ Documentation document(ParserResult info, Callable<Boolean> cancel) {

@Override
public FileObject getFileObject() {
if (bundleFile != null) {
return bundleFile;
}
return element.getSnapshot().getSource().getFileObject();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@
import org.netbeans.api.project.SourceGroup;
import org.netbeans.modules.j2ee.core.api.support.SourceGroups;
import org.netbeans.modules.j2ee.metadata.model.api.MetadataModel;
import org.netbeans.modules.j2ee.metadata.model.api.MetadataModelAction;
import org.netbeans.modules.j2ee.metadata.model.api.MetadataModelException;
import org.netbeans.modules.web.el.spi.ResourceBundle;
import org.netbeans.modules.web.jsf.JSFUtils;
import org.netbeans.modules.web.jsf.api.facesmodel.Application;
Expand All @@ -53,41 +51,35 @@ public static List<ResourceBundle> getResourceBundles(final Project project) {
return Collections.emptyList();
}
try {
return model.runReadAction(new MetadataModelAction<JsfModel, List<ResourceBundle>>() {

@Override
public List<ResourceBundle> run(JsfModel metadata) throws Exception {
List<Application> applications = metadata.getElements(Application.class);
List<ResourceBundle> result = new ArrayList<>();
for (Application application : applications) {
for (org.netbeans.modules.web.jsf.api.facesmodel.ResourceBundle bundle : application.getResourceBundles()) {
if (bundle.getBaseName() == null) {
continue;
}
List<FileObject> files = new ArrayList<>();
// java source source groups
for (SourceGroup sourceGroup : SourceGroups.getJavaSourceGroups(project)) {
FileObject bundleFile = getBundleFileInSourceGroup(sourceGroup, bundle);
if (bundleFile != null) {
files.add(bundleFile);
}
return model.runReadAction(metadata -> {
List<Application> applications = metadata.getElements(Application.class);
List<ResourceBundle> result = new ArrayList<>();
for (Application application : applications) {
for (org.netbeans.modules.web.jsf.api.facesmodel.ResourceBundle bundle : application.getResourceBundles()) {
if (bundle.getBaseName() == null) {
continue;
}
List<FileObject> files = new ArrayList<>();
// java source source groups
for (SourceGroup sourceGroup : SourceGroups.getJavaSourceGroups(project)) {
FileObject bundleFile = getBundleFileInSourceGroup(sourceGroup, bundle);
if (bundleFile != null) {
files.add(bundleFile);
}
// resource source groups
for (SourceGroup sourceGroup : ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_RESOURCES)) {
FileObject bundleFile = getBundleFileInSourceGroup(sourceGroup, bundle);
if (bundleFile != null) {
files.add(bundleFile);
}
}
// resource source groups
for (SourceGroup sourceGroup : ProjectUtils.getSources(project).getSourceGroups(JavaProjectConstants.SOURCES_TYPE_RESOURCES)) {
FileObject bundleFile = getBundleFileInSourceGroup(sourceGroup, bundle);
if (bundleFile != null) {
files.add(bundleFile);
}

result.add(new ResourceBundle(bundle.getBaseName(), bundle.getVar(), files));
}

result.add(new ResourceBundle(bundle.getBaseName(), bundle.getVar(), files));
}
return result;
}
return result;
});
} catch (MetadataModelException ex) {
LOGGER.log(Level.INFO, "Failed to read resource bundles for " + project, ex);
} catch (IOException | IllegalStateException ex) {
LOGGER.log(Level.INFO, "Failed to read resource bundles for " + project, ex);
}
Expand All @@ -96,13 +88,12 @@ public List<ResourceBundle> run(JsfModel metadata) throws Exception {

private static FileObject getBundleFileInSourceGroup(SourceGroup sourceGroup, org.netbeans.modules.web.jsf.api.facesmodel.ResourceBundle bundle) {
int lastDelim = bundle.getBaseName().lastIndexOf("/"); //NOI18N
String bundleName = bundle.getBaseName().substring(lastDelim + 1);
if (lastDelim <= 0) {
// in root folder or default package
String bundleName = bundle.getBaseName().substring(1);
return getBundleInFolder(sourceGroup.getRootFolder(), bundleName);
} else {
// in the subfolder or non-default package
String bundleName = bundle.getBaseName().substring(lastDelim + 1);
String parentFolder = bundle.getBaseName().replace(".", "/").substring(0, lastDelim); //NOI18N
return getBundleInFolder(sourceGroup.getRootFolder().getFileObject(parentFolder), bundleName);
}
Expand Down
Loading