28 import java.util.List; 29 import java.util.regex.Pattern; 30 import jdk.internal.jline.console.completer.Completer; 31 import jdk.nashorn.api.tree.AssignmentTree; 32 import jdk.nashorn.api.tree.BinaryTree; 33 import jdk.nashorn.api.tree.CompilationUnitTree; 34 import jdk.nashorn.api.tree.CompoundAssignmentTree; 35 import jdk.nashorn.api.tree.ConditionalExpressionTree; 36 import jdk.nashorn.api.tree.ExpressionTree; 37 import jdk.nashorn.api.tree.ExpressionStatementTree; 38 import jdk.nashorn.api.tree.FunctionCallTree; 39 import jdk.nashorn.api.tree.IdentifierTree; 40 import jdk.nashorn.api.tree.InstanceOfTree; 41 import jdk.nashorn.api.tree.MemberSelectTree; 42 import jdk.nashorn.api.tree.NewTree; 43 import jdk.nashorn.api.tree.SimpleTreeVisitorES5_1; 44 import jdk.nashorn.api.tree.Tree; 45 import jdk.nashorn.api.tree.UnaryTree; 46 import jdk.nashorn.api.tree.Parser; 47 import jdk.nashorn.api.scripting.NashornException; 48 import jdk.nashorn.internal.objects.Global; 49 import jdk.nashorn.internal.runtime.Context; 50 import jdk.nashorn.internal.runtime.ScriptRuntime; 51 52 // A simple source completer for nashorn 53 final class NashornCompleter implements Completer { 54 private final Context context; 55 private final Global global; 56 private final Parser parser; 57 58 NashornCompleter(final Context context, final Global global) { 59 this.context = context; 60 this.global = global; 61 this.parser = Parser.create(); 62 } 63 64 // Pattern to match a unfinished member selection expression. object part and "." 65 // but property name missing pattern. 66 private static final Pattern SELECT_PROP_MISSING = Pattern.compile(".*\\.\\s*"); 67 68 @Override 69 public int complete(final String test, final int cursor, final List<CharSequence> result) { 70 // check that cursor is at the end of test string. Do not complete in the middle! 71 if (cursor != test.length()) { 72 return cursor; 73 } 74 75 // do we have an incomplete member selection expression that misses property name? 76 final boolean endsWithDot = SELECT_PROP_MISSING.matcher(test).matches(); 77 78 // If this is an incomplete member selection, then it is not legal code 79 // Make it legal by adding a random property name "x" to it. 80 final String exprToEval = endsWithDot? test + "x" : test; 81 82 final ExpressionTree topExpr = getTopLevelExpression(parser, exprToEval); 83 if (topExpr == null) { 84 // did not parse to be a top level expression, no suggestions! 85 return cursor; 86 } 87 88 89 // Find 'right most' expression of the top level expression 90 final Tree rightMostExpr = getRightMostExpression(topExpr); 91 if (rightMostExpr instanceof MemberSelectTree) { 92 return completeMemberSelect(test, cursor, result, (MemberSelectTree)rightMostExpr, endsWithDot); 93 } else if (rightMostExpr instanceof IdentifierTree) { 94 return completeIdentifier(test, cursor, result, (IdentifierTree)rightMostExpr); 95 } else { 96 // expression that we cannot handle for completion 97 return cursor; 98 } 99 } 100 101 private int completeMemberSelect(final String test, final int cursor, final List<CharSequence> result, 102 final MemberSelectTree select, final boolean endsWithDot) { 103 final ExpressionTree objExpr = select.getExpression(); 104 final String objExprCode = test.substring((int)objExpr.getStartPosition(), (int)objExpr.getEndPosition()); 105 106 // try to evaluate the object expression part as a script 107 Object obj = null; 108 try { 109 obj = context.eval(global, objExprCode, global, "<suggestions>"); 110 } catch (Exception ignored) { 111 // throw the exception - this is during tab-completion 112 } 113 114 if (obj != null && obj != ScriptRuntime.UNDEFINED) { 115 if (endsWithDot) { 116 // no user specified "prefix". List all properties of the object 117 result.addAll(PropertiesHelper.getProperties(obj)); 118 return cursor; 119 } else { 120 // list of properties matching the user specified prefix 121 final String prefix = select.getIdentifier(); 122 result.addAll(PropertiesHelper.getProperties(obj, prefix)); 123 return cursor - prefix.length(); 124 } | 28 import java.util.List; 29 import java.util.regex.Pattern; 30 import jdk.internal.jline.console.completer.Completer; 31 import jdk.nashorn.api.tree.AssignmentTree; 32 import jdk.nashorn.api.tree.BinaryTree; 33 import jdk.nashorn.api.tree.CompilationUnitTree; 34 import jdk.nashorn.api.tree.CompoundAssignmentTree; 35 import jdk.nashorn.api.tree.ConditionalExpressionTree; 36 import jdk.nashorn.api.tree.ExpressionTree; 37 import jdk.nashorn.api.tree.ExpressionStatementTree; 38 import jdk.nashorn.api.tree.FunctionCallTree; 39 import jdk.nashorn.api.tree.IdentifierTree; 40 import jdk.nashorn.api.tree.InstanceOfTree; 41 import jdk.nashorn.api.tree.MemberSelectTree; 42 import jdk.nashorn.api.tree.NewTree; 43 import jdk.nashorn.api.tree.SimpleTreeVisitorES5_1; 44 import jdk.nashorn.api.tree.Tree; 45 import jdk.nashorn.api.tree.UnaryTree; 46 import jdk.nashorn.api.tree.Parser; 47 import jdk.nashorn.api.scripting.NashornException; 48 import jdk.nashorn.tools.PartialParser; 49 import jdk.nashorn.internal.objects.Global; 50 import jdk.nashorn.internal.runtime.Context; 51 import jdk.nashorn.internal.runtime.ScriptRuntime; 52 53 // A simple source completer for nashorn 54 final class NashornCompleter implements Completer { 55 private final Context context; 56 private final Global global; 57 private final PartialParser partialParser; 58 private final Parser parser; 59 60 NashornCompleter(final Context context, final Global global, final PartialParser partialParser) { 61 this.context = context; 62 this.global = global; 63 this.partialParser = partialParser; 64 this.parser = Parser.create(); 65 } 66 67 // Pattern to match a unfinished member selection expression. object part and "." 68 // but property name missing pattern. 69 private static final Pattern SELECT_PROP_MISSING = Pattern.compile(".*\\.\\s*"); 70 71 @Override 72 public int complete(final String test, final int cursor, final List<CharSequence> result) { 73 // check that cursor is at the end of test string. Do not complete in the middle! 74 if (cursor != test.length()) { 75 return cursor; 76 } 77 78 // get the start of the last expression embedded in the given code 79 // using the partial parsing support - so that we can complete expressions 80 // inside statements, function call argument lists, array index etc. 81 final int exprStart = partialParser.getLastExpressionStart(context, test); 82 if (exprStart == -1) { 83 return cursor; 84 } 85 86 87 // extract the last expression string 88 final String exprStr = test.substring(exprStart); 89 90 // do we have an incomplete member selection expression that misses property name? 91 final boolean endsWithDot = SELECT_PROP_MISSING.matcher(exprStr).matches(); 92 93 // If this is an incomplete member selection, then it is not legal code. 94 // Make it legal by adding a random property name "x" to it. 95 final String completeExpr = endsWithDot? exprStr + "x" : exprStr; 96 97 final ExpressionTree topExpr = getTopLevelExpression(parser, completeExpr); 98 if (topExpr == null) { 99 // did not parse to be a top level expression, no suggestions! 100 return cursor; 101 } 102 103 104 // Find 'right most' expression of the top level expression 105 final Tree rightMostExpr = getRightMostExpression(topExpr); 106 if (rightMostExpr instanceof MemberSelectTree) { 107 return completeMemberSelect(exprStr, cursor, result, (MemberSelectTree)rightMostExpr, endsWithDot); 108 } else if (rightMostExpr instanceof IdentifierTree) { 109 return completeIdentifier(exprStr, cursor, result, (IdentifierTree)rightMostExpr); 110 } else { 111 // expression that we cannot handle for completion 112 return cursor; 113 } 114 } 115 116 private int completeMemberSelect(final String exprStr, final int cursor, final List<CharSequence> result, 117 final MemberSelectTree select, final boolean endsWithDot) { 118 final ExpressionTree objExpr = select.getExpression(); 119 final String objExprCode = exprStr.substring((int)objExpr.getStartPosition(), (int)objExpr.getEndPosition()); 120 121 // try to evaluate the object expression part as a script 122 Object obj = null; 123 try { 124 obj = context.eval(global, objExprCode, global, "<suggestions>"); 125 } catch (Exception ignored) { 126 // throw the exception - this is during tab-completion 127 } 128 129 if (obj != null && obj != ScriptRuntime.UNDEFINED) { 130 if (endsWithDot) { 131 // no user specified "prefix". List all properties of the object 132 result.addAll(PropertiesHelper.getProperties(obj)); 133 return cursor; 134 } else { 135 // list of properties matching the user specified prefix 136 final String prefix = select.getIdentifier(); 137 result.addAll(PropertiesHelper.getProperties(obj, prefix)); 138 return cursor - prefix.length(); 139 } |