Search in sources :

Example 1 with RubyPsiInterpreter

use of org.jetbrains.plugins.ruby.ruby.interpret.RubyPsiInterpreter in project intellij-plugins by JetBrains.

the class RubyMotionUtilImpl method doCalculateSdkAndFrameworks.

private Trinity<String, String[], ProjectType> doCalculateSdkAndFrameworks(RFile file) {
    final ProjectType projectType = calculateProjectType(file);
    final Ref<String> sdkVersion = new Ref<>(getDefaultSdkVersion(projectType));
    final Set<String> frameworks = new HashSet<>();
    Collections.addAll(frameworks, projectType == ProjectType.OSX ? DEFAULT_OSX_FRAMEWORKS : projectType == ProjectType.ANDROID ? DEFAULT_ANDROID_FRAMEWORKS : DEFAULT_IOS_FRAMEWORKS);
    final RubyPsiInterpreter interpreter = new RubyPsiInterpreter(true);
    final PsiCallable callable = new PsiCallable() {

        @Override
        public void processCall(RCallArguments arguments) {
            final String command = arguments.getCommand();
            RAssignmentExpression assign = RAssignmentExpressionNavigator.getAssignmentByLeftPart(arguments.getCallElement());
            assign = assign == null ? RSelfAssignmentExpressionNavigator.getSelfAssignmentByLeftPart(arguments.getCallElement()) : assign;
            RBinaryExpression shift = assign == null ? RShiftExpressionNavigator.getShiftExpressionByLeftPart(arguments.getCallElement()) : null;
            final RPsiElement value = assign != null ? assign.getValue() : shift != null ? shift.getRightOperand() : null;
            if (value == null) {
                return;
            }
            final IElementType type = assign != null ? assign.getOperationType() : shift.getOperationType();
            if ("sdk_version".equals(command)) {
                sdkVersion.set(TextUtil.removeQuoting(value.getText()));
            } else if ("frameworks".equals(command)) {
                if (value instanceof RArray) {
                    final String[] array = TextUtil.arrayToString((RArray) value).split(", ");
                    if (type == RubyTokenTypes.tASSGN) {
                        frameworks.clear();
                        Collections.addAll(frameworks, array);
                    } else if (type == RubyTokenTypes.tMINUS_OP_ASGN) {
                        for (String s : array) {
                            frameworks.remove(s);
                        }
                    } else {
                        Collections.addAll(frameworks, array);
                    }
                } else {
                    frameworks.add(TextUtil.removeQuoting(value.getText()));
                }
            }
        }
    };
    interpreter.registerCallable(new PsiCallable() {

        @Override
        public void processCall(RCallArguments arguments) {
            arguments.interpretBlock(callable);
        }
    }, "Motion::Project::App.setup");
    interpreter.interpret(file, callable);
    return Trinity.create(sdkVersion.get(), ArrayUtil.toStringArray(frameworks), projectType);
}
Also used : RPsiElement(org.jetbrains.plugins.ruby.ruby.lang.psi.RPsiElement) PsiCallable(org.jetbrains.plugins.ruby.ruby.interpret.PsiCallable) RubyPsiInterpreter(org.jetbrains.plugins.ruby.ruby.interpret.RubyPsiInterpreter) RAssignmentExpression(org.jetbrains.plugins.ruby.ruby.lang.psi.expressions.RAssignmentExpression) IElementType(com.intellij.psi.tree.IElementType) RArray(org.jetbrains.plugins.ruby.ruby.lang.psi.expressions.RArray) RCallArguments(org.jetbrains.plugins.ruby.ruby.interpret.RCallArguments) RBinaryExpression(org.jetbrains.plugins.ruby.ruby.lang.psi.expressions.RBinaryExpression)

Aggregations

IElementType (com.intellij.psi.tree.IElementType)1 PsiCallable (org.jetbrains.plugins.ruby.ruby.interpret.PsiCallable)1 RCallArguments (org.jetbrains.plugins.ruby.ruby.interpret.RCallArguments)1 RubyPsiInterpreter (org.jetbrains.plugins.ruby.ruby.interpret.RubyPsiInterpreter)1 RPsiElement (org.jetbrains.plugins.ruby.ruby.lang.psi.RPsiElement)1 RArray (org.jetbrains.plugins.ruby.ruby.lang.psi.expressions.RArray)1 RAssignmentExpression (org.jetbrains.plugins.ruby.ruby.lang.psi.expressions.RAssignmentExpression)1 RBinaryExpression (org.jetbrains.plugins.ruby.ruby.lang.psi.expressions.RBinaryExpression)1