Skip to content

Commit eba5233

Browse files
committed
Highlight block parameters like method parameters
Removes the disabled rubyBlockParameter syntax group. See commit 074200f.
1 parent 4952435 commit eba5233

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

spec/spec_helper.rb

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ def assert_correct_indenting(extension='rb', string)
2525
expect(IO.read(filename)).to eq string
2626
end
2727

28-
def assert_correct_highlighting(extension='rb', string, pattern, group)
28+
def assert_correct_highlighting(extension='rb', string, patterns, group)
2929
filename = "test.#{extension}"
3030

3131
IO.write filename, string
3232

3333
vim.edit filename
3434

35-
expect(vim.echo("TestSyntax('#{pattern}', '#{group}')")).to eq '1'
35+
Array(patterns).each do |pattern|
36+
expect(vim.echo("TestSyntax('#{pattern}', '#{group}')")).to eq '1'
37+
end
3638
end
3739
end

spec/syntax/blocks_spec.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
require 'spec_helper'
2+
3+
describe "Syntax highlighting" do
4+
specify "block parameters" do
5+
assert_correct_highlighting <<~'EOF', 'bar', 'rubySymbol'
6+
foo { |bar:| 42 }
7+
EOF
8+
assert_correct_highlighting <<~'EOF', ['bar\ze:', 'baz\ze:'], 'rubySymbol'
9+
foo { |bar: 'bar', baz: 'baz'| 42 }
10+
EOF
11+
end
12+
specify "block parameters with default values including '|'" do
13+
assert_correct_highlighting <<~'EOF', ['|\zebar', 'qux)\zs|'], 'rubyBlockParameterList'
14+
foo { |bar=(baz|qux)| 42 }
15+
EOF
16+
end
17+
end

syntax/ruby.vim

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ endfunction
6565
com! -nargs=* SynFold call s:run_syntax_fold(<q-args>)
6666

6767
" Not-Top Cluster {{{1
68-
syn cluster rubyNotTop contains=@rubyCommentNotTop,@rubyStringNotTop,@rubyRegexpSpecial,@rubyDeclaration,@rubyExceptionHandler,@rubyClassOperator,rubyConditional,rubyModuleName,rubyClassName,rubySymbolDelimiter
68+
syn cluster rubyNotTop contains=@rubyCommentNotTop,@rubyStringNotTop,@rubyRegexpSpecial,@rubyDeclaration,@rubyExceptionHandler,@rubyClassOperator,rubyConditional,rubyModuleName,rubyClassName,rubySymbolDelimiter,rubyParentheses
6969

7070
" Whitespace Errors {{{1
7171
if exists("ruby_space_errors")
@@ -91,6 +91,7 @@ if exists("ruby_operators") || exists("ruby_pseudo_operators")
9191
syn match rubyBooleanOperator "\%(\w\|[^\x00-\x7F]\)\@1<!!\|&&\|||"
9292
syn match rubyRangeOperator "\.\.\.\="
9393
syn match rubyAssignmentOperator "=>\@!\|-=\|/=\|\*\*=\|\*=\|&&=\|&=\|||=\||=\|%=\|+=\|>>=\|<<=\|\^="
94+
syn match rubyAssignmentOperator "=>\@!" containedin=rubyBlockParameterList " TODO: this is inelegant
9495
syn match rubyEqualityOperator "===\|==\|!=\|!\~\|=\~"
9596

9697
syn region rubyBracketOperator matchgroup=rubyOperator start="\%(\%(\w\|[^\x00-\x7F]\)[?!]\=\|[]})]\)\@2<=\[" end="]" contains=ALLBUT,@rubyNotTop
@@ -106,6 +107,7 @@ if exists("ruby_operators") || exists("ruby_pseudo_operators")
106107
syn match rubyProcOperator "\%([[(|,]\_s*\)\@<=&"
107108
syn match rubyProcOperator "\s\@1<=&\%(\h\|[^\x00-\x7F]\|[:$@]\|->\)\@="
108109

110+
syn cluster rubyProperOperator contains=rubyTernaryOperator,rubyArithmeticOperator,rubyComparisonOperator,rubyBitwiseOperator,rubyBooleanOperator,rubyRangeOperator,rubyAssignmentOperator,rubyEqualityOperator,rubyDefinedOperator,rubyEnglishBooleanOperator
109111
syn cluster rubyClassOperator contains=rubyEigenClassOperator,rubySuperClassOperator
110112
syn cluster rubyPseudoOperator contains=rubyDotOperator,rubyScopeOperator,rubyEigenClassOperator,rubySuperClassOperator,rubyLambdaOperator,rubySplatOperator,rubyDoubleSplatOperator,rubyProcOperator
111113
syn cluster rubyOperator contains=ruby.*Operator
@@ -190,8 +192,8 @@ SynFold ':' syn region rubySymbol matchgroup=rubySymbolDelimiter start="[]})\"':
190192

191193
syn match rubyCapitalizedMethod "\%(\%(^\|[^.]\)\.\s*\)\@<!\<\u\%(\w\|[^\x00-\x7F]\)*\>\%(\s*(\)\@="
192194

193-
syn match rubyBlockParameter "\%(\h\|[^\x00-\x7F]\)\%(\w\|[^\x00-\x7F]\)*" contained
194-
syn region rubyBlockParameterList start="\%(\%(\<do\>\|{\)\_s*\)\@32<=|" end="|" oneline display contains=rubyBlockParameter,rubySplatOperator,rubyDoubleSplatOperator,rubyProcOperator
195+
syn region rubyParentheses start="(" end=")" contains=ALLBUT,@rubyNotTop containedin=rubyBlockParameterList
196+
syn region rubyBlockParameterList start="\%(\%(\<do\>\|{\)\_s*\)\@32<=|" end="|" contains=ALLBUT,@rubyNotTop,@rubyProperOperator
195197

196198
if exists('ruby_global_variable_error')
197199
syn match rubyGlobalVariableError "$[^A-Za-z_]" display
@@ -310,6 +312,7 @@ SynFold '%' syn region rubySymbol matchgroup=rubyPercentSymbolDelimiter start="%
310312

311313
" Here Documents {{{1
312314
syn region rubyHeredocStart matchgroup=rubyHeredocDelimiter start=+\%(\%(class\|::\|\.\@1<!\.\)\_s*\|\%([]})"'`]\)\s\|\w\)\@<!<<[-~]\=\zs\%(\%(\h\|[^\x00-\x7F]\)\%(\w\|[^\x00-\x7F]\)*\)+ end=+$+ oneline contains=ALLBUT,@rubyNotTop
315+
313316
syn region rubyHeredocStart matchgroup=rubyHeredocDelimiter start=+\%(\%(class\|::\|\.\@1<!\.\)\_s*\|\%([]})"'`]\)\s\|\w\)\@<!<<[-~]\=\zs"\%([^"]*\)"+ end=+$+ oneline contains=ALLBUT,@rubyNotTop
314317
syn region rubyHeredocStart matchgroup=rubyHeredocDelimiter start=+\%(\%(class\|::\|\.\@1<!\.\)\_s*\|\%([]})"'`]\)\s\|\w\)\@<!<<[-~]\=\zs'\%([^']*\)'+ end=+$+ oneline contains=ALLBUT,@rubyNotTop
315318
syn region rubyHeredocStart matchgroup=rubyHeredocDelimiter start=+\%(\%(class\|::\|\.\@1<!\.\)\_s*\|\%([]})"'`]\)\s\|\w\)\@<!<<[-~]\=\zs`\%([^`]*\)`+ end=+$+ oneline contains=ALLBUT,@rubyNotTop
@@ -335,7 +338,7 @@ syn match rubyMethodName "\<\%([_[:alpha:]]\|[^\x00-\x7F]\)\%([_[:alnum:]]\|[^\x
335338
syn match rubyMethodName "\%(\s\|^\)\@1<=\%([_[:alpha:]]\|[^\x00-\x7F]\)\%([_[:alnum:]]\|[^\x00-\x7F]\)*[?!=]\=\%(\s\|$\)\@=" contained containedin=rubyAliasDeclaration,rubyAliasDeclaration2
336339
syn match rubyMethodName "\%([[:space:].]\|^\)\@1<=\%(\[\]=\=\|\*\*\|[-+!~]@\=\|[*/%|&^~]\|<<\|>>\|[<>]=\=\|<=>\|===\|[=!]=\|[=!]\~\|!\|`\)\%([[:space:];#(]\|$\)\@=" contained containedin=rubyAliasDeclaration,rubyAliasDeclaration2,rubyMethodDeclaration
337340

338-
syn cluster rubyDeclaration contains=rubyAliasDeclaration,rubyAliasDeclaration2,rubyMethodDeclaration,rubyModuleDeclaration,rubyClassDeclaration,rubyMethodName,rubyBlockParameter
341+
syn cluster rubyDeclaration contains=rubyAliasDeclaration,rubyAliasDeclaration2,rubyMethodDeclaration,rubyModuleDeclaration,rubyClassDeclaration,rubyMethodName
339342

340343
" Keywords {{{1
341344
" Note: the following keywords have already been defined:
@@ -459,7 +462,7 @@ endif
459462
syn match rubyDefinedOperator "\<defined?" display
460463

461464
" More Symbols {{{1
462-
syn match rubySymbol "\%([{(,]\_s*\)\@<=\%(\h\|[^\x00-\x7F]\)\%(\w\|[^\x00-\x7F]\)*[?!]\=::\@!"he=e-1
465+
syn match rubySymbol "\%([{(|,]\_s*\)\@<=\%(\h\|[^\x00-\x7F]\)\%(\w\|[^\x00-\x7F]\)*[?!]\=::\@!"he=e-1
463466
syn match rubySymbol "[]})\"':]\@1<!\<\%(\h\|[^\x00-\x7F]\)\%(\w\|[^\x00-\x7F]\)*[!?]\=:[[:space:],]\@="he=e-1
464467

465468
" __END__ Directive {{{1

0 commit comments

Comments
 (0)