README
¶
Fast, IDE-like Vim Syntax Highlighting for Go
vim-go-syntax provides fast, IDE-like Vim syntax highlighting for Go.
Feedback is welcome! This syntax is still fairly new and I'd like feedback or issues to improve it. Please see if there's already an issue open or create a new one.
Syntax highlighting can make reading code easier by using colour to convey
additional information. Existing Vim syntax definitions for Go are OK, but fail
to correctly highlight useful parts of the syntax, which motivated me to write
vim-go-syntax from the ground up using the Go language specification. Not only
does it fix many of the shortfalls of existing syntax definitions, it's more
than 2x faster even with all features enabled!
Comparison of vim-go (left) with vim-go-syntax (right) using my colour
scheme; see :help group-name to see how your colour scheme highlights Vim's
default syntax groups. Most syntax highlighting features can be disabled if
you'd prefer - see Configuration.
Key Features
Key features compared to vim-go and the built-in Vim syntax:
- Correctly display user-defined types and packages
- E.g in structs, struct literals, parameters, slices, maps,
var/constdeclarations, type parameters, type assertions, type switches,make(),new(), etc.
- E.g in structs, struct literals, parameters, slices, maps,
- Correctly highlights imported package names
- Godoc highlighting in
vim-go - More customisable Configuration
- Vastly more fine-grained highlighting
- Parentheses, Braces, and Brackets highlighted and individually configurable
- E.g. struct and function braces can be different
- Correctly highlight functions, including:
- User-defined types and packages in receiver, parameters, and return types
- Multiline parameters/return values, including successive parameters of the same type
- Identifier/type differentiation in function types (e.g.
func (int, MyType)vsfunc (a, b MyType)) and return types
- Correctly handle generics:
- Type parameters and their constraints (including user-defined types) are highlighted correctly
- User-defined types in type arguments are highlighted correctly
- Type arguments don't break function call or struct value highlighting
- Multiline field chains, even with comments
- Correct use of Vim's syntax groups
- More consistent with syntax definitions of other languages. See
:help group-nameto see your current colour scheme (or:help :hito create your own).
- More consistent with syntax definitions of other languages. See
- Many other small improvements, including labels, embedded types, invalid rune error highlighting, and better string behaviour.
Configuration
Listed in the table below are various options that can be used to customise
highlighting. These can be set at any time before the syntax is loaded, usually
your .vimrc is best.
Each option can be:
- A
1or a0to enable/disable the highlighting. For example:
let g:go_highlight_parens = 0
let g:go_highlight_fields = 1
- A syntax group name; see '
:help group-name' to see what default groups are available and how your colour scheme styles them. For example, to make function calls look like types:
let g:go_highlight_function_calls = 'Type'
- A highlight description; see '
:help hi' for syntax and options. For example, to make type parameters blue and italic:
let g:go_highlight_type_parameters = 'ctermfg=4 cterm=italic'
If you want even more customisation, you can override the default highlighting
by adding custom highlighting rules to ~/.vim/after/syntax/go.vim (you'll need
to create this file first). The previous configuration examples could also be
achieved like this:
" ~/.vim/after/syntax/go.vim
hi clear goParens
hi link goField Identifier
hi link goFuncCall Type
hi goTypeParam ctermfg=4 cterm=italic
See syntax/go.vim for the names of all syntax and
highlight groups.
Summary of options (see below table for descriptions):
| Configuration Option | Default | Default Group |
|---|---|---|
g:go_highlight_braces |
Enabled | Delimiter |
g:go_highlight_brackets |
Enabled | Delimiter |
g:go_highlight_builtins |
Enabled | Special |
g:go_highlight_comma |
Disabled | Delimiter |
g:go_highlight_dot |
Enabled | Operator |
g:go_highlight_fields |
Disabled | Identifier |
g:go_highlight_functions |
Enabled | Function |
g:go_highlight_function_parens |
Enabled | Delimiter |
g:go_highlight_function_braces |
Enabled | Delimiter |
g:go_highlight_function_calls |
Enabled | Function |
g:go_highlight_function_call_parens |
Enabled | Delimiter |
g:go_highlight_generate_tags |
Enabled | PreProc |
g:go_highlight_rune_literal_error |
Enabled | Error |
g:go_highlight_labels |
Enabled | Label |
g:go_highlight_map_brackets |
Enabled | Delimiter |
g:go_highlight_operators |
Enabled | Operator |
g:go_highlight_function_parameters |
Enabled | Identifier |
g:go_highlight_parens |
Enabled | Delimiter |
g:go_highlight_semicolon |
Disabled | Delimiter |
g:go_highlight_short_variable_declarations |
Enabled | Identifier |
g:go_highlight_slice_brackets |
Enabled | Delimiter |
g:go_highlight_format_strings |
Enabled | SpecialChar |
g:go_highlight_struct_type_fields |
Disabled | Identifier |
g:go_highlight_struct_tags |
Enabled | PreProc |
g:go_highlight_struct_fields |
Disabled | Identifier |
g:go_highlight_types |
Enabled | Type |
g:go_highlight_type_parameters |
Enabled | Identifier |
g:go_highlight_variable_assignments |
Disabled | Special |
g:go_highlight_variable_declarations |
Enabled | Identifier |
g:go_highlight_braces- All braces (
{}). More specific brace options (e.g.g:go_highlight_function_braces) will take precedence over this option.
- All braces (
g:go_highlight_brackets- All brackets (
[]). More specific bracket options (e.g.g:go_highlight_map_brackets) will take precedence over this option.
- All brackets (
g:go_highlight_builtins- Highlight built-in functions differently from other functions.
g:go_highlight_comma- Commas.
g:go_highlight_dot- Dot operators, e.g. the dot in
foo.bar()
- Dot operators, e.g. the dot in
g:go_highlight_fields- Fields in expressions, e.g.
barinfoo.bar = 123
- Fields in expressions, e.g.
g:go_highlight_functions- Function declaration names, e.g.
fooinfunc foo() { }. Also applies to method names.
- Function declaration names, e.g.
g:go_highlight_function_parens- Parentheses around parameter list and receiver type.
g:go_highlight_function_braces- The braces of the function block.
g:go_highlight_function_calls- Function calls. Turning this off does not affect other syntax elements (e.g. generics, function call parentheses).
g:go_highlight_function_call_parens- The parentheses of a function call.
g:go_highlight_generate_tags- Generate comments, e.g.
//go:generate .... Turning this off makes them look like regular comments.
- Generate comments, e.g.
g:go_highlight_rune_literal_error- Invalid rune literals.
g:go_highlight_labels- User-defined labels.
g:go_highlight_map_brackets- The brackets in map types, e.g.
map[string]int.
- The brackets in map types, e.g.
g:go_highlight_operators- Operators including assignment, e.g.
+,-,&&,||,=,:=, etc.
- Operators including assignment, e.g.
g:go_highlight_function_parameters- Parameter names, e.g.
barinfunc foo(bar int)
- Parameter names, e.g.
g:go_highlight_parens- All parentheses. More specific parenthesis options (e.g.
g:go_highlight_function_parens) will take precedence.
- All parentheses. More specific parenthesis options (e.g.
g:go_highlight_semicolon- Semicolons.
g:go_highlight_short_variable_declarations- Highlight the names of new variables defined by the declaration syntax,
e.g.
aandbina, b := foo().
- Highlight the names of new variables defined by the declaration syntax,
e.g.
g:go_highlight_slice_brackets- The brackets in slice types, e.g.
[]string.
- The brackets in slice types, e.g.
g:go_highlight_format_strings- Format placeholders in in format strings, e.g.
%sinfmt.Printf("%s", foo)
- Format placeholders in in format strings, e.g.
g:go_highlight_struct_type_fields- Field names in struct types, e.g.
Barintype Foo struct { Bar int }
- Field names in struct types, e.g.
g:go_highlight_struct_tags- Struct tags, the backtick-delimited strings in structs, e.g.
`json:bar`instruct { Bar int `json:"bar"` }.
- Struct tags, the backtick-delimited strings in structs, e.g.
g:go_highlight_struct_fields- Field names in struct literals, e.g.
Barinf := Foo{ Bar: 123 }.
- Field names in struct literals, e.g.
g:go_highlight_types- Type declaration names, e.g.
Foointype Foo struct { Bar int }. Even with this option off, types parameters etc. will still be highlighted as types.
- Type declaration names, e.g.
g:go_highlight_type_parameters- Type parameter names in type parameter lists, e.g. the
Tin the brackets oftype Foo[T] { Bar T }; theTin the struct will be highlighted as a type.
- Type parameter names in type parameter lists, e.g. the
g:go_highlight_variable_assignments- Variable names in assignments, e.g.
aandbina, b = foo().
- Variable names in assignments, e.g.
g:go_highlight_variable_declarations- Names in
varorconstassignments, e.g.ainvar a []string. Also applies to short variable declarations (i.e.:=) unlessg:go_highlight_short_variable_declarationsis set.
- Names in
Caveats
- Type arguments to types and functions are limited to at most three nested
generic types, e.g.
val := Type1[Type2[Type3[Foo]]]{ ... }. The number of arguments at each level is unlimited, e.g.Type1[Type2[A, B, C, ...]] - Structs literals (
MyStruct{...}) can't have a space between the name and the braces, despite the fact that Go permits this. - Type conversion (
MyType(someVal)) is highlighted like a function call
What's Next
- Syntax Folding
- Possibly integrate with
vim-goto get actual type information to highlight things like type conversion correctly
Documentation
¶
There is no documentation for this package.