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

Commit 0f96750

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

File tree

2 files changed

+189
-1
lines changed

2 files changed

+189
-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: 84 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,86 @@ describe 'Go grammar', ->
634634
testVarAssignment tokens[8], 'z'
635635
testOpAssignment tokens[10], ':='
636636
testOpTermination tokens[16], ';'
637+
638+
639+
describe 'testing type highlighting', ->
640+
typeCheckers = [
641+
{
642+
strToCheck: 'a.A',
643+
expected:
644+
[{value: 'a', scope: 'entity.name.package.go'},
645+
{value: '.', scope: 'punctuation.other.period.go'},
646+
{value: 'A', scope: 'entity.name.type.go'}]
647+
},
648+
{
649+
strToCheck: 'A',
650+
expected:
651+
[{value: 'A', scope: 'entity.name.type.go'}]
652+
},
653+
{
654+
strToCheck: '[]A',
655+
expected:
656+
[{value: '[', scope: 'punctuation.other.bracket.square.go'},
657+
{value: ']', scope: 'punctuation.other.bracket.square.go'},
658+
{value: 'A', scope: 'entity.name.type.go'}]
659+
},
660+
{
661+
strToCheck: 'map[int]B',
662+
expected:
663+
[{value: 'map', scope: 'keyword.map.go'},
664+
{value: '[', scope: 'punctuation.other.bracket.square.go'},
665+
{value: 'int', scope: 'storage.type.numeric.go'},
666+
{value: ']', scope: 'punctuation.other.bracket.square.go'},
667+
{value: 'B', scope: 'entity.name.type.go'}]
668+
},
669+
{
670+
strToCheck: 'map[B]int',
671+
expected:
672+
[{value: 'map', scope: 'keyword.map.go'},
673+
{value: '[', scope: 'punctuation.other.bracket.square.go'},
674+
{value: 'B', scope: 'entity.name.type.go'},
675+
{value: ']', scope: 'punctuation.other.bracket.square.go'},
676+
{value: 'int', scope: 'storage.type.numeric.go'}]
677+
},
678+
]
679+
testValueAndScope = (token, expected) ->
680+
expect(token.value).toBe expected.value
681+
expect(token.scopes).toEqual ['source.go', expected.scope]
682+
verifyInit = (offset, expected, tokens) ->
683+
for i in [0..expected.length-1]
684+
console.log tokens[i], expected[i]
685+
testValueAndScope tokens[i+offset], expected[i]
686+
describe 'in initialization statements', ->
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+
describe 'in initialize statements with make', ->
694+
toTest = typeCheckers.concat [{
695+
strToCheck: 'string',
696+
expected:
697+
[{value: 'string', scope: 'storage.type.string.go'}]
698+
},
699+
{
700+
strToCheck: 'chan <- A',
701+
expected:
702+
[{value: 'chan', scope: 'entity.name.type.go'},
703+
{value: '<-', scope: 'entity.name.type.go'},
704+
{value: 'A', scope: 'entity.name.type.go'}]
705+
}]
706+
it 'tokenizes the package, type and puctuation in simple make statemnt', ->
707+
for checker in typeCheckers
708+
{tokens} = grammar.tokenizeLine 'make(' + checker.strToCheck+ ')'
709+
verifyInit 2, checker.expected, tokens
710+
testValueAndScope tokens[0], {value: 'make', scope: 'support.function.builtin.go'}
711+
testValueAndScope tokens[1], {value: '(', scope: 'punctuation.other.bracket.round.go'}
712+
testValueAndScope tokens[2 + checker.expected.length], {value: ')', scope: 'punctuation.other.bracket.round.go'}
713+
it 'tokenizes the package, type and puctuation in a non-simple make statemnt', ->
714+
for checker in typeCheckers
715+
{tokens} = grammar.tokenizeLine 'make(' + checker.strToCheck+ ','
716+
verifyInit 2, checker.expected, tokens
717+
testValueAndScope tokens[0], {value: 'make', scope: 'support.function.builtin.go'}
718+
testValueAndScope tokens[1], {value: '(', scope: 'punctuation.other.bracket.round.go'}
719+
testValueAndScope tokens[2 + checker.expected.length], {value: ',', scope: 'punctuation.other.comma.go'}

0 commit comments

Comments
 (0)