Skip to content

JSON: Provide semantic analyser to improve rendering #8528

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

Merged
Merged
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 @@ -403,7 +403,7 @@ protected FileObject getTestFile(String relFilePath) {
if (!wholeInputFile.exists()) {
NbTestCase.fail("File " + wholeInputFile + " not found.");
}
FileObject fo = FileUtil.toFileObject(wholeInputFile);
FileObject fo = FileUtil.toFileObject(FileUtil.normalizeFile(wholeInputFile));
assertNotNull(fo);

return fo;
Expand Down
2 changes: 1 addition & 1 deletion webcommon/javascript2.editor/nbproject/project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.

javac.release=11
javac.release=17
javac.compilerargs=-Xlint -Xlint:-serial
javadoc.arch=${basedir}/arch.xml
nbm.needs.restart=true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ protected static boolean isPropertyNameContext(TokenSequence<JsTokenId> ts) {
return false;
}

private static boolean acceptTokenChains(TokenSequence tokenSequence, List<Object[]> tokenIdChains, boolean movePrevious) {
private static boolean acceptTokenChains(TokenSequence<JsTokenId> tokenSequence, List<Object[]> tokenIdChains, boolean movePrevious) {
for (Object[] tokenIDChain : tokenIdChains){
if (acceptTokenChain(tokenSequence, tokenIDChain, movePrevious)){
return true;
Expand All @@ -314,7 +314,7 @@ private static boolean acceptTokenChains(TokenSequence tokenSequence, List<Objec
return false;
}

private static boolean acceptTokenChain(TokenSequence tokenSequence, Object[] tokenIdChain, boolean movePrevious) {
private static boolean acceptTokenChain(TokenSequence<JsTokenId> tokenSequence, Object[] tokenIdChain, boolean movePrevious) {
int orgTokenSequencePos = tokenSequence.offset();
boolean accept = true;
boolean moreTokens = movePrevious ? tokenSequence.movePrevious() : true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ public ElementKind getKind() {

@Override
public Set<Modifier> getModifiers() {
return Collections.EMPTY_SET;
return Collections.emptySet();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,7 @@ protected void formatName(HtmlFormatter formatter) {
@Override
public String getRhsHtml(HtmlFormatter formatter) {
String location = null;
if (element instanceof JsElement) {
JsElement jsElement = (JsElement) element;
if (element instanceof JsElement jsElement) {
if (jsElement.isPlatform()) {
location = Bundle.JsCompletionItem_lbl_js_platform();
} else if (jsElement.getSourceLabel() != null) {
Expand Down Expand Up @@ -184,7 +183,7 @@ public Set<Modifier> getModifiers() {
Set<Modifier> modifiers;

if (getElement() == null || getElement().getModifiers().isEmpty()) {
modifiers = Collections.EMPTY_SET;
modifiers = Collections.emptySet();
} else {
modifiers = EnumSet.noneOf(Modifier.class);
modifiers.addAll(getElement().getModifiers());
Expand All @@ -206,8 +205,8 @@ public boolean isSmart() {
@Override
public int getSortPrioOverride() {
int order = 100;
if (element instanceof JsElement) {
if (((JsElement)element).isPlatform()) {
if (element instanceof JsElement jsElement) {
if (jsElement.isPlatform()) {
if (ModelUtils.PROTOTYPE.equals(element.getName())) { //NOI18N
order = 1;
} else {
Expand Down Expand Up @@ -261,7 +260,7 @@ public static class JsFunctionCompletionItem extends JsCompletionItem {
private final Map<String, Set<String>> parametersTypes;
JsFunctionCompletionItem(ElementHandle element, CompletionRequest request, Set<String> resolvedReturnTypes, Map<String, Set<String>> parametersTypes) {
super(element, request);
this.returnTypes = resolvedReturnTypes != null ? resolvedReturnTypes : Collections.EMPTY_SET;
this.returnTypes = resolvedReturnTypes != null ? resolvedReturnTypes : Collections.emptySet();
this.parametersTypes = parametersTypes != null ? parametersTypes : Collections.<String, Set<String>>emptyMap();
}

Expand Down Expand Up @@ -370,8 +369,8 @@ private boolean asObject() {
boolean result = false;
char firstChar = getName().charAt(0);
JsElement.Kind jsKind = null;
if (element instanceof JsElement) {
jsKind = ((JsElement)element).getJSKind();
if (element instanceof JsElement jsElement) {
jsKind = jsElement.getJSKind();
}
if ((jsKind != null && jsKind == JsElement.Kind.CONSTRUCTOR) || Character.isUpperCase(firstChar)) {
boolean isAfterNew = isAfterNewKeyword();
Expand Down Expand Up @@ -429,7 +428,7 @@ public ImageIcon getIcon() {

public static class JsCallbackCompletionItem extends JsCompletionItem {
private static ImageIcon callbackIcon = null;
private IndexedElement.FunctionIndexedElement function;
private final IndexedElement.FunctionIndexedElement function;

public JsCallbackCompletionItem(IndexedElement.FunctionIndexedElement element, CompletionRequest request) {
super(element, request);
Expand Down Expand Up @@ -592,40 +591,40 @@ public String getCustomInsertTemplate() {
}

switch(type) {
case SIMPLE:
case SIMPLE -> {
builder.append(getName());
break;
case ENDS_WITH_SPACE:
}
case ENDS_WITH_SPACE -> {
builder.append(getName());
builder.append(" ${cursor}"); //NOI18N
break;
case CURSOR_INSIDE_BRACKETS:
}
case CURSOR_INSIDE_BRACKETS -> {
builder.append(getName());
builder.append("(${cursor})"); //NOI18N
break;
case ENDS_WITH_CURLY_BRACKETS:
}
case ENDS_WITH_CURLY_BRACKETS -> {
builder.append(getName());
builder.append(" {${cursor}}"); //NOI18N
break;
case ENDS_WITH_SEMICOLON:
}
case ENDS_WITH_SEMICOLON -> {
builder.append(getName());
CharSequence text = request.info.getSnapshot().getText();
int index = request.anchor + request.prefix.length();
if (index == text.length() || ';' != text.charAt(index)) { //NOI18N
builder.append(";"); //NOI18N
}
break;
case ENDS_WITH_COLON:
}
case ENDS_WITH_COLON -> {
builder.append(getName());
builder.append(" ${cursor}:"); //NOI18N
break;
case ENDS_WITH_DOT:
}
case ENDS_WITH_DOT -> {
builder.append(getName());
builder.append(".${cursor}"); //NOI18N
break;
default:
}
default -> {
assert false : type.toString();
break;
}
}
return builder.toString();
}
Expand All @@ -642,7 +641,7 @@ public static class JsPropertyCompletionItem extends JsCompletionItem {

JsPropertyCompletionItem(ElementHandle element, CompletionRequest request, Set<String> resolvedTypes) {
super(element, request);
this.resolvedTypes = resolvedTypes != null ? resolvedTypes : Collections.EMPTY_SET;
this.resolvedTypes = resolvedTypes != null ? resolvedTypes : Collections.emptySet();
}

@Override
Expand Down Expand Up @@ -696,11 +695,7 @@ public static void create( Map<String, List<JsElement>> items, CompletionRequest
return;
}
switch (element.getJSKind()) {
case CONSTRUCTOR:
case FUNCTION:
case METHOD:
case GENERATOR:
case ARROW_FUNCTION:
case CONSTRUCTOR, FUNCTION, METHOD, GENERATOR, ARROW_FUNCTION -> {
Set<String> returnTypes = new HashSet<>();
HashMap<String, Set<String>> allParameters = new LinkedHashMap<>();
if (element instanceof JsFunction) {
Expand All @@ -726,18 +721,18 @@ public static void create( Map<String, List<JsElement>> items, CompletionRequest
}
allParameters.put(jsObject.getName(), paramTypes);
}
} else if (element instanceof IndexedElement.FunctionIndexedElement) {
} else if (element instanceof IndexedElement.FunctionIndexedElement functionIndexedElement) {
// count return types
HashSet<TypeUsage> returnTypeUsages = new HashSet<>();
for (String type : ((IndexedElement.FunctionIndexedElement) element).getReturnTypes()) {
for (String type : functionIndexedElement.getReturnTypes()) {
returnTypeUsages.add(new TypeUsage(type, -1, false));
}
Collection<TypeUsage> resolveTypes = ModelUtils.resolveTypes(returnTypeUsages,
Model.getModel(request.info, false),
jsIndex, false);
returnTypes.addAll(Utils.getDisplayNames(resolveTypes));
// count parameters type
LinkedHashMap<String, Collection<String>> parameters = ((IndexedElement.FunctionIndexedElement) element).getParameters();
LinkedHashMap<String, Collection<String>> parameters = functionIndexedElement.getParameters();
for (Map.Entry<String, Collection<String>> paramEntry : parameters.entrySet()) {
Set<String> paramTypes = new HashSet<>();
for (String type : paramEntry.getValue()) {
Expand All @@ -763,20 +758,13 @@ public static void create( Map<String, List<JsElement>> items, CompletionRequest
: new JsGeneratorCompletionItem(element, request, returnTypes, allParameters);
signatures.put(signature, item);
}
break;
case PARAMETER:
case PROPERTY:
case PROPERTY_GETTER:
case PROPERTY_SETTER:
case FIELD:
case VARIABLE:
}
case PARAMETER, PROPERTY, PROPERTY_GETTER, PROPERTY_SETTER, FIELD, VARIABLE -> {
Set<String> typesToDisplay = new HashSet<>();
Collection<? extends TypeUsage> assignment = null;
if (element instanceof JsObject) {
JsObject jsObject = (JsObject) element;
if (element instanceof JsObject jsObject) {
assignment = jsObject.getAssignments();
} else if (element instanceof IndexedElement) {
IndexedElement iElement = (IndexedElement) element;
} else if (element instanceof IndexedElement iElement) {
assignment = iElement.getAssignments();
}
if (assignment != null && !assignment.isEmpty()) {
Expand Down Expand Up @@ -808,19 +796,20 @@ public static void create( Map<String, List<JsElement>> items, CompletionRequest
}
}
// signatures
signature = element.getName() + ":" + createTypeSignature(typesToDisplay);
String signature = element.getName() + ":" + createTypeSignature(typesToDisplay);
if (!signatures.containsKey(signature)) {
// add the item to the cc only if doesn't exist any similar
JsCompletionItem item = new JsPropertyCompletionItem(element, request, typesToDisplay);
signatures.put(signature, item);
}
break;
default:
signature = element.getName();
}
default -> {
String signature = element.getName();
if (!signatures.containsKey(signature)) {
JsCompletionItem item = new JsCompletionItem(element, request);
signatures.put(signature, item);
}
}
}
}
for (JsCompletionItem item: signatures.values()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,15 @@ public boolean isRenameAllowed(ParserResult info, int caretOffset, String[] expl

@Override
public Set<OffsetRange> getRenameRegions(ParserResult info, int caretOffset) {
if (info instanceof JsParserResult) {
JsParserResult pResult = (JsParserResult)info;
if (info instanceof JsParserResult pResult) {
Set<OffsetRange> findOccurrenceRanges = OccurrencesFinderImpl.findOccurrenceRanges(pResult, info.getSnapshot().getEmbeddedOffset(caretOffset));
HashSet<OffsetRange> sourceRanges = new HashSet<>(findOccurrenceRanges.size());
for (OffsetRange range : findOccurrenceRanges) {
sourceRanges.add(LexUtilities.getLexerOffsets(pResult, range));
}
return sourceRanges;
} else {
return Collections.EMPTY_SET;
return Collections.emptySet();
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
Expand All @@ -55,52 +56,56 @@ public JsKeyStrokeHandler() {
}

@Override
@SuppressWarnings("deprecation")
public boolean beforeCharInserted(Document doc, int caretOffset, JTextComponent target, char ch) throws BadLocationException {
return false;
}

@Override
@SuppressWarnings("deprecation")
public boolean afterCharInserted(Document doc, int caretOffset, JTextComponent target, char ch) throws BadLocationException {
return false;
}

@Override
@SuppressWarnings("deprecation")
public boolean charBackspaced(Document doc, int caretOffset, JTextComponent target, char ch) throws BadLocationException {
return false;
}

@Override
@SuppressWarnings("deprecation")
public int beforeBreak(Document doc, int caretOffset, JTextComponent target) throws BadLocationException {
return -1;
}

@Override
@SuppressWarnings("deprecation")
public OffsetRange findMatching(Document doc, int caretOffset) {
return OffsetRange.NONE;
}

@Override
public List<OffsetRange> findLogicalRanges(final ParserResult info, final int caretOffset) {
final Set<OffsetRange> ranges = new LinkedHashSet<>();
if (info instanceof JsParserResult) {
final JsParserResult jsParserResult = (JsParserResult) info;
if (info instanceof JsParserResult jsParserResult) {
FunctionNode root = jsParserResult.getRoot();
final TokenSequence<? extends JsTokenId> ts = LexUtilities.getJsTokenSequence(jsParserResult.getSnapshot(), caretOffset);
if (root != null && ts != null) {

root.accept(new NodeVisitor(new LexicalContext()) {
root.accept(new NodeVisitor<LexicalContext>(new LexicalContext()) {

private OffsetRange getOffsetRange(IdentNode node) {
// because the truffle parser doesn't set correctly the finish offset, when there are comments after the indent node
return new OffsetRange(node.getStart(), node.getStart() + node.getName().length());
}

private OffsetRange getOffsetRange(Node node) {
if (node instanceof FunctionNode) {
return getOffsetRange((FunctionNode) node);
}
if (node instanceof IdentNode) {
return getOffsetRange((IdentNode)node);
Objects.requireNonNull(node);
if (node instanceof FunctionNode functionNode) {
return getOffsetRange(functionNode);
} else if (node instanceof IdentNode identNode) {
return getOffsetRange(identNode);
}
return new OffsetRange(node.getStart(), node.getFinish());
}
Expand Down Expand Up @@ -182,7 +187,7 @@ public boolean enterVarNode(VarNode node) {
}

@Override
public boolean enterLiteralNode(LiteralNode node) {
public boolean enterLiteralNode(LiteralNode<?> node) {
if (node.isString() && node.getStart() <= caretOffset && caretOffset <= node.getFinish()) {
// include the " or '
ranges.add(new OffsetRange(node.getStart() - 1, node.getFinish() + 1));
Expand Down Expand Up @@ -239,6 +244,7 @@ public boolean enterCallNode(CallNode node) {
}

@Override
@SuppressWarnings("deprecation")
public int getNextWordOffset(Document doc, int caretOffset, boolean reverse) {
return -1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package org.netbeans.modules.javascript2.editor;

import org.netbeans.api.lexer.Language;
import org.netbeans.core.spi.multiview.MultiViewElement;
import org.netbeans.core.spi.multiview.text.MultiViewEditorElement;
import org.netbeans.modules.csl.api.*;
Expand Down Expand Up @@ -69,7 +70,7 @@ public JsLanguage() {
}

@Override
public org.netbeans.api.lexer.Language getLexerLanguage() {
public Language<JsTokenId> getLexerLanguage() {
// has to be done here since JS hasn't its own project, also see issue #165915
// It was moved here from the JsLanguage initialization since the the language is called much earlier than the
// JavaScipt is really needed. Calling it in the #getLexerLanguage() should ensure to be the CP registration
Expand All @@ -92,6 +93,7 @@ public Parser getParser() {
}

@Override
@SuppressWarnings("deprecation")
public boolean hasStructureScanner() {
return true;
}
Expand Down
Loading
Loading