OyvindSabo / gremlint

Linter/Code formatter for Gremlin
https://gremlint.com
Apache License 2.0
11 stars 6 forks source link

Make indentation work for code which is not Gremlin #71

Closed OyvindSabo closed 3 years ago

OyvindSabo commented 3 years ago

Consider that we want to format the following query with a max line length of 70:

hasField = { field -> __.has(field) }

profitQuery = g.V().
filter(hasField('sett_price')).
filter(hasField('buy_price')).
project('product', 'profit').
by('name').
by{ it.get().value('sell_price') -
    it.get().value('buy_price') };

With an indentation of 0 spaces it is formatted like this (as expected):

hasField = { field -> __.has(field) }

profitQuery = g.V().
  filter(hasField('sett_price')).
  filter(hasField('buy_price')).
  project('product', 'profit').
    by('name').
    by{ it.get().value('sell_price') -
        it.get().value('buy_price') };

However, with an indentation of 20 spaces it is formatted like this:

hasField = { field -> __.has(field) }

profitQuery =                     g.V().
                      filter(hasField('sett_price')).
                      filter(hasField('buy_price')).
                      project('product', 'profit').
                        by('name').
                        by{ it.get().value('sell_price') -
                            it.get().value('buy_price') };

I would expect it to be formatted like this:

                    hasField = { field -> __.has(field) }

                    profitQuery = g.V().
                      filter(hasField('sett_price')).
                      filter(hasField('buy_price')).
                      project('product', 'profit').
                        by('name').
                        by{ it.get().value('sell_price') -
                            it.get().value('buy_price') };
OyvindSabo commented 3 years ago

Perhaps the easiest way to solve this in a robust way would be to not add the globally selected indentation until after the final query has been recreated. Then there will be no need to struggle with working around indentations appearing in the middle of a line when a query starts in the middle of a line. When cheking whether lines should wrap, we can just use a max line length equal to the specified max line length minus the global indentation.