swish icon indicating copy to clipboard operation
swish copied to clipboard

Add menu item for formatting the source code

Open rillig opened this issue 6 years ago • 1 comments

When I edit the source code of my program, the editor doesn't get the indentation correct:

  • Type demo :-, press Enter, press Backspace 4 times, press Tab, press Enter. Now the indentation uses a mixture of tabs and spaces, which will look wrong if the next editor has a different tab width.

Type the following text:

demo :-
	L = [
        [
        karte(A)
        ],
          [
          karte(B),
            ],
          ],
    write(L), nl.
  • The second [ should be indented by 4 more spaces.
  • The third [ should not be indented by an additional 2 spaces.
  • Each closing bracket should be aligned with its corresponding opening bracket.
  • The karte(B) should be indented by an additional 4 spaces.

To fix these, either the automatic indentation should be fixed, or at least there should be a menu item with which I can re-indent the existing code.

rillig avatar Dec 30 '19 13:12 rillig

There are a number of issue.

  • The list layout code expects the first element of the list on the same line as the [. It will then indent subsequent elements to the same level and align the final ] if it it is preceded by a newline. We could of course argue what should happen if the first argument is on a newline. AFAIK that is not very common in the Prolog world.
  • The failure to align after karte(B), and the last ] are due to the fact that there is a , after the last list element. That is not allowed in Prolog.
  • Re-layout of a block is done by selecting the block and using SHIFT-TAB. Without a selection SHIFT-TAB re-indents the current line.

See https://swish.swi-prolog.org/p/kGbNFuOK.pl

JanWielemaker avatar Dec 30 '19 15:12 JanWielemaker