jq.vim icon indicating copy to clipboard operation
jq.vim copied to clipboard

Add indent expr support

Open A4-Tacks opened this issue 1 year ago • 5 comments

Close #21

  • Although it may not work perfectly in some situations, it is better than nothing
  • Added end semicolon highlighting for name definition to assist indentation

A4-Tacks avatar Jan 08 '25 13:01 A4-Tacks

Thanks for your contribution!

Although it may not work perfectly in some situations, it is better than nothing

Can you give me some more details about what situations won't work perfectly and what works well. Then we could try and correct the issues. Right now this repo eventually gets merged into the vim codebase so I would rather have it working and validated.

Potential Issues I saw:

  1. Handling of Single line statements
  • Issue: Single line constructs like if ... then ... else ... end might not be properly indented.
  • Example: if foo then bar else baz end
  • The script does not distinguish between multi-line blocks and single-line constructs, which might lead to unnecessary indentation.
  1. Empty or Dangling Lines -Issue: The script does not explicitly handle empty lines or lines with only a closing bracket/braces (}, ], )). Example:
{
foo: .bar
}

The closing } might not decrease the indentation properly if the previous line (foo: .bar) isn't interpreted correctly.

  1. Multi-Line Strings
  • Issue: Multi-line strings, which are valid in jq, might confuse the indentation logic since they don't end with expected tokens. Example: "This is a multi-line string" The script could mistakenly adjust the indentation after the first line.
  1. Missing Jq keywords Issue: The script assumes specific keywords and symbols for indentation but does not handle all possible jq constructs (e.g., reduce, foreach, or custom-defined keywords in def). I commented on your PR with a suggestion for this.
reduce .[] as $item ({}; . + $item)
  1. Nested Structures Issue: Deeply nested blocks might not indent properly due to the cumulative effect of indent calculations. Example:
if foo then
    if bar then
        .baz
    end
end

The script might fail to calculate the correct indentation level for the second if block, especially if the previous lines are stripped incorrectly.

vito-c avatar Jan 13 '25 05:01 vito-c

Also since you added some highlighting could you add a screenshot with and without the highlighting. I think we would probably have to add a flag for that.

vito-c avatar Jan 13 '25 06:01 vito-c

Also since you added some highlighting could you add a screenshot with and without the highlighting. I think we would probably have to add a flag for that.

It only affects the highlighting of semicolons at the end of the name definiaton

old: Screenshot_20250113_193722

current: Screenshot_20250113_193603

A4-Tacks avatar Jan 13 '25 11:01 A4-Tacks

The indentation of parentheses at the end and beginning of unpaired lines may be incorrect

  # some code
  reduce .[] as $i (0;
  .+$i
)

Perhaps we can match reduce and foreach at the beginning of the line to handle certain situations

A4-Tacks avatar Jan 13 '25 12:01 A4-Tacks

Reducing multi-level indentation in the same line is troublesome, and how much indentation should be added to multiple parentheses in the same line is a more complex issue. Perhaps the current one is good enough

    # some code
  end)

In the following two codes

  # some code
  (@text | if . then
    .
  end)
  # yes
  # some code
  (
    @text
    | if . then
      .
    end)
    # no

If we want to distinguish these situations, the complexity and performance may not be acceptable

A4-Tacks avatar Jan 13 '25 12:01 A4-Tacks