Search in sources :

Example 11 with PsiFileFactory

use of com.intellij.psi.PsiFileFactory in project intellij-community by JetBrains.

the class SimplifyQuantifierAction method invoke.

public void invoke(@NotNull Project project, Editor editor, PsiFile file) throws IncorrectOperationException {
    if (myReplacement == null) {
        myQuantifier.delete();
    } else {
        final PsiFileFactory factory = PsiFileFactory.getInstance(project);
        final ASTNode modifier = myQuantifier.getModifier();
        final PsiFile f = factory.createFileFromText("dummy.regexp", RegExpFileType.INSTANCE, "a" + myReplacement + (modifier != null ? modifier.getText() : ""));
        final RegExpPattern pattern = PsiTreeUtil.getChildOfType(f, RegExpPattern.class);
        assert pattern != null;
        final RegExpClosure closure = (RegExpClosure) pattern.getBranches()[0].getAtoms()[0];
        myQuantifier.replace(closure.getQuantifier());
    }
}
Also used : PsiFileFactory(com.intellij.psi.PsiFileFactory) RegExpPattern(org.intellij.lang.regexp.psi.RegExpPattern) RegExpClosure(org.intellij.lang.regexp.psi.RegExpClosure) ASTNode(com.intellij.lang.ASTNode) PsiFile(com.intellij.psi.PsiFile)

Example 12 with PsiFileFactory

use of com.intellij.psi.PsiFileFactory in project intellij-community by JetBrains.

the class CustomAntElementsRegistry method loadContentAsFile.

public static PsiFile loadContentAsFile(Project project, InputStream stream, LanguageFileType fileType) throws IOException {
    final StringBuilder builder = new StringBuilder();
    try {
        int nextByte;
        while ((nextByte = stream.read()) >= 0) {
            builder.append((char) nextByte);
        }
    } finally {
        stream.close();
    }
    final PsiFileFactory factory = PsiFileFactory.getInstance(project);
    return factory.createFileFromText("_ant_dummy__." + fileType.getDefaultExtension(), fileType, builder, LocalTimeCounter.currentTime(), false, false);
}
Also used : PsiFileFactory(com.intellij.psi.PsiFileFactory)

Example 13 with PsiFileFactory

use of com.intellij.psi.PsiFileFactory in project intellij-community by JetBrains.

the class GroovyTemplatesFactory method createFromTemplate.

public static PsiFile createFromTemplate(@NotNull final PsiDirectory directory, @NotNull final String name, @NotNull String fileName, @NotNull String templateName, boolean allowReformatting, @NonNls String... parameters) throws IncorrectOperationException {
    final FileTemplate template = FileTemplateManager.getInstance(directory.getProject()).getInternalTemplate(templateName);
    Project project = directory.getProject();
    Properties properties = new Properties(FileTemplateManager.getInstance(project).getDefaultProperties());
    JavaTemplateUtil.setPackageNameAttribute(properties, directory);
    properties.setProperty(NAME_TEMPLATE_PROPERTY, name);
    properties.setProperty(LOW_CASE_NAME_TEMPLATE_PROPERTY, StringUtil.decapitalize(name));
    for (int i = 0; i < parameters.length; i += 2) {
        properties.setProperty(parameters[i], parameters[i + 1]);
    }
    String text;
    try {
        text = template.getText(properties);
    } catch (Exception e) {
        String message = "Unable to load template for " + FileTemplateManager.getInstance(project).internalTemplateToSubject(templateName);
        throw new RuntimeException(message, e);
    }
    final PsiFileFactory factory = PsiFileFactory.getInstance(project);
    PsiFile file = factory.createFileFromText(fileName, GroovyFileType.GROOVY_FILE_TYPE, text);
    file = (PsiFile) directory.add(file);
    if (file != null && allowReformatting && template.isReformatCode()) {
        new ReformatCodeProcessor(project, file, null, false).run();
    }
    return file;
}
Also used : Project(com.intellij.openapi.project.Project) PsiFileFactory(com.intellij.psi.PsiFileFactory) FileTemplate(com.intellij.ide.fileTemplates.FileTemplate) PsiFile(com.intellij.psi.PsiFile) ReformatCodeProcessor(com.intellij.codeInsight.actions.ReformatCodeProcessor) Properties(java.util.Properties) IncorrectOperationException(com.intellij.util.IncorrectOperationException)

Example 14 with PsiFileFactory

use of com.intellij.psi.PsiFileFactory in project intellij-community by JetBrains.

the class RenameUtil method createIdentifierNode.

@NotNull
public static ASTNode createIdentifierNode(PsiManager manager, String name) throws IncorrectOperationException {
    if (isKeyword(name)) {
        name = "\\" + name;
    } else if (!isIdentifier(name)) {
        throw new IncorrectOperationException("Illegal identifier: " + name);
    }
    final PsiFileFactory f = PsiFileFactory.getInstance(manager.getProject());
    final RncFile file = (RncFile) f.createFileFromText("dummy.rnc", RncFileType.getInstance(), name + " = bar");
    final ASTNode astNode = findFirstGrammarNode(file);
    final ASTNode newNode = astNode.findChildByType(RncTokenTypes.IDENTIFIERS);
    assert newNode != null;
    return newNode;
}
Also used : PsiFileFactory(com.intellij.psi.PsiFileFactory) ASTNode(com.intellij.lang.ASTNode) IncorrectOperationException(com.intellij.util.IncorrectOperationException) RncFile(org.intellij.plugins.relaxNG.compact.psi.RncFile) NotNull(org.jetbrains.annotations.NotNull)

Example 15 with PsiFileFactory

use of com.intellij.psi.PsiFileFactory in project intellij-community by JetBrains.

the class RenameUtil method createLiteralNode.

public static ASTNode createLiteralNode(PsiManager manager, String content) {
    final PsiFileFactory f = PsiFileFactory.getInstance(manager.getProject());
    final RncFile file = (RncFile) f.createFileFromText("dummy.rnc", RncFileType.getInstance(), "include \"" + content + "\"");
    final ASTNode include = findFirstGrammarNode(file);
    final ASTNode literal = include.findChildByType(RncTokenTypes.LITERAL);
    assert literal != null;
    return literal;
}
Also used : PsiFileFactory(com.intellij.psi.PsiFileFactory) ASTNode(com.intellij.lang.ASTNode) RncFile(org.intellij.plugins.relaxNG.compact.psi.RncFile)

Aggregations

PsiFileFactory (com.intellij.psi.PsiFileFactory)19 PsiFile (com.intellij.psi.PsiFile)13 ASTNode (com.intellij.lang.ASTNode)5 PsiFileFactoryImpl (com.intellij.psi.impl.PsiFileFactoryImpl)4 LightVirtualFile (com.intellij.testFramework.LightVirtualFile)4 RegExpPattern (org.intellij.lang.regexp.psi.RegExpPattern)4 IncorrectOperationException (com.intellij.util.IncorrectOperationException)3 RncFile (org.intellij.plugins.relaxNG.compact.psi.RncFile)3 NotNull (org.jetbrains.annotations.NotNull)3 Document (com.intellij.openapi.editor.Document)2 PsiElement (com.intellij.psi.PsiElement)2 Nullable (org.jetbrains.annotations.Nullable)2 ReformatCodeProcessor (com.intellij.codeInsight.actions.ReformatCodeProcessor)1 XmlZenCodingGenerator (com.intellij.codeInsight.template.emmet.generators.XmlZenCodingGenerator)1 ZenCodingGenerator (com.intellij.codeInsight.template.emmet.generators.ZenCodingGenerator)1 TemplateImpl (com.intellij.codeInsight.template.impl.TemplateImpl)1 CreateFileAction (com.intellij.ide.actions.CreateFileAction)1 FileTemplate (com.intellij.ide.fileTemplates.FileTemplate)1 FileType (com.intellij.openapi.fileTypes.FileType)1 Project (com.intellij.openapi.project.Project)1