-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hanging attributes #55
Open
AndrewRadev
wants to merge
12
commits into
mustache:master
Choose a base branch
from
AndrewRadev:hanging-attributes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4467c80
Check for a hanging attribute
AndrewRadev f17f6ba
Fix bug with component in-line
AndrewRadev a954d79
Save and restore state, just in case
AndrewRadev 97fa57f
Merge branch 'master' into hanging-attributes
AndrewRadev 87c3197
Fix bug with closing pattern after }} on the previous line
AndrewRadev e513ffb
Merge branch 'master' into hanging-attributes
AndrewRadev d03754a
Add an example of hanging attributes
AndrewRadev f2c0542
Merge branch 'master' into hanging-attributes
AndrewRadev a6d16d4
Fix unclosed <a> tag
AndrewRadev 34356ba
Fix double comment
AndrewRadev 79bf5f4
Move pattern out as a script-local var
AndrewRadev 85df17c
Remove (hopefully) unnecessary check
AndrewRadev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -45,6 +45,8 @@ if exists("*GetHandlebarsIndent") | |||||||||||||||||||
finish | ||||||||||||||||||||
endif | ||||||||||||||||||||
|
||||||||||||||||||||
let s:componentClosingPattern = '\v^\s*\{\{\/\S*\}\}\s*' | ||||||||||||||||||||
|
||||||||||||||||||||
function! GetHandlebarsIndent(...) | ||||||||||||||||||||
" The value of a single shift-width | ||||||||||||||||||||
if exists('*shiftwidth') | ||||||||||||||||||||
|
@@ -79,16 +81,55 @@ function! GetHandlebarsIndent(...) | |||||||||||||||||||
" all indent rules only apply if the block opening/closing | ||||||||||||||||||||
" tag is on a separate line | ||||||||||||||||||||
|
||||||||||||||||||||
" indent after block {{#block | ||||||||||||||||||||
" check for a hanging attribute | ||||||||||||||||||||
let lastLnumCol = col([lnum, '$']) - 1 | ||||||||||||||||||||
if synIDattr(synID(lnum, lastLnumCol, 1), "name") =~ '^mustache' | ||||||||||||||||||||
\ && prevLine !~# '}}\s*$' | ||||||||||||||||||||
let hangingAttributePattern = '{{[#^]\=\%(\k\|[/-]\)\+\s\+\zs\k\+=' | ||||||||||||||||||||
let standaloneComponentPattern = '^\s*{{\%(\k\|[/-]\)\+\s*$' | ||||||||||||||||||||
|
||||||||||||||||||||
if prevLine =~ hangingAttributePattern | ||||||||||||||||||||
" {{component attribute=value | ||||||||||||||||||||
" other=value}} | ||||||||||||||||||||
let [line, col] = searchpos(hangingAttributePattern, 'Wbn', lnum) | ||||||||||||||||||||
if line == lnum | ||||||||||||||||||||
return col - 1 | ||||||||||||||||||||
endif | ||||||||||||||||||||
elseif prevLine =~ standaloneComponentPattern | ||||||||||||||||||||
" {{component | ||||||||||||||||||||
" attribute=value}} | ||||||||||||||||||||
return indent(lnum) + sw | ||||||||||||||||||||
endif | ||||||||||||||||||||
endif | ||||||||||||||||||||
|
||||||||||||||||||||
" check for a closing }}, indent according to the opening one | ||||||||||||||||||||
let saved_pos = getpos('.') | ||||||||||||||||||||
if prevLine =~# '}}$' && prevLine !~# '^\s*{{' && | ||||||||||||||||||||
\ currentLine !~# s:componentClosingPattern && | ||||||||||||||||||||
\ search('}}$', 'Wb') | ||||||||||||||||||||
let [line, col] = searchpairpos('{{', '', '}}', 'Wb') | ||||||||||||||||||||
if line > 0 | ||||||||||||||||||||
if strpart(getline(line), col - 1, 3) =~ '{{[#^]' | ||||||||||||||||||||
" then it's a block component, indent a shiftwidth more | ||||||||||||||||||||
return indent(line) + sw | ||||||||||||||||||||
else | ||||||||||||||||||||
return indent(line) | ||||||||||||||||||||
endif | ||||||||||||||||||||
Comment on lines
+112
to
+117
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
We could almost do this, see what I mean? the |
||||||||||||||||||||
else | ||||||||||||||||||||
call setpos('.', saved_pos) | ||||||||||||||||||||
endif | ||||||||||||||||||||
endif | ||||||||||||||||||||
|
||||||||||||||||||||
" indent after block: {{#block, {{^block | ||||||||||||||||||||
if prevLine =~# '\v\s*\{\{[#^].*\s*' | ||||||||||||||||||||
let ind = ind + sw | ||||||||||||||||||||
endif | ||||||||||||||||||||
" but not if the block ends on the same line | ||||||||||||||||||||
if prevLine =~# '\v\s*\{\{\#(.+)(\s+|\}\}).*\{\{\/\1' | ||||||||||||||||||||
if prevLine =~# '\v\s*\{\{[#^](.+)(\s+|\}\}).*\{\{\/\1' | ||||||||||||||||||||
let ind = ind - sw | ||||||||||||||||||||
endif | ||||||||||||||||||||
" unindent after block close {{/block}} | ||||||||||||||||||||
if currentLine =~# '\v^\s*\{\{\/\S*\}\}\s*' | ||||||||||||||||||||
if currentLine =~# s:componentClosingPattern | ||||||||||||||||||||
let ind = ind - sw | ||||||||||||||||||||
endif | ||||||||||||||||||||
" indent after component block {{a-component | ||||||||||||||||||||
|
@@ -105,7 +146,7 @@ function! GetHandlebarsIndent(...) | |||||||||||||||||||
let closingLnum = search('}}\s*$', 'Wbc', lnum) | ||||||||||||||||||||
let [openingLnum, col] = searchpairpos('{{', '', '}}', 'Wb') | ||||||||||||||||||||
if openingLnum > 0 && closingLnum > 0 | ||||||||||||||||||||
if strpart(getline(openingLnum), col - 1, 3) !~ '{{[#^]' | ||||||||||||||||||||
if strpart(getline(openingLnum), col - 1, 3) !~# '{{[#^]' | ||||||||||||||||||||
let ind = ind - sw | ||||||||||||||||||||
endif | ||||||||||||||||||||
else | ||||||||||||||||||||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this part of hanging indent, too? Or is this a separate improvement? I just want to keep this PR as clean as possible.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see now that this is necessary for the hanging indent. However, it looks like it duplicates some other functionality.
I notice that on this line:
https://github.com/mustache/vim-mustache-handlebars/pull/55/files#diff-eeee5d46b419bb17834d0b1a86a43f8fR114
It's returning
indent(line) + sw
, which is also happening later:Would it be possible to restrict this section to just the "hanging indent"-related conditionals?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't the same case, though. The case that was already there applies on
prevLine
, while this new case is about the opening line of theprevLine
.Basically, the one that maches
prevLine
(the existing code) works for this case:While the new code works on this case:
The previous line in this case is not the block opener, it's the
three=four}}
line. What's the indent on the next line from this one? Well, we need to find its opening line, which is the{{#foo
line and take that one + one shiftwidth (if it's a block opener).For a different explanation, consider the comments -- the existing code is
indent after block: {{#block, {{^block
. The new code is not indenting after a block tag, it's indenting after the closing part of any tag.The only way I can see to unify them is to extend the new code to handle the old code's case, because "find the opening part" is the more generic logic. But trying it out, I seem to be breaking the
{{else if
case in the example, and I don't know why.To be honest, I don't think it's worth the risk to try to combine the two cases, especially since it would make the one merged case more complicated and possibly trickier to change in the future. Let me know what you think and whether I've explained the difference between the cases well enough.