Skip to content

XML formatter produces inconsistent line wrapping and indentation #1131

@janson403

Description

@janson403

The current XML formatting functionality produces inconsistent results, for example, when processing MyBatis mapper files. Specifically, the formatter treats elements with nearly identical character lengths differently without a clear logical basis. For example, the tag for "username" remains on a single line, whereas the tag for "password"—which has the same character length and structure—is forced into a line break with erratic indentation. This creates a staggered, "zigzag" visual effect that significantly degrades code readability.

The desired behavior is a standardized formatting style similar to that of the prettier-xml plugin. Regardless of the element's length, the formatter should apply consistent indentation to both XML elements and their nested text content. Each logical SQL segment should be cleanly separated, and tags like and should maintain a clear hierarchy rather than being collapsed into a single line. This consistency is crucial for maintaining large mapper files and performing clean code reviews.

Current Inconsistent Formatting:

EmpMapper.xml

  <update id="updateEmp"> update emp <set>
      <if test="username != null and username != ''">username=#{username},</if>
      <if test="password != null and password != ''">
    password=#{password},</if>
      <if test="name != null and name != ''">name=#{name},</if>
      <if test="gender != null">
    gender=#{gender},</if>
      <if test="phone != null and phone != ''">phone=#{phone},</if>
      <if test="job != null">
    job=#{job},</if>
      <if test="salary != null">salary=#{salary},</if>
      <if test="image != null and image != ''">
    image=#{image},</if>
      <if test="entryDate != null">entry_date=#{entryDate},</if>
      <if test="deptId != null">
    dept_id=#{deptId},</if> update_time=#{updateTime} </set> where id=#{id} </update>
</mapper>

Expected Formatting (Prettier-style, just an example):

<update id="updateEmp">
    UPDATE emp
    <set>
        <if test="username != null and username != ''">username = #{username},</if>
        <if test="password != null and password != ''">password = #{password},</if>
        <if test="name != null and name != ''">name = #{name},</if>
        <if test="gender != null">gender = #{gender},</if>
        <if test="phone != null and phone != ''">phone = #{phone},</if>
        <if test="job != null">job = #{job},</if>
        <if test="salary != null">salary = #{salary},</if>
        <if test="image != null and image != ''">image = #{image},</if>
        <if test="entryDate != null">entry_date = #{entryDate},</if>
        <if test="deptId != null">dept_id = #{deptId},</if>
        update_time = #{updateTime}
    </set>
    WHERE id = #{id}
</update>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions