Wednesday, September 19, 2012

Re: dynamic syntax highlighting

Miek Gieben, Wed 2012-09-19 @ 15:53:49+0200:
> [ Quoting <tmhedberg@gmail.com> in "Re: dynamic syntax > highlighting..." ]
> > Miek Gieben, Wed 2012-09-19 @ 14:14:17+0200:
> > > Is something like this even remotely possible with Vim? Can it be
> > > done with a vimscript?
> >
> > In order for it to work in the general case, you'd have to embed a
> > parser for the language in question in Vim. Not a trivial thing to
> > do, especially in VimScript. I'm sure that it could theoretically be
> > done, but it would be a lot of work, and would probably slow down
> > syntax highlighting (which is already slow in some cases)
> > considerably.
>
> Is a full parser really needed? My gut feeling tells me if there is
> omni-completion available for a language, something like this should
> be reasonable easy to add. (But I could be completely wrong as I know
> nothing about Vim's internals).

In specific cases, like in your Go data type example, you could probably
get by with a lot less than a full parser, using regex matches and other
heuristics. But in the general case, as in, if we wanted to add support
for language grammar-based highlighting for arbitrary grammatical
constructs in arbitrary languages, I don't think you could get away with
anything less than actually parsing the language.

Omnicompletion is an example of the "specific cases" approach. In the
handful of languages that actually have omnicompletion support, we are
not, as far as I am aware, actually parsing the language fully, but
rather using clever searches and other "tricks" to approximate a parser
as far as the completion features actually require it. This kind of
technique can only take you so far, though, and if you propose
highlighting based on grammar, then unlike completion, that would
potentially affect the appearance of every token in every buffer in that
language, and thus we couldn't really just "skip over" complexity and
make do with a partial implementation.

That said, I am also far from an expert on Vim internals, so anyone who
can prove me wrong should feel free to do so. :)

No comments:

Post a Comment