com.intellij.psi.PsiFileFactory#getInstance ( )源码实例Demo

下面列出了com.intellij.psi.PsiFileFactory#getInstance ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: reasonml-idea-plugin   文件: PromptConsole.java
PromptConsole(@NotNull Project project, ConsoleViewImpl consoleView) {
    m_consoleView = consoleView;

    EditorFactory editorFactory = EditorFactory.getInstance();
    PsiFileFactory fileFactory = PsiFileFactory.getInstance(project);

    Document outputDocument = ((EditorFactoryImpl) editorFactory).createDocument(true);
    UndoUtil.disableUndoFor(outputDocument);
    m_outputEditor = (EditorImpl) editorFactory.createViewer(outputDocument, project, CONSOLE);

    PsiFile file = fileFactory.createFileFromText("PromptConsoleDocument.ml", OclLanguage.INSTANCE, "");
    Document promptDocument = file.getViewProvider().getDocument();
    m_promptEditor = (EditorImpl) editorFactory.createEditor(promptDocument, project, CONSOLE);

    setupOutputEditor();
    setupPromptEditor();

    m_consoleView.print("(* ctrl+enter to send a command, ctrl+up/down to cycle through history *)\r\n", USER_INPUT);

    m_mainPanel.add(m_outputEditor.getComponent(), BorderLayout.CENTER);
    m_mainPanel.add(m_promptEditor.getComponent(), BorderLayout.SOUTH);
}
 
public static PsiFile createFromTemplate(final PsiDirectory directory, final String name,
                                         String fileName, String templateName,
                                         @NonNls String... parameters) throws IncorrectOperationException {

    final FileTemplate template = FileTemplateManager.getInstance(directory.getProject()).getInternalTemplate(templateName);
    String text;

    try {
        text = template.getText();
    } catch (Exception e) {
        throw new RuntimeException("Unable to load template for " +
                FileTemplateManager.getInstance().internalTemplateToSubject(templateName), e);
    }

    final PsiFileFactory factory = PsiFileFactory.getInstance(directory.getProject());

    final PsiFile file = factory.createFileFromText(fileName, WeexFileType.INSTANCE, text);
    CodeStyleManager.getInstance(directory.getProject()).reformat(file);
    return (PsiFile) directory.add(file);
}
 
/**
 * Creates the syntax tree for a Dart file at a specific path and returns the innermost element with the given text.
 */
@NotNull
protected <E extends PsiElement> E setUpDartElement(String filePath, String fileText, String elementText, Class<E> expectedClass) {
  final int offset = fileText.indexOf(elementText);
  if (offset < 0) {
    throw new IllegalArgumentException("'" + elementText + "' not found in '" + fileText + "'");
  }

  final PsiFileFactory factory = PsiFileFactory.getInstance(fixture.getProject());
  final PsiFile file = filePath != null
                       ? factory.createFileFromText(filePath, DartLanguage.INSTANCE, fileText)
                       : factory.createFileFromText(DartLanguage.INSTANCE, fileText);

  PsiElement elt = file.findElementAt(offset);
  while (elt != null) {
    if (elementText.equals(elt.getText())) {
      return expectedClass.cast(elt);
    }
    elt = elt.getParent();
  }

  throw new RuntimeException("unable to find element with text: " + elementText);
}
 
/**
 * Creates the syntax tree for a Dart file at a specific path and returns the innermost element with the given text.
 */
@NotNull
protected <E extends PsiElement> E setUpDartElement(String filePath, String fileText, String elementText, Class<E> expectedClass) {
  final int offset = fileText.indexOf(elementText);
  if (offset < 0) {
    throw new IllegalArgumentException("'" + elementText + "' not found in '" + fileText + "'");
  }

  final PsiFileFactory factory = PsiFileFactory.getInstance(fixture.getProject());
  final PsiFile file = filePath != null
                       ? factory.createFileFromText(filePath, DartLanguage.INSTANCE, fileText)
                       : factory.createFileFromText(DartLanguage.INSTANCE, fileText);

  PsiElement elt = file.findElementAt(offset);
  while (elt != null) {
    if (elementText.equals(elt.getText())) {
      return expectedClass.cast(elt);
    }
    elt = elt.getParent();
  }

  throw new RuntimeException("unable to find element with text: " + elementText);
}
 
源代码5 项目: idea-gitignore   文件: IgnoreTemplatesFactory.java
/**
 * Creates new Gitignore file or uses an existing one.
 *
 * @param directory working directory
 * @return file
 */
@Nullable
public PsiFile createFromTemplate(final PsiDirectory directory) throws IncorrectOperationException {
    final String filename = fileType.getIgnoreLanguage().getFilename();
    final PsiFile currentFile = directory.findFile(filename);
    if (currentFile != null) {
        return currentFile;
    }
    final PsiFileFactory factory = PsiFileFactory.getInstance(directory.getProject());
    final IgnoreLanguage language = fileType.getIgnoreLanguage();

    String content = StringUtil.join(TEMPLATE_NOTE, Constants.NEWLINE);
    if (language.isSyntaxSupported() && !IgnoreBundle.Syntax.GLOB.equals(language.getDefaultSyntax())) {
        content = StringUtil.join(
                content,
                IgnoreBundle.Syntax.GLOB.getPresentation(),
                Constants.NEWLINE,
                Constants.NEWLINE
        );
    }
    final PsiFile file = factory.createFileFromText(filename, fileType, content);
    return (PsiFile) directory.add(file);
}
 
源代码6 项目: EasyCode   文件: SaveFile.java
/**
 * 构建对象
 *
 * @param path     路径
 * @param fileName 文件没
 * @param reformat 是否重新格式化代码
 */
public SaveFile(Project project, String path, String fileName, String content, boolean reformat, boolean operateTitle) {
    this.path = path;
    this.project = project;
    // 构建文件对象
    PsiFileFactory psiFileFactory = PsiFileFactory.getInstance(project);
    LOG.assertTrue(content != null);
    FileType fileType = FileTypeManager.getInstance().getFileTypeByFileName(fileName);
    // 换行符统一使用\n
    this.file = psiFileFactory.createFileFromText(fileName, fileType, content.replace("\r", ""));
    this.virtualFile = new LightVirtualFile(fileName, fileType, content.replace("\r", ""));
    this.reformat = reformat;
    this.operateTitle = operateTitle;
}
 
源代码7 项目: reasonml-idea-plugin   文件: ORSignatureTest.java
@SuppressWarnings({"SameParameterValue", "ConstantConditions"})
@NotNull
private ORSignature makeSignature(@NotNull Language lang, String sig, boolean debug) {
    PsiFileFactory instance = PsiFileFactory.getInstance(getProject());
    PsiFile psiFile = instance.createFileFromText("Dummy." + lang.getAssociatedFileType().getDefaultExtension(), lang, "let x:" + sig);
    if (debug) {
        System.out.println(DebugUtil.psiToString(psiFile, true, true));
    }
    Collection<PsiSignatureItem> items = PsiTreeUtil.findChildrenOfType(psiFile, PsiSignatureItem.class);
    return new ORSignature(RmlLanguage.INSTANCE, items);
}
 
源代码8 项目: consulo   文件: ScratchFileCreationHelper.java
@Nullable
public static PsiFile parseHeader(@Nonnull Project project, @Nonnull Language language, @Nonnull String text) {
  LanguageFileType fileType = language.getAssociatedFileType();
  CharSequence fileSnippet = StringUtil.first(text, 10 * 1024, false);
  PsiFileFactory fileFactory = PsiFileFactory.getInstance(project);
  return fileFactory.createFileFromText(PathUtil.makeFileName("a", fileType == null ? "" : fileType.getDefaultExtension()), language, fileSnippet);
}
 
源代码9 项目: json2java4idea   文件: NewClassCommandAction.java
@Inject
public NewClassCommandAction(@Nonnull Project project,
                             @Nonnull @Assisted("Name") String name,
                             @Nonnull @Assisted("Json") String json,
                             @Nonnull @Assisted PsiDirectory directory,
                             @Nonnull @Assisted JavaConverter converter) {
    super(project);
    this.fileFactory = PsiFileFactory.getInstance(project);
    this.directoryService = JavaDirectoryService.getInstance();
    this.name = Preconditions.checkNotNull(name);
    this.json = Preconditions.checkNotNull(json);
    this.directory = Preconditions.checkNotNull(directory);
    this.converter = Preconditions.checkNotNull(converter);
}
 
源代码10 项目: json2java4idea   文件: FormatterTest.java
@Before
@Override
public void setUp() throws Exception {
    super.setUp();
    final Project project = getProject();
    fileFactory = PsiFileFactory.getInstance(project);
}
 
源代码11 项目: antlr4-intellij-adaptor   文件: Trees.java
public static PsiElement createLeafFromText(Project project, Language language, PsiElement context,
											String text, IElementType type)
{
	PsiFileFactoryImpl factory = (PsiFileFactoryImpl) PsiFileFactory.getInstance(project);
	PsiElement el = factory.createElementFromText(text, language, type, context);
	if ( el==null ) return null;
	return PsiTreeUtil.getDeepestFirst(el); // forces parsing of file!!
	// start rule depends on root passed in
}
 
源代码12 项目: antlr4-intellij-adaptor   文件: Trees.java
public static PsiFile createFile(Project project, Language language, String text) {
	LanguageFileType ftype = language.getAssociatedFileType();
	if ( ftype==null ) return null;
	String ext = ftype.getDefaultExtension();
	String fileName = "___fubar___."+ext; // random name but must have correct extension
	PsiFileFactoryImpl factory = (PsiFileFactoryImpl)PsiFileFactory.getInstance(project);
	return factory.createFileFromText(fileName, language, text, false, false);
}
 
源代码13 项目: intellij-plugin-v4   文件: MyPsiUtils.java
public static PsiElement createLeafFromText(Project project, PsiElement context,
											String text, IElementType type)
{
	PsiFileFactoryImpl factory = (PsiFileFactoryImpl)PsiFileFactory.getInstance(project);
	PsiElement el = factory.createElementFromText(text,
												  ANTLRv4Language.INSTANCE,
												  type,
												  context);
	return PsiTreeUtil.getDeepestFirst(el); // forces parsing of file!!
	// start rule depends on root passed in
}
 
源代码14 项目: haystack   文件: FlutterReduxGen.java
private void genStructure(PageModel pageModel) {
    Project project = directory.getProject();
    PsiFileFactory factory = PsiFileFactory.getInstance(project);
    PsiDirectoryFactory directoryFactory =
            PsiDirectoryFactory.getInstance(directory.getProject());
    String packageName = directoryFactory.getQualifiedName(directory, true);

    FileSaver fileSaver = new IDEFileSaver(factory, directory, DartFileType.INSTANCE);

    fileSaver.setListener(fileName -> {
        int ok = Messages.showOkCancelDialog(
                textResources.getReplaceDialogMessage(fileName),
                textResources.getReplaceDialogTitle(), "OK", "NO",
                UIUtil.getQuestionIcon());
        return ok == 0;
    });

    final String moduleName =
            FileIndexFacade.getInstance(project).getModuleForFile(directory.getVirtualFile()).getName();

    Map<String, Object> rootMap = new HashMap<String, Object>();
    rootMap.put("ProjectName", moduleName);
    rootMap.put("PageType", pageModel.pageType);
    if (pageModel.pageType.equals(CUSTOMSCROLLVIEW)) {
        rootMap.put("GenerateCustomScrollView", true);
    } else {
        rootMap.put("GenerateCustomScrollView", false);
    }
    rootMap.put("PageName", pageModel.pageName);
    rootMap.put("ModelEntryName", pageModel.modelName);
    rootMap.put("GenerateListView", pageModel.genListView);
    rootMap.put("GenerateBottomTabBar", pageModel.genBottomTabBar);
    rootMap.put("GenerateAppBar", pageModel.genAppBar);
    rootMap.put("GenerateDrawer", pageModel.genDrawer);
    rootMap.put("GenerateTopTabBar", pageModel.genTopTabBar);
    rootMap.put("GenerateWebView", pageModel.genWebView);
    rootMap.put("GenerateActionButton", pageModel.genActionButton);

    rootMap.put("viewModelQuery", pageModel.viewModelQuery);
    rootMap.put("viewModelGet", pageModel.viewModelGet);
    rootMap.put("viewModelCreate", pageModel.viewModelCreate);
    rootMap.put("viewModelUpdate", pageModel.viewModelUpdate);
    rootMap.put("viewModelDelete", pageModel.viewModelDelete);

    rootMap.put("GenSliverFixedExtentList", pageModel.genSliverFixedList);
    rootMap.put("GenSliverGrid", pageModel.genSliverGrid);
    rootMap.put("GenSliverToBoxAdapter", pageModel.genSliverToBoxAdapter);
    rootMap.put("FabInAppBar", pageModel.genSliverFab);
    rootMap.put("GenSliverTabBar", pageModel.genSliverTabBar);
    rootMap.put("GenSliverTabView", pageModel.genSliverTabView);
    rootMap.put("IsCustomWidget", pageModel.isCustomWidget);

    if (pageModel.genActionButton) {
        rootMap.put("HasActionSearch", pageModel.hasActionSearch);
        rootMap.put("ActionList", pageModel.actionList);
        rootMap.put("ActionBtnCount", pageModel.actionList.size());
    } else {
        rootMap.put("HasActionSearch", false);
        rootMap.put("ActionList", new ArrayList<String>());
        rootMap.put("ActionBtnCount", 0);
    }
    if (!pageModel.isUIOnly) {
        for (ClassModel classModel : pageModel.classModels) {
            if (classModel.getName().equals(pageModel.modelName)) {
                rootMap.put("genDatabase", classModel.isGenDBModule());

                if (classModel.getUniqueField() != null) {
                    rootMap.put("clsUNName", classModel.getUniqueField());
                    rootMap.put("clsUNNameType", classModel.getUniqueFieldType());
                }
            }
            generateModelEntry(classModel, rootMap);
        }

        generateRepository(rootMap);
        generateRedux(rootMap);
    }
    generateFeature(rootMap, pageModel.isCustomWidget, pageModel.genSliverTabView);
}
 
public ProtoElementFactory(Project project) {
    this.project = project;
    factory = PsiFileFactory.getInstance(project);
}
 
源代码16 项目: consulo   文件: TodoCheckinHandlerWorker.java
private MyEditedFileProcessor(final Project project, Acceptor acceptor, final TodoFilter todoFilter) {
  myAcceptor = acceptor;
  myTodoFilter = todoFilter;
  myPsiFileFactory = PsiFileFactory.getInstance(project);
}
 
源代码17 项目: buck   文件: BcfgElementFactory.java
/** Create a {@link BcfgFile} seeded with the given text. */
public static BcfgFile createFile(Project project, String text) {
  PsiFileFactory psiFileFactory = PsiFileFactory.getInstance(project);
  return (BcfgFile) psiFileFactory.createFileFromText(BcfgLanguage.INSTANCE, text);
}
 
源代码18 项目: buck   文件: BuckElementFactory.java
/** Create a {@link BuckFile} seeded with the given text. */
public static BuckFile createFile(Project project, String text) {
  PsiFileFactory psiFileFactory = PsiFileFactory.getInstance(project);
  return (BuckFile) psiFileFactory.createFileFromText(BuckLanguage.INSTANCE, text);
}
 
源代码19 项目: buck   文件: BcfgElementFactoryTest.java
@Override
public void setUp() throws Exception {
  super.setUp();
  PsiFileFactory fileFactory = PsiFileFactory.getInstance(getProject());
  assertNotNull(fileFactory);
}
 
源代码20 项目: vue-for-idea   文件: VueTemplatesFactory.java
public static PsiFile createFromTemplate(final PsiDirectory directory, final String name,
                                         String fileName, String templateName,
                                         @NonNls String... parameters) throws IncorrectOperationException {

    final FileTemplate template = FileTemplateManager.getDefaultInstance().getTemplate(templateName);

    Properties properties = new Properties(FileTemplateManager.getDefaultInstance().getDefaultProperties());

    Project project = directory.getProject();
    properties.setProperty("PROJECT_NAME", project.getName());
    properties.setProperty("NAME", fileName);


    String text;

    try {
        text = template.getText(properties);
    } catch (Exception e) {
        throw new RuntimeException("Unable to load template for " +
                FileTemplateManager.getInstance().internalTemplateToSubject(templateName), e);
    }

    final PsiFileFactory factory = PsiFileFactory.getInstance(directory.getProject());

    final PsiFile file = factory.createFileFromText(fileName, VueFileType.INSTANCE, text);

    return (PsiFile) directory.add(file);
}