admin管理员组

文章数量:1355648

I am working with Pascal/Delphi code and need to format a selection of code with custom indentation rules (e.g. 4 spaces for indentation). I want to apply these formatting rules to the selected code without affecting the entire document.

For example, I have this unformatted code:

procedure MyProcedure;
begin
if SomeCondition then
begin
DoSomething;
for i := 0 to 10 do
begin
DoSomethingElse;
end;
end;
end;

I want to quickly apply 4 spaces for indentation to the selected portion, including the begin and end of the loop, so the code becomes properly indented like this:

procedure MyProcedure;
    begin
    if SomeCondition then
        begin
        DoSomething;
        for i := 0 to 10 do
            begin
            DoSomethingElse;
            end;
        end;
    end;

I am working with Pascal/Delphi code and need to format a selection of code with custom indentation rules (e.g. 4 spaces for indentation). I want to apply these formatting rules to the selected code without affecting the entire document.

For example, I have this unformatted code:

procedure MyProcedure;
begin
if SomeCondition then
begin
DoSomething;
for i := 0 to 10 do
begin
DoSomethingElse;
end;
end;
end;

I want to quickly apply 4 spaces for indentation to the selected portion, including the begin and end of the loop, so the code becomes properly indented like this:

procedure MyProcedure;
    begin
    if SomeCondition then
        begin
        DoSomething;
        for i := 0 to 10 do
            begin
            DoSomethingElse;
            end;
        end;
    end;
Share Improve this question edited Mar 28 at 13:26 AmigoJack 6,1852 gold badges19 silver badges34 bronze badges asked Mar 28 at 5:45 user5005768-hduser5005768-hd 1,4593 gold badges30 silver badges66 bronze badges 6
  • 1 The GExperts code formatter only formats the selected lines (if there is a selection, otherwise it formats the whole unit). But it does much more than just indent, so you might not like the result. OTOH there are lots of configuration options, so you might be able to create a configuration to suit your needs. – dummzeuch Commented Mar 28 at 8:22
  • What Delphi version? Early ones did not support code formatting at all, and so you had to use a third-party plugin such as GExperts. Later versions support code formatting internally. This question is version-specific, so you should include a tag for that version. – Ken White Commented Mar 28 at 12:42
  • 2 That's a very odd formatting you wish to achieve (begin being also indented, so everything within it is on the same level). And you should tell us which IDE you're using, since Pascal/Delphi is unbound to the IDE strictly speaking - nobody can know if you use Lazarus, Delphi or even something else. – AmigoJack Commented Mar 28 at 13:29
  • 4 FWIW, I've been writing Delphi code almost daily since the late 1990s, and I can barely read this "properly indented" code. – Andreas Rejbrand Commented Mar 28 at 18:14
  • @AndreasRejbrand, the "properly indented" code you can barely read with your feedback of a late 90s developper is (however) what we have learned at school 10+ years before. I confess that I only use Ctrl+D in the Delphi IDE to share scripts and comply with present uses. I can understand that the brain sometimes operates better with the formatting we used inally for decades. Laziness to change or comfort? – Fred62 Commented Mar 30 at 10:47
 |  Show 1 more comment

2 Answers 2

Reset to default 3

Tested using the built-in code formatter in Delphi 12.3:

Make sure you have Modeling installed in Tools|Manage Features.

  1. Set the Block Indent to 4 spaces. (Tools|Editor|Language and select Delphi).

  2. In the Delphi formatter (Tools|Language|Formatter|Delphi|Indentation), set the following:

    • Indent Begin and End keywords: True
    • Indent blocks between Begin and End: False
    • Indent Function Bodies: True
  3. Use Ctrl+D to format the code. If a block is selected, just that block will be formatted. Otherwise, the entire unit will be formatted.

Formatter options can be found here.

  1. Tools/Options.../Editor/Language/(Language Delphi)/Block Indent: set to 4, click Save.

  2. Select a piece of code (the finished fragment), press Ctrl+D.

  3. If the formatter didn't work, copy the piece of code to another page of the editor and repeat formatting there.

Additional formatting options can be found in: Tools/Options.../Language/Formatter/Delphi.

本文标签: How to easily and quickly apply custom indentation format rule to selected PascalDelphi codeStack Overflow