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 } | 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 PartialParser partialParser; 57 private final Parser parser; 58 59 NashornCompleter(final Context context, final Global global, final PartialParser partialParser) { 60 this.context = context; 61 this.global = global; 62 this.partialParser = partialParser; 63 this.parser = Parser.create(); 64 } 65 66 // Pattern to match a unfinished member selection expression. object part and "." 67 // but property name missing pattern. 68 private static final Pattern SELECT_PROP_MISSING = Pattern.compile(".*\\.\\s*"); 69 70 @Override 71 public int complete(final String test, final int cursor, final List<CharSequence> result) { 72 // check that cursor is at the end of test string. Do not complete in the middle! 73 if (cursor != test.length()) { 74 return cursor; 75 } 76 77 // get the start of the last expression embedded in the given code 78 // using the partial parsing support - so that we can complete expressions 79 // inside statements, function call argument lists, array index etc. 80 final int exprStart = partialParser.getLastExpressionStart(context, test); 81 if (exprStart == -1) { 82 return cursor; 83 } 84 85 86 // extract the last expression string 87 final String exprStr = test.substring(exprStart); 88 89 // do we have an incomplete member selection expression that misses property name? 90 final boolean endsWithDot = SELECT_PROP_MISSING.matcher(exprStr).matches(); 91 92 // If this is an incomplete member selection, then it is not legal code. 93 // Make it legal by adding a random property name "x" to it. 94 final String completeExpr = endsWithDot? exprStr + "x" : exprStr; 95 96 final ExpressionTree topExpr = getTopLevelExpression(parser, completeExpr); 97 if (topExpr == null) { 98 // did not parse to be a top level expression, no suggestions! 99 return cursor; 100 } 101 102 103 // Find 'right most' expression of the top level expression 104 final Tree rightMostExpr = getRightMostExpression(topExpr); 105 if (rightMostExpr instanceof MemberSelectTree) { 106 return completeMemberSelect(exprStr, cursor, result, (MemberSelectTree)rightMostExpr, endsWithDot); 107 } else if (rightMostExpr instanceof IdentifierTree) { 108 return completeIdentifier(exprStr, cursor, result, (IdentifierTree)rightMostExpr); 109 } else { 110 // expression that we cannot handle for completion 111 return cursor; 112 } 113 } 114 115 private int completeMemberSelect(final String exprStr, final int cursor, final List<CharSequence> result, 116 final MemberSelectTree select, final boolean endsWithDot) { 117 final ExpressionTree objExpr = select.getExpression(); 118 final String objExprCode = exprStr.substring((int)objExpr.getStartPosition(), (int)objExpr.getEndPosition()); 119 120 // try to evaluate the object expression part as a script 121 Object obj = null; 122 try { 123 obj = context.eval(global, objExprCode, global, "<suggestions>"); 124 } catch (Exception ignored) { 125 // throw the exception - this is during tab-completion 126 } 127 128 if (obj != null && obj != ScriptRuntime.UNDEFINED) { 129 if (endsWithDot) { 130 // no user specified "prefix". List all properties of the object 131 result.addAll(PropertiesHelper.getProperties(obj)); 132 return cursor; 133 } else { 134 // list of properties matching the user specified prefix 135 final String prefix = select.getIdentifier(); 136 result.addAll(PropertiesHelper.getProperties(obj, prefix)); 137 return cursor - prefix.length(); 138 } |