Skip to content

Commit 7697eb3

Browse files
committed
New projection system for keyword highlighting
1 parent 5c5330d commit 7697eb3

File tree

2 files changed

+44
-14
lines changed

2 files changed

+44
-14
lines changed

after/syntax/ruby/rails.vim

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ hi def link rubyMacro Macro
99
hi def link rubyRoute rubyControl
1010
hi def link rubySchema rubyControl
1111
hi def link rubyResponse rubyControl
12+
hi def link rubyAction rubyControl
1213
hi def link rubyUrlHelper rubyHelper
1314
hi def link rubyViewHelper rubyHelper
1415
hi def link rubyTestHelper rubyHelper
@@ -228,14 +229,31 @@ syn keyword rubyAttribute cattr_accessor cattr_reader cattr_writer mattr_accesso
228229
syn keyword rubyAttribute thread_cattr_accessor thread_cattr_reader thread_cattr_writer thread_mattr_accessor thread_mattr_reader thread_mattr_writer
229230
syn keyword rubyMacro alias_attribute concern concerning delegate delegate_missing_to with_options
230231

231-
let s:keywords = split(join(filter(rails#buffer().projected('keywords'),
232-
\ 'type(v:val) == type("")'), ' '))
233-
let s:special = filter(copy(s:keywords), 'v:val =~# ''^\h\k*[?!]$''')
234-
let s:regular = filter(copy(s:keywords), 'v:val =~# ''^\h\k*$''')
235-
let s:group = rails#buffer().type_name('helper', 'view') ? 'rubyHelper' : 'rubyMacro'
236-
if !empty(s:special)
237-
exe 'syn match' s:group '"\<\%('.join(s:special, '\|').'\)"'
238-
endif
239-
if !empty(s:regular)
240-
exe 'syn keyword' s:group join(s:regular, ' ')
241-
endif
232+
let s:special = {
233+
\ '[': '\>\[\@=',
234+
\ ']': '\>[[.]\@!',
235+
\ '{': '\>\%(\s*{\|\s*do\>\)\@=',
236+
\ '}': '\>\%(\s*{\|\s*do\>\)\@!'}
237+
function! s:highlight(group, ...) abort
238+
let value = rails#buffer().projected(a:0 ? a:1 : a:group)
239+
let words = split(join(filter(value, 'type(v:val) == type("")'), ' '))
240+
let special = filter(copy(words), 'type(v:val) == type("") && v:val =~# ''^\h\k*[][{}?!]$''')
241+
let regular = filter(copy(words), 'type(v:val) == type("") && v:val =~# ''^\h\k*$''')
242+
if !empty(special)
243+
exe 'syn match' a:group substitute(
244+
\ '"\<\%('.join(special, '\|').'\)"',
245+
\ '[][{}]', '\=get(s:special, submatch(0), submatch(0))', 'g')
246+
endif
247+
if !empty(regular)
248+
exe 'syn keyword' a:group join(regular, ' ')
249+
endif
250+
endfunction
251+
252+
call s:highlight(
253+
\ rails#buffer().type_name('helper', 'view') ? 'rubyHelper' : 'rubyMacro',
254+
\ 'keywords')
255+
call s:highlight('rubyMacro')
256+
call s:highlight('rubyAction')
257+
call s:highlight('rubyHelper')
258+
call s:highlight('rubyEntity')
259+
call s:highlight('rubyEntities')

doc/rails.txt

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -633,7 +633,7 @@ Vim syntax looks a lot like JSON, but with funky |line-continuation|:
633633
\ "test/unit/{}_uploader_test.rb",
634634
\ "spec/models/{}_uploader_spec.rb"
635635
\ ],
636-
\ "keywords": "process version"
636+
\ "rubyMacro": ["process", "version"]
637637
\ },
638638
\ "features/support/*.rb": {"command": "support"},
639639
\ "features/support/env.rb": {"command": "support"}}
@@ -682,7 +682,7 @@ The full list of available options is as follows:
682682
Determines the |:compiler| plugin to use with |rails-:Runner|.
683683
*rails-projection-keywords*
684684
"keywords" ~
685-
Provides a whitespace delimited list of keywords to syntax highlight.
685+
Obsolete: see rubyMacro, rubyAction, and rubyHelper below.
686686
*rails-projection-command*
687687
"command" ~
688688
Names a navigation command to be created. Use the same name on
@@ -705,11 +705,23 @@ The full list of available options is as follows:
705705
If you provide a ! after the argument to the navigation command (that
706706
is, :Euploader foo!, NOT :Euploader! foo), and a new file is created,
707707
this will be used as an array of lines to populate the body.
708-
*rails-projection-params*
708+
*rails-projection-railsParams*
709709
"railsParams" ~
710710
Provides a dictionary of defaults for URL parameters for
711711
|rails-:Preview|, for example {"user_id": "2"}. The fallback for
712712
unspecified parameters is "1".
713+
*rails-projection-rubyMacro*
714+
"rubyMacro" ~
715+
List of keywords to highlight as class declarations (like
716+
before_action or validates_presence_of).
717+
*rails-projection-rubyAction*
718+
"rubyAction" ~
719+
List of keywords to highlight as imperative methods (like
720+
redirect_to or create_table).
721+
*rails-projection-rubyHelper*
722+
"rubyHelper" ~
723+
List of keywords to highlight as functional methods (like
724+
params or any view helper).
713725

714726
CONFIGURATION *rails-configuration*
715727

0 commit comments

Comments
 (0)