PsiElement (final @NonNls String refText, final Project project, final String prefix, final String suffix, final PsiElement parent, final TextRange textRange, int[] refIdx) { String text = refText; if (parent != null) { final String allText = parent.getContainingFile().getText(); final TextRange parentRange = parent.getTextRange(); LOG.assertTrue(parentRange.getStartOffset() <= textRange.getStartOffset(), parent + "; prefix:" + prefix + "; suffix:" + suffix); String beg = allText.substring(parentRange.getStartOffset(), textRange.getStartOffset()); //noinspection SSBasedInspection (suggested replacement breaks behavior) if (StringUtil.stripQuotesAroundValue(beg).trim().isEmpty() && prefix == null) beg = ""; LOG.assertTrue(textRange.getEndOffset() <= parentRange.getEndOffset(), parent + "; prefix:" + prefix + "; suffix:" + suffix); String end = allText.substring(textRange.getEndOffset(), parentRange.getEndOffset()); //noinspection SSBasedInspection (suggested replacement breaks behavior) if (StringUtil.stripQuotesAroundValue(end).trim().isEmpty() && suffix == null) end = ""; final String start = beg + (prefix != null ? prefix : ""); refIdx[0] = start.length(); text = start + refText + (suffix != null ? suffix : "") + end; } final PsiElementFactory factory = JavaPsiFacade.getElementFactory(project); return parent instanceof PsiStatement ? factory.createStatementFromText(text, parent) : parent instanceof PsiCodeBlock ? factory.createCodeBlockFromText(text, parent) : factory.createExpressionFromText(text, parent); }