Skip to content

Commit

Permalink
Merge pull request apache#849 from arusinha/netbeans-862
Browse files Browse the repository at this point in the history
[NETBEANS-862] Added auto-complete support for var keyword in JDK-11 …
  • Loading branch information
rtaneja1 authored Sep 17, 2018
2 parents 29e67db + 891a74d commit af05e17
Show file tree
Hide file tree
Showing 19 changed files with 1,242 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -233,17 +233,24 @@ public static enum Options {
};

private static final SourceVersion SOURCE_VERSION_RELEASE_10;
private static final SourceVersion SOURCE_VERSION_RELEASE_11;

static {
SourceVersion r10;
SourceVersion r10, r11;

try {
r10 = SourceVersion.valueOf("RELEASE_10");
} catch (IllegalArgumentException ex) {
r10 = null;
}
try {
r11 = SourceVersion.valueOf("RELEASE_11");
} catch (IllegalArgumentException ex) {
r11 = null;
}

SOURCE_VERSION_RELEASE_10 = r10;
SOURCE_VERSION_RELEASE_11 = r11;
}

private final ItemFactory<T> itemFactory;
Expand Down Expand Up @@ -886,6 +893,16 @@ private void insideVariable(Env env) throws IOException {
addTypes(env, EnumSet.of(CLASS, INTERFACE, TYPE_PARAMETER), controller.getTypes().getDeclaredType(te));
}
} else {
if (path.getParentPath().getLeaf().getKind() == Tree.Kind.LAMBDA_EXPRESSION) {
LambdaExpressionTree let = (LambdaExpressionTree) path.getParentPath().getLeaf();

if (let.getParameters().size() == 1) {
addVarTypeForLambdaParam(env);
} else if (isLambdaVarType(env, let)) {
addVarTypeForLambdaParam(env);
return;
}
}
boolean isLocal = !TreeUtilities.CLASS_TREE_KINDS.contains(parent.getKind());
addMemberModifiers(env, var.getModifiers().getFlags(), isLocal);
addTypes(env, EnumSet.of(CLASS, INTERFACE, ENUM, ANNOTATION_TYPE, TYPE_PARAMETER), null);
Expand Down Expand Up @@ -1904,6 +1921,13 @@ private void insideLambdaExpression(Env env) throws IOException {
addPrimitiveTypeKeywords(env);
addKeyword(env, FINAL_KEYWORD, SPACE, false);
}
else {
boolean isFirstParamVarType = isLambdaVarType(env, let);

if (isFirstParamVarType) {
addVarTypeForLambdaParam(env);
}
}
break;
}
}
Expand Down Expand Up @@ -2350,6 +2374,12 @@ private void insideParens(Env env) throws IOException {
}
env.addToExcludes(elem);
}
DeclaredType type = (DeclaredType) smart;
TypeElement element = (TypeElement) type.asElement();

if (elements.isFunctionalInterface(element)) {
addVarTypeForLambdaParam(env);
}
} else if (smart.getKind() == TypeKind.ARRAY) {
try {
TypeMirror tm = smart;
Expand Down Expand Up @@ -5869,4 +5899,43 @@ private boolean withinBounds(Env env, TypeMirror type, List<? extends TypeMirror
}
return true;
}

private void addVarTypeForLambdaParam(final Env env) throws IOException {
if (SOURCE_VERSION_RELEASE_11 == null || env.getController().getSourceVersion().compareTo(SOURCE_VERSION_RELEASE_11) < 0) {
return;
}
results.add(itemFactory.createKeywordItem(VAR_KEYWORD, SPACE, anchorOffset, false));
}

/**
*
* @param env : env
* @param tree : Lambda expression tree
* @return true if first param of lambda expr is of 'var' type
*/
private boolean isLambdaVarType(Env env, Tree tree) {

if (tree.getKind() != Tree.Kind.LAMBDA_EXPRESSION) {
return false;
}
LambdaExpressionTree let = (LambdaExpressionTree) tree;
if (let.getParameters().isEmpty()) {
return false;
}

boolean isFirstParamVarType = false;

VariableTree firstParamTree = let.getParameters().get(0);
int firstParamStartPos = (int) env.getSourcePositions().getStartPosition(env.getRoot(), firstParamTree);
TokenSequence<JavaTokenId> ts = findLastNonWhitespaceToken(env, let, env.getOffset());
ts.move(firstParamStartPos);
ts.movePrevious();

//TreeUtilities.isVarType() API can't be used as FirstParamTree might not be complete.
while (ts.token().id() != JavaTokenId.COMMA && !isFirstParamVarType && ts.moveNext()) {
isFirstParamVarType = ts.token().id() == JavaTokenId.VAR;
}
return isFirstParamVarType;

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
boolean
byte
char
double
final
float
int
long
short
AbstractMethodError
Appendable
ArithmeticException
ArrayIndexOutOfBoundsException
ArrayStoreException
AssertionError
AutoCloseable
Boolean
BootstrapMethodError
Byte
CharSequence
Character
Class
ClassCastException
ClassCircularityError
ClassFormatError
ClassLoader
ClassNotFoundException
ClassValue
CloneNotSupportedException
Cloneable
Comparable
Compiler
Deprecated
Double
Enum
EnumConstantNotPresentException
Error
Exception
ExceptionInInitializerError
Float
Foo
FunctionalInterface
IllegalAccessError
IllegalAccessException
IllegalArgumentException
IllegalMonitorStateException
IllegalStateException
IllegalThreadStateException
IncompatibleClassChangeError
IndexOutOfBoundsException
InheritableThreadLocal
InstantiationError
InstantiationException
Integer
InternalError
InterruptedException
Iterable
LinkageError
Long
Math
NegativeArraySizeException
NoClassDefFoundError
NoSuchFieldError
NoSuchFieldException
NoSuchMethodError
NoSuchMethodException
NullPointerException
Number
NumberFormatException
Object
OutOfMemoryError
Override
Package
Process
ProcessBuilder
Readable
ReflectiveOperationException
Runnable
Runtime
RuntimeException
RuntimePermission
SafeVarargs
SecurityException
SecurityManager
Short
StackOverflowError
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
StringIndexOutOfBoundsException
SuppressWarnings
System
Test
TestAnnotation
Thread
ThreadDeath
ThreadGroup
ThreadLocal
Throwable
TypeNotPresentException
UnknownError
UnsatisfiedLinkError
UnsupportedClassVersionError
UnsupportedOperationException
VerifyError
VirtualMachineError
Void
java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
Foo
String[] args
Test t
public static void main(String[] args)
boolean
byte
char
double
false
float
int
long
new
null
short
true
var
AbstractMethodError
Appendable
ArithmeticException
ArrayIndexOutOfBoundsException
ArrayStoreException
AssertionError
AutoCloseable
Boolean
BootstrapMethodError
Byte
CharSequence
Character
Class
ClassCastException
ClassCircularityError
ClassFormatError
ClassLoader
ClassNotFoundException
ClassValue
CloneNotSupportedException
Cloneable
Comparable
Compiler
Deprecated
Double
Enum
EnumConstantNotPresentException
Error
Exception
ExceptionInInitializerError
Float
FunctionalInterface
IllegalAccessError
IllegalAccessException
IllegalArgumentException
IllegalMonitorStateException
IllegalStateException
IllegalThreadStateException
IncompatibleClassChangeError
IndexOutOfBoundsException
InheritableThreadLocal
InstantiationError
InstantiationException
Integer
InternalError
InterruptedException
Iterable
LinkageError
Long
Math
NegativeArraySizeException
NoClassDefFoundError
NoSuchFieldError
NoSuchFieldException
NoSuchMethodError
NoSuchMethodException
NullPointerException
Number
NumberFormatException
Object
OutOfMemoryError
Override
Package
Process
ProcessBuilder
Readable
ReflectiveOperationException
Runnable
Runtime
RuntimeException
RuntimePermission
SafeVarargs
SecurityException
SecurityManager
Short
StackOverflowError
StackTraceElement
StrictMath
String
StringBuffer
StringBuilder
StringIndexOutOfBoundsException
SuppressWarnings
System
Test
TestAnnotation
Thread
ThreadDeath
ThreadGroup
ThreadLocal
Throwable
TypeNotPresentException
UnknownError
UnsatisfiedLinkError
UnsupportedClassVersionError
UnsupportedOperationException
VerifyError
VirtualMachineError
Void
java
Loading

0 comments on commit af05e17

Please sign in to comment.