Skip to content

Commit 87ed8dd

Browse files
committed
Explain how to easily format or indent source code
1 parent 0327bd3 commit 87ed8dd

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

content/developer/guide.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,84 @@ indentation style (that is the standard indentation style for Erlang
5858
programs). If you're not using Emacs for ejabberd development, indent
5959
the code using it first before making a PR/commit.
6060

61+
### Format
62+
63+
You can completely reformat your source code to the standard
64+
by surrounding the desired code with directives `@format-begin` and `@format-end`,
65+
for example:
66+
``` erlang
67+
%% @format-begin
68+
foo(xx ,yy) ->
69+
xxyy;
70+
71+
foo(A,B)->
72+
A
73+
+B
74+
.
75+
%% @format-end
76+
```
77+
78+
then run `make format` and it will format and indent all the instructed lines and files.
79+
The resulting source code will be:
80+
``` erlang
81+
%% @format-begin
82+
foo(xx, yy) ->
83+
xxyy;
84+
foo(A, B) ->
85+
A + B.
86+
%% @format-end
87+
```
88+
89+
This can be applied to any desired part of your module, or all of it,
90+
see for example [mod_adhoc_api.erl](https://github.com/processone/ejabberd/blob/master/src/mod_adhoc_api.erl).
91+
In that file, the `@format-begin` directive is provided early in the file,
92+
and the `@format-end` directive is not even needed.
93+
94+
You can integrate that step in your development cycle,
95+
for example configuring your git to automatically run
96+
that procedure before pushing your changes to the upstream repository.
97+
For that, add a file in your local ejabberd git repository
98+
named `.git/hooks/pre-push` with the following content:
99+
100+
``` sh
101+
#!/bin/sh
102+
103+
echo "---> Formatting source code..."
104+
./tools/rebar3-format.sh ./rebar3
105+
if git diff --quiet --exit-code; then
106+
exit 0
107+
else
108+
echo "---> After formatting ejabberd source code, some files have changed:"
109+
echo ""
110+
git status --short
111+
echo ""
112+
echo "---> Please review those changes and include them in your commit before pushing upstream."
113+
exit 1
114+
fi
115+
```
116+
117+
Furthermore, you can add an alias in git:
118+
``` sh
119+
git config --global alias.format '!$(pwd)/.git/hooks/pre-push'
120+
```
121+
and now you can run the hook easily anytime:
122+
```
123+
git format
124+
```
125+
126+
### Indent with Emacs
127+
128+
If you are only interested in lines indentation, not in full code formatting,
129+
install Emacs and surround the desired code or the whole file with lines:
130+
``` erlang
131+
%% @indent-begin
132+
foo(A,B) ->
133+
A+B.
134+
%% @indent-end
135+
```
136+
then run `make indent` and it will call Emacs to indent all the instructed files.
137+
138+
61139
## Start-up procedure
62140

63141
ejabberd is written as a standard OTP application, so the startup

0 commit comments

Comments
 (0)