void (@NotNull Project project, @NotNull ProblemDescriptor descriptor) { PatternReference myReference = (PatternReference)descriptor.getPsiElement().getReference(); final RncFile rncfile = (RncFile)PsiFileFactory.getInstance(project).createFileFromText("dummy.rnc", RncFileType.getInstance(), "dummy = xxx"); final RncGrammar grammar = rncfile.getGrammar(); assert grammar != null; final RncDefine def = (RncDefine)grammar.getFirstChild(); final RncGrammar scope = myReference.getScope(); assert scope != null; assert def != null; final RncDefine e = (RncDefine)scope.add(def); // ensures proper escaping (start -> \start) def.setName(myReference.getCanonicalText()); final SmartPsiElementPointer<RncDefine> p = SmartPointerManager.getInstance(project).createSmartPsiElementPointer(e); final ASTNode blockNode = e.getParent().getNode(); assert blockNode != null; final ASTNode newNode = e.getNode(); assert newNode != null; CodeStyleManager.getInstance(e.getManager().getProject()).reformatNewlyAddedElement(blockNode, newNode); final RncDefine d = p.getElement(); assert d != null; final RncElement definition = d.getPattern(); assert definition != null; final int offset = definition.getTextRange().getStartOffset(); definition.delete(); VirtualFile virtualFile = myReference.getElement().getContainingFile().getVirtualFile(); if (virtualFile != null) { FileEditorManager.getInstance(project).openTextEditor(new OpenFileDescriptor(project, virtualFile, offset), true); } }