【发布时间】:2012-03-14 09:59:33
【问题描述】:
自动格式化 (gg=G) 非常适合这样的代码(来自here 的示例):
fun()
{
for(...)
{
for(...)
{
if(...)
{
}
}
}
}
变成
fun()
{
for(...)
{
for(...)
{
if(...)
{
}
}
}
}
但是对于像这样的更复杂的代码它会失败(从here复制)
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
</body>
</html>
变成:
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("p").click(function(){
$(this).hide();
});
});
</script>
</head>
<body>
<p>If you click on me, I will disappear.</p>
</body>
</html>
为什么 <p> 标签在正文中没有缩进?这是vim格式化程序的缺点还是我使用不正确?
编辑:感谢所有提到我应该将filetype plugin indent on 放入我的.vimrc 文件的人。这使得缩进好多了。但是,有时它仍然会失败。观察(复制自here)
<!DOCTYPE html>
<html>
<body>
<div style="text-align:center">
<button onclick="playPause()">Play/Pause</button>
<button onclick="makeBig()">Big</button>
<button onclick="makeSmall()">Small</button>
<button onclick="makeNormal()">Normal</button>
<br />
<video id="video1">
<source src="mov_bbb.mp4" type="video/mp4" />
<source src="mov_bbb.ogg" type="video/ogg" />
Your browser does not support HTML5 video.
</video>
</div>
<script type="text/javascript">
var myVideo=document.getElementById("video1");
function playPause()
{
if (myVideo.paused)
myVideo.play();
else
myVideo.pause();
}
function makeBig()
{
myVideo.height=(myVideo.videoHeight*2);
}
function makeSmall()
{
myVideo.height=(myVideo.videoHeight/2);
}
function makeNormal()
{
myVideo.height=(myVideo.videoHeight);
}
</script>
<p>Video courtesy of <a href="http://www.bigbuckbunny.org/" target="_blank">Big Buck Bunny</a>.</p>
</body>
</html>
完全没有变化。它没有意识到这些函数嵌套在 <script> 标记内。将文件类型设置为js.html 或html.js 也无济于事
【问题讨论】:
-
它是否像 any HTML 的预期那样缩进? (例如,根本没有 JavaScript)
-
这里似乎已经回答了:stackoverflow.com/questions/3276392/…
-
@Chriseyre2000 我正在对其进行测试,看看该解决方案是否使其更可靠
-
@Chriseyre2000 它确实使它更可靠,但在某些情况下继续失败。
标签: html vim formatting