dynamic syntax highlighting

33 views
Skip to first unread message

Miek Gieben

unread,
Sep 19, 2012, 8:14:17 AM9/19/12
to v...@vim.org
Hello,

While typing some Go code, I had the following idea: dynamic syntax
highlighting.

I was typing:

type Boo struct {
// ...
}

Now, Vim could "know" Boo is a type and apply the syntax highlighting for Type
accordingly.

Is something like this even remotely possible with Vim? Can it be done with a
vimscript?

Regards,

--
Miek Gieben http://miek.nl
signature.asc

Miek Gieben

unread,
Sep 19, 2012, 8:34:49 AM9/19/12
to v...@vim.org
[ Quoting <mi...@miek.nl> in "dynamic syntax highlighting..." ]
> type Boo struct {
> // ...
> }
>
> Now, Vim could "know" Boo is a type and apply the syntax highlighting for Type
> accordingly.

And... when this is removed the highlighting should be removed as well. That
could complicate matters significantly.
signature.asc

Taylor Hedberg

unread,
Sep 19, 2012, 9:48:07 AM9/19/12
to vim...@googlegroups.com, v...@vim.org
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.
signature.asc

Miek Gieben

unread,
Sep 19, 2012, 9:53:49 AM9/19/12
to v...@vim.org
[ Quoting <tmhe...@gmail.com> in "Re: dynamic syntax highlighting..." ]
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).
signature.asc

Taylor Hedberg

unread,
Sep 19, 2012, 10:12:24 AM9/19/12
to v...@vim.org
Miek Gieben, Wed 2012-09-19 @ 15:53:49+0200:
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. :)
signature.asc

Miek Gieben

unread,
Sep 19, 2012, 11:09:41 AM9/19/12
to v...@vim.org
[ Quoting <tmhe...@gmail.com> in "Re: dynamic syntax highlighting..." ]
> 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. :)

What you write makes a lot of sense :)

However http://majutsushi.github.com/tagbar/ does detect some of the
stuff I want to highlight. I think I'll poke in that package to see
what I can come up with.
signature.asc

Ben Fritz

unread,
Sep 19, 2012, 11:43:35 AM9/19/12
to vim...@googlegroups.com, v...@vim.org
On Wednesday, September 19, 2012 7:32:27 AM UTC-5, Miek Gieben wrote:
> Hello,
>
>
>
> While typing some Go code, I had the following idea: dynamic syntax
>
> highlighting.
>
>
>
> I was typing:
>
>
>
> type Boo struct {
>
> // ...
>
> }
>
>
>
> Now, Vim could "know" Boo is a type and apply the syntax highlighting for Type
>
> accordingly.
>
>
>
> Is something like this even remotely possible with Vim? Can it be done with a
>
> vimscript?
>
>
>

I don't remember the plugin name, but I remember seeing a couple plugins which use ctags (like tagbar and taglist do) to build a list of tags to highlight in Vim syntax. Try looking for "ctags syntax vim" or similar:

https://www.google.com/search?q=ctags+syntax+vim

Miek Gieben

unread,
Sep 19, 2012, 3:25:36 PM9/19/12
to v...@vim.org
[ Quoting <mi...@miek.nl> in "Re: dynamic syntax highlighting..." ]
> What you write makes a lot of sense :)
>
> However http://majutsushi.github.com/tagbar/ does detect some of the
> stuff I want to highlight. I think I'll poke in that package to see
> what I can come up with.

OK, sofar (with zero vimscript knowledge) I'm going to do this:

1 use tagbar to get a list of whatever (currently focussing on Types)
2 use http://www.drchip.org/astronaut/vim/vbafiles/hilinks.vba.gz hilinktrace
to get a handle on what syntax is used.
3 do a:
* :syntax clear
* :syntax on
* :syntax keyword goType += Radix "Radix is an example

The goType keyword should become apparent in step 2 (somehow)
signature.asc

Miek Gieben

unread,
Sep 19, 2012, 3:45:16 PM9/19/12
to v...@vim.org
[ Quoting <mi...@miek.nl> in "Re: dynamic syntax highlighting..." ]
> 1 use tagbar to get a list of whatever (currently focussing on Types)
> 2 use http://www.drchip.org/astronaut/vim/vbafiles/hilinks.vba.gz hilinktrace
> to get a handle on what syntax is used.
> 3 do a:
> * :syntax clear
> * :syntax on
> * :syntax keyword goType += Radix "Radix is an example
>
> The goType keyword should become apparent in step 2 (somehow)

It's even easier, the syntax highlighting file uses <lang>Type, so
for Go its goType for the types. So

1. use tagbar to get a list of whatever (currently focussing on Type)
2. :syntax clear
:syntax on
:syntax keyword goType += TypeYouFound
signature.asc

Miek Gieben

unread,
Sep 20, 2012, 2:28:21 AM9/20/12
to v...@vim.org
[ Quoting <mi...@miek.nl> in "Re: dynamic syntax highlighting..." ]
> It's even easier, the syntax highlighting file uses <lang>Type, so

On https://github.com/miekg/tagbar is a test/alpha implementation of
a plugin (modified from the original tagbar) that adds syntax highlighting
for user defined types. It only tested for Go code.

Interesting bits here:
https://github.com/miekg/tagbar/commit/693f26a1d54ae3122b666d7fc5b052e8559d7946#L0R2428

Issues:

* Only works if the type is defined in the current file (maybe tagbar can handle
multiple files as well, don't know)
* I'm using 'windo' which may be a bit blunt
* Throw away stuff from tagbar I don't need
signature.asc
Reply all
Reply to author
Forward
0 new messages