Skip to content
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

Indent code in Go HTML templates #709

Merged
merged 1 commit into from
Feb 13, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions indent/gohtmltmpl.vim
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,42 @@ if exists("b:did_indent")
endif

runtime! indent/html.vim

" Indent Golang HTML templates
setlocal indentexpr=GetGoHTMLTmplIndent(v:lnum)
setlocal indentkeys+==else,=end

" Only define the function once.
if exists("*GetGoHTMLTmplIndent")
finish
endif

function! GetGoHTMLTmplIndent(lnum)
" Get HTML indent
if exists('*HtmlIndent')
let ind = HtmlIndent()
else
let ind = HtmlIndentGet(a:lnum)
endif

" The value of a single shift-width
if exists('*shiftwidth')
let sw = shiftwidth()
else
let sw = &sw
endif

" If need to indent based on last line
Copy link
Owner

Choose a reason for hiding this comment

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

What do you mean here exactly?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Indention usually based on last line, for below example, the second line need to be indented as last line has a if condition

{{if true}}
  hell world
{{end}}

Copy link
Owner

Choose a reason for hiding this comment

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

Thanks 👍

let last_line = getline(a:lnum-1)
if last_line =~ '^\s*{{\s*\%(if\|else\|range\|with\|define\|block\).*}}'
let ind += sw
endif

" End of FuncMap block
let current_line = getline(a:lnum)
if current_line =~ '^\s*{{\s*\%(else\|end\).*}}'
let ind -= sw
endif

return ind
endfunction