I recently opened a ticket on the IntelliJ IDEA tracker listing a bug in a very specific situation.
Suppose you have a JSP that contains JSON data. Suppose also that a part of that data is a formatted HTML code and you don’t want auto-formatter to touch it. It doesn’t really matter what’s inside but you want that part of your code remain the same.
In the simplest form you could have something like that:
<%@ page contentType="application/json" %> <%--@formatter:off--%> { "please": "don't", "format": "this", "code": "!!!1111" }
Notice that I used @formatter:off
control structure. But despite that being used, IntelliJ IDEA will still format your code. This happens by the way only for the contentType="application/json"
, the normal JSPs are unaffected. The reason why that happens is listed I believe in the ticket I submitted.
There is however a trick to force the Formatter to work:
<%@ page contentType="application/json" %> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <c:if test="false> // @formatter:off </c:if> { "please": "don't", "format": "this", "code": "!!!1111" }
The first part of the solution is to use the normal Java comments to force the Code Formatter to stop. That works fine. But since we’re inside JSP, that means the comment will be sent to a user in plain text. We don’t want that so the second part will be to wrap it with
<c:if test="false> </c:if>
tags.
The affected IntelliJ IDEA version is 2016.3.4 and I hope it will be fixed in future versions.