Skip to content

Commit e016f70

Browse files
Merge pull request #8550 from NicolaIsotta/jsf-ac
improve jsf class autocomplete
2 parents f792fc4 + b600e0f commit e016f70

File tree

2 files changed

+41
-35
lines changed

2 files changed

+41
-35
lines changed

enterprise/web.jsf.editor/nbproject/project.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
# under the License.
1717

1818

19-
javac.source=1.8
19+
javac.release=17
2020
javac.compilerargs=-Xlint -Xlint:-serial
2121

2222
test-unit-sys-prop.web.project.jars=\

enterprise/web.jsf.editor/src/org/netbeans/modules/web/jsf/editor/HtmlSourceTask.java

Lines changed: 40 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,15 @@
1818
*/
1919
package org.netbeans.modules.web.jsf.editor;
2020

21-
import java.util.Arrays;
2221
import java.util.Collection;
2322
import java.util.Collections;
23+
import java.util.EnumSet;
2424
import java.util.HashMap;
25+
import java.util.List;
2526
import java.util.Map;
27+
2628
import javax.swing.text.Document;
29+
2730
import org.netbeans.api.html.lexer.HTMLTokenId;
2831
import org.netbeans.api.lexer.InputAttributes;
2932
import org.netbeans.modules.html.editor.api.gsf.HtmlParserResult;
@@ -34,20 +37,25 @@
3437
import org.netbeans.modules.parsing.spi.SchedulerTask;
3538
import org.netbeans.modules.parsing.spi.ParserResultTask;
3639
import org.netbeans.modules.parsing.spi.TaskFactory;
40+
import org.netbeans.modules.web.jsfapi.api.Attribute;
3741
import org.netbeans.modules.web.jsfapi.api.DefaultLibraryInfo;
3842
import org.netbeans.modules.web.jsfapi.api.Library;
3943
import org.netbeans.modules.web.jsfapi.api.LibraryComponent;
40-
import org.netbeans.modules.web.jsfapi.api.NamespaceUtils;
44+
import org.netbeans.modules.web.jsfapi.api.LibraryInfo;
4145
import org.netbeans.modules.web.jsfapi.api.Tag;
4246

4347
/**
44-
*
48+
*
4549
* @author Marek Fukala
4650
*/
4751
public final class HtmlSourceTask extends ParserResultTask<HtmlParserResult> {
4852

4953
private static final String CSS_CLASS_MAP_PROPERTY_KEY = "cssClassTagAttrMap"; //semi api - defined in HtmlLexer
50-
private static final String STYLE_CLASS_ATTR_NAME = "styleClass"; //NOI18N
54+
private static final String CLASS = "Class"; //NOI18N
55+
private static final String CLASSES = "Classes"; //NOI18N
56+
private static final EnumSet<DefaultLibraryInfo> LIBRARIES_TO_SKIP = EnumSet.of(DefaultLibraryInfo.FACELETS, DefaultLibraryInfo.JSF,
57+
DefaultLibraryInfo.COMPOSITE, DefaultLibraryInfo.JSF_CORE, DefaultLibraryInfo.JSTL_CORE, DefaultLibraryInfo.JSTL_CORE_FUNCTIONS,
58+
DefaultLibraryInfo.PASSTHROUGH);
5159

5260
public static class Factory extends TaskFactory {
5361

@@ -105,45 +113,43 @@ public void run(HtmlParserResult result, SchedulerEvent event) {
105113
}
106114

107115
//enable css class embedding in default facelets libraries tags
108-
//TODO this should be done in some more generic way but so far I haven't
109-
//found a way how to get an info if a tag's attribute represents css class or not.
110-
//It seems that almost only html library contains such tags, we should
111-
//probably create some metadata also for third party libraries
112-
113-
//check if the default html library is defined
114-
String prefix = NamespaceUtils.getForNs(result.getNamespaces(), DefaultLibraryInfo.HTML.getNamespace());
115-
if (prefix != null) {
116-
//html lib declared, lets build a map of tags containing attributes whose values are
117-
//supposed to represent a css class. The map is then put into the document's
118-
//input attributes and then html lexer takes this information into account
119-
//when lexing the html code
120-
Map<String, Collection<String>> cssClassTagAttrMap = new HashMap<>();
121-
Library lib = sup.getLibrary(DefaultLibraryInfo.HTML.getNamespace());
116+
Map<String, Collection<String>> cssClassTagAttrMap = new HashMap<>();
117+
118+
//lets build a map of tags containing attributes whose values are
119+
//supposed to represent a css class. The map is then put into the document's
120+
//input attributes and then html lexer takes this information into account
121+
//when lexing the html code
122+
for (Map.Entry<String, String> entry : result.getNamespaces().entrySet()) {
123+
String prefix = entry.getValue();
124+
if (prefix == null) {
125+
continue;
126+
}
127+
String namespace = entry.getKey();
128+
LibraryInfo libraryInfo = DefaultLibraryInfo.forNamespace(namespace);
129+
if (libraryInfo instanceof DefaultLibraryInfo dli && LIBRARIES_TO_SKIP.contains(dli)) {
130+
continue;
131+
}
132+
133+
Library lib = sup.getLibrary(namespace);
122134
if (lib != null) {
123135
Collection<? extends LibraryComponent> components = lib.getComponents();
124136
for (LibraryComponent comp : components) {
125137
Tag tag = comp.getTag();
126-
//hacking datatable's attributes embedding - waiting for Tomasz' tag metadata API
127-
if ("dataTable".equals(tag.getName())) { //NOI18N
128-
cssClassTagAttrMap.put(prefix + ":" + tag.getName(),
129-
Arrays.asList(new String[]{STYLE_CLASS_ATTR_NAME,
130-
"headerClass", "footerClass", "rowClasses", "columnClasses", "captionClass"})); //NOI18N
131-
} else {
132-
if (tag.getAttribute(STYLE_CLASS_ATTR_NAME) != null) {
133-
cssClassTagAttrMap.put(prefix + ":" + tag.getName(), Collections.singletonList(STYLE_CLASS_ATTR_NAME));
134-
}
138+
if (tag == null) {
139+
continue;
140+
}
141+
List<String> cssClassAttributes = tag.getAttributes().stream()
142+
.map(Attribute::getName)
143+
.filter(name -> name.endsWith(CLASS) || name.endsWith(CLASSES))
144+
.toList();
145+
if (!cssClassAttributes.isEmpty()) {
146+
cssClassTagAttrMap.put(prefix + ":" + tag.getName(), cssClassAttributes);
135147
}
136148
}
137149
}
138-
139-
inputAttributes.setValue(HTMLTokenId.language(), CSS_CLASS_MAP_PROPERTY_KEY, cssClassTagAttrMap, true);
140-
141-
} else {
142-
//remove the map, the html library is not declared (anymore)
143-
inputAttributes.setValue(HTMLTokenId.language(), CSS_CLASS_MAP_PROPERTY_KEY, null, true);
144150
}
145151

152+
inputAttributes.setValue(HTMLTokenId.language(), CSS_CLASS_MAP_PROPERTY_KEY, cssClassTagAttrMap, true);
146153

147154
}
148155
}
149-

0 commit comments

Comments
 (0)