Search in sources :

Example 6 with PsiFileFactory

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

the class PostfixLiveTemplate method copyFile.

@NotNull
public static PsiFile copyFile(@NotNull PsiFile file, @NotNull StringBuilder fileContentWithoutKey) {
    final PsiFileFactory psiFileFactory = PsiFileFactory.getInstance(file.getProject());
    PsiFile copy = psiFileFactory.createFileFromText(file.getName(), file.getFileType(), fileContentWithoutKey);
    if (copy instanceof PsiFileImpl) {
        ((PsiFileImpl) copy).setOriginalFile(TemplateLanguageUtil.getBaseFile(file));
    }
    VirtualFile vFile = copy.getVirtualFile();
    if (vFile != null) {
        vFile.putUserData(UndoConstants.DONT_RECORD_UNDO, Boolean.TRUE);
    }
    return copy;
}
Also used : VirtualFile(com.intellij.openapi.vfs.VirtualFile) PsiFileFactory(com.intellij.psi.PsiFileFactory) PsiFileImpl(com.intellij.psi.impl.source.PsiFileImpl) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 7 with PsiFileFactory

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

the class LanguageTextField method createDocument.

public static Document createDocument(String value, @Nullable Language language, Project project, @NotNull SimpleDocumentCreator documentCreator) {
    if (language != null) {
        final PsiFileFactory factory = PsiFileFactory.getInstance(project);
        final FileType fileType = language.getAssociatedFileType();
        assert fileType != null;
        final long stamp = LocalTimeCounter.currentTime();
        final PsiFile psiFile = factory.createFileFromText("Dummy." + fileType.getDefaultExtension(), fileType, value, stamp, true, false);
        documentCreator.customizePsiFile(psiFile);
        final Document document = PsiDocumentManager.getInstance(project).getDocument(psiFile);
        assert document != null;
        return document;
    } else {
        return EditorFactory.getInstance().createDocument(value);
    }
}
Also used : PsiFileFactory(com.intellij.psi.PsiFileFactory) FileType(com.intellij.openapi.fileTypes.FileType) PsiFile(com.intellij.psi.PsiFile) Document(com.intellij.openapi.editor.Document)

Example 8 with PsiFileFactory

use of com.intellij.psi.PsiFileFactory in project intellij by bazelbuild.

the class BuildElementGenerator method createDummyFile.

public PsiFile createDummyFile(String contents) {
    PsiFileFactory factory = PsiFileFactory.getInstance(project);
    LightVirtualFile virtualFile = new LightVirtualFile(DUMMY_FILENAME, BuildFileType.INSTANCE, contents);
    PsiFile psiFile = ((PsiFileFactoryImpl) factory).trySetupPsiForFile(virtualFile, BuildFileLanguage.INSTANCE, false, true);
    assert psiFile != null;
    return psiFile;
}
Also used : PsiFileFactory(com.intellij.psi.PsiFileFactory) LightVirtualFile(com.intellij.testFramework.LightVirtualFile) PsiFileFactoryImpl(com.intellij.psi.impl.PsiFileFactoryImpl) PsiFile(com.intellij.psi.PsiFile)

Example 9 with PsiFileFactory

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

the class RegExpFactory method createPatternFromText.

@NotNull
public static RegExpPattern createPatternFromText(@NotNull CharSequence text, @NotNull PsiElement context) {
    final PsiFileFactory factory = PsiFileFactory.getInstance(context.getProject());
    final PsiFile file = factory.createFileFromText("dummy.regexp", RegExpFileType.INSTANCE, text);
    final RegExpPattern pattern = PsiTreeUtil.getChildOfType(file, RegExpPattern.class);
    assert pattern != null;
    return pattern;
}
Also used : PsiFileFactory(com.intellij.psi.PsiFileFactory) RegExpPattern(org.intellij.lang.regexp.psi.RegExpPattern) PsiFile(com.intellij.psi.PsiFile) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with PsiFileFactory

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

the class GroupSurrounder method surroundElements.

@Nullable
public TextRange surroundElements(@NotNull Project project, @NotNull Editor editor, @NotNull PsiElement[] elements) throws IncorrectOperationException {
    assert elements.length == 1 || PsiTreeUtil.findCommonParent(elements) == elements[0].getParent();
    final PsiElement e = elements[0];
    final ASTNode node = e.getNode();
    assert node != null;
    final ASTNode parent = node.getTreeParent();
    final StringBuilder s = new StringBuilder();
    for (int i = 0; i < elements.length; i++) {
        final PsiElement element = elements[i];
        if (element instanceof RegExpElementImpl) {
            s.append(((RegExpElementImpl) element).getUnescapedText());
        } else {
            s.append(element.getText());
        }
        if (i > 0) {
            final ASTNode child = element.getNode();
            assert child != null;
            parent.removeChild(child);
        }
    }
    final PsiFileFactory factory = PsiFileFactory.getInstance(project);
    final PsiFile f = factory.createFileFromText("dummy.regexp", RegExpFileType.INSTANCE, makeReplacement(s));
    final RegExpPattern pattern = PsiTreeUtil.getChildOfType(f, RegExpPattern.class);
    assert pattern != null;
    final RegExpAtom element = pattern.getBranches()[0].getAtoms()[0];
    if (isInsideStringLiteral(e)) {
        final Document doc = editor.getDocument();
        PsiDocumentManager.getInstance(project).doPostponedOperationsAndUnblockDocument(doc);
        final TextRange tr = e.getTextRange();
        doc.replaceString(tr.getStartOffset(), tr.getEndOffset(), StringUtil.escapeStringCharacters(element.getText()));
        return TextRange.from(e.getTextRange().getEndOffset(), 0);
    } else {
        final PsiElement n = e.replace(element);
        return TextRange.from(n.getTextRange().getEndOffset(), 0);
    }
}
Also used : PsiFileFactory(com.intellij.psi.PsiFileFactory) RegExpPattern(org.intellij.lang.regexp.psi.RegExpPattern) RegExpElementImpl(org.intellij.lang.regexp.psi.impl.RegExpElementImpl) RegExpAtom(org.intellij.lang.regexp.psi.RegExpAtom) ASTNode(com.intellij.lang.ASTNode) PsiFile(com.intellij.psi.PsiFile) TextRange(com.intellij.openapi.util.TextRange) Document(com.intellij.openapi.editor.Document) PsiElement(com.intellij.psi.PsiElement) Nullable(org.jetbrains.annotations.Nullable)

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