public static PsiDirectory getJavaSrc(PsiFile psiFile) { PsiDirectory psiDirectory = null; if (psiFile instanceof PsiJavaFileImpl) { String packageName = ((PsiJavaFileImpl) psiFile).getPackageName(); String[] arg = packageName.split("\\."); psiDirectory = psiFile.getContainingDirectory(); for (int i = 0; i < arg.length; i++) { psiDirectory = psiDirectory.getParent(); if (psiDirectory == null) { break; } } } return psiDirectory; }
private PsiClass findExtensionClass( PsiElement element ) { PsiFile containingFile = element.getContainingFile(); if( !(containingFile instanceof PsiJavaFileImpl) ) { return null; } PsiJavaFileImpl file = (PsiJavaFileImpl)containingFile; for( PsiClass psiClass : file.getClasses() ) { if( psiClass.getModifierList().findAnnotation( Extension.class.getName() ) != null ) { return psiClass; } } return null; }
/** * Get parent which the cursor locate. * * @return */ private PsiClass getPsiClassFromContext() { // Get all classes in current file. PsiClass[] classes = ((PsiJavaFileImpl) psiFile).getClasses(); int len = classes.length; // Return null if there is no class in this file if (len <= 0) { return null; } // If there is only one class in this file, return it directly. if (1 == len) { return classes[0]; } // If there is more than one classes, return the class which cursor located. int offset = editor.getCaretModel().getOffset(); // the location of cursor. PsiElement elementAt = psiFile.findElementAt(offset); return PsiTreeUtil.getParentOfType(elementAt, PsiClass.class); }
public void actionPerformed(AnActionEvent e) { project = e.getProject(); if (null == project) { return; } PsiFile pf = e.getData(LangDataKeys.PSI_FILE); editor = e.getData(PlatformDataKeys.EDITOR); if (pf == null || editor == null) { return; } if (!(pf instanceof PsiJavaFileImpl)) { return; } psiFile = (PsiJavaFileImpl) pf; final PsiClass targetPsiClass = getPsiClassFromContext(); if (null == targetPsiClass) { return; } factory = JavaPsiFacade.getElementFactory(project); new WriteCommandAction.Simple(project, targetPsiClass.getContainingFile()) { @Override protected void run() throws Throwable { startGenerate(targetPsiClass); } }.execute(); }
@NotNull @Override @RequiredReadAction public PsiFile transform(String fileName, @NotNull MsilFile msilFile) { DotNetNamedElement[] members = msilFile.getMembers(); StringBuilder builder = new StringBuilder(); for(DotNetNamedElement member : members) { JavaClassStubBuilder build = StubBuilder.build((DotNetTypeDeclaration) member); if(build == null) { continue; } build.buildToText(builder, null); builder.append("\n"); } VirtualFile virtualFile = new MsilFileRepresentationVirtualFile(fileName, JavaFileType.INSTANCE, builder); SingleRootFileViewProvider viewProvider = new SingleRootFileViewProvider(PsiManager.getInstance(msilFile.getProject()), virtualFile, true); final PsiJavaFileImpl file = new PsiJavaFileImpl(viewProvider); viewProvider.forceCachedPsi(file); ((PsiManagerEx) PsiManager.getInstance(msilFile.getProject())).getFileManager().setViewProvider(virtualFile, viewProvider); new WriteCommandAction.Simple<Object>(file.getProject(), file) { @Override protected void run() throws Throwable { CodeStyleManager.getInstance(getProject()).reformat(file); } }.execute(); return file; }
@Override public PsiFile createFile(final FileViewProvider viewProvider) { return new PsiJavaFileImpl(viewProvider); }
private static boolean mayContainClassesInside(PsiElement psiElement, final CharSequence fileBuffer) { PsiFile psiFile = psiElement.getContainingFile(); boolean mayHaveClassesInside = false; if (psiFile instanceof PsiJavaFileImpl) { Lexer originalLexer = JavaParserDefinition.createLexer(((PsiJavaFileImpl)psiFile).getLanguageLevel()); FilterLexer lexer = new FilterLexer(originalLexer, new FilterLexer.SetFilter(StdTokenSets.WHITE_SPACE_OR_COMMENT_BIT_SET)); final TextRange range = psiElement.getTextRange(); lexer.start(fileBuffer, range.getStartOffset(), range.getEndOffset()); boolean isInNewExpression = false; boolean isRightAfterNewExpression = false; int angleLevel = 0; int parenLevel = 0; do { IElementType tokenType = lexer.getTokenType(); if (tokenType == null) break; if (tokenType == JavaTokenType.NEW_KEYWORD) { isInNewExpression = true; } else if (tokenType == JavaTokenType.LPARENTH) { if (isInNewExpression) parenLevel++; } else if (tokenType == JavaTokenType.LT) { if (isInNewExpression) angleLevel++; } else if (tokenType == JavaTokenType.GT) { if (isInNewExpression) angleLevel--; } else if (tokenType == JavaTokenType.RPARENTH) { if (isInNewExpression) { parenLevel--; if (parenLevel == 0) { isRightAfterNewExpression = true; } } } else if (tokenType == JavaTokenType.LBRACE) { if (isInNewExpression || isRightAfterNewExpression) { mayHaveClassesInside = true; } } else if (tokenType == JavaTokenType.LBRACKET) { if (parenLevel == 0 && angleLevel == 0) isInNewExpression = false; } else if (tokenType == JavaTokenType.INTERFACE_KEYWORD || tokenType == JavaTokenType.CLASS_KEYWORD || tokenType == JavaTokenType.ENUM_KEYWORD) { mayHaveClassesInside = true; } if (isInNewExpression && isRightAfterNewExpression) { isInNewExpression = false; } else { isRightAfterNewExpression = false; } lexer.advance(); } while (!mayHaveClassesInside); } return mayHaveClassesInside; }
@Override public PsiFile createFile(@NotNull final FileViewProvider viewProvider) { return new PsiJavaFileImpl(viewProvider); }