Search in sources :

Example 1 with RPsiElement

use of org.jetbrains.plugins.ruby.ruby.lang.psi.RPsiElement in project intellij-plugins by JetBrains.

the class MotionDebuggerTypesHelper method resolveToDeclaration.

@Override
public PsiElement resolveToDeclaration(XSourcePosition position, LLValue var) {
    final String name = var.getName();
    final VirtualFile file = position.getFile();
    final int offset = position.getOffset();
    final PsiFile psiFile = PsiManager.getInstance(myProcess.getProject()).findFile(file);
    if (psiFile == null)
        return null;
    final PsiElement element = RubyPsiUtil.getSignificantLeafToTheRight(psiFile.findElementAt(offset));
    final RContainer container = element != null ? RubyPsiUtil.getParentContainerOrSelf(element) : null;
    if (container == null)
        return null;
    ScopeVariable variable = null;
    for (ScopeVariable scopeVariable : container.getScope().getAllDeclaredVariables()) {
        if (name.equals(scopeVariable.getName())) {
            variable = scopeVariable;
            break;
        }
    }
    if (variable == null)
        return null;
    final RPsiElement item = ContainerUtil.getFirstItem(variable.getDeclarations());
    if (item == null)
        return null;
    return item;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) RPsiElement(org.jetbrains.plugins.ruby.ruby.lang.psi.RPsiElement) RContainer(org.jetbrains.plugins.ruby.ruby.lang.psi.holders.RContainer) ScopeVariable(org.jetbrains.plugins.ruby.ruby.codeInsight.resolve.scope.ScopeVariable) PsiFile(com.intellij.psi.PsiFile) PsiElement(com.intellij.psi.PsiElement) RPsiElement(org.jetbrains.plugins.ruby.ruby.lang.psi.RPsiElement)

Example 2 with RPsiElement

use of org.jetbrains.plugins.ruby.ruby.lang.psi.RPsiElement in project intellij-plugins by JetBrains.

the class RubyMotionTypeProvider method calculateObjCName.

private static String calculateObjCName(final String methodName, final List<RArgument> arguments) {
    final StringBuilder objCNameBuilder = new StringBuilder(methodName).append(":");
    for (RArgument argument : arguments) {
        if (argument instanceof RNamedArgument) {
            final RPsiElement nameIdentifier = ((RNamedArgument) argument).getNameIdentifier();
            assert nameIdentifier != null;
            objCNameBuilder.append(nameIdentifier.getName()).append(":");
        }
    }
    return objCNameBuilder.toString();
}
Also used : RNamedArgument(org.jetbrains.plugins.ruby.ruby.lang.psi.controlStructures.methods.RNamedArgument) RPsiElement(org.jetbrains.plugins.ruby.ruby.lang.psi.RPsiElement) RArgument(org.jetbrains.plugins.ruby.ruby.lang.psi.controlStructures.methods.RArgument)

Example 3 with RPsiElement

use of org.jetbrains.plugins.ruby.ruby.lang.psi.RPsiElement 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)

Example 4 with RPsiElement

use of org.jetbrains.plugins.ruby.ruby.lang.psi.RPsiElement in project intellij-plugins by JetBrains.

the class RubyMotionTypeProvider method createTypeForRef.

@Nullable
private static RType createTypeForRef(@NotNull RReference ref, @NotNull Module module) {
    final RPsiElement value = ref.getValue();
    // method name may be identifier on constant
    if (value instanceof RPossibleCall) {
        final String shortName = ((RPossibleCall) value).getPossibleCommand();
        final String sdkVersion = RubyMotionUtil.getInstance().getSdkVersion(module);
        final String[] frameworks = RubyMotionUtil.getInstance().getRequiredFrameworks(module);
        boolean isIdSelector = false;
        for (String framework : frameworks) {
            if (BridgeSupportLoader.getInstance().isIdSelector(shortName, sdkVersion, framework)) {
                isIdSelector = true;
                break;
            }
        }
        if (isIdSelector) {
            final Symbol callSymbol = ResolveUtil.resolveToSymbolWithCaching(ref.getReference());
            if (callSymbol instanceof FunctionSymbol) {
                final Function function = ((FunctionSymbol) callSymbol).getFunction();
                if (function.isId()) {
                    return RTypeUtil.createTypeSameAsReceiverInstance(ref);
                }
            }
        }
    }
    return null;
}
Also used : RPsiElement(org.jetbrains.plugins.ruby.ruby.lang.psi.RPsiElement) Function(org.jetbrains.plugins.ruby.motion.bridgesupport.Function) FunctionSymbol(org.jetbrains.plugins.ruby.motion.symbols.FunctionSymbol) MotionClassSymbol(org.jetbrains.plugins.ruby.motion.symbols.MotionClassSymbol) ConstantSymbol(org.jetbrains.plugins.ruby.motion.symbols.ConstantSymbol) ClassModuleSymbol(org.jetbrains.plugins.ruby.ruby.codeInsight.symbols.v2.ClassModuleSymbol) FunctionSymbol(org.jetbrains.plugins.ruby.motion.symbols.FunctionSymbol) Symbol(org.jetbrains.plugins.ruby.ruby.codeInsight.symbols.structure.Symbol) RPossibleCall(org.jetbrains.plugins.ruby.ruby.lang.psi.RPossibleCall) Nullable(org.jetbrains.annotations.Nullable)

Example 5 with RPsiElement

use of org.jetbrains.plugins.ruby.ruby.lang.psi.RPsiElement in project intellij-plugins by JetBrains.

the class SelectorKeysProvider method determineArgNumberAndPath.

private static Pair<Integer, List<String>> determineArgNumberAndPath(ParamContext context) {
    final RPsiElement element = context.getValueElement();
    final PsiElement parent = element.getParent();
    final PsiElement argument = parent instanceof RAssoc ? parent : element;
    final RListOfExpressions expressions = RListOfExpressionsNavigator.getByPsiElement(argument);
    if (expressions == null)
        return Pair.create(-1, null);
    final List<String> path = new ArrayList<>();
    for (int i = 1; i < expressions.getElements().size(); i++) {
        PsiElement arg = expressions.getElements().get(i);
        if (arg == argument)
            return Pair.create(i, path);
        if (arg instanceof RAssoc) {
            path.add(((RAssoc) arg).getKeyText());
        }
    }
    return Pair.create(-1, null);
}
Also used : RPsiElement(org.jetbrains.plugins.ruby.ruby.lang.psi.RPsiElement) RAssoc(org.jetbrains.plugins.ruby.ruby.lang.psi.assoc.RAssoc) RListOfExpressions(org.jetbrains.plugins.ruby.ruby.lang.psi.expressions.RListOfExpressions) PsiElement(com.intellij.psi.PsiElement) RPsiElement(org.jetbrains.plugins.ruby.ruby.lang.psi.RPsiElement)

Aggregations

RPsiElement (org.jetbrains.plugins.ruby.ruby.lang.psi.RPsiElement)5 PsiElement (com.intellij.psi.PsiElement)2 VirtualFile (com.intellij.openapi.vfs.VirtualFile)1 PsiFile (com.intellij.psi.PsiFile)1 IElementType (com.intellij.psi.tree.IElementType)1 Nullable (org.jetbrains.annotations.Nullable)1 Function (org.jetbrains.plugins.ruby.motion.bridgesupport.Function)1 ConstantSymbol (org.jetbrains.plugins.ruby.motion.symbols.ConstantSymbol)1 FunctionSymbol (org.jetbrains.plugins.ruby.motion.symbols.FunctionSymbol)1 MotionClassSymbol (org.jetbrains.plugins.ruby.motion.symbols.MotionClassSymbol)1 ScopeVariable (org.jetbrains.plugins.ruby.ruby.codeInsight.resolve.scope.ScopeVariable)1 Symbol (org.jetbrains.plugins.ruby.ruby.codeInsight.symbols.structure.Symbol)1 ClassModuleSymbol (org.jetbrains.plugins.ruby.ruby.codeInsight.symbols.v2.ClassModuleSymbol)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 RPossibleCall (org.jetbrains.plugins.ruby.ruby.lang.psi.RPossibleCall)1 RAssoc (org.jetbrains.plugins.ruby.ruby.lang.psi.assoc.RAssoc)1 RArgument (org.jetbrains.plugins.ruby.ruby.lang.psi.controlStructures.methods.RArgument)1 RNamedArgument (org.jetbrains.plugins.ruby.ruby.lang.psi.controlStructures.methods.RNamedArgument)1