void (GrString grString, String quotes) { GroovyPsiElementFactory factory = GroovyPsiElementFactory.getInstance(grString.getProject()); final PsiElement firstChild = grString.getFirstChild(); final PsiElement lastChild = grString.getLastChild(); final GrExpression template = factory.createExpressionFromText(quotes + "$x" + quotes); if (firstChild != null && firstChild.getNode().getElementType() == GroovyTokenTypes.mGSTRING_BEGIN && !quotes.equals(firstChild.getText())) { grString.getNode().replaceChild(firstChild.getNode(), template.getFirstChild().getNode()); } if (lastChild != null && lastChild.getNode().getElementType() == GroovyTokenTypes.mGSTRING_END && !quotes.equals(lastChild.getText())) { grString.getNode().replaceChild(lastChild.getNode(), template.getLastChild().getNode()); } }