Skip to content

Add indent expr support - #23

Open
A4-Tacks wants to merge 3 commits into
vito-c:masterfrom
A4-Tacks:indent-expr
Open

Add indent expr support#23
A4-Tacks wants to merge 3 commits into
vito-c:masterfrom
A4-Tacks:indent-expr

Conversation

@A4-Tacks

@A4-Tacks A4-Tacks commented Jan 8, 2025

Copy link
Copy Markdown

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

- 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 A4-Tacks changed the title Add indent expr Add indent expr support Jan 8, 2025
Comment thread indent/jq.vim
Comment thread indent/jq.vim Outdated
let indent = 0

if pline =~# '\v^\s*%(\|\s*)?%(def|try|then|if|elif|else)>|[:{([]$'
\|| pline =~# '\v<%(try|then|if|elif|else)$'

@vito-c vito-c Jan 13, 2025

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

" Define a list of command names
let s:jq_commands = '\v(def|try|then|if|elif|else)'

" Updated condition using the variable
if previous_line =~# '^\s*%(\|\s*)?' . s:jq_commands . '>|[:{([]$'
            \ || previous_line =~# '<' . s:jq_commands . '$'
    let indent_adjustment += 1
endif

we should add a jq_commands actually could you reuse the jqKeywords here?

" jqKeywords
do all of those keywords work for you?

then you could do something like this:

 " Function to check if a line starts with a jqKeyword
    function! IsJqKeyword(line) abort
        let col = match(a:line, '\S') + 1
        let syntax_id = synID(a:lnum, col, 1)
        let syntax_name = synIDattr(syntax_id, 'name')
        return syntax_name ==# 'jqKeywords'
    endfunction

@vito-c

vito-c commented Jan 13, 2025

Copy link
Copy Markdown
Owner

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

vito-c commented Jan 13, 2025

Copy link
Copy Markdown
Owner

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.

@A4-Tacks

Copy link
Copy Markdown
Author

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

Copy link
Copy Markdown
Author

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

Add `reduce` and `foreach` matching
@A4-Tacks

Copy link
Copy Markdown
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

indent support

2 participants