Problem
When naming a structure method with the same name as a type name, the method is highlighted as a type name when it is called. While it is not necessarily bad practice to give a method the same name as a type, this can lead to confusion and reduced code readability.
Illustrating problem
type Test struct{}
func (t Test) Testing() error {
t.normalName()
t.error()
t.int()
t.any()
return nil
}
func (t Test) normalName() {}
func (t Test) error() {}
func (t Test) int() {}
func (t Test) any() {}
![image](https://user-images.githubusercontent.com/65008219/229570007-887107e1-38f2-485c-9864-ad934c0f1aa2.png)
Using the Ayu Mirage theme, all function calls should be the same color as t.normalName()
.
Motivation
The proposed change aims to improve code readability and clarity by ensuring that method calls with the same name as a type are highlighted as normal function calls, rather than as type names.
Reason
In the go.tmLanguage.json
file, a regular expression is used to identify the type names in the code. For example, this is the pattern for the error
type:
"storage_types": {
"patterns": [
{
"match": "\\berror\\b",
"name": "storage.type.error.go"
}
]
}
However, this pattern fails to distinguish between method calls and type names when they share the same name. As a result, method calls with the same name as a type are highlighted as type names.
Possible solution
One possible solution is to modify the regular expression in the storage_types
section of go.tmLanguage.json
to check for a dot (.
) preceding the type name. This would ensure that the regular expression only matches type names that are not part of a method call. Here's an example of how this could be done for the error
type:
"storage_types": {
"patterns": [
{
"match": "(?<!\\.)\\berror\\b",
"name": "storage.type.error.go"
}
]
}
By adding (?<!\\.)
before the type name, the regular expression ensures that the type name is not preceded by a dot, which indicates that it is part of a method call.
This solution can be applied to all type names in go.tmLanguage.json
to ensure that method calls with the same name as a type are highlighted as normal function calls.