I've been experimenting with Jedi Code Formatter, trying to find the options that suit me. My personal preference is what in the C world is called "Whitesmiths" style - begin/end pairs are indented at the same level as the block they contain, which to me (religious war coming...) makes sense as it matches the Pascal language definition.
Anyway, this can be achieved with JCF using the "Extra indent for being/end inside procedures option", with this result:
procedure SaveYamlObject(AEmitter: TYamlEmitter; const AName: string;
AValue: TOTPersistent);
begin
AEmitter.ScalarEvent('', '', AName, True, False, yssPlainScalar);
if Assigned(AValue) then
begin
AValue.SaveToYaml(AEmitter);
end
else
begin
AEmitter.MappingStartEvent('', '', False, ympFlowMapping);
AEmitter.MappingEndEvent;
end;
end;
The downside is that it also messes up try/finally/except blocks, producing this....
emitter := TYamlEmitter.Create;
try
stream := TStringStream.Create;
emitter.SetOutput(stream);
yamlVer.Initialize;
emitter.StreamStartEvent;
emitter.DocumentStartEvent(yamlVer, nil, True);
owning.SaveToYaml(emitter);
emitter.DocumentEndEvent(True);
emitter.StreamEndEvent;
yamlText := stream.DataString;
finally
emitter.Free;
end;
which should be formatted as:
emitter := TYamlEmitter.Create;
try
stream := TStringStream.Create;
emitter.SetOutput(stream);
yamlVer.Initialize;
emitter.StreamStartEvent;
emitter.DocumentStartEvent(yamlVer, nil, True);
owning.SaveToYaml(emitter);
emitter.DocumentEndEvent(True);
emitter.StreamEndEvent;
yamlText := stream.DataString;
finally
emitter.Free;
end;
(ie the try/finally should NOT be indented).
The cuplrit is in Indenter.pas:
// IndentBeginEnd option to indent begin/end words a bit extra
if FormattingSettings.Indent.IndentBeginEnd then
begin
if (pt.TokenType in [ttTry, ttExcept, ttFinally, ttBegin, ttEnd]) and InStatements(pt) then
begin
// filter out the begin/end that starts and ends a procedure
if not pt.HasParentNode(nBlock, 2) then
begin
Result := Result + FormattingSettings.Indent.IndentBeginEndSpaces;
end;
end;
end;
Is there any good reason why ttTry, ttExcept and ttFinally are being indented here?
This looks like a simple fix for me to make, but if I want a PR to be accepted for this should there be a new option added so that formatting for any existing users of this option won't be affected?
Alistair.