{"record":{"id":"5a0ad826f6f456e5","repo":"JetBrains/intellij-community","slug":"cannot-handle-content-change-for","errorCode":null,"errorMessage":"cannot handle content change for: ","messagePattern":"cannot handle content change for: ","errorType":"exception","errorClass":"IncorrectOperationException","httpStatus":null,"severity":"error","filePath":"java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/reference/impl/manipulators/FragmentManipulator.java","lineNumber":24,"sourceCode":"import com.intellij.psi.AbstractElementManipulator;\nimport com.intellij.psi.JavaPsiFacade;\nimport com.intellij.psi.PsiExpression;\nimport com.intellij.psi.PsiFragment;\nimport com.intellij.util.IncorrectOperationException;\nimport org.jetbrains.annotations.NotNull;\n\npublic final class FragmentManipulator extends AbstractElementManipulator<PsiFragment> {\n  @Override\n  public PsiFragment handleContentChange(@NotNull PsiFragment expr, @NotNull TextRange range, String newContent) throws IncorrectOperationException {\n    String oldText = expr.getText();\n    if (oldText.startsWith(\"\\\"\")) {\n      newContent = StringUtil.escapeStringCharacters(newContent);\n    }\n    else if (oldText.startsWith(\"'\") && newContent.length() <= 1) {\n      newContent = newContent.length() == 1 && newContent.charAt(0) == '\\''? \"\\\\'\" : newContent;\n    }\n    else {\n      throw new IncorrectOperationException(\"cannot handle content change for: \" + oldText + \", expr: \" + expr);\n    }\n\n    String newText = oldText.substring(0, range.getStartOffset()) + newContent + oldText.substring(range.getEndOffset());\n    final PsiExpression newExpr = JavaPsiFacade.getElementFactory(expr.getProject()).createExpressionFromText(newText, null);\n    return (PsiFragment)expr.replace(newExpr);\n  }\n\n  @Override\n  public @NotNull TextRange getRangeInElement(final @NotNull PsiFragment element) {\n    return getValueRange(element);\n  }\n\n  public static @NotNull TextRange getValueRange(@NotNull PsiFragment fragment) {\n    return fragment.createLiteralTextEscaper().getRelevantTextRange();\n  }\n}","sourceCodeStart":6,"sourceCodeEnd":40,"githubUrl":"https://github.com/JetBrains/intellij-community/blob/be881553f2a76ac8b4ea53950d93f0de78295c19/java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/reference/impl/manipulators/FragmentManipulator.java#L6-L40","documentation":"FragmentManipulator.handleContentChange edits the value inside a PsiFragment (Java 21 fragment expressions in string templates). It only knows how to re-escape content when the old text starts with a double quote (string) or a single quote with 0-1 char content (char); any other fragment text (e.g. already broken/empty literal) throws IncorrectOperationException(\"cannot handle content change for: ...\").","triggerScenarios":"Calling handleContentChange on a PsiFragment whose text does not begin with '\"' or \"'\" — for example a fragment with malformed text after a failed parse, or newContent longer than 1 char for a char fragment (falls into the else branch).","commonSituations":"String template editing intentions (IDEA-329350-era preview feature); programmatic rewriters of fragment literals; tests feeding raw values into the manipulator via ElementManipulators.handleContentChange.","solutions":["Pre-check the fragment text before manipulating: only call when expr.getText().startsWith(\"\\\"\") or (startsWith(\"'\") and newContent length <= 1).","Replace the whole fragment expression (expr.replace(factory.createExpressionFromText(...))) instead of in-place content change for unsupported shapes.","For string fragments, let StringUtil.escapeStringCharacters handle escaping by routing through the string branch, i.e. ensure the fragment keeps its quotes."],"exampleFix":"// before\nElementManipulators.handleContentChange(fragment, range, newContent); // char fragment with multi-char content -> throws\n\n// after\nString old = fragment.getText();\nif (old.startsWith(\"\\\"\") || (old.startsWith(\"'\") && newContent.length() <= 1)) {\n  ElementManipulators.handleContentChange(fragment, range, newContent);\n} else {\n  fragment.replace(factory.createExpressionFromText(\"\\\"\" + newContent + \"\\\"\", null));\n}","handlingStrategy":"validation","validationCode":"String old = fragment.getText();\nboolean ok = old.startsWith(\"\\\"\") || (old.startsWith(\"'\") && newContent.length() <= 1);\nif (!ok) return;","typeGuard":null,"tryCatchPattern":"try { ElementManipulators.handleContentChange(fragment, range, c); } catch (IncorrectOperationException e) { fragment.replace(factoryExpr); }","preventionTips":["Ensure fragment literals keep their quotes before content edits.","Replace the whole expression for unsupported fragment shapes."],"tags":["java","psi","string-templates","fragments","element-manipulator"],"backgroundTag":null,"analyzedSha":"be881553f2a76ac8b4ea53950d93f0de78295c19","analyzedAt":"2026-08-14T14:13:06.425Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}