@Override public FileReferenceNode handleContentChange(@NotNull FileReferenceNode fileReferenceNode, @NotNull TextRange range, String newContent) throws IncorrectOperationException { String oldText = fileReferenceNode.getText(); String newText = oldText.substring(0, range.getStartOffset()) + newContent + oldText .substring(range.getEndOffset()); Project project = fileReferenceNode.getProject(); PsiFileFactoryImpl factory = (PsiFileFactoryImpl) PsiFileFactory.getInstance(project); IElementType type = ProtoParserDefinition.rule(ProtoParser.RULE_fileReference); ScopeNode context = fileReferenceNode.getContext(); PsiElement newNode = factory .createElementFromText(newText, ProtoLanguage.INSTANCE, type, context); if (newNode == null) { throw new IncorrectOperationException(); } return (FileReferenceNode) fileReferenceNode.replace(newNode); }
@Override public FieldReferenceNode handleContentChange(@NotNull FieldReferenceNode node, @NotNull TextRange range, String newContent) throws IncorrectOperationException { String oldText = node.getText(); String newText = oldText.substring(0, range.getStartOffset()) + newContent + oldText .substring(range.getEndOffset()); Project project = node.getProject(); PsiFileFactoryImpl factory = (PsiFileFactoryImpl) PsiFileFactory.getInstance(project); IElementType type = ProtoParserDefinition.rule(ProtoParser.RULE_fieldRerefence); ScopeNode context = node.getContext(); PsiElement newNode = factory .createElementFromText(newText, ProtoLanguage.INSTANCE, type, context); if (newNode == null) { throw new IncorrectOperationException(); } return (FieldReferenceNode) node.replace(newNode); }
@Override public TypeReferenceNode handleContentChange(@NotNull TypeReferenceNode node, @NotNull TextRange range, String newContent) throws IncorrectOperationException { String oldText = node.getText(); String newText = oldText.substring(0, range.getStartOffset()) + newContent + oldText .substring(range.getEndOffset()); Project project = node.getProject(); PsiFileFactoryImpl factory = (PsiFileFactoryImpl) PsiFileFactory.getInstance(project); IElementType type = ProtoParserDefinition.rule(ProtoParser.RULE_typeReference); ScopeNode context = node.getContext(); PsiElement newNode = factory .createElementFromText(newText, ProtoLanguage.INSTANCE, type, context); if (newNode == null) { throw new IncorrectOperationException(); } return (TypeReferenceNode) node.replace(newNode); }
/** * Set name. */ public PsiElement setName(@NotNull String name) throws IncorrectOperationException { ASTNode node = getNode(); IElementType elementType = node.getElementType(); if (elementType instanceof RuleIElementType) { RuleIElementType ruleElementType = (RuleIElementType) elementType; int ruleIndex = ruleElementType.getRuleIndex(); Project project = getProject(); PsiFileFactoryImpl factory = (PsiFileFactoryImpl) PsiFileFactory.getInstance(project); IElementType type = ProtoParserDefinition.rule(ruleIndex); ScopeNode context = getContext(); PsiElement newNode = factory.createElementFromText(name, ProtoLanguage.INSTANCE, type, context); if (newNode == null) { throw new IncorrectOperationException(); } return replace(newNode); } throw new IncorrectOperationException(OPERATION_NOT_SUPPORTED); }
public static String format(final Project project, Language language, String text) { final PsiFile psiFile = PsiFileFactoryImpl.getInstance(project).createFileFromText("virtual", language, text); new WriteCommandAction.Simple<String>(project, psiFile) { @Override protected void run() throws Throwable { CodeStyleManager.getInstance(project).reformat(psiFile, false); } }.execute(); String formatted = psiFile.getText(); new WriteCommandAction.Simple<String>(project, psiFile) { @Override protected void run() throws Throwable { psiFile.delete(); } }.execute(); if (formatted == null) { return text; } return formatted; }
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 }
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); }
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 }
public static PsiFile createFile(Project project, String text) { String fileName = "a.g4"; // random name but must be .g4 PsiFileFactoryImpl factory = (PsiFileFactoryImpl)PsiFileFactory.getInstance(project); return factory.createFileFromText(fileName, ANTLRv4Language.INSTANCE, text, false, false); }