Skip to content
This repository was archived by the owner on Dec 15, 2022. It is now read-only.

Commit 31d3860

Browse files
committed
Added support for type grouping when initializing or when calling make
1 parent 8062238 commit 31d3860

File tree

2 files changed

+190
-1
lines changed

2 files changed

+190
-1
lines changed

grammars/go.cson

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,20 @@
149149
'1':
150150
'name': 'entity.name.type.go'
151151
}
152+
{
153+
'include': '#types_init'
154+
}
155+
{
156+
'comment': 'Make statements'
157+
'match': '(?<=make\\()([^,\\)]*)'
158+
'captures':
159+
'1':
160+
'patterns': [
161+
{
162+
'include': '#types'
163+
}
164+
]
165+
}
152166
{
153167
'comment': 'Shorthand variable declaration and assignments'
154168
'match': '[a-zA-Z_]\\w*(?:,\\s*[a-zA-Z_]\\w*)*(?=\\s*:=)'
@@ -488,3 +502,94 @@
488502
]
489503
}
490504
]
505+
'types_init':
506+
'patterns': [
507+
{
508+
'match': '(([\\w_][\\w\\d_]*\\.)?[\\w_][\\w\\d_]*)(?=\\{)'
509+
'captures':
510+
'1':
511+
'patterns': [
512+
{
513+
'include': '#types'
514+
}
515+
]
516+
}
517+
{
518+
'match': '(?<=map\\[)([^\\]]+)(\\])([^\\{]+)(?=\\{)'
519+
'captures':
520+
'1':
521+
'patterns': [
522+
{
523+
'include': '#types'
524+
}
525+
]
526+
'2':
527+
'patterns': [
528+
{
529+
'include': '#brackets'
530+
}
531+
]
532+
'3':
533+
'patterns': [
534+
{
535+
'include': '#types'
536+
}
537+
]
538+
}
539+
]
540+
'types':
541+
'patterns': [
542+
{
543+
'include': '#brackets'
544+
}
545+
{
546+
'include': '#storage_types'
547+
}
548+
{
549+
'include': '#keywords'
550+
}
551+
{
552+
'match': '\\b(chan)\\b(\\s+|\\s*<-\\s*|\\s*->\\s*)(.+)'
553+
'captures':
554+
'1':
555+
'name': 'keyword.go'
556+
'3':
557+
'patterns': [
558+
{
559+
'include': '#types'
560+
}
561+
]
562+
}
563+
{
564+
'match': '\\b(map)\\b\\[([^\\]]+)\\]([^\\{]+){'
565+
'captures':
566+
'1':
567+
'name': 'keyword.go'
568+
'2':
569+
'patterns': [
570+
{
571+
'include': '#types'
572+
}
573+
]
574+
'3':
575+
'patterns': [
576+
{
577+
'include': '#types'
578+
}
579+
]
580+
}
581+
{
582+
'match': '(([\\w_][\\w\\d_]*)(\\.))?([\\w_][\\w\\d_]*)'
583+
'captures':
584+
'2':
585+
'name': 'entity.name.package.go'
586+
'3':
587+
'patterns': [
588+
{
589+
'include': '#delimiters'
590+
}
591+
]
592+
'4':
593+
'name': 'entity.name.type.go'
594+
}
595+
]

spec/go-spec.coffee

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -601,7 +601,7 @@ describe 'Go grammar', ->
601601
testVarDeclaration decl[1], 'foo'
602602
testOpAddress decl[3], '*'
603603
testOpBracket closing[0], ')'
604-
604+
605605
it 'tokenizes all parts of variable initializations correctly', ->
606606
[kwd, decl, init, _, closing] = grammar.tokenizeLines 'var (\n\tm = map[string]int{\n\t\t"key": 10,\n\t}\n)'
607607
testVar kwd[0]
@@ -634,3 +634,87 @@ describe 'Go grammar', ->
634634
testVarAssignment tokens[8], 'z'
635635
testOpAssignment tokens[10], ':='
636636
testOpTermination tokens[16], ';'
637+
638+
639+
describe 'testing type highlighting', ->
640+
describe 'in initialization statements', ->
641+
typeCheckers = [
642+
{
643+
strToCheck: 'a.A',
644+
expected:
645+
[{ value: 'a', scope: 'entity.name.package.go'},
646+
{ value: '.', scope: 'punctuation.other.period.go'},
647+
{ value: 'A', scope: 'entity.name.type.go'}]
648+
},
649+
{
650+
strToCheck: 'A',
651+
expected:
652+
[{value: 'A', scope: 'entity.name.type.go'}]
653+
},
654+
{
655+
strToCheck: '[]A',
656+
expected:
657+
[{value: '[', scope: 'punctuation.other.bracket.square.go'},
658+
{value: ']', scope: 'punctuation.other.bracket.square.go'},
659+
{value: 'A', scope: 'entity.name.type.go'}]
660+
},
661+
{
662+
strToCheck: 'map[int]B',
663+
expected:
664+
[{value: 'map', scope: 'keyword.map.go'},
665+
{value: '[', scope: 'punctuation.other.bracket.square.go'},
666+
{value: 'int', scope: 'storage.type.numeric.go'},
667+
{value: ']', scope: 'punctuation.other.bracket.square.go'},
668+
{value: 'B', scope: 'entity.name.type.go'}]
669+
},
670+
{
671+
strToCheck: 'map[B]int',
672+
expected:
673+
[{value: 'map', scope: 'keyword.map.go'},
674+
{value: '[', scope: 'punctuation.other.bracket.square.go'},
675+
{value: 'B', scope: 'entity.name.type.go'},
676+
{value: ']', scope: 'punctuation.other.bracket.square.go'},
677+
{value: 'int', scope: 'storage.type.numeric.go'}]
678+
},
679+
]
680+
testValueAndScope = (token, expected) ->
681+
expect(token.value).toBe expected.value
682+
expect(token.scopes).toEqual ['source.go', expected.scope]
683+
verifyInit = (offset, expected, tokens) ->
684+
for i in [0..expected.length-1]
685+
console.log tokens[i], expected[i]
686+
testValueAndScope tokens[i+offset], expected[i]
687+
describe 'in initialize statements with curly braces ', ->
688+
it 'tokenizes the package, type and puctuation', ->
689+
for checker in typeCheckers
690+
{tokens} = grammar.tokenizeLine checker.strToCheck+'{'
691+
verifyInit 0, checker.expected, tokens
692+
testValueAndScope tokens[checker.expected.length], {value: '{', scope: 'punctuation.other.bracket.curly.go'}
693+
694+
describe 'in initialize statements with make', ->
695+
toTest = typeCheckers.concat [{
696+
strToCheck: 'string',
697+
expected:
698+
[{value: 'string', scope: 'storage.type.string.go'}]
699+
},
700+
{
701+
strToCheck: 'chan <- A',
702+
expected:
703+
[{value: 'chan', scope: 'entity.name.type.go'},
704+
{value: '<-', scope: 'entity.name.type.go'},
705+
{value: 'A', scope: 'entity.name.type.go'}]
706+
}]
707+
it 'tokenizes the package, type and puctuation in simple make statemnt', ->
708+
for checker in typeCheckers
709+
{tokens} = grammar.tokenizeLine 'make(' + checker.strToCheck+ ')'
710+
verifyInit 2, checker.expected, tokens
711+
testValueAndScope tokens[0], {value: 'make', scope: 'support.function.builtin.go'}
712+
testValueAndScope tokens[1], {value: '(', scope: 'punctuation.other.bracket.round.go'}
713+
testValueAndScope tokens[2 + checker.expected.length], {value: ')', scope: 'punctuation.other.bracket.round.go'}
714+
it 'tokenizes the package, type and puctuation in a non-simple make statemnt', ->
715+
for checker in typeCheckers
716+
{tokens} = grammar.tokenizeLine 'make(' + checker.strToCheck+ ','
717+
verifyInit 2, checker.expected, tokens
718+
testValueAndScope tokens[0], {value: 'make', scope: 'support.function.builtin.go'}
719+
testValueAndScope tokens[1], {value: '(', scope: 'punctuation.other.bracket.round.go'}
720+
testValueAndScope tokens[2 + checker.expected.length], {value: ',', scope: 'punctuation.other.comma.go'}

0 commit comments

Comments
 (0)