Skip to content

Can I somehow exclude a region of code from formatting? #238

@discordier

Description

@discordier

Thanks a lot for the linter, formatter and your courses.

Maybe I am missing something but I did not find any annotation to disable the formatter for a region like we can disable the linter.

A use case when I want to disable the formatter is, when I have lists of vertices that are generated from calculations.
To make the calculations more expressive, I format them as "table".

Example for creating the vertices of a hexagon:

 	var vertices: PackedVector3Array = [
		Vector3(center.x               , 0, center.z - outer_radius  ), # 0 top
		Vector3(center.x + inner_radius, 0, center.z - outer_radius_2), # 1 top right
		Vector3(center.x + inner_radius, 0, center.z + outer_radius_2), # 2 bottom right
		Vector3(center.x               , 0, center.z + outer_radius  ), # 3 bottom
		Vector3(center.x - inner_radius, 0, center.z + outer_radius_2), # 4 bottom left
		Vector3(center.x - inner_radius, 0, center.z - outer_radius_2), # 5 top left
	]

The formatter decides this is the correct layout:

 	var vertices: PackedVector3Array = [
		Vector3(center.x, 0, center.z - outer_radius), # 0 top
		Vector3(center.x + inner_radius, 0, center.z - outer_radius_2), # 1 top right
		Vector3(center.x + inner_radius, 0, center.z + outer_radius_2), # 2 bottom right
		Vector3(center.x, 0, center.z + outer_radius), # 3 bottom
		Vector3(center.x - inner_radius, 0, center.z + outer_radius_2), # 4 bottom left
		Vector3(center.x - inner_radius, 0, center.z - outer_radius_2), # 5 top left
	]

While this is correct from style guide view, I'd love to override here because the result is causing more cognitive load than seeing the table structure.

I'd love to have something like:
Example:

 	var vertexes: PackedVector3Array = [
		# gd-formatter:disable
		Vector3(center.x               , 0, center.z - outer_radius  ), # 0 top
		Vector3(center.x + inner_radius, 0, center.z - outer_radius_2), # 1 top right
		Vector3(center.x + inner_radius, 0, center.z + outer_radius_2), # 2 bottom right
		Vector3(center.x               , 0, center.z + outer_radius  ), # 3 bottom
		Vector3(center.x - inner_radius, 0, center.z + outer_radius_2), # 4 bottom left
		Vector3(center.x - inner_radius, 0, center.z - outer_radius_2), # 5 top left
		# gd-formatter:enable
	]

To define a code region that shall not be touched.

Further use case are matrices and the like - in the end, all structured initialization code.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions