You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
shooflenet/articles/vim_indenting.article.html

10 lines
2.6 KiB

<article>
<h1>Indent Adventures</h1>
<p>Look, I know it's wrong. I know I'm not supposed to do inline javascript. But I just want to put everything together! I want code to be easily accessible from the <em>single</em> place that it's relevant! That's <em>important</em> to me. Rest assured: Once my projects get large, I start pulling them apart. But in the mean time, the code I write (being generally game-related as it is) is extremely specific to the presentation, and I think it's actually inappropriate (not to mention inefficient, from my point of view) to separate them widely. There's just one problem:</p>
<p>Vim sucks at autoindenting.</p>
<p>Augh, this whole morning has been essentially wasted, because I spent it trying - <em>trying my hardest</em> - to find a vim indenting plugin that will indent javascript inline in HTML. It turns out that, as far as I can find, <em>no such thing exists.</em> There are a million wonderful javascript indenters, which work beautifully, but which only work on files that are javascript through-and-through. Unfortunately, my heart is set on keeping my javascript intertwined lovingly with my HTML, until such time as I feel like I should stop doing that.</p>
<p>So I had to fix it myself. <em>The pain.</em> It turns out vim scripting is, like the rest of this horrible death-train of an editor, obtuse, stuck in the past, and hard to work with. I guess I'll give thanks for the fact that I was able to edit it at all... But I feel like that's kind of a basic thing for an extensible editor. Anyway.</p>
<p>I got a copy of the HTML indent rules off of a Gary Bernhardt bitbucket link I found from my googling all morning, and delved into it. It turns out that the rules for indenting and dedenting on lines with bracket are only dependent on whether or not the lines in question contain open and close braces - and not <em>how many of those there are.</em> The practical effect of this: <code class="lang-js">function () {};</code> is dedented the same way <code class="lang-js">};</code> is. <strong>What?</strong></p>
<p>It's not even hard to fix (<a href="/static/html.vim">and I have managed to</a>) - all you need to do is count the braces and do some subtraction. For every brace closed on this line which isn't also opened on this line, dedent this line. For every brace opened on the previous line which isn't closed on the previous line, indent this line. <em>That's it.</em> As of this writing, my indent script has an unfortunate flaw - it'll treat <code class="lang-js">} {</code> exactly the same as <code class="lang-js">{ }</code>. This is a pretty big problem, but I need to do <em>some</em> work today.</p>
</article>