/** * @param psiElement checking element * @return true if this is process.env.*** variable */ public static boolean checkPsiElement(@NotNull PsiElement psiElement) { if(!(psiElement instanceof LeafPsiElement)) { return false; } IElementType elementType = ((LeafPsiElement) psiElement).getElementType(); if(!elementType.toString().equals("JS:IDENTIFIER")) { return false; } PsiElement parent = psiElement.getParent(); if(!(parent instanceof JSReferenceExpression)) { return false; } return ((JSReferenceExpression) parent).getCanonicalText().startsWith("process.env"); }
@Nullable @Override public PsiElement getElementByReference(@NotNull PsiReference ref, int flags) { if (ref instanceof JSTextReference) { final PsiElement element = ref.getElement(); final JSCallExpression call = PsiTreeUtil.getParentOfType(element, JSCallExpression.class); final JSExpression expression = call != null ? call.getMethodExpression() : null; if (expression instanceof JSReferenceExpression) { JSReferenceExpression callee = (JSReferenceExpression)expression; JSExpression qualifier = callee.getQualifier(); if (qualifier != null && "component".equals(callee.getReferencedName()) && EmberIndexUtil.hasEmberJS(element.getProject())) { return element; } } } return null; }
public static boolean elementIsAttachPoint(PsiElement element) { /* It's hard to detect when an element is an attach point, because of the use of this inside other functions this.attachpoint that.attachpoint ideally we would parse the template file in the beginning and cache all of the attach points, maybe that's a todo item... */ if(element == null || element.getParent() == null || !(element.getParent() instanceof JSReferenceExpression)) { return false; } // we can exclude JSCallExpressions at least because you will never reference an attach point like // this.attachpoint(...) if(element.getParent().getParent() instanceof JSCallExpression) { return false; } return true; }
public boolean isDefineFirstCollection(PsiElement element) { PsiElement jsArrayLiteral = element.getParent(); if (null != jsArrayLiteral && jsArrayLiteral instanceof JSArrayLiteralExpression) { PsiElement jsArgumentList = jsArrayLiteral.getParent(); if (null != jsArgumentList && jsArgumentList instanceof JSArgumentList) { PsiElement jsReferenceExpression = jsArgumentList.getPrevSibling(); if (null != jsReferenceExpression && jsReferenceExpression instanceof JSReferenceExpression) { if (jsReferenceExpression.getText().equals(Settings.REQUIREJS_DEFINE_FUNCTION_NAME)) { return true; } } } } return false; }
@Override public String getName() { final T stub = getStub(); if(stub != null) { return stub.getName(); } final PsiElement name = getNameIdentifier(); if(name != null) { if(name instanceof JSReferenceExpression) { return ((JSReferenceExpression) name).getReferencedName(); } } return name != null ? name.getText() : ""; }
@NotNull @Override @RequiredReadAction public String[] getReferenceTexts() { final JSReferenceListStub stub = getStub(); if(stub != null) { return stub.getReferenceTexts(); } final JSReferenceExpression[] referenceExpressions = getExpressions(); if(referenceExpressions.length == 0) { return ArrayUtil.EMPTY_STRING_ARRAY; } int count = referenceExpressions.length; final String[] result = ArrayUtil.newStringArray(count); for(int i = 0; i < count; ++i) { result[i] = referenceExpressions[i].getText(); } return result; }
@Override @RequiredReadAction public String getName() { final JSClassStub classStub = getStub(); if(classStub != null) { return classStub.getName(); } PsiElement nameIdentifier = getNameIdentifier(); if(nameIdentifier instanceof JSReferenceExpression) { return ((JSReferenceExpression) nameIdentifier).getReferencedName(); } else if(nameIdentifier != null) { return nameIdentifier.getText(); } return null; }
@RequiredReadAction @NotNull @Override public JavaScriptType getType() { JSExpression methodExpression = getMethodExpression(); if(methodExpression instanceof JSReferenceExpression) { PsiElement resolvedElement = ((JSReferenceExpression) methodExpression).resolve(); if(resolvedElement instanceof JSClass) { return new JavaScriptClassType((JSClass) resolvedElement); } } return super.getType(); }
@Override @RequiredReadAction public boolean isReferenceTo(PsiElement element) { final PsiElement element2 = resolve(); boolean proxyExpanded = false; if(element instanceof JSDefinitionExpression) { final JSExpression expression = ((JSDefinitionExpression) element).getExpression(); if(expression instanceof JSReferenceExpression) { return ((JSReferenceExpression) expression).isReferenceTo(element2); } } if(element != element2 && element instanceof JSProperty && element2 instanceof JSProperty) { return ((JSProperty) element).getName().equals(((JSProperty) element2).getName()); } return proxyExpanded && element == element2; }
@Override @RequiredReadAction public String getName() { final JSFunctionStub stub = getStub(); if(stub != null) { return stub.getName(); } final PsiElement name = getNameIdentifier(); if(name != null) { if(name instanceof JSReferenceExpression) { return ((JSReferenceExpression) name).getReferencedName(); } else { return name.getText(); } } return null; }
private static JSExpression findExpressionInRange(PsiFile file, int startOffset, int endOffset) { PsiElement element1 = file.findElementAt(startOffset); PsiElement element2 = file.findElementAt(endOffset - 1); if(element1 instanceof PsiWhiteSpace) { startOffset = element1.getTextRange().getEndOffset(); } if(element2 instanceof PsiWhiteSpace) { endOffset = element2.getTextRange().getStartOffset(); } JSExpression expression = PsiTreeUtil.findElementOfClassAtRange(file, startOffset, endOffset, JSExpression.class); if(expression == null || expression.getTextRange().getEndOffset() != endOffset) { return null; } if(expression instanceof JSReferenceExpression && expression.getParent() instanceof JSCallExpression) { return null; } return expression; }
@Override protected JSElementVisitor createVisitor(final ProblemsHolder holder) { return new JSElementVisitor() { @Override public void visitJSReferenceExpression(final JSReferenceExpression node) { for(ResolveResult r : node.multiResolve(false)) { final PsiElement element = r.getElement(); if((element instanceof JSDefinitionExpression && element.getParent() instanceof JSAssignmentExpression) || element == node.getParent()) { continue; } if(JSDocumentationUtils.isDeprecated(element)) { holder.registerProblem(node.getReferenceNameElement(), JavaScriptBundle.message("javascript.deprecated.symbol.used.name.message"), ProblemHighlightType.LIKE_DEPRECATED); break; } } } }; }
@Override public void visitJSCallExpression(JSCallExpression jsCallExpression) { super.visitJSCallExpression(jsCallExpression); final JSExpression methodExpression; try { methodExpression = jsCallExpression.getMethodExpression(); } catch (Exception e) { return; //catching an intelliJ CCE } if (!(methodExpression instanceof JSReferenceExpression)) { return; } final JSReferenceExpression referenceExpression = (JSReferenceExpression) methodExpression; final JSExpression qualifier = referenceExpression.getQualifier(); @NonNls final String methodName = referenceExpression.getReferencedName(); if (!"eval".equals(methodName) && !"setTimeout".equals(methodName) && !"setInterval".equals(methodName)) { return; } registerError(methodExpression); }
@Override public void visitJSNewExpression(JSNewExpression jsNewExpression) { super.visitJSNewExpression( jsNewExpression); final JSExpression methodExpression; try { methodExpression = jsNewExpression.getMethodExpression(); } catch (Exception e) { return; //catching an intelliJ CCE } if (!(methodExpression instanceof JSReferenceExpression)) { return; } final JSReferenceExpression referenceExpression = (JSReferenceExpression) methodExpression; final JSExpression qualifier = referenceExpression.getQualifier(); if (qualifier != null) { return; } @NonNls final String methodName = referenceExpression.getReferencedName(); if (!"Function".equals(methodName)) { return; } registerError(methodExpression); }
@Override public void visitJSCallExpression( @NotNull JSCallExpression expression) { super.visitJSCallExpression(expression); final JSExpression reference = expression.getMethodExpression(); if(!(reference instanceof JSReferenceExpression)) { return; } final JSExpression qualifier = ((JSReferenceExpression)reference).getQualifier(); if (qualifier == null) { return; } if (!isCallExpression(qualifier)) { return; } registerFunctionCallError(expression); }
@SuppressWarnings({"HardCodedStringLiteral"}) @Override public void visitJSReferenceExpression(JSReferenceExpression referenceExpression) { super.visitJSReferenceExpression(referenceExpression); final JSExpression qualifier = referenceExpression.getQualifier(); if (qualifier == null) { return; } final String qualifierText = qualifier.getText(); if (!"document".equalsIgnoreCase(qualifierText)) { return; } final String methodName = referenceExpression.getReferencedName(); if (!"images".equalsIgnoreCase(methodName) && !"body".equalsIgnoreCase(methodName) && !"applets".equalsIgnoreCase(methodName) && !"links".equalsIgnoreCase(methodName) && !"forms".equalsIgnoreCase(methodName) && !"anchors".equalsIgnoreCase(methodName) ) { return; } registerError(referenceExpression); }
@Override public void visitJSReferenceExpression( @NotNull JSReferenceExpression reference) { if(usedInInnerFunction) { return; } super.visitJSReferenceExpression(reference); if(inInnerFunction) { final PsiElement element = reference.resolve(); if(variable.equals(element)) { usedInInnerFunction = true; } } }
@Override public void visitElement(PsiElement element) { if(element instanceof JSReferenceExpression) { String possibleKey = JsPsiHelper.checkReferenceExpression((JSReferenceExpression) element); if(possibleKey != null) { collectedItems.add(new KeyUsagePsiElement(possibleKey, element)); } } super.visitElement(element); }
/** * Checks whole process.env.SOME_KEY reference element * @param referenceExpression checking expression * @return null or Environment key */ @Nullable public static String checkReferenceExpression(@NotNull JSReferenceExpression referenceExpression) { String text = referenceExpression.getCanonicalText(); if(!text.startsWith("process.env.") || text.length() < 13) { return null; } return text.substring(12); }
@RequiredReadAction @Nullable @Override public LineMarkerInfo getLineMarkerInfo(@NotNull PsiElement element) { if(element.getNode().getElementType() == JSTokenTypes.IDENTIFIER && element.getParent() instanceof JSReferenceExpression && element.getParent().getParent() instanceof JSFunction) { UnityFunctionManager functionManager = UnityFunctionManager.getInstance(); Map<String, UnityFunctionManager.FunctionInfo> map = functionManager.getFunctionsByType().get(Unity3dTypes.UnityEngine.MonoBehaviour); if(map == null) { return null; } UnityFunctionManager.FunctionInfo functionInfo = map.get(element.getText()); if(functionInfo == null) { return null; } Unity3dModuleExtension extension = ModuleUtilCore.getExtension(element, Unity3dModuleExtension.class); if(extension == null) { return null; } JSFunction jsFunction = (JSFunction) element.getParent().getParent(); if(jsFunction.getParent() instanceof JSFile) { if(!isEqualParameters(functionInfo.getParameters(), jsFunction)) { return null; } return new LineMarkerInfo<>(element, element.getTextRange(), Unity3dIcons.EventMethod, Pass.LINE_MARKERS, new ConstantFunction<>(functionInfo.getDescription()), null, GutterIconRenderer.Alignment.LEFT); } } return null; }
public static boolean containsTestsInFiles(JSFile file) { JSSourceElement[] statements = file.getStatements(); for(JSSourceElement statement : statements) { if(statement instanceof JSExpressionStatement) { JSExpression expression = ((JSExpressionStatement) statement).getExpression(); if(expression instanceof JSCallExpression) { JSExpression methodExpression = ((JSCallExpression) expression).getMethodExpression(); if(methodExpression instanceof JSReferenceExpression) { JSExpression qualifier = ((JSReferenceExpression) methodExpression).getQualifier(); if(qualifier != null) { continue; } String referencedName = ((JSReferenceExpression) methodExpression).getReferencedName(); if("describe".equals(referencedName)) { JSArgumentList argumentList = ((JSCallExpression) expression).getArgumentList(); if(argumentList != null && argumentList.getArguments().length == 2) { return true; } } } } } } return false; }
@RequiredReadAction @Override protected void buildTemplate(Template template, JSReferenceExpression referenceExpression, Set<JavaScriptFeature> features, boolean staticContext, PsiFile file, PsiElement anchorParent) { boolean classFeature = features.contains(JavaScriptFeature.CLASS); String referencedName = classFeature ? referenceExpression.getReferencedName() : referenceExpression.getText(); BaseCreateFix.addAccessModifier(template, referenceExpression, classFeature, staticContext); writeFunctionAndName(template, referencedName, features); template.addTextSegment("("); addParameters(template, referenceExpression, file, features); template.addTextSegment(")"); if(classFeature) { template.addTextSegment(":"); addReturnType(template, referenceExpression, file); } JSClass clazz = BaseCreateFix.findClass(file, anchorParent); if(clazz == null || !clazz.isInterface()) { template.addTextSegment(" {"); addBody(template, referenceExpression, file); template.addTextSegment("}"); } else { addSemicolonSegment(template, file); template.addEndVariable(); } }
@Override protected void addParameters(Template template, JSReferenceExpression referenceExpression, PsiFile file, Set<JavaScriptFeature> features) { JSCallExpression methodInvokation = (JSCallExpression) referenceExpression.getParent(); final JSArgumentList list = methodInvokation.getArgumentList(); final JSExpression[] expressions = list.getArguments(); int paramCount = expressions.length; for(int i = 0; i < paramCount; ++i) { if(i != 0) { template.addTextSegment(", "); } String var = null; final JSExpression passedParameterValue = expressions[i]; if(passedParameterValue instanceof JSReferenceExpression) { var = ((JSReferenceExpression) passedParameterValue).getReferencedName(); } if(var == null || var.length() == 0) { var = "param" + (i != 0 ? Integer.toString(i + 1) : ""); } final String var1 = var; Expression expression = new MyExpression(var1); template.addVariable(var, expression, expression, true); if(features.contains(JavaScriptFeature.CLASS)) { template.addTextSegment(":"); BaseCreateFix.guessExprTypeAndAddSuchVariable(passedParameterValue, template, var1, file, features); } } }
@RequiredReadAction @Override protected void buildTemplate(final Template template, JSReferenceExpression referenceExpression, Set<JavaScriptFeature> features, boolean staticContext, PsiFile file, PsiElement anchorParent) { super.buildTemplate(template, referenceExpression, features, staticContext, file, anchorParent); if(myIsMethod && !features.contains(JavaScriptFeature.CLASS)) { addSemicolonSegment(template, file); } }
public static boolean isLHSExpression(JSExpression expr) { if(expr instanceof JSDefinitionExpression) { expr = ((JSDefinitionExpression) expr).getExpression(); } if(expr instanceof JSReferenceExpression) { return true; } if(expr instanceof JSParenthesizedExpression) { return isLHSExpression(((JSParenthesizedExpression) expr).getInnerExpression()); } if(expr instanceof JSIndexedPropertyAccessExpression) { return true; } if(expr instanceof JSCallExpression) { return true; } if(expr instanceof JSNewExpression) { return true; } return false; }
@Override public String getName() { final JSPackageStatementStub stub = getStub(); if(stub != null) { return stub.getName(); } final PsiElement node = getNameIdentifier(); if(node != null) { return ((JSReferenceExpression) node).getReferencedName(); } return null; }
@Override public PsiElement setName(@NonNls @NotNull String name) throws IncorrectOperationException { VirtualFile virtualFile = getContainingFile().getVirtualFile(); String expectedPackageNameFromFile = JSResolveUtil.getExpectedPackageNameFromFile(virtualFile, getProject(), false); if(expectedPackageNameFromFile != null && expectedPackageNameFromFile.equals(getQualifiedName())) { try { JSPsiImplUtils.doRenameParentDirectoryIfNeeded(virtualFile, name, this); } catch(IOException ex) { throw new IncorrectOperationException("", ex); } } PsiElement child = getNameIdentifier(); if(child instanceof JSReferenceExpression) { JSReferenceExpression expr = (JSReferenceExpression) child; PsiElement element = expr.getReferenceNameElement(); if(element != null) { JSChangeUtil.doIdentifierReplacement(expr, element, name); } } return this; }
@RequiredReadAction @NotNull @Override public JSReferenceExpression[] getExpressions() { return findChildrenByClass(JSReferenceExpression.class); }
@Override @Nullable public String getNamespace() { final JSAttributeListStub attributeListStub = getStub(); if(attributeListStub != null) { return attributeListStub.getNamespace(); } final JSReferenceExpression namespaceElement = getNamespaceElement(); return namespaceElement != null ? namespaceElement.getText() : null; }
@Override public String getName() { final JSExpression expression = getExpression(); if(expression instanceof JSReferenceExpression) { return ((JSReferenceExpression) expression).getReferencedName(); } return null; }
private static boolean isInJS(final JSReferenceExpression node) { final PsiElement parent = node.getParent(); if(parent != null && !(parent.getLanguage() instanceof JavaScriptLanguage)) { return false; } return true; }
public JavaScriptKeywordCompletionContributor() { extend(CompletionType.BASIC, StandardPatterns.psiElement().withParent(JSReferenceExpression.class), new CompletionProvider() { @RequiredReadAction @Override public void addCompletions(@NotNull CompletionParameters parameters, ProcessingContext context, @NotNull CompletionResultSet result) { PsiElement position = parameters.getPosition(); JSReferenceExpression parent = (JSReferenceExpression) position.getParent(); if(parent.getQualifier() != null) { return; } boolean parentIsStatement = parent.getParent() instanceof JSExpressionStatement; LookupElementBuilder functionKeyword = LookupElementBuilder.create("function"); functionKeyword = functionKeyword.bold(); if(parentIsStatement) { functionKeyword = functionKeyword.withInsertHandler(SpaceInsertHandler.INSTANCE); } else { functionKeyword = functionKeyword.withInsertHandler(ParenthesesInsertHandler.getInstance(false)); functionKeyword = functionKeyword.withPresentableText("function()"); } result.addElement(functionKeyword); result.addElement(LookupElementBuilder.create("var").withInsertHandler(SpaceInsertHandler.INSTANCE).bold()); } }); }
protected CreateClassDialog(final Project project, String className, String packageName, boolean isInterface) { super(project, false); setTitle(JavaScriptBundle.message(isInterface ? "create.interface.dialog.title" : "create.class.dialog.title")); setModal(true); myPackageName.getDocument().addDocumentListener(new DocumentAdapter() { @Override protected void textChanged(final DocumentEvent e) { String text = getPackageName(); boolean enabled; if(text.length() == 0) { enabled = true; } else { ASTNode node = JSChangeUtil.createJSTreeFromText(project, text); PsiElement elt; enabled = node != null && (elt = node.getPsi()) instanceof JSExpressionStatement && (elt = ((JSExpressionStatement) elt).getExpression()) instanceof JSReferenceExpression && ((JSReferenceExpression) elt).getReferencedName() != null && elt.textMatches(text); } getOKAction().setEnabled(enabled); } }); myClassName.setText(className); myPackageName.setText(packageName); init(); }
@RequiredReadAction @Override protected void buildTemplate(final Template template, final JSReferenceExpression referenceExpression, final Set<JavaScriptFeature> features, boolean staticContext, final PsiFile file, final PsiElement anchorParent) { template.addTextSegment("/** @namespace "); template.addTextSegment(referenceExpression.getText() + " */"); template.addEndVariable(); }
@RequiredReadAction @Override protected void buildTemplate(final Template template, final JSReferenceExpression referenceExpression, final Set<JavaScriptFeature> features, boolean staticContext, final PsiFile file, final PsiElement anchorParent) { boolean classFeature = features.contains(JavaScriptFeature.CLASS); final JSExpression qualifier = addAccessModifier(template, referenceExpression, classFeature, staticContext); if(qualifier == null || classFeature) { template.addTextSegment(isConstant ? CONSTANT_STATEMENT_START : VAR_STATEMENT_START); } template.addTextSegment(classFeature ? referenceExpression.getReferencedName() : referenceExpression.getText()); template.addEndVariable(); if(classFeature) { template.addTextSegment(":"); } else { template.addTextSegment(" = "); } if(classFeature) { guessTypeAndAddTemplateVariable(template, referenceExpression, file); if(isConstant) { template.addTextSegment(" = "); addCompletionVar(template); } } else { addCompletionVar(template); } addSemicolonSegment(template, file); }
@Override protected void addParameters(Template template, JSReferenceExpression refExpr, PsiFile file, Set<JavaScriptFeature> features) { if(!myIsGetter) { template.addTextSegment(refExpr.getReferencedName() + ":"); guessTypeAndAddTemplateVariable(template, refExpr, file); } }
@Override protected void addReturnType(Template template, JSReferenceExpression referenceExpression, PsiFile file) { if(myIsGetter) { guessTypeAndAddTemplateVariable(template, referenceExpression, file); } else { template.addTextSegment("void"); } }
@Override protected void addBody(Template template, JSReferenceExpression refExpr, PsiFile file) { String varName = refExpr.getReferencedName(); String paramName = varName; JSCodeStyleSettings settings = CodeStyleSettingsManager.getInstance(file.getProject()).getCurrentSettings().getCustomSettings(JSCodeStyleSettings .class); varName = settings.FIELD_PREFIX + varName; if(varName.equals(paramName)) { varName = StringUtil.fixVariableNameDerivedFromPropertyName(varName); } if(myIsGetter) { template.addTextSegment("return "); addVarName(template, varName); template.addEndVariable(); } else { addVarName(template, varName); template.addEndVariable(); template.addTextSegment(" = " + paramName); } addSemicolonSegment(template, file); }
private Collection<JSQualifiedNamedElement> getCandidates(Editor editor, PsiFile file) { final Collection<JSQualifiedNamedElement> candidates; if(myReference instanceof JSReferenceExpression && ((JSReferenceExpression) myReference).getQualifier() == null) { Collection<JSQualifiedNamedElement> c = getCandidates(editor, file, myReference.getCanonicalText()); filterDefaultPackage(c); candidates = new THashSet<JSQualifiedNamedElement>(c, JSPsiImplUtils.QUALIFIED_NAME_HASHING_STRATEGY); } else { JSQualifiedNamedElement invalidResult = null; for(ResolveResult r : myReference.multiResolve(false)) { PsiElement element = r.getElement(); if(element instanceof JSQualifiedNamedElement) { invalidResult = (JSQualifiedNamedElement) element; } } if(invalidResult != null) { candidates = new SmartList<JSQualifiedNamedElement>(); candidates.add(invalidResult); } else { candidates = Collections.emptyList(); } } return candidates; }
protected void registerFunctionCallError(JSCallExpression expression){ final JSExpression methodExpression = expression.getMethodExpression(); PsiElement errorLocation = null; if (methodExpression instanceof JSReferenceExpression) { errorLocation = ((JSReferenceExpression)methodExpression).getReferenceNameElement(); } else if (methodExpression instanceof JSFunction) { final PsiElement node = ((JSFunction)methodExpression).getNameIdentifier(); if (node != null) errorLocation = node; else errorLocation = methodExpression; } registerError(errorLocation); }