JetBrains/intellij-community · error · IncorrectOperationException
cannot handle content change for:
Error message
cannot handle content change for:
What it means
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: ...").
Source
Thrown at java/java-psi-impl/src/com/intellij/psi/impl/source/resolve/reference/impl/manipulators/FragmentManipulator.java:24
import com.intellij.psi.AbstractElementManipulator;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiExpression;
import com.intellij.psi.PsiFragment;
import com.intellij.util.IncorrectOperationException;
import org.jetbrains.annotations.NotNull;
public final class FragmentManipulator extends AbstractElementManipulator<PsiFragment> {
@Override
public PsiFragment handleContentChange(@NotNull PsiFragment expr, @NotNull TextRange range, String newContent) throws IncorrectOperationException {
String oldText = expr.getText();
if (oldText.startsWith("\"")) {
newContent = StringUtil.escapeStringCharacters(newContent);
}
else if (oldText.startsWith("'") && newContent.length() <= 1) {
newContent = newContent.length() == 1 && newContent.charAt(0) == '\''? "\\'" : newContent;
}
else {
throw new IncorrectOperationException("cannot handle content change for: " + oldText + ", expr: " + expr);
}
String newText = oldText.substring(0, range.getStartOffset()) + newContent + oldText.substring(range.getEndOffset());
final PsiExpression newExpr = JavaPsiFacade.getElementFactory(expr.getProject()).createExpressionFromText(newText, null);
return (PsiFragment)expr.replace(newExpr);
}
@Override
public @NotNull TextRange getRangeInElement(final @NotNull PsiFragment element) {
return getValueRange(element);
}
public static @NotNull TextRange getValueRange(@NotNull PsiFragment fragment) {
return fragment.createLiteralTextEscaper().getRelevantTextRange();
}
}View on GitHub (pinned to be881553f2)
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.
Example fix
// before
ElementManipulators.handleContentChange(fragment, range, newContent); // char fragment with multi-char content -> throws
// after
String old = fragment.getText();
if (old.startsWith("\"") || (old.startsWith("'") && newContent.length() <= 1)) {
ElementManipulators.handleContentChange(fragment, range, newContent);
} else {
fragment.replace(factory.createExpressionFromText("\"" + newContent + "\"", null));
} Defensive patterns
Strategy: validation
Validate before calling
String old = fragment.getText();
boolean ok = old.startsWith("\"") || (old.startsWith("'") && newContent.length() <= 1);
if (!ok) return; Try / catch
try { ElementManipulators.handleContentChange(fragment, range, c); } catch (IncorrectOperationException e) { fragment.replace(factoryExpr); } Prevention
- Ensure fragment literals keep their quotes before content edits.
- Replace the whole expression for unsupported fragment shapes.
When it happens
Trigger: 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).
Common situations: String template editing intentions (IDEA-329350-era preview feature); programmatic rewriters of fragment literals; tests feeding raw values into the manipulator via ElementManipulators.handleContentChange.
Related errors
- cannot handle content change for:
- This template did not produce a Java class or an interface\n
- The file '{}' was expected to be of JAVA file type, but got:
- Cannot create package '{0}' in source folder {1}
- Method {0} is not static
AI-assisted analysis of JetBrains/intellij-community@be881553f2 (2026-08-14).
Data as JSON: /api/errors/5a0ad826f6f456e5.
Report an issue: GitHub.