Skip to content

4. Formatter

Pawel Lampe edited this page Oct 1, 2024 · 3 revisions

gdformat is an uncompromising GDScript code formatter. The only configurable thing is max line length allowed. The rest will be taken care of by gdformat in a one, consistent way.

Formatting with gdformat

Formatting may lead to data loss, so it's highly recommended to use it along with Version Control System (VCS) e.g. git

gdformat is the uncompromising GDScript code formatter. The only configurable thing is max line length allowed (--line-length). The rest will be taken care of by gdformat in a one, consistent way.

To run a formatter you need to perform installation first. Once done, given a test.gd file:

class X:
	var x=[1,2,{'a':1}]
	var y=[1,2,3,]     # trailing comma
	func foo(a:int,b,c=[1,2,3]):
		if a in c and \
		   b > 100:
			print('foo')
func bar():
	print('bar')

when you run gdformat test.gd, the test.gd file will be reformatted as follows:

class X:
	var x = [1, 2, {'a': 1}]
	var y = [
		1,
		2,
		3,
	]  # trailing comma

	func foo(a: int, b, c = [1, 2, 3]):
		if a in c and b > 100:
			print('foo')


func bar():
	print('bar')

If the program formats your files successfully, it will return the exit code 0. Non-zero code will be returned otherwise.

Skipping certain directories

To skip certain directories, you can invoke:

gdformat --dump-default-config

to generate gdformatrc file. This file can then be modified to instruct formatter to skip certain directories:

excluded_directories: !!set
  .git: null
  addons: null

after next gdformat execution it will pick the gdformatrc file if it exists and it will act accordingly.

Clone this wiki locally