public static String findPackageForMxml(final PsiElement expression) { String s = null; final PsiFile containingFile = expression.getContainingFile(); if(containingFile.getLanguage() == JavaScriptSupportLoader.ECMA_SCRIPT_L4 && containingFile.getContext() != null) { final PsiFile contextContainigFile = containingFile.getContext().getContainingFile(); VirtualFile file = contextContainigFile.getVirtualFile(); if(file == null && contextContainigFile.getOriginalFile() != null) { file = contextContainigFile.getOriginalFile().getVirtualFile(); } s = getExpectedPackageNameFromFile(file, containingFile.getProject(), true); } return s; }
public static JSClass getClassFromTagNameInMxml(final PsiElement psiElement) { XmlTag tag = psiElement != null ? PsiTreeUtil.getNonStrictParentOfType(psiElement, XmlTag.class) : null; if(tag != null && (tag.getNamespacePrefix().length() > 0 || JavaScriptSupportLoader.isFlexMxmFile(tag.getContainingFile()))) { if(isScriptContextTag(tag)) { tag = ((XmlFile) tag.getContainingFile()).getDocument().getRootTag(); } final XmlElementDescriptor descriptor = tag.getDescriptor(); if(descriptor != null) { PsiElement decl = descriptor.getDeclaration(); if(decl instanceof JSClass) { return ((JSClass) decl); } else if(decl instanceof XmlFile) { return XmlBackedJSClassImpl.getXmlBackedClass((XmlFile) decl); } } } return null; }
public static String findScriptNs(XmlTag rootTag) { String ns = rootTag.getNamespace(); if(JavaScriptSupportLoader.isFlexMxmFile(rootTag.getContainingFile())) { ns = ""; for(String testNs : JavaScriptSupportLoader.MXML_URIS) { if(rootTag.getPrefixByNamespace(testNs) != null) { ns = testNs; break; } } } return ns; }
@NotNull @Override public String[] getNames(Project project, boolean includeNonProjectItems) { final Set<String> result = new HashSet<String>(); result.addAll(StubIndex.getInstance().getAllKeys(JavaScriptIndexKeys.ELEMENTS_BY_NAME, project)); FileBasedIndex.getInstance().processAllKeys(FilenameIndex.NAME, new Processor<String>() { @Override public boolean process(String s) { if(JavaScriptSupportLoader.isFlexMxmFile(s)) { result.add(FileUtil.getNameWithoutExtension(s)); } return true; } }, project); return result.toArray(new String[result.size()]); }
@Override public void actionPerformed(final AnActionEvent e) { Editor editor = e.getData(PlatformDataKeys.EDITOR); PsiFile psifile = e.getData(LangDataKeys.PSI_FILE); Project project = e.getData(PlatformDataKeys.PROJECT); final VirtualFile file = e.getData(PlatformDataKeys.VIRTUAL_FILE); if(JavaScriptSupportLoader.isFlexMxmFile(file)) { editor = BaseCodeInsightAction.getInjectedEditor(project, editor); psifile = PsiUtilBase.getPsiFileInEditor(editor, project); } new JavaScriptGenerateAccessorHandler(getGenerationMode()).invoke(project, editor, psifile); }
private static void process(final JSNamedElement node, final ProblemsHolder holder) { if(node.getContainingFile().getLanguage() != JavaScriptSupportLoader.ECMA_SCRIPT_L4) { return; } PsiElement nameIdentifier = node.getNameIdentifier(); if(nameIdentifier != null && JSPsiImplUtils.getTypeFromDeclaration(node) == null && (!(node instanceof JSParameter) || !((JSParameter) node).isRest())) { holder.registerProblem(nameIdentifier, JavaScriptBundle.message(node instanceof JSFunction ? "js.untyped.function.problem" : "js.untyped" + ".variable.problem", nameIdentifier.getText()), ProblemHighlightType.GENERIC_ERROR_OR_WARNING, new AddTypeToDclFix()); } }
public EmberJSParser(PsiBuilder builder) { super(JavaScriptSupportLoader.JAVASCRIPT_1_5, builder); myExpressionParser = new EmberJSExpressionParser(); myStatementParser = new StatementParser<EmberJSParser>(this) { @Override protected void doParseStatement(boolean canHaveClasses) { final IElementType firstToken = builder.getTokenType(); if (firstToken == JSTokenTypes.LBRACE) { parseExpressionStatement(); checkForSemicolon(); return; } if (isIdentifierToken(firstToken)) { final IElementType nextToken = builder.lookAhead(1); if (nextToken == JSTokenTypes.IN_KEYWORD) { parseInStatement(); return; } } if (builder.getTokenType() == JSTokenTypes.LPAR) { if (parseInStatement()) { return; } } super.doParseStatement(canHaveClasses); } private boolean parseInStatement() { PsiBuilder.Marker statement = builder.mark(); if (!getExpressionParser().parseInExpression()) { statement.drop(); return false; } statement.done(JSElementTypes.EXPRESSION_STATEMENT); return true; } }; }
private static String doGenerateDoc(final JSFunction function) { StringBuilder builder = new StringBuilder(); final JSParameterList parameterList = function.getParameterList(); final PsiFile containingFile = function.getContainingFile(); final boolean ecma = containingFile.getLanguage() == JavaScriptSupportLoader.ECMA_SCRIPT_L4; if(parameterList != null) { for(JSParameter parameter : parameterList.getParameters()) { builder.append("* @param ").append(parameter.getName()); //String s = JSPsiImplUtils.getTypeFromDeclaration(parameter); //if (s != null) builder.append(" : ").append(s); builder.append("\n"); } } if(ecma) { String s = JSPsiImplUtils.getTypeFromDeclaration(function); if(s != null && !"void".equals(s)) { builder.append("* @return "); //builder.append(s); builder.append("\n"); } } return builder.toString(); }
public static Collection<JSQualifiedNamedElement> findElementsByName(String name, Project project, GlobalSearchScope scope) { final Set<JSQualifiedNamedElement> result = new HashSet<JSQualifiedNamedElement>(); Collection<JSQualifiedNamedElement> jsQualifiedNamedElements = StubIndex.getElements(JavaScriptIndexKeys.ELEMENTS_BY_NAME, name, project, scope, JSQualifiedNamedElement.class); for(JSQualifiedNamedElement e : jsQualifiedNamedElements) { result.add((JSQualifiedNamedElement) e.getNavigationElement()); } Collection<VirtualFile> files = new ArrayList<VirtualFile>(); files.addAll(FileBasedIndex.getInstance().getContainingFiles(FilenameIndex.NAME, name + JavaScriptSupportLoader.MXML_FILE_EXTENSION_DOT, scope)); files.addAll(FileBasedIndex.getInstance().getContainingFiles(FilenameIndex.NAME, name + JavaScriptSupportLoader.MXML_FILE_EXTENSION2_DOT, scope)); for(final VirtualFile file : files) { if(!file.isValid()) { continue; } final PsiFile psiFile = PsiManager.getInstance(project).findFile(file); if(psiFile != null) { result.add(XmlBackedJSClassImpl.getXmlBackedClass((XmlFile) psiFile)); } } return result; }
public static String getQNameToStartHierarchySearch(final JSFunction node) { PsiElement parentNode = node.getParent(); parentNode = getClassReferenceForXmlFromContext(parentNode); if(parentNode instanceof JSClass) { final JSAttributeList attributeList = node.getAttributeList(); if(attributeList == null || !attributeList.hasModifier(JSAttributeList.ModifierType.OVERRIDE) || attributeList.hasModifier(JSAttributeList.ModifierType.STATIC) || attributeList.getAccessType() == JSAttributeList.AccessType.PRIVATE) { return null; } if(parentNode instanceof JSClass) { return ((JSClass) parentNode).getQualifiedName(); } } else if(node instanceof JSFunctionExpression && parentNode.getContainingFile().getLanguage() != JavaScriptSupportLoader.ECMA_SCRIPT_L4) { final ContextResolver resolver = new ContextResolver(node.getFirstChild()); return resolver.getQualifierAsString(); } else if(parentNode instanceof JSFile && parentNode.getContainingFile().getLanguage() != JavaScriptSupportLoader.ECMA_SCRIPT_L4) { return node.getName(); } return null; }
public ImplicitJSVariableImpl(final String name, String qName, PsiFile containingFile) { super(containingFile.getManager(), JavaScriptSupportLoader.ECMA_SCRIPT_L4.getBaseLanguage()); myContainingFile = containingFile; myName = name; myType = qName; }
public static boolean isAdequatePlaceForImport(final PsiNamedElement parent, @NotNull PsiElement place) { if(parent instanceof JSFile && !parent.getLanguage().isKindOf(JavaScriptSupportLoader.ECMA_SCRIPT_L4)) { return false; } if(place instanceof JSReferenceExpression) { final PsiElement placeParent = place.getParent(); if(placeParent instanceof JSReferenceExpression) { final PsiElement currentParent = JSResolveUtil.getTopReferenceParent(placeParent); if(JSResolveUtil.isSelfReference(currentParent, place) || //currentParent instanceof JSDefinitionExpression || currentParent instanceof JSReferenceList) { return false; } } } else if(place instanceof JSDocTagValue) { // further conditions to come } else { if(!(place instanceof JSFile)) { return false; } } return true; }
public static String getSignatureForParameter(final JSParameter p, boolean skipType) { final String s = skipType ? null : p.getTypeString(); if(s != null && s.length() > 0) { final boolean ecmal4 = p.getContainingFile().getLanguage() == JavaScriptSupportLoader.ECMA_SCRIPT_L4; String result; if(ecmal4) { if(p.isRest()) { result = "..."; } else { result = p.getName() + ":" + s; } } else { result = "[" + s + "] " + p.getName(); } final String initializerText = p.getInitializerText(); if(initializerText != null) { result += " = " + initializerText; } return result; } return p.getName(); }
protected static String getExceptionVarTypeBasedOnContext(@NotNull PsiElement context) { if(context.getContainingFile().getLanguage() == JavaScriptSupportLoader.ECMA_SCRIPT_L4) { return ":Error"; } return ""; }
@Override public void update(final AnActionEvent e) { final VirtualFile file = e.getData(PlatformDataKeys.VIRTUAL_FILE); boolean status = false; if(file != null) { if(file.getFileType() == JavaScriptFileType.INSTANCE) { final Editor editor = e.getData(PlatformDataKeys.EDITOR); final PsiFile psifile = e.getData(LangDataKeys.PSI_FILE); if(editor != null && psifile != null) { status = psifile.getLanguage() == JavaScriptSupportLoader.ECMA_SCRIPT_L4; } } else if(JavaScriptSupportLoader.isFlexMxmFile(file)) { status = true; } } e.getPresentation().setEnabled(status); e.getPresentation().setVisible(status); }
private static ProblemHighlightType getHighlightTypeForTypeOrSignatureProblem(@NotNull PsiElement node) { if(node.getContainingFile().getLanguage() == JavaScriptSupportLoader.ECMA_SCRIPT_L4) { return ProblemHighlightType.GENERIC_ERROR; } return ProblemHighlightType.GENERIC_ERROR_OR_WARNING; }
@Override protected JSExpression findIntroducedExpression(final PsiFile file, final int start, final int end, Editor editor) { if(file.getLanguage() != JavaScriptSupportLoader.ECMA_SCRIPT_L4) { CommonRefactoringUtil.showErrorHint(file.getProject(), editor, JavaScriptBundle.message("javascript.introduce.field.error.not.available.in.javascript" + ".code"), getRefactoringName(), null); return null; } return super.findIntroducedExpression(file, start, end, editor); }
@Override public void processIntention(@NotNull PsiElement element) throws IncorrectOperationException { assert (element instanceof JSVarStatement); final JSVarStatement varStatement = (JSVarStatement) element; StringBuilder declarationBuffer = new StringBuilder(); List<String> initializations = new ArrayList<String>(); for (JSVariable variable : varStatement.getVariables()) { declarationBuffer.append((declarationBuffer.length() == 0) ? VAR_KEYWORD : ",") .append(variable.getName()); String s = JSPsiImplUtils.getTypeFromDeclaration(variable); final PsiFile containingFile = element.getContainingFile(); if (s == null && containingFile.getLanguage() == JavaScriptSupportLoader.ECMA_SCRIPT_L4) { s = JSResolveUtil.getExpressionType(variable.getInitializer(), containingFile); } if (s != null) { declarationBuffer.append(":").append(s); } if (variable.hasInitializer()) { initializations.add(variable.getName() + '=' + variable.getInitializer().getText() + ';'); } } declarationBuffer.append(';'); // Do replacement. JSStatement newStatement = JSElementFactory.replaceStatement(varStatement, declarationBuffer.toString()); for (final String initialization : initializations) { newStatement = JSElementFactory.addStatementAfter(newStatement, initialization); } }
public static boolean isNewResolveAndCompletion(PsiFile psiFile) { return psiFile.getLanguage().isKindOf(JavaScriptSupportLoader.ECMA_SCRIPT_L4) || JavaScriptSupportLoader.isFlexMxmFile(psiFile); }
public ResolveProcessor(final String name, PsiElement _place) { myName = name; place = _place; final boolean b = place instanceof JSReferenceExpression && ((JSReferenceExpression) place).getQualifier() == null; allowUnqualifiedStaticsFromInstance = b; if(myName != null && place instanceof JSReferenceExpression) { final ASTNode node = place.getNode().findChildByType(JSTokenTypes.COLON_COLON); String explicitNs = null; // TODO: e.g. protected is also ns if(node != null) { final JSExpression expression = ((JSReferenceExpression) place).getQualifier(); if(expression instanceof JSReferenceExpression) { anyNsAllowed = ((JSReferenceExpression) expression).resolve() instanceof JSVariable; } explicitNs = expression != null ? expression.getText() : null; } else { final JSExpression qualifier = ((JSReferenceExpression) place).getQualifier(); if(qualifier instanceof JSReferenceExpression && qualifier.getNode().findChildByType(JSTokenTypes.COLON_COLON) != null) { anyNsAllowed = ((JSReferenceExpression) qualifier).resolve() instanceof JSVariable; explicitNs = ((JSReferenceExpression) qualifier).getReferencedName(); } } if(explicitNs != null && !anyNsAllowed) { openedNses = new THashSet<String>(); openedNses.add(explicitNs); defaultNsIsNotAllowed = true; } } if(myName == null && place instanceof JSReferenceExpression) { PsiElement parent = place.getParent(); if(parent instanceof JSReferenceList) { if(parent.getNode().getElementType() == JSElementTypes.EXTENDS_LIST) { final PsiElement element = parent.getParent(); if(element instanceof JSClass) { if(((JSClass) element).isInterface()) { myAcceptOnlyClasses = false; myAcceptOnlyInterfaces = true; } else { myAcceptOnlyClasses = true; myAcceptOnlyInterfaces = false; } } } else { myAcceptOnlyClasses = false; myAcceptOnlyInterfaces = true; } } } ecma = place != null && place.getContainingFile().getLanguage() == JavaScriptSupportLoader.ECMA_SCRIPT_L4; }
public static boolean isInlineComponentTag(XmlTag tag) { return COMPONENT_TAG_NAME.equals(tag.getLocalName()) && JavaScriptSupportLoader.isMxmlNs(tag.getNamespace()) && !(tag.getParent() instanceof XmlDocument); }
static ProblemHighlightType getUnresolveReferenceHighlightType(final @Nullable JSExpression qualifier, @NotNull JSExpression node) { JSClass jsClass; final PsiFile containingFile = node.getContainingFile(); if(qualifier != null) { jsClass = JSResolveUtil.findClassOfQualifier(qualifier, containingFile); if(jsClass == null && (qualifier instanceof JSReferenceExpression)) { ResolveResult[] results = ((JSReferenceExpression) qualifier).multiResolve(false); if(results.length != 0) { PsiElement resultElement = results[0].getElement(); String type = null; if(resultElement instanceof JSVariable) { type = ((JSVariable) resultElement).getTypeString(); } else if(resultElement instanceof JSFunction) { type = ((JSFunction) resultElement).getReturnTypeString(); } if("*".equals(type)) { return ProblemHighlightType.LIKE_UNKNOWN_SYMBOL; } jsClass = JSResolveUtil.getClassOfContext(resultElement); } } } else { jsClass = JSResolveUtil.getClassOfContext(node); } final boolean ecmaL4File = containingFile.getLanguage() == JavaScriptSupportLoader.ECMA_SCRIPT_L4; if(jsClass != null && ecmaL4File && (!(jsClass instanceof XmlBackedJSClassImpl) || jsClass.getContainingFile().getFileType() == XmlFileType.INSTANCE)) { final JSAttributeList attributeList = jsClass.getAttributeList(); if(attributeList == null || !attributeList.hasModifier(JSAttributeList.ModifierType.DYNAMIC)) { return ProblemHighlightType.ERROR; } } if(ecmaL4File && jsClass == null && node.getParent() instanceof JSReferenceList) { return ProblemHighlightType.ERROR; } return ProblemHighlightType.LIKE_UNKNOWN_SYMBOL; }
@Override public boolean supports(PsiFile file) { return file.getLanguage() == JavaScriptSupportLoader.ECMA_SCRIPT_L4 || JavaScriptSupportLoader.isFlexMxmFile(file); }
@Override protected ProblemHighlightType getProblemHighlightType(PsiElement location) { return location.getContainingFile().getLanguage() == JavaScriptSupportLoader.ECMA_SCRIPT_L4 ? ProblemHighlightType.GENERIC_ERROR:super.getProblemHighlightType(location); }