This patch make sure that function and macro definitions, as well as
do-blocks, end with an explicit `return` statement before the last
expression in the body. The following exceptions are made:
- If the last expression is a `for` or `while` loop (which both always
evaluate to `nothing`) `return` is added *after* the loop.
- If the last expression is a `if` or `try` block the `return` is only
added in case there is no `return` inside any of the branches.
- If the last expression is a `let` or `begin` block the `return` is
only added in case there is no `return` inside the block.
- If the last expression is a macro call the `return` is only added in
case there is no `return` inside the macro.
- If the last expression is a function call, and the function name is
`throw`, `rethrow`, or `error`, no `return` is added. This is because
it is pretty obvious that these calls terminate the function without
the explicit `return`.
Since adding `return` changes the expression that a macro will see, this
rule is disabled for function definitions inside of macros with the
exception of some known ones from Base (e.g. `@inline`, `@generated`,
...).
Closes#43.
- Listlike expressions (e.g. tuples, calls, paren-blocks, vectors) that
span multiple lines now always require a leading and a trailing
newline (and thus a trailing comma from previous listlike spacing
code). For example
```diff
-f(a,
- b)
+f(
+ a,
+ b,
+)
```
- Listlike expressions are now hard indents instead of the
soft/continuation style from before. This looks better.
- Vectors (K"vect" and K"ref") are now spaced like lists.
- Fix some bugs in spaces-around-operators when there where hidden
newlines at the beginning of the call chain.
Closes#9.
This is a huge patch and adds formatting of indentation. It is slightly
difficult to come up with a good set of rules but I think what is here
is pretty nice. There are two types of indentation: block/hard
indentation and continuation/soft indentation.
Block indentation are context independent and are thus "hard" or
required in the sense that they always happen. Block indentation are
enabled for all `x - end` blocks where `x` is one of `begin`, `do`,
`for`, `function`, `if`, `let`, `quote`, `struct`, `try`, `while`. A
notable exception is `module`, but one idea is to indent nested modules
(in the same file).
Continuation or soft indents are context dependent in the sense that if
nested within another soft indent they extra indents are skipped. Soft
indentation applies to all expressions spanning multiple lines.
For example, newlines in operator chains:
```julia
a + b * c +
d # soft indent
```
function calls
```julia
foo(
a, b c, d # soft indent
)
```
etc.
This patch changes the code quite a bit:
- Use an IOBuffer for the source text instead of a string. This make it
simpler to use byte index instead of string index.
- Utilize the seekability of the output IOBuffer to backtrack changes
when child nodes want to format something.
- Build an formatted tree as we go: unchanged nodes alias the source
tree and we build new nodes when things change.