当前位置: 首页>>代码示例>>Java>>正文


Java PsiDirectory.createFile方法代码示例

本文整理汇总了Java中com.intellij.psi.PsiDirectory.createFile方法的典型用法代码示例。如果您正苦于以下问题:Java PsiDirectory.createFile方法的具体用法?Java PsiDirectory.createFile怎么用?Java PsiDirectory.createFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在com.intellij.psi.PsiDirectory的用法示例。


在下文中一共展示了PsiDirectory.createFile方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: applyFix

import com.intellij.psi.PsiDirectory; //导入方法依赖的package包/类
@Override
public void applyFix(@NotNull Project project, @NotNull ProblemDescriptor descriptor) {
    final PsiElement element = descriptor.getPsiElement().getParent();
    PsiDirectory directory;
    String path = null;

    if (element instanceof StringLiteralExpression) {
        path = ((StringLiteralExpression) element).getContents();
    }

    if (path == null) {
        return;
    }

    if (path.startsWith("/")) {
        directory = ViewsUtil.getRootDirectory(element);
        path = path.substring(1);
    } else {
        directory = ViewsUtil.getContextDirectory(element);
    }

    if (directory == null) {
        return;
    }

    while (directory != null && path.contains("/")) {
        final String subdirectory = path.substring(0, path.indexOf('/'));
        path = path.substring(path.indexOf('/') + 1);

        directory = directory.findSubdirectory(subdirectory);
    }

    if (directory != null) {
        if (!path.contains(".")) {
            path = path + ".php";
        }

        final PsiFile viewPsiFile = directory.createFile(path);
        FileEditorManager.getInstance(project).openFile(viewPsiFile.getVirtualFile(), true);

        final FileTemplate[] templates = FileTemplateManager.getDefaultInstance().getTemplates(FileTemplateManager.DEFAULT_TEMPLATES_CATEGORY);
        FileTemplate template = null;
        for (FileTemplate fileTemplate : templates) {
            if (fileTemplate.getName().equals("PHP File")) {
                template = fileTemplate;
                break;
            }
        }

        if (template != null && viewPsiFile.getViewProvider().getDocument() != null) {
            final Properties properties = FileTemplateManager.getDefaultInstance().getDefaultProperties();
            template.setLiveTemplateEnabled(true);
            template.setReformatCode(true);
            try {
                viewPsiFile.getViewProvider().getDocument().insertString(0, template.getText(properties));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
 
开发者ID:nvlad,项目名称:yii2support,代码行数:62,代码来源:MissedViewLocalQuickFix.java

示例2: doCreate

import com.intellij.psi.PsiDirectory; //导入方法依赖的package包/类
private PsiFile doCreate( PsiDirectory dir, String className, String fqnExtended ) throws IncorrectOperationException
{
  Project project = dir.getProject();

  String fileName = className + ".java";
  VirtualFile srcRoot = ProjectRootManager.getInstance( project ).getFileIndex().getSourceRootForFile( dir.getVirtualFile() );
  dir = getPsiDirectoryForExtensionClass( dir, fqnExtended, srcRoot );

  final PsiPackage pkg = JavaDirectoryService.getInstance().getPackage( dir );
  if( pkg == null )
  {
    throw new IncorrectOperationException( ManBundle.message( "error.new.artifact.nopackage" ) );
  }

  String text =
    "package " + pkg.getQualifiedName() + ";\n" +
    "\n" +
    "import manifold.ext.api.Extension;\n" +
    "import manifold.ext.api.This;\n" +
    "import " + fqnExtended + ";\n" +
    "\n" +
    "@Extension\n" +
    "public class " + className + " {\n" +
    "  public static " + processTypeVars( dir, fqnExtended, StubBuilder::makeTypeVar ) + "void helloWorld(@This " + ClassUtil.extractClassName( fqnExtended ) + processTypeVars( dir, fqnExtended, PsiNamedElement::getName ) + " thiz) {\n" +
    "    System.out.println(\"hello world!\");\n" +
    "  }\n" +
    "}";

  dir.checkCreateFile( fileName );

  final PsiFile file = dir.createFile( fileName );

  try
  {
    new CharSink()
    {
      public Writer openStream() throws IOException
      {
        return new OutputStreamWriter( file.getVirtualFile().getOutputStream( null ) );
      }
    }.write( text );
  }
  catch( IOException e )
  {
    throw new IncorrectOperationException( e.getMessage(), (Throwable)e );
  }

  return file;
}
 
开发者ID:manifold-systems,项目名称:manifold-ij,代码行数:50,代码来源:CreateExtensionMethodsClassAction.java


注:本文中的com.intellij.psi.PsiDirectory.createFile方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。