Skip to content

Commit 1ad8bf5

Browse files
pkopriv2vim-scripts
authored andcommitted
Version b2.0
Completely Rewrote Fixed: Determines comment lines by synIDAttr Added: Anonymous function handling
0 parents  commit 1ad8bf5

File tree

4 files changed

+1027
-0
lines changed

4 files changed

+1027
-0
lines changed

README

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
This is a mirror of http://www.vim.org/scripts/script.php?script_id=3081
2+
3+
Hosted at: http://code.google.com/p/web-indent/
4+
5+
*Update: (5/14/10) - Beta 3.0 Released (See Below) (First Release Candidate)
6+
*Update: (5/6/10) - Beta 2.2 Released (See Below)
7+
*Update: (5/6/10) - Beta 2.1 Released (See Below)
8+
*Update: (5/5/10) - Beta 2.0 Released (See Below)
9+
*Update: (5/3/10) - Beta 1.0 Released (See Below)
10+
11+
Summary:
12+
================================================================================
13+
I wrote this basically due to the lack of any good javascript indent plugin out there. And due to javascripts lax syntactical structure cindent() just wont cut it.
14+
15+
Web-Indent handles all basic forms of add notes, bugs and multi-line and singe line objects:
16+
var x = {
17+
y : {
18+
z;
19+
}
20+
}
21+
22+
var y = [
23+
x,
24+
y,
25+
z
26+
]
27+
28+
var z = {x : x, y : y, w: w};
29+
30+
t; // no indent
31+
32+
Handles all basic control structures (with or without leading '{'):
33+
if(x)
34+
// comment
35+
y;
36+
z;
37+
38+
Handles inline and nextline control structure block initializers ('{' in the vernacular):
39+
if(x) {
40+
x;
41+
}
42+
else
43+
{
44+
z;
45+
}
46+
47+
Handles multi-line function declarations:
48+
var x = function(
49+
y, // notice one tab over
50+
z,
51+
w) {
52+
t;
53+
}
54+
55+
Handles multi-line control structure statements:
56+
if( x == y ||
57+
y == z ||
58+
z == w)
59+
{
60+
t;
61+
}
62+
63+
This indenter is also fully compatible with Mootools and esp. its Class package. I haven't tested it with any of the other frameworks, but due to their similar syntactical nature, I'm assuming they will work as will.
64+
65+
Included Files:
66+
================================================================================
67+
~test.js - A sample js file with various coding patterns. Set tabstop=6 and shiftwidth=6 and comments should line up correctly.
68+
~indent/javascript.vim - The core indenting file
69+
~indent/html.vim - An updated version of the pre-packaged html indenter that utilizes javascript.vim (MUST BE LOCATED IN SAME DIRECTORY!!)
70+
71+
Release Notes:
72+
================================================================================
73+
~Beta 3.0 Released: (5/14/10)
74+
- Fixed: Runaway Indentation.
75+
- Fixed: indentkeys now correctly invoke indent.
76+
- Fixed: Comment lines with keywords are correctly indented. Thanks to trojhlav for pointing it out.
77+
- Notes:
78+
\ First Release Candidate (v 1.0)
79+
\ Correctly indented entire jQuery source.
80+
\ Correctly indented entire MooTools source.
81+
- Concerns:
82+
\ Still no support for continuation lines.
83+
~
84+
85+
86+
~Beta 2.2 Released: (5/6/10)
87+
- Added: Support for enabling/disabling logging. (Enabled by default. To Disable: let g:js_indent_log = 0 somewhere in your .vimrc script)
88+
- Added: Support for while() control block.
89+
- Added: Support for do {} while(); control block.
90+
- Added: Support for with() {} control block.
91+
- Added: Skeleton support for all MULTI-LINE cntrl blocks SINGLE LINE cntrl blocks DO NOT HAVE SKELETON VERSIONS!!
92+
- Added: Skeleton support for function declarations.
93+
- Added: Skeleton support for basic objects.
94+
- Added: Tests for skeleton code.
95+
- Fixed: Html indent now correctly calls GetJsIndent() while inside <script type="text/javascript"></script>
96+
- Fixed: Html will now source the javascript.vim (indent file) via two methods:
97+
1. If g:js_indent is set and points to the location of the indent file, it will source it.
98+
2. Looks in your runtime path for indent/javascript.vim (sources first one found)
99+
- Added: Tests for HTML + JS
100+
- Notes:
101+
\ None
102+
~
103+
104+
~Beta 2.1 Released: (5/6/10)
105+
- Fixed: Object ending regex is now anchored to beginning and end of line and cannot contain preceding {'s or ['s.
106+
- Fixed: Multi Line Invocations.
107+
- Dependencies:
108+
\ Syntax Script must return *Comment* for any type of comment when invoked with synIDattr
109+
- Concerns:
110+
\ Multi Line Invocations must end in ; or , to be recognized (although JS doesn't specifically require this) (so as not confuse with multi line declarations)
111+
\ Does not recognize continuation lines.
112+
- Notes:
113+
\ Entire test.js file is now correctly indented (except for continuations)
114+
\ NOW SUPPORTS SKELETON CODE
115+
if(x) {
116+
} else if(y) {
117+
} else {
118+
}
119+
~
120+
121+
~Beta 2.0 Released: (5/5/10)
122+
- Now hosted on Google Code: http://code.google.com/p/web-indent/
123+
- Completely rewrote script
124+
- Fixed: Comments are determined by synIDAttr instead of Regex
125+
- Added: Anonymous function support. (function(){})();
126+
- Dependencies:
127+
\ None
128+
- Concerns:
129+
\ Too many if statements, but extremely well organized.
130+
\ Sometimes vim has trouble indenting an entire file. Have to scroll through to location above problem area and use =G again. Is this a problem with how the scripts get loaded?
131+
- Notes
132+
\ None
133+
~
134+
135+
~ Beta 1.0 Released: (5/3/10)
136+
- Initial Release
137+
- Dependencies:
138+
\ Javascript Syntax: http://www.vim.org/scripts/script.php?script_id=1491
139+
- Concerns:
140+
\ Comments are determined by regex (==> Cannot determine middle of block comments)
141+
\ Code is poorly organized and hard to understand.
142+
~
143+
144+
PS: I'm also not sure if I'm allowed to repost the html.vim, since I couldn't find a license for it. I'm pretty sure it was the one packaged with vim by default. If anyone has any objections, I'll take it down immediately.

indent/html.vim

Lines changed: 250 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,250 @@
1+
2+
" Description: html indenter
3+
" Author: Johannes Zellner <johannes@zellner.org>
4+
" Last Change: Mo, 05 Jun 2006 22:32:41 CEST
5+
" Restoring 'cpo' and 'ic' added by Bram 2006 May 5
6+
" Globals: g:html_indent_tags -- indenting tags
7+
" g:html_indent_strict -- inhibit 'O O' elements
8+
" g:html_indent_strict_table -- inhibit 'O -' elements
9+
10+
" Only load this indent file when no other was loaded.
11+
if exists("b:did_indent")
12+
finish
13+
endif
14+
let b:did_indent = 1
15+
16+
17+
source javascript.vim
18+
19+
" [-- local settings (must come before aborting the script) --]
20+
setlocal indentexpr=HtmlIndentGetter(v:lnum)
21+
setlocal indentkeys=o,O,*<Return>,<>>,{,}
22+
23+
24+
if exists('g:html_indent_tags')
25+
unlet g:html_indent_tags
26+
endif
27+
28+
" [-- helper function to assemble tag list --]
29+
fun! <SID>HtmlIndentPush(tag)
30+
if exists('g:html_indent_tags')
31+
let g:html_indent_tags = g:html_indent_tags.'\|'.a:tag
32+
else
33+
let g:html_indent_tags = a:tag
34+
endif
35+
endfun
36+
37+
38+
" [-- <ELEMENT ? - - ...> --]
39+
call <SID>HtmlIndentPush('a')
40+
call <SID>HtmlIndentPush('abbr')
41+
call <SID>HtmlIndentPush('acronym')
42+
call <SID>HtmlIndentPush('address')
43+
call <SID>HtmlIndentPush('b')
44+
call <SID>HtmlIndentPush('bdo')
45+
call <SID>HtmlIndentPush('big')
46+
call <SID>HtmlIndentPush('blockquote')
47+
call <SID>HtmlIndentPush('button')
48+
call <SID>HtmlIndentPush('caption')
49+
call <SID>HtmlIndentPush('center')
50+
call <SID>HtmlIndentPush('cite')
51+
call <SID>HtmlIndentPush('code')
52+
call <SID>HtmlIndentPush('colgroup')
53+
call <SID>HtmlIndentPush('del')
54+
call <SID>HtmlIndentPush('dfn')
55+
call <SID>HtmlIndentPush('dir')
56+
call <SID>HtmlIndentPush('div')
57+
call <SID>HtmlIndentPush('dl')
58+
call <SID>HtmlIndentPush('em')
59+
call <SID>HtmlIndentPush('fieldset')
60+
call <SID>HtmlIndentPush('font')
61+
call <SID>HtmlIndentPush('form')
62+
call <SID>HtmlIndentPush('frameset')
63+
call <SID>HtmlIndentPush('h1')
64+
call <SID>HtmlIndentPush('h2')
65+
call <SID>HtmlIndentPush('h3')
66+
call <SID>HtmlIndentPush('h4')
67+
call <SID>HtmlIndentPush('h5')
68+
call <SID>HtmlIndentPush('h6')
69+
call <SID>HtmlIndentPush('i')
70+
call <SID>HtmlIndentPush('iframe')
71+
call <SID>HtmlIndentPush('ins')
72+
call <SID>HtmlIndentPush('kbd')
73+
call <SID>HtmlIndentPush('label')
74+
call <SID>HtmlIndentPush('legend')
75+
call <SID>HtmlIndentPush('map')
76+
call <SID>HtmlIndentPush('menu')
77+
call <SID>HtmlIndentPush('noframes')
78+
call <SID>HtmlIndentPush('noscript')
79+
call <SID>HtmlIndentPush('object')
80+
call <SID>HtmlIndentPush('ol')
81+
call <SID>HtmlIndentPush('optgroup')
82+
" call <SID>HtmlIndentPush('pre')
83+
call <SID>HtmlIndentPush('q')
84+
call <SID>HtmlIndentPush('s')
85+
call <SID>HtmlIndentPush('samp')
86+
call <SID>HtmlIndentPush('script')
87+
call <SID>HtmlIndentPush('select')
88+
call <SID>HtmlIndentPush('small')
89+
call <SID>HtmlIndentPush('span')
90+
call <SID>HtmlIndentPush('strong')
91+
call <SID>HtmlIndentPush('style')
92+
call <SID>HtmlIndentPush('sub')
93+
call <SID>HtmlIndentPush('sup')
94+
call <SID>HtmlIndentPush('table')
95+
call <SID>HtmlIndentPush('textarea')
96+
call <SID>HtmlIndentPush('title')
97+
call <SID>HtmlIndentPush('tt')
98+
call <SID>HtmlIndentPush('u')
99+
call <SID>HtmlIndentPush('ul')
100+
call <SID>HtmlIndentPush('var')
101+
102+
103+
" [-- <ELEMENT ? O O ...> --]
104+
if !exists('g:html_indent_strict')
105+
call <SID>HtmlIndentPush('body')
106+
call <SID>HtmlIndentPush('head')
107+
call <SID>HtmlIndentPush('html')
108+
call <SID>HtmlIndentPush('tbody')
109+
endif
110+
111+
112+
" [-- <ELEMENT ? O - ...> --]
113+
if !exists('g:html_indent_strict_table')
114+
call <SID>HtmlIndentPush('th')
115+
call <SID>HtmlIndentPush('td')
116+
call <SID>HtmlIndentPush('tr')
117+
call <SID>HtmlIndentPush('tfoot')
118+
call <SID>HtmlIndentPush('thead')
119+
endif
120+
121+
delfun <SID>HtmlIndentPush
122+
123+
let s:cpo_save = &cpo
124+
set cpo-=C
125+
126+
" [-- count indent-increasing tags of line a:lnum --]
127+
fun! <SID>HtmlIndentOpen(lnum, pattern)
128+
let s = substitute('x'.getline(a:lnum),
129+
\ '.\{-}\(\(<\)\('.a:pattern.'\)\>\)', "\1", 'g')
130+
let s = substitute(s, "[^\1].*$", '', '')
131+
return strlen(s)
132+
endfun
133+
134+
" [-- count indent-decreasing tags of line a:lnum --]
135+
fun! <SID>HtmlIndentClose(lnum, pattern)
136+
let s = substitute('x'.getline(a:lnum),
137+
\ '.\{-}\(\(<\)/\('.a:pattern.'\)\>>\)', "\1", 'g')
138+
let s = substitute(s, "[^\1].*$", '', '')
139+
return strlen(s)
140+
endfun
141+
142+
" [-- count indent-increasing '{' of (java|css) line a:lnum --]
143+
fun! <SID>HtmlIndentOpenAlt(lnum)
144+
return strlen(substitute(getline(a:lnum), '[^{]\+', '', 'g'))
145+
endfun
146+
147+
" [-- count indent-decreasing '}' of (java|css) line a:lnum --]
148+
fun! <SID>HtmlIndentCloseAlt(lnum)
149+
return strlen(substitute(getline(a:lnum), '[^}]\+', '', 'g'))
150+
endfun
151+
152+
" [-- return the sum of indents respecting the syntax of a:lnum --]
153+
fun! <SID>HtmlIndentSum(lnum, style)
154+
if a:style == match(getline(a:lnum), '^\s*</')
155+
if a:style == match(getline(a:lnum), '^\s*</\<\('.g:html_indent_tags.'\)\>')
156+
let open = <SID>HtmlIndentOpen(a:lnum, g:html_indent_tags)
157+
let close = <SID>HtmlIndentClose(a:lnum, g:html_indent_tags)
158+
if 0 != open || 0 != close
159+
return open - close
160+
endif
161+
endif
162+
endif
163+
if '' != &syntax &&
164+
\ synIDattr(synID(a:lnum, 1, 1), 'name') =~ '\(css\|java\).*' &&
165+
\ synIDattr(synID(a:lnum, strlen(getline(a:lnum)), 1), 'name')
166+
\ =~ '\(css\|java\).*'
167+
if a:style == match(getline(a:lnum), '^\s*}')
168+
return <SID>HtmlIndentOpenAlt(a:lnum) - <SID>HtmlIndentCloseAlt(a:lnum)
169+
endif
170+
endif
171+
return 0
172+
endfun
173+
174+
fun! HtmlIndentGetter(lnum)
175+
" Find a non-empty line above the current line.
176+
let lnum = prevnonblank(a:lnum - 1)
177+
178+
" Hit the start of the file, use zero indent.
179+
if lnum == 0
180+
return 0
181+
endif
182+
183+
let restore_ic = &ic
184+
setlocal ic " ignore case
185+
186+
" [-- special handling for <pre>: no indenting --]
187+
if getline(a:lnum) =~ '\c</pre>'
188+
\ || 0 < searchpair('\c<pre>', '', '\c</pre>', 'nWb')
189+
\ || 0 < searchpair('\c<pre>', '', '\c</pre>', 'nW')
190+
" we're in a line with </pre> or inside <pre> ... </pre>
191+
if restore_ic == 0
192+
setlocal noic
193+
endif
194+
return -1
195+
endif
196+
197+
" [-- special handling for <javascript>: use cindent --]
198+
let js = '<script\s*type\s*=\s*javascript'
199+
200+
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
201+
" by Tye Zdrojewski <zdro@yahoo.com>, 05 Jun 2006
202+
" ZDR: This needs to be an AND (we are 'after the start of the pair' AND
203+
" we are 'before the end of the pair'). Otherwise, indentation
204+
" before the start of the script block will be affected; the end of
205+
" the pair will still match if we are before the beginning of the
206+
" pair.
207+
"
208+
209+
if 0 < searchpair(js, '', '</script>', 'nWb')
210+
\ && 0 < searchpair(js, '', '</script>', 'nW')
211+
" we're inside javascript
212+
if getline(lnum) !~ js && getline(a:lnum) != '</script>'
213+
if restore_ic == 0
214+
setlocal noic
215+
endif
216+
217+
let l:ind = GetJsIndent(a:lnum)
218+
219+
echo l:ind
220+
return l:ind
221+
endif
222+
endif
223+
224+
if getline(lnum) =~ '\c</pre>'
225+
" line before the current line a:lnum contains
226+
" a closing </pre>. --> search for line before
227+
" starting <pre> to restore the indent.
228+
let preline = prevnonblank(search('\c<pre>', 'bW') - 1)
229+
if preline > 0
230+
if restore_ic == 0
231+
setlocal noic
232+
endif
233+
return indent(preline)
234+
endif
235+
endif
236+
237+
let ind = <SID>HtmlIndentSum(lnum, -1)
238+
let ind = ind + <SID>HtmlIndentSum(a:lnum, 0)
239+
240+
if restore_ic == 0
241+
setlocal noic
242+
endif
243+
244+
return indent(lnum) + (&sw * ind)
245+
endfun
246+
247+
let &cpo = s:cpo_save
248+
unlet s:cpo_save
249+
250+
" [-- EOF <runtime>/indent/html.vim --]

0 commit comments

Comments
 (0)