Search in sources :

Example 6 with JSVariable

use of com.intellij.lang.javascript.psi.JSVariable in project intellij-plugins by JetBrains.

the class ActionScriptImplementedMethodProcessor method processMembers.

@Override
protected void processMembers(@NotNull List<PsiElement> results) {
    JSFunctionCollector functionsCollector = null;
    for (PsiElement _function : results) {
        if (!(_function instanceof JSFunction)) {
            continue;
        }
        final JSFunction function = (JSFunction) _function;
        final String name = function.getName();
        if (name == null)
            continue;
        if (functionsCollector == null) {
            functionsCollector = collectVisibleFunctions();
        }
        JSFunction o = functionsCollector.findFunctionWithTheSameKind(name, function.getKind());
        if (o == null) {
            if (JSPsiImplUtils.isGetterOrSetter(function)) {
                JSVariable var = (JSVariable) JSInheritanceUtil.findMember(name, myJsClass, Fields, null, true);
                if (var != null && ActionScriptResolveUtil.fieldIsImplicitAccessorMethod(function, var)) {
                    continue;
                }
            }
            addNonImplementedFunction(function);
        } else {
            addImplementedFunction(function, o);
        }
    }
}
Also used : JSFunction(com.intellij.lang.javascript.psi.JSFunction) JSVariable(com.intellij.lang.javascript.psi.JSVariable) PsiElement(com.intellij.psi.PsiElement)

Example 7 with JSVariable

use of com.intellij.lang.javascript.psi.JSVariable in project intellij-plugins by JetBrains.

the class FlexMoveInnerClassProcessor method performRefactoring.

@Override
protected void performRefactoring(@NotNull UsageInfo[] usages) {
    try {
        ActionScriptCreateClassOrInterfaceFix.createClass(myClassName, myPackageName, myTargetDirectory, false);
    } catch (Exception e) {
        Messages.showErrorDialog(myProject, e.getMessage(), getCommandName());
        return;
    }
    final PsiFile sourceFile = myElement.getContainingFile();
    Collection<String> importsInTargetFile = new HashSet<>();
    Collection<String> namespacesInTargetFile = new HashSet<>();
    List<FormatFixer> formatters = new ArrayList<>();
    //JSRefactoringUtil.addRemovalFormatters(mySourceClass, myMembersToMove, Condition.TRUE, Condition.TRUE, postponedFormatters);
    JSClass targetClass = myElement instanceof JSClass ? (JSClass) myElement : null;
    JSRefactoringUtil.fixOutgoingReferences(myElement, importsInTargetFile, namespacesInTargetFile, Collections.singletonList(((JSAttributeListOwner) myElement)), targetClass, false, false);
    myElement.setName(myClassName);
    Collection<UsageInfo> usagesToProcess = new ArrayList<>(Arrays.asList(usages));
    for (Iterator<UsageInfo> i = usagesToProcess.iterator(); i.hasNext(); ) {
        UsageInfo usage = i.next();
        PsiElement element;
        if (usage instanceof NonCodeUsageInfo || (element = usage.getElement()) == null || !PsiTreeUtil.isAncestor(myElement, element, false)) {
            continue;
        }
        if (element instanceof JSReferenceExpression) {
            ((JSReferenceExpression) element).bindToElement(myElement);
        } else if (element instanceof PsiNamedElement) {
            ((PsiNamedElement) element).setName(myClassName);
        } else {
            continue;
        }
        i.remove();
    }
    final PsiElement clazz = ActionScriptClassResolver.findClassByQNameStatic(StringUtil.getQualifiedName(myPackageName, myClassName), GlobalSearchScope.projectScope(myProject));
    PsiElement toInsert = myElement instanceof JSVariable ? JSRefactoringUtil.getVarStatementCopy((JSVariable) myElement) : myElement.copy();
    final PsiElement inserted = clazz.replace(toInsert);
    PsiFile insertedContainingFile = inserted.getContainingFile();
    JSQualifiedNamedElement newClass = inserted instanceof JSVarStatement ? ((JSVarStatement) inserted).getVariables()[0] : (JSQualifiedNamedElement) inserted;
    SmartPsiElementPointer<JSQualifiedNamedElement> newClassPointer = SmartPointerManager.getInstance(myProject).createSmartPsiElementPointer(newClass);
    JSRefactoringUtil.handleDocCommentAndFormat(inserted, formatters);
    JSRefactoringUtil.deleteWithNoPostponedFormatting(myElement);
    if (myPackageName.length() > 0) {
        for (UsageInfo usage : usagesToProcess) {
            if (usage instanceof NonCodeUsageInfo || usage.getFile() != sourceFile)
                continue;
            final PsiElement element = usage.getElement();
            if (element == null)
                continue;
            ImportUtils.doImport(element, StringUtil.getQualifiedName(myPackageName, myClassName), true);
        }
    }
    JSRefactoringUtil.postProcess(sourceFile, newClass, Collections.singletonList(sourceFile), importsInTargetFile, namespacesInTargetFile, formatters, true, false);
    boolean makePublic = false;
    newClass = newClassPointer.getElement();
    List<NonCodeUsageInfo> nonCodeUsages = new ArrayList<>();
    for (UsageInfo usage : usagesToProcess) {
        if (usage instanceof NonCodeUsageInfo) {
            nonCodeUsages.add((NonCodeUsageInfo) usage);
        } else {
            JSReferenceExpression refExpr = (JSReferenceExpression) usage.getElement();
            if (refExpr == null) {
                continue;
            }
            makePublic |= JSPsiImplUtils.getQNameForMove(refExpr, newClass) != null;
            refExpr.bindToElement(newClass);
        }
    }
    JSChangeVisibilityUtil.setVisibility((JSAttributeListOwner) newClass, makePublic ? JSAttributeList.AccessType.PUBLIC : JSAttributeList.AccessType.PACKAGE_LOCAL);
    myNonCodeUsages = nonCodeUsages.toArray(new NonCodeUsageInfo[nonCodeUsages.size()]);
    if (myMoveCallback != null) {
        myMoveCallback.refactoringCompleted();
    }
    OpenFileDescriptor descriptor = new OpenFileDescriptor(myProject, insertedContainingFile.getVirtualFile(), newClass.getTextOffset());
    FileEditorManager.getInstance(myProject).openTextEditor(descriptor, true);
}
Also used : NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo) JSQualifiedNamedElement(com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement) JSVariable(com.intellij.lang.javascript.psi.JSVariable) JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) OpenFileDescriptor(com.intellij.openapi.fileEditor.OpenFileDescriptor) JSVarStatement(com.intellij.lang.javascript.psi.JSVarStatement) JSClass(com.intellij.lang.javascript.psi.ecmal4.JSClass) FormatFixer(com.intellij.lang.javascript.refactoring.FormatFixer) NonCodeUsageInfo(com.intellij.refactoring.util.NonCodeUsageInfo)

Example 8 with JSVariable

use of com.intellij.lang.javascript.psi.JSVariable in project oxy-template-support-plugin by mutant-industries.

the class InnerJsTypeEvaluator method checkForEachDefinition.

// -----------------------------------------------------------------------------------------------------------------
@Nullable
private static JSType checkForEachDefinition(@NotNull final PsiElement element) {
    PsiElement elementLocal = element;
    if (elementLocal.getParent() instanceof JSVarStatement) {
        elementLocal = elementLocal.getParent();
    }
    // repeat macro - var keyword is missing
    if (elementLocal instanceof PsiPackage || !(elementLocal.getFirstChild() instanceof JSVariable)) {
        return null;
    }
    PsiElement elementAt = elementLocal.getContainingFile().getViewProvider().findElementAt(elementLocal.getNode().getStartOffset(), OxyTemplate.INSTANCE);
    assert elementAt != null;
    MacroAttribute attribute = PsiTreeUtil.getParentOfType(elementAt, MacroAttribute.class);
    if (attribute == null || !MacroIndex.REPEAT_MACRO_VARIABLE_DEFINITION.equals(attribute.getMacroParamName().getText())) {
        return null;
    }
    MacroCall macroCall = PsiTreeUtil.getParentOfType(attribute, MacroCall.class);
    assert macroCall != null;
    for (MacroAttribute macroAttribute : macroCall.getMacroAttributeList()) {
        if (MacroIndex.REPEAT_MACRO_LIST_DEFINITION.equals(macroAttribute.getMacroParamName().getText())) {
            MacroParam macroParam;
            if ((macroParam = macroAttribute.getMacroParam()) == null) {
                return null;
            }
            PsiElement list = elementLocal.getContainingFile().getViewProvider().findElementAt(macroParam.getNode().getStartOffset() + macroParam.getTextLength() - 1, OxyTemplateInnerJs.INSTANCE);
            JSReferenceExpression statement = PsiTreeUtil.getParentOfType(list, JSReferenceExpression.class);
            if (statement != null) {
                JSSimpleTypeProcessor typeProcessor = new JSSimpleTypeProcessor();
                JSTypeEvaluator.evaluateTypes(statement, statement.getContainingFile(), typeProcessor);
                if (typeProcessor.getType() instanceof JSArrayTypeImpl) {
                    return ((JSArrayTypeImpl) typeProcessor.getType()).getType();
                }
            }
        }
    }
    return null;
}
Also used : JSReferenceExpression(com.intellij.lang.javascript.psi.JSReferenceExpression) JSArrayTypeImpl(com.intellij.lang.javascript.psi.types.JSArrayTypeImpl) MacroAttribute(ool.intellij.plugin.psi.MacroAttribute) PsiPackage(com.intellij.psi.PsiPackage) JSSimpleTypeProcessor(com.intellij.lang.javascript.psi.resolve.JSSimpleTypeProcessor) JSVarStatement(com.intellij.lang.javascript.psi.JSVarStatement) JSVariable(com.intellij.lang.javascript.psi.JSVariable) PsiElement(com.intellij.psi.PsiElement) MacroCall(ool.intellij.plugin.psi.MacroCall) MacroParam(ool.intellij.plugin.psi.MacroParam) Nullable(org.jetbrains.annotations.Nullable)

Aggregations

JSVariable (com.intellij.lang.javascript.psi.JSVariable)8 JSFunction (com.intellij.lang.javascript.psi.JSFunction)3 PsiElement (com.intellij.psi.PsiElement)3 JSReferenceExpression (com.intellij.lang.javascript.psi.JSReferenceExpression)2 JSVarStatement (com.intellij.lang.javascript.psi.JSVarStatement)2 JSClass (com.intellij.lang.javascript.psi.ecmal4.JSClass)2 NonNls (org.jetbrains.annotations.NonNls)2 FlexUnitSupport (com.intellij.lang.javascript.flex.flexunit.FlexUnitSupport)1 JSElement (com.intellij.lang.javascript.psi.JSElement)1 JSParameter (com.intellij.lang.javascript.psi.JSParameter)1 JSPsiElementBase (com.intellij.lang.javascript.psi.JSPsiElementBase)1 JSAttributeList (com.intellij.lang.javascript.psi.ecmal4.JSAttributeList)1 JSAttributeListOwner (com.intellij.lang.javascript.psi.ecmal4.JSAttributeListOwner)1 JSQualifiedNamedElement (com.intellij.lang.javascript.psi.ecmal4.JSQualifiedNamedElement)1 JSOverrideHandler (com.intellij.lang.javascript.psi.resolve.JSOverrideHandler)1 JSSimpleTypeProcessor (com.intellij.lang.javascript.psi.resolve.JSSimpleTypeProcessor)1 JSArrayTypeImpl (com.intellij.lang.javascript.psi.types.JSArrayTypeImpl)1 FormatFixer (com.intellij.lang.javascript.refactoring.FormatFixer)1 JSExtractFunctionSettings (com.intellij.lang.javascript.refactoring.extractMethod.JSExtractFunctionSettings)1 BaseCreateMethodsFix (com.intellij.lang.javascript.validation.fixes.BaseCreateMethodsFix)1