Skip to content

JShell Integration: Improve handling of "var" support #8327

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 2 commits 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
276 changes: 165 additions & 111 deletions ide/lexer/src/org/netbeans/lib/lexer/JoinLexerInputOperation.java

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions ide/lexer/src/org/netbeans/lib/lexer/token/JoinToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -126,4 +126,8 @@ protected String dumpInfoTokenType() {
return "JoiT"; // NOI18N
}

@Override
public String toString() {
return "JoinToken{" + "joinedParts=" + joinedParts + ", completeLength=" + completeLength + ", extraTokenListSpanCount=" + extraTokenListSpanCount + '}';
}
}
5 changes: 5 additions & 0 deletions ide/lexer/src/org/netbeans/lib/lexer/token/PartToken.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,4 +83,9 @@ protected String dumpInfoTokenType() {
return "ParT[" + (partTokenIndex+1) + "/" + joinToken().joinedParts().size() + "]"; // NOI18N
}

@Override
public String toString() {
return dumpInfoTokenType();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ protected Level logLevel() {

public void testRandom() throws Exception {
test(1214497020179L);
// test(1212495582649L);
test(1212495582649L);
test(1740600568165L);
// test(0L);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@
<attr name="inherit.rules" boolvalue="true"/>
<file name="org-netbeans-modules-jshell-support-LocationSuppressRule.instance"/>
<file name="org-netbeans-modules-jshell-support-SemicolonMissingRule.instance"/>
<file name="org-netbeans-modules-jshell-support-VarNotAllowedHereRule.instance"/>
</folder>
</folder>
</folder>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public String createMessage(CompilationInfo info, Diagnostic d, int offset, Tree
off++;
}
final Document doc = info.getSnapshot().getSource().getDocument(false);
if (info.getSnapshot().getOriginalOffset(off) == -1) {
if (info.getSnapshot().getOriginalOffset(off) == -1 || info.getSnapshot().getOriginalOffset(off + 1) == -1) {
// suppress semicolon message for end-of-snippet location.
return ""; // NOI18N
}
Expand All @@ -70,10 +70,10 @@ public String createMessage(CompilationInfo info, Diagnostic d, int offset, Tree
doc.render(new Runnable() {
public void run() {
if (foff < doc.getLength()) {
match[0] = true;
match[0] = true;
} else try {
if (seq.charAt(foff) != doc.getText(foff, 1).charAt(0)) {
match[0] = true;
match[0] = true;
}
} catch (BadLocationException ex) {
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.jshell.support;

import com.sun.source.util.TreePath;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.tools.Diagnostic;
import org.netbeans.api.editor.mimelookup.MimePath;
import org.netbeans.api.java.source.CompilationInfo;
import org.netbeans.api.java.source.SourceUtils;
import org.netbeans.modules.java.hints.friendapi.OverrideErrorMessage;

/**
* Suppresses error warning that var is not allowed here. The parsing of a
* `var x = ...` statement without a trailing semicolon leads to this warning,
* which is invalid.
*/
public class VarNotAllowedHereRule implements OverrideErrorMessage {
private static final Set<String> CODES = new HashSet<>(Arrays.asList("compiler.err.restricted.type.not.allowed.here"));

@Override
public String createMessage(CompilationInfo info, Diagnostic d, int offset, TreePath treePath, Data data) {
if (!info.getSnapshot().getMimeType().equals("text/x-repl")) {
MimePath mp = info.getSnapshot().getMimePath();
if (!"text/x-repl".equals(mp.getMimeType(0))) {
return null;
}
}
Object dp = SourceUtils.getDiagnosticParam(d, 0);
if("var".equals(String.valueOf(dp))) {
return "";
} else {
return null;
}
}

@Override
public Set getCodes() {
return CODES;
}

@Override
public List run(CompilationInfo compilationInfo, String diagnosticKey, int offset, TreePath treePath, Data data) {
return null;
}

@Override
public String getId() {
return getClass().getName();
}

@Override
public String getDisplayName() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

@Override
public void cancel() {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.netbeans.modules.jshell.parsing;

import org.junit.Test;
import org.netbeans.api.lexer.Token;
import org.netbeans.api.lexer.TokenHierarchy;
import org.netbeans.api.lexer.TokenSequence;
import org.netbeans.modules.jshell.model.JShellToken;

public class LexerTest {

@Test
public void testVarLexing() {
TokenHierarchy<?> hi = TokenHierarchy.create("[1]-> var ", JShellToken.language());
enumerateTokenSequence(hi.tokenSequence(), "", false);
}

@Test
public void testVarLexing2() {
TokenHierarchy<?> hi = TokenHierarchy.create("[1]-> var String = \"Hallo Welt\"", JShellToken.language());
enumerateTokenSequence(hi.tokenSequence(), "", false);
}

private void enumerateTokenSequence(TokenSequence<?> ts, String indent, boolean debug) {
ts.moveStart();
while (ts.moveNext()) {
Token t = ts.token();
if (debug) {
System.out.printf("%s%s/%s%n", indent, t.id().primaryCategory(), t.id().name());
}
if (t != null) {
TokenSequence embeddedTs = ts.embedded();
if (embeddedTs != null) {
enumerateTokenSequence(embeddedTs, indent + " ", debug);
}
}
}
}
}
Loading