public void registerPageLanguage(final Project project, final XmlFile containingFile, final String languageName) { new WriteCommandAction.Simple(project, getFamilyName()) { @Override protected void run() { final PsiFileFactory factory = PsiFileFactory.getInstance(project); final XmlFile dummyFile = (XmlFile)factory.createFileFromText("_Dummy_.fxml", StdFileTypes.XML, "<?language " + languageName + "?>"); final XmlDocument document = dummyFile.getDocument(); if (document != null) { final XmlProlog prolog = document.getProlog(); final Collection<XmlProcessingInstruction> instructions = PsiTreeUtil.findChildrenOfType(prolog, XmlProcessingInstruction.class); LOG.assertTrue(instructions.size() == 1); final XmlDocument xmlDocument = containingFile.getDocument(); if (xmlDocument != null) { final XmlProlog xmlProlog = xmlDocument.getProlog(); if (xmlProlog != null) { final PsiElement element = xmlProlog.addBefore(instructions.iterator().next(), xmlProlog.getFirstChild()); xmlProlog.addAfter(PsiParserFacade.SERVICE.getInstance(project).createWhiteSpaceFromText("\n\n"), element); } } } } }.execute(); }
protected void modify(@NonNls @NotNull String qualifiedName) { final PsiClass psiClass = findClassAndAssert(qualifiedName); final PsiFile psiFile = psiClass.getContainingFile(); final PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(myProject); final PsiComment comment = parserFacade.createBlockCommentFromText(psiFile.getLanguage(), "Foo"); WriteCommandAction.runWriteCommandAction( myProject, new Runnable() { @Override public void run() { psiFile.add(comment); } } ); FileDocumentManager manager = FileDocumentManager.getInstance(); manager.saveAllDocuments(); }
private void registerPageLanguage(final Project project, final XmlFile containingFile, final String languageName) { new WriteCommandAction.Simple(project, getFamilyName()) { @Override protected void run() { final PsiFileFactory factory = PsiFileFactory.getInstance(project); final XmlFile dummyFile = (XmlFile)factory.createFileFromText("_Dummy_.fxml", StdFileTypes.XML, "<?language " + languageName + "?>"); final XmlDocument document = dummyFile.getDocument(); if (document != null) { final XmlProlog prolog = document.getProlog(); final Collection<XmlProcessingInstruction> instructions = PsiTreeUtil.findChildrenOfType(prolog, XmlProcessingInstruction.class); LOG.assertTrue(instructions.size() == 1); final XmlDocument xmlDocument = containingFile.getDocument(); if (xmlDocument != null) { final XmlProlog xmlProlog = xmlDocument.getProlog(); if (xmlProlog != null) { final PsiElement element = xmlProlog.addBefore(instructions.iterator().next(), xmlProlog.getFirstChild()); xmlProlog.addAfter(PsiParserFacade.SERVICE.getInstance(project).createWhiteSpaceFromText("\n\n"), element); } } } } }.execute(); }
private void registerPageLanguage(final Project project, final XmlFile containingFile, final String languageName) { new WriteCommandAction.Simple(project, getFamilyName()) { @Override protected void run() { final PsiFileFactory factory = PsiFileFactory.getInstance(project); final XmlFile dummyFile = (XmlFile)factory.createFileFromText("_Dummy_.fxml", XmlFileType.INSTANCE, "<?language " + languageName + "?>"); final XmlDocument document = dummyFile.getDocument(); if (document != null) { final XmlProlog prolog = document.getProlog(); final Collection<XmlProcessingInstruction> instructions = PsiTreeUtil.findChildrenOfType(prolog, XmlProcessingInstruction.class); LOG.assertTrue(instructions.size() == 1); final XmlDocument xmlDocument = containingFile.getDocument(); if (xmlDocument != null) { final XmlProlog xmlProlog = xmlDocument.getProlog(); if (xmlProlog != null) { final PsiElement element = xmlProlog.addBefore(instructions.iterator().next(), xmlProlog.getFirstChild()); xmlProlog.addAfter(PsiParserFacade.SERVICE.getInstance(project).createWhiteSpaceFromText("\n\n"), element); } } } } }.execute(); }
private static ASTNode insertWhitespaceIfNeeded(ASTNode anchorNode, final ASTNode elementNode, final ASTNode parentNode, final ASTNode insertionPlaceNode) throws IncorrectOperationException { ParserDefinition parserDef = LanguageParserDefinitions.INSTANCE.forLanguage(parentNode.getPsi().getLanguage()); final TokenSet comments = parserDef.getCommentTokens(parentNode.getPsi().getLanguage().getVersions()[0]); final TokenSet whitespaces = parserDef.getWhitespaceTokens(parentNode.getPsi().getLanguage().getVersions()[0]); if(anchorNode != null && ((!whitespaces.contains(anchorNode.getElementType()) && !whitespaces.contains(elementNode.getElementType())) || comments.contains(anchorNode.getElementType()) || comments.contains(elementNode.getElementType()) || elementNode.getPsi() instanceof PsiComment)) { String commentString = " "; if(comments.contains(anchorNode.getElementType()) || comments.contains(elementNode.getElementType()) || elementNode.getPsi() instanceof PsiComment) { commentString = "\n"; } final ASTNode wsNode = PsiParserFacade.SERVICE.getInstance(parentNode.getPsi().getProject()).createWhiteSpaceFromText(commentString).getNode(); parentNode.addChild(wsNode, insertionPlaceNode); anchorNode = wsNode; } return anchorNode; }
@NotNull public static PsiComment createComment(@NotNull Project project, @NotNull String commentText, @NotNull Language language) { final PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(project); return parserFacade.createLineOrBlockCommentFromText(language, commentText); }
@RequiredReadAction public static void addModifier(@NotNull CSharpModifierList modifierList, @NotNull DotNetModifier modifier) { PsiElement anchor = modifierList.getLastChild(); CSharpFieldDeclaration field = CSharpFileFactory.createField(modifierList.getProject(), modifier.getPresentableText() + " int b"); PsiElement modifierElement = field.getModifierList().getModifierElement(modifier); PsiElement psiElement = modifierList.addAfter(modifierElement, anchor); modifierList.addAfter(PsiParserFacade.SERVICE.getInstance(modifierList.getProject()).createWhiteSpaceFromText(" "), psiElement); }
@Override public void invoke(@NotNull final Project project, Editor editor, PsiFile file) throws IncorrectOperationException { ApplicationManager.getApplication().invokeLater( new Runnable() { @Override public void run() { if (classModel.getBodyPsi() == null) return; final StringBuilder builder = new StringBuilder(); GetterSetterMethodBuilder.build(builder, field, generateGetter); new WriteCommandAction.Simple(project) { @Override public void run() { List<HaxeNamedComponent> elements = HaxeElementGenerator.createNamedSubComponentsFromText(project, builder.toString()); PsiElement body = classModel.getBodyPsi(); PsiElement anchor = body.getLastChild(); for (PsiElement element : elements) { PsiElement newLine = createNewLine(); anchor = body.addAfter(element, anchor); anchor = body.addBefore(newLine, anchor); } } private PsiElement createNewLine() { return PsiParserFacade.SERVICE.getInstance(project).createWhiteSpaceFromText("\n\n"); } }.execute(); } } ); }
public void doInsert( @NotNull PsiFile buildFile, @NotNull final String targetName, @NotNull PantsTargetAddress addressToAdd ) throws IncorrectOperationException { final PyCallExpression targetDefinitionExpression = PantsPsiUtil.findTargets(buildFile).get(targetName); if (targetDefinitionExpression == null) { return; } final Project project = buildFile.getProject(); final PyElementGenerator generator = PyElementGenerator.getInstance(project); final String targetAddressStringToAdd = addressToAdd.toString(); final PyExpression dependenciesArgument = targetDefinitionExpression.getKeywordArgument("dependencies"); if (dependenciesArgument == null) { final PyKeywordArgument keywordArgument = generator.createKeywordArgument(LanguageLevel.forElement(buildFile), "dependencies", "['"+ targetAddressStringToAdd + "']"); targetDefinitionExpression.addArgument(keywordArgument); } else if (dependenciesArgument instanceof PyListLiteralExpression) { PyExpression position = null; // we assume all elements are sorted. for (PyExpression expression : ((PyListLiteralExpression)dependenciesArgument).getElements()) { if (expression instanceof PyStringLiteralExpression && targetAddressStringToAdd.compareTo(((PyStringLiteralExpression)expression).getStringValue()) < 0) { // found a position to insert break; } position = expression; } final PyStringLiteralExpression literalToAdd = generator.createStringLiteralAlreadyEscaped("'" + targetAddressStringToAdd + "'"); if (position != null) { final PsiElement newLine = PsiParserFacade.SERVICE.getInstance(project).createWhiteSpaceFromText("\n"); final PsiElement addedLiteral = dependenciesArgument.addAfter(literalToAdd, position); dependenciesArgument.getNode().addChild(newLine.getNode(), addedLiteral.getNode()); } else { dependenciesArgument.add(literalToAdd); } CodeStyleManager.getInstance(project).reformat(dependenciesArgument); } FileDocumentManager.getInstance().saveAllDocuments(); // dump VFS to FS before refreshing PantsUtil.refreshAllProjects(project); }
protected PsiElement modifyDeclaration(@NotNull PsiElement declaration) { PsiElement parent = declaration.getParent(); parent.addAfter(PsiParserFacade.SERVICE.getInstance(declaration.getProject()).createWhiteSpaceFromText("\n"), declaration); return declaration; }
protected static void addStatements(HaxeBlockStatement block, PsiElement[] elements) throws IncorrectOperationException { block.addRangeAfter(elements[0], elements[elements.length - 1], block.getFirstChild()); final PsiElement newLineNode = PsiParserFacade.SERVICE.getInstance(block.getProject()).createWhiteSpaceFromText("\n"); block.addAfter(newLineNode, block.getFirstChild()); }
protected PsiElement afterAddHandler(PsiElement element, PsiElement anchor) { final PsiElement newLineNode = PsiParserFacade.SERVICE.getInstance(element.getProject()).createWhiteSpaceFromText("\n\n"); anchor.getParent().addBefore(newLineNode, anchor); return anchor; }
@Nonnull public static PsiComment createComment(@Nonnull Project project, @Nonnull String commentText, @Nonnull Language language) { final PsiParserFacade parserFacade = PsiParserFacade.SERVICE.getInstance(project); return parserFacade.createLineOrBlockCommentFromText(language, commentText); }
@RequiredReadAction private static void addUsingStatementAfter(@NotNull PsiElement afterElement, @NotNull CSharpUsingNamespaceStatement newStatement) { Project project = afterElement.getProject(); PsiElement parent = afterElement.getParent(); PsiElement whiteSpaceFromText = PsiParserFacade.SERVICE.getInstance(project).createWhiteSpaceFromText("\n"); parent.addAfter(whiteSpaceFromText, afterElement); parent.addAfter(newStatement, afterElement.getNode().getTreeNext().getPsi()); }