@NotNull @Override public PsiParameter createParameter(@NotNull final String name, @NotNull final PsiType type) throws IncorrectOperationException { PsiUtil.checkIsIdentifier(myManager, name); if (PsiType.NULL.equals(type)) { throw new IncorrectOperationException("Cannot create parameter with type \"null\"."); } final String text = type.getCanonicalText(true) + " " + name; PsiParameter parameter = createParameterFromText(text, null); final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myManager.getProject()); PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, JavaCodeStyleSettingsFacade.getInstance(myManager.getProject()).isGenerateFinalParameters()); GeneratedMarkerVisitor.markGenerated(parameter); parameter = (PsiParameter)JavaCodeStyleManager.getInstance(myManager.getProject()).shortenClassReferences(parameter); return (PsiParameter)codeStyleManager.reformat(parameter); }
public static void bindToElementViaStaticImport(final PsiClass qualifierClass, final String staticName, final PsiImportList importList) throws IncorrectOperationException { final String qualifiedName = qualifierClass.getQualifiedName(); final List<PsiJavaCodeReferenceElement> refs = getImportsFromClass(importList, qualifiedName); if (refs.size() < JavaCodeStyleSettingsFacade.getInstance(qualifierClass.getProject()).getNamesCountToUseImportOnDemand()) { importList.add(JavaPsiFacade.getInstance(qualifierClass.getProject()).getElementFactory().createImportStaticStatement(qualifierClass, staticName)); } else { for (PsiJavaCodeReferenceElement ref : refs) { final PsiImportStaticStatement importStatement = PsiTreeUtil.getParentOfType(ref, PsiImportStaticStatement.class); if (importStatement != null) { importStatement.delete(); } } importList.add(JavaPsiFacade.getInstance(qualifierClass.getProject()).getElementFactory().createImportStaticStatement(qualifierClass, "*")); } }
private static void addNewLineToTag(CompositeElement tag, Project project) { LOG.assertTrue(tag != null && tag.getElementType() == DOC_TAG); ASTNode current = tag.getLastChildNode(); while (current != null && current.getElementType() == DOC_COMMENT_DATA && isWhitespaceCommentData(current)) { current = current.getTreePrev(); } if (current != null && current.getElementType() == DOC_COMMENT_LEADING_ASTERISKS) return; final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(tag); final ASTNode newLine = Factory.createSingleLeafElement(DOC_COMMENT_DATA, "\n", 0, 1, treeCharTab, SharedImplUtil.getManagerByTree(tag)); tag.addChild(newLine, null); ASTNode leadingWhitespaceAnchor = null; if (JavaCodeStyleSettingsFacade.getInstance(project).isJavaDocLeadingAsterisksEnabled()) { final TreeElement leadingAsterisk = Factory.createSingleLeafElement(DOC_COMMENT_LEADING_ASTERISKS, "*", 0, 1, treeCharTab, SharedImplUtil.getManagerByTree(tag)); leadingWhitespaceAnchor = tag.addInternal(leadingAsterisk, leadingAsterisk, null, Boolean.TRUE); } final TreeElement commentData = Factory.createSingleLeafElement(DOC_COMMENT_DATA, " ", 0, 1, treeCharTab, SharedImplUtil.getManagerByTree(tag)); tag.addInternal(commentData, commentData, leadingWhitespaceAnchor, Boolean.TRUE); }
public JavaCoreProjectEnvironment(Disposable parentDisposable, CoreApplicationEnvironment applicationEnvironment) { super(parentDisposable, applicationEnvironment); myProject.registerService(PsiElementFactory.class, new PsiElementFactoryImpl(myPsiManager)); myProject.registerService(JavaPsiImplementationHelper.class, createJavaPsiImplementationHelper()); myProject.registerService(PsiResolveHelper.class, new PsiResolveHelperImpl(myPsiManager)); myProject.registerService(LanguageLevelProjectExtension.class, new CoreLanguageLevelProjectExtension()); myProject.registerService(JavaResolveCache.class, new JavaResolveCache(myMessageBus)); myProject.registerService(JavaCodeStyleSettingsFacade.class, new CoreJavaCodeStyleSettingsFacade()); myProject.registerService(JavaCodeStyleManager.class, new CoreJavaCodeStyleManager()); myPackageIndex = createCorePackageIndex(); myProject.registerService(PackageIndex.class, myPackageIndex); myFileManager = createCoreFileManager(); myProject.registerService(JavaFileManager.class, myFileManager); JavaPsiFacadeImpl javaPsiFacade = new JavaPsiFacadeImpl(myProject, myPsiManager, myFileManager, myMessageBus); myProject.registerService(JavaPsiFacade.class, javaPsiFacade); }
@NotNull @Override public PsiParameter createParameter(@NotNull final String name, @NotNull final PsiType type) throws IncorrectOperationException { PsiUtil.checkIsIdentifier(myManager, name); if (PsiType.NULL.equals(type)) { throw new IncorrectOperationException("Cannot create parameter with type \"null\"."); } final String text = type.getCanonicalText() + " " + name; PsiParameter parameter = createParameterFromText(text, null); final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myManager.getProject()); PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, JavaCodeStyleSettingsFacade.getInstance(myManager.getProject()).isGenerateFinalParameters()); GeneratedMarkerVisitor.markGenerated(parameter); parameter = (PsiParameter)JavaCodeStyleManager.getInstance(myManager.getProject()).shortenClassReferences(parameter); return (PsiParameter)codeStyleManager.reformat(parameter); }
@NotNull @Override public PsiDeclarationStatement createVariableDeclarationStatement(@NotNull final String name, @NotNull final PsiType type, final PsiExpression initializer) throws IncorrectOperationException { if (!JavaPsiFacade.getInstance(myManager.getProject()).getNameHelper().isIdentifier(name)) { throw new IncorrectOperationException("\"" + name + "\" is not an identifier."); } if (PsiType.NULL.equals(type)) { throw new IncorrectOperationException("Cannot create variable with type \"null\"."); } @NonNls final String text = "X " + name + (initializer != null ? " = x" : "") + ";"; final PsiDeclarationStatement statement = (PsiDeclarationStatement)createStatementFromText(text, null); final PsiVariable variable = (PsiVariable)statement.getDeclaredElements()[0]; replace(variable.getTypeElement(), createTypeElement(type), text); PsiUtil.setModifierProperty(variable, PsiModifier.FINAL, JavaCodeStyleSettingsFacade.getInstance(myManager.getProject()).isGenerateFinalLocals()); if (initializer != null) { replace(variable.getInitializer(), initializer, text); } GeneratedMarkerVisitor.markGenerated(statement); return statement; }
public JavaCoreProjectEnvironment(Disposable parentDisposable, CoreApplicationEnvironment applicationEnvironment) { super(parentDisposable, applicationEnvironment); myProject.registerService(PsiElementFactory.class, new PsiElementFactoryImpl(myPsiManager)); myProject.registerService(JavaPsiImplementationHelper.class, createJavaPsiImplementationHelper()); myProject.registerService(PsiResolveHelper.class, new PsiResolveHelperImpl(myPsiManager)); myProject.registerService(LanguageLevelProjectExtension.class, new CoreLanguageLevelProjectExtension()); myProject.registerService(JavaResolveCache.class, new JavaResolveCache(myMessageBus)); myProject.registerService(JavaCodeStyleSettingsFacade.class, new CoreJavaCodeStyleSettingsFacade()); myProject.registerService(JavaCodeStyleManager.class, new CoreJavaCodeStyleManager()); registerProjectExtensionPoint(PsiElementFinder.EP_NAME, PsiElementFinder.class); myPackageIndex = createCorePackageIndex(); myProject.registerService(PackageIndex.class, myPackageIndex); myFileManager = createCoreFileManager(); myProject.registerService(JavaFileManager.class, myFileManager); JavaPsiFacadeImpl javaPsiFacade = new JavaPsiFacadeImpl(myProject, myPsiManager, myFileManager, myMessageBus); registerProjectComponent(JavaPsiFacade.class, javaPsiFacade); myProject.registerService(JavaPsiFacade.class, javaPsiFacade); }
@NotNull @Override public PsiParameter createParameter(@NotNull final String name, @NotNull final PsiType type) throws IncorrectOperationException { PsiUtil.checkIsIdentifier(myManager, name); if(PsiType.NULL.equals(type)) { throw new IncorrectOperationException("Cannot create parameter with type \"null\"."); } final String text = type.getCanonicalText() + " " + name; PsiParameter parameter = createParameterFromText(text, null); final CodeStyleManager codeStyleManager = CodeStyleManager.getInstance(myManager.getProject()); PsiUtil.setModifierProperty(parameter, PsiModifier.FINAL, JavaCodeStyleSettingsFacade.getInstance(myManager.getProject()).isGenerateFinalParameters()); GeneratedMarkerVisitor.markGenerated(parameter); parameter = (PsiParameter) JavaCodeStyleManager.getInstance(myManager.getProject()).shortenClassReferences(parameter); return (PsiParameter) codeStyleManager.reformat(parameter); }
@NotNull @Override public PsiDeclarationStatement createVariableDeclarationStatement(@NotNull final String name, @NotNull final PsiType type, final PsiExpression initializer) throws IncorrectOperationException { if(!PsiNameHelper.getInstance(myManager.getProject()).isIdentifier(name)) { throw new IncorrectOperationException("\"" + name + "\" is not an identifier."); } if(PsiType.NULL.equals(type)) { throw new IncorrectOperationException("Cannot create variable with type \"null\"."); } final String text = "X " + name + (initializer != null ? " = x" : "") + ";"; final PsiDeclarationStatement statement = (PsiDeclarationStatement) createStatementFromText(text, null); final PsiVariable variable = (PsiVariable) statement.getDeclaredElements()[0]; replace(variable.getTypeElement(), createTypeElement(type), text); PsiUtil.setModifierProperty(variable, PsiModifier.FINAL, JavaCodeStyleSettingsFacade.getInstance(myManager.getProject()).isGenerateFinalLocals()); if(initializer != null) { replace(variable.getInitializer(), initializer, text); } GeneratedMarkerVisitor.markGenerated(statement); return statement; }
public JavaCoreProjectEnvironment(Disposable parentDisposable, CoreApplicationEnvironment applicationEnvironment) { super(parentDisposable, applicationEnvironment); myProject.registerService(PsiElementFactory.class, new PsiElementFactoryImpl(myPsiManager)); myProject.registerService(JavaPsiImplementationHelper.class, createJavaPsiImplementationHelper()); myProject.registerService(PsiResolveHelper.class, new PsiResolveHelperImpl(myPsiManager)); myProject.registerService(JavaResolveCache.class, new JavaResolveCache(myMessageBus)); myProject.registerService(JavaCodeStyleSettingsFacade.class, new CoreJavaCodeStyleSettingsFacade()); myProject.registerService(JavaCodeStyleManager.class, new CoreJavaCodeStyleManager()); registerProjectExtensionPoint(PsiElementFinder.EP_NAME, PsiElementFinder.class); /* myPackageIndex = createCorePackageIndex(); myProject.registerService(PackageIndex.class, myPackageIndex); */ myFileManager = createCoreFileManager(); myProject.registerService(JavaFileManager.class, myFileManager); PsiPackageManager manager = new PsiPackageManagerImpl(getProject(), DirectoryIndex.getInstance(getProject())); myProject.registerService(PsiPackageManager.class, manager); JavaPsiFacadeImpl javaPsiFacade = new JavaPsiFacadeImpl(myProject, manager); registerProjectComponent(JavaPsiFacade.class, javaPsiFacade); myProject.registerService(JavaPsiFacade.class, javaPsiFacade); }
@NotNull @Override public PsiDeclarationStatement createVariableDeclarationStatement(@NonNls @NotNull String name, @NotNull PsiType type, @Nullable PsiExpression initializer, @Nullable PsiElement context) throws IncorrectOperationException { if (!isIdentifier(name)) { throw new IncorrectOperationException("\"" + name + "\" is not an identifier."); } if (PsiType.NULL.equals(type)) { throw new IncorrectOperationException("Cannot create variable with type \"null\"."); } String text = "X " + name + (initializer != null ? " = x" : "") + ";"; PsiDeclarationStatement statement = (PsiDeclarationStatement)createStatementFromText(text, context); PsiVariable variable = (PsiVariable)statement.getDeclaredElements()[0]; replace(variable.getTypeElement(), createTypeElement(type), text); boolean generateFinalLocals = JavaCodeStyleSettingsFacade.getInstance(myManager.getProject()).isGenerateFinalLocals(); PsiUtil.setModifierProperty(variable, PsiModifier.FINAL, generateFinalLocals); if (initializer != null) { replace(variable.getInitializer(), initializer, text); } GeneratedMarkerVisitor.markGenerated(statement); return statement; }
private PsiElement bindToClass(PsiClass aClass) throws IncorrectOperationException { String qName = aClass.getQualifiedName(); boolean preserveQualification = JavaCodeStyleSettingsFacade.getInstance(getProject()).useFQClassNames() && isFullyQualified(); JavaPsiFacade facade = JavaPsiFacade.getInstance(getProject()); if (qName == null) { qName = aClass.getName(); PsiClass psiClass = facade.getResolveHelper().resolveReferencedClass(qName, this); if (!getManager().areElementsEquivalent(psiClass, aClass)) { throw cannotBindError(aClass); } } else if (facade.findClass(qName, getResolveScope()) == null && !preserveQualification) { return this; } List<PsiAnnotation> annotations = getAnnotations(); String text = qName; PsiReferenceParameterList parameterList = getParameterList(); if (parameterList != null) { text += parameterList.getText(); } PsiJavaCodeReferenceElement ref = facade.getParserFacade().createReferenceFromText(text, getParent()); getTreeParent().replaceChildInternal(this, (TreeElement)ref.getNode()); ((PsiJavaCodeReferenceElementImpl)ref).setAnnotations(annotations); if (!preserveQualification) { JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(aClass.getProject()); ref = (PsiJavaCodeReferenceElement)codeStyleManager.shortenClassReferences(ref, JavaCodeStyleManager.UNCOMPLETE_CODE); } return ref; }
private static void addNewLineToTag(CompositeElement tag, Project project) { LOG.assertTrue(tag != null && tag.getElementType() == DOC_TAG); ASTNode current = tag.getLastChildNode(); while(current != null && current.getElementType() == DOC_COMMENT_DATA && isWhitespaceCommentData(current)) { current = current.getTreePrev(); } if(current != null && current.getElementType() == DOC_COMMENT_LEADING_ASTERISKS) { return; } final CharTable treeCharTab = SharedImplUtil.findCharTableByTree(tag); final ASTNode newLine = Factory.createSingleLeafElement(DOC_COMMENT_DATA, "\n", 0, 1, treeCharTab, SharedImplUtil.getManagerByTree(tag)); tag.addChild(newLine, null); ASTNode leadingWhitespaceAnchor = null; if(JavaCodeStyleSettingsFacade.getInstance(project).isJavaDocLeadingAsterisksEnabled()) { final TreeElement leadingAsterisk = Factory.createSingleLeafElement(DOC_COMMENT_LEADING_ASTERISKS, "*", 0, 1, treeCharTab, SharedImplUtil.getManagerByTree(tag)); leadingWhitespaceAnchor = tag.addInternal(leadingAsterisk, leadingAsterisk, null, Boolean.TRUE); } final TreeElement commentData = Factory.createSingleLeafElement(DOC_COMMENT_DATA, " ", 0, 1, treeCharTab, SharedImplUtil.getManagerByTree(tag)); tag.addInternal(commentData, commentData, leadingWhitespaceAnchor, Boolean.TRUE); }
protected int getIndentSize() { return JavaCodeStyleSettingsFacade.getInstance(getProject()).getIndentSize(); }
private PsiElement bindToClass(@NotNull PsiClass aClass, @NotNull PsiFile containingFile) throws IncorrectOperationException { String qName = aClass.getQualifiedName(); Project project = containingFile.getProject(); boolean preserveQualification = JavaCodeStyleSettingsFacade.getInstance(project).useFQClassNames() && isFullyQualified(containingFile); JavaPsiFacade facade = JavaPsiFacade.getInstance(project); if (qName == null) { qName = aClass.getName(); PsiClass psiClass = facade.getResolveHelper().resolveReferencedClass(qName, this); if (!getManager().areElementsEquivalent(psiClass, aClass)) { throw cannotBindError(aClass); } } else if (facade.findClass(qName, getResolveScope()) == null && !preserveQualification) { return this; } List<PsiAnnotation> annotations = getAnnotations(); String text = qName; PsiReferenceParameterList parameterList = getParameterList(); if (parameterList != null) { text += parameterList.getText(); } PsiJavaCodeReferenceElement ref; try { ref = facade.getParserFacade().createReferenceFromText(text, getParent()); } catch (IncorrectOperationException e) { throw new IncorrectOperationException(e.getMessage() + " [qname=" + qName + " class=" + aClass + ";" + aClass.getClass().getName() + "]"); } ((PsiJavaCodeReferenceElementImpl)ref).setAnnotations(annotations); getTreeParent().replaceChildInternal(this, (TreeElement)ref.getNode()); if (!preserveQualification) { JavaCodeStyleManager codeStyleManager = JavaCodeStyleManager.getInstance(project); ref = (PsiJavaCodeReferenceElement)codeStyleManager.shortenClassReferences(ref, JavaCodeStyleManager.INCOMPLETE_CODE); } return ref; }
@Override public String getTemplateString(@NotNull PsiElement element) { String finalPart = JavaCodeStyleSettingsFacade.getInstance(element.getProject()).isGenerateFinalLocals() ? "final " : ""; return "for (" + finalPart + "$type$ $name$ : $expr$) {\n $END$\n}"; }
public static boolean addStaticImport(@NotNull String qualifierClass, @NonNls @NotNull String memberName, @NotNull PsiElement context) { if (!nameCanBeStaticallyImported(qualifierClass, memberName, context)) { return false; } final PsiClass containingClass = PsiTreeUtil.getParentOfType(context, PsiClass.class); if (InheritanceUtil.isInheritor(containingClass, qualifierClass)) { return true; } final PsiFile psiFile = context.getContainingFile(); if (!(psiFile instanceof PsiJavaFile)) { return false; } final PsiJavaFile javaFile = (PsiJavaFile)psiFile; final PsiImportList importList = javaFile.getImportList(); if (importList == null) { return false; } final PsiImportStatementBase existingImportStatement = importList.findSingleImportStatement(memberName); if (existingImportStatement != null) { if (existingImportStatement instanceof PsiImportStaticStatement) { final PsiImportStaticStatement importStaticStatement = (PsiImportStaticStatement)existingImportStatement; if (!memberName.equals(importStaticStatement.getReferenceName())) { return false; } final PsiClass targetClass = importStaticStatement.resolveTargetClass(); return targetClass != null && qualifierClass.equals(targetClass.getQualifiedName()); } return false; } final PsiImportStaticStatement onDemandImportStatement = findOnDemandImportStaticStatement(importList, qualifierClass); if (onDemandImportStatement != null && !hasOnDemandImportConflict(qualifierClass + '.' + memberName, javaFile)) { return true; } final Project project = context.getProject(); final GlobalSearchScope scope = context.getResolveScope(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiClass aClass = psiFacade.findClass(qualifierClass, scope); if (aClass == null) { return false; } final String qualifiedName = aClass.getQualifiedName(); if (qualifiedName == null) { return false; } final List<PsiImportStaticStatement> imports = getMatchingImports(importList, qualifiedName); final int onDemandCount = JavaCodeStyleSettingsFacade.getInstance(project).getNamesCountToUseImportOnDemand(); final PsiElementFactory elementFactory = psiFacade.getElementFactory(); if (imports.size() + 1 < onDemandCount) { importList.add(elementFactory.createImportStaticStatement(aClass, memberName)); } else { for (PsiImportStaticStatement importStatement : imports) { importStatement.delete(); } importList.add(elementFactory.createImportStaticStatement(aClass, "*")); } return true; }
public static boolean addStaticImport(@NotNull String qualifierClass, @NonNls @NotNull String memberName, @NotNull PsiElement context) { if (!nameCanBeStaticallyImported(qualifierClass, memberName, context)) { return false; } final PsiClass containingClass = PsiTreeUtil.getParentOfType(context, PsiClass.class); if (InheritanceUtil.isInheritor(containingClass, qualifierClass)) { return true; } final PsiFile psiFile = context.getContainingFile(); if (!(psiFile instanceof PsiJavaFile)) { return false; } final PsiJavaFile javaFile = (PsiJavaFile)psiFile; final PsiImportList importList = javaFile.getImportList(); if (importList == null) { return false; } final PsiImportStatementBase existingImportStatement = importList.findSingleImportStatement(memberName); if (existingImportStatement != null) { return false; } final PsiImportStaticStatement onDemandImportStatement = findOnDemandImportStaticStatement(importList, qualifierClass); if (onDemandImportStatement != null && !hasOnDemandImportStaticConflict(qualifierClass, memberName, context)) { return true; } final Project project = context.getProject(); final GlobalSearchScope scope = context.getResolveScope(); final JavaPsiFacade psiFacade = JavaPsiFacade.getInstance(project); final PsiClass aClass = psiFacade.findClass(qualifierClass, scope); if (aClass == null) { return false; } final String qualifiedName = aClass.getQualifiedName(); if (qualifiedName == null) { return false; } final List<PsiImportStaticStatement> imports = getMatchingImports(importList, qualifiedName); int onDemandCount = JavaCodeStyleSettingsFacade.getInstance(project).getNamesCountToUseImportOnDemand(); final PsiElementFactory elementFactory = psiFacade.getElementFactory(); if (imports.size() < onDemandCount) { importList.add(elementFactory.createImportStaticStatement(aClass, memberName)); } else { for (PsiImportStaticStatement importStatement : imports) { importStatement.delete(); } importList.add(elementFactory.createImportStaticStatement(aClass, "*")); } return true; }