syntax highlighting for class names

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

syntax highlighting for class names

Mark Volkmann-2
In the following text

class Foo {

I want to highlight "Foo".

I tried the following in .vim/syntax/javascript.vim:

syntax region javaScriptClassName start=/class / end=/ /

In .vimrc I have:

hi javaScriptClassName ctermfg=blue

That isn't working. Any idea what I'm doing wrong?

--
R. Mark Volkmann
Object Computing, Inc.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: syntax highlighting for class names

Mark Volkmann-2
I wanted to highlight Foo and Bar in the following line:

class Foo extends Bar {

I have this working now using the following:

syntax region javaScriptClassName start=/^class \| class /hs=e+1 end=/ /he=s-1 oneline
" Can't start the start pattern with a space because javaScriptClassName already consumed it.
syntax region javaScriptExtendsName start=/extends /hs=e+1 end=/ /he=s-1 oneline

However, I also wanted to treat "class" and "extends" as keywords and highlight those.
When I do that, my regions above stop working.
Not sure why I can't have both.

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: syntax highlighting for class names

Paul Isambert
Le dimanche 01 mars 2015 à 06:36, Mark Volkmann a écrit:

> I wanted to highlight Foo and Bar in the following line:
>
> class Foo extends Bar {
>
> I have this working now using the following:
>
> syntax region javaScriptClassName start=/^class \| class /hs=e+1 end=/ /he=s-1 oneline
> " Can't start the start pattern with a space because javaScriptClassName already consumed it.
> syntax region javaScriptExtendsName start=/extends /hs=e+1 end=/ /he=s-1 oneline
>
> However, I also wanted to treat "class" and "extends" as keywords and highlight those.
> When I do that, my regions above stop working.
> Not sure why I can't have both.

You should use contained groups, e.g.:

    syn keyword jsClassKeyword class extends contained
    syn match jsClass +^\s*class.\+\ze{+ contains=jsClassKeyword

See “:help syn-contains”.

Best,
Paul

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: syntax highlighting for class names

Mark Volkmann-2
Thanks Paul! I'm probably close now, but not quite there. Do you see anything wrong with this?

syntax keyword jsClassKeywords class extends contained
syntax region javaScriptClassName start=/^class \| class /hs=e+1 end=/ /he=s-1 "contains=jsClassKeywords

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|

Re: syntax highlighting for class names

Paul Isambert


Le dimanche 01 mars 2015 à 10:07, Mark Volkmann a écrit:
> Thanks Paul! I'm probably close now, but not quite there. Do you see anything wrong with this?
>
> syntax keyword jsClassKeywords class extends contained
> syntax region javaScriptClassName start=/^class \| class /hs=e+1 end=/ /he=s-1 "contains=jsClassKeywords

First, your “contains” keyword in the second line is commented out.
Second, ending the region with a space is ill-advised (even though, to
be honest, I don’t understand why the region stops at the space after
“class”, given that start and stop patterns aren’t supposed to
overlap ... anybody?). An opening brace is the right way to go.
Finally, but that’s just personal preference, if you don’t need a
region, use a simple match:

    syntax match javaScriptClassName +^\s*class.\+i\ze{+ contains=jsClassKeywords

If you really want a region:

    syntax region javaScriptClassName start=+^\s*class+ end=+{+me=e-1 contains=jsClassKeywords

I hope that’ll be what you want.

Best,
Paul

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

---
You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
For more options, visit https://groups.google.com/d/optout.