Java Code Examples for com.intellij.psi.PsiFileFactory#createFileFromText()

The following examples show how to use com.intellij.psi.PsiFileFactory#createFileFromText() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PromptConsole.java    From reasonml-idea-plugin with MIT License 6 votes vote down vote up
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);
}
 
Example 2
Source File: WeexTemplateFactory.java    From weex-language-support with MIT License 6 votes vote down vote up
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);
}
 
Example 3
Source File: AbstractDartElementTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * 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);
}
 
Example 4
Source File: AbstractDartElementTest.java    From flutter-intellij with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
/**
 * 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);
}
 
Example 5
Source File: IgnoreTemplatesFactory.java    From idea-gitignore with MIT License 6 votes vote down vote up
/**
 * 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);
}
 
Example 6
Source File: SaveFile.java    From EasyCode with MIT License 5 votes vote down vote up
/**
 * 构建对象
 *
 * @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;
}
 
Example 7
Source File: ORSignatureTest.java    From reasonml-idea-plugin with MIT License 5 votes vote down vote up
@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);
}
 
Example 8
Source File: PostfixLiveTemplate.java    From consulo with Apache License 2.0 5 votes vote down vote up
@Nonnull
public static PsiFile copyFile(@Nonnull PsiFile file, @Nonnull StringBuilder fileContentWithoutKey) {
  final PsiFileFactory psiFileFactory = PsiFileFactory.getInstance(file.getProject());
  PsiFile copy = psiFileFactory.createFileFromText(file.getName(), file.getFileType(), fileContentWithoutKey);
  VirtualFile vFile = copy.getVirtualFile();
  if (vFile != null) {
    vFile.putUserData(UndoConstants.DONT_RECORD_UNDO, Boolean.TRUE);
  }
  return copy;
}
 
Example 9
Source File: ScratchFileCreationHelper.java    From consulo with Apache License 2.0 5 votes vote down vote up
@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);
}
 
Example 10
Source File: BcfgElementFactory.java    From buck with Apache License 2.0 4 votes vote down vote up
/** 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);
}
 
Example 11
Source File: BuckElementFactory.java    From buck with Apache License 2.0 4 votes vote down vote up
/** 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);
}
 
Example 12
Source File: FormatterTestCase.java    From consulo with Apache License 2.0 4 votes vote down vote up
protected PsiFile createFileFromText(String text, String fileName, final PsiFileFactory fileFactory) {
  return fileFactory.createFileFromText(fileName, getFileType(fileName), text, LocalTimeCounter.currentTime(), true, false);
}
 
Example 13
Source File: VueTemplatesFactory.java    From vue-for-idea with BSD 3-Clause "New" or "Revised" License 3 votes vote down vote up
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);
}