Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(Go) Method Highlighting #4196

Open
RickoNoNo3 opened this issue Jan 1, 2025 · 2 comments
Open

(Go) Method Highlighting #4196

RickoNoNo3 opened this issue Jan 1, 2025 · 2 comments
Labels
bug help welcome Could use help from community language

Comments

@RickoNoNo3
Copy link

Describe the issue
Just see the screenshot, methods of structs have different highlighting to simple functions.

Which language seems to have the issue?

Go

Are you using highlight or highlightAuto?

hljs.highlightAll();

Sample Code to Reproduce

<pre><code>func deferFunc(err *error)</code></pre>
<br>
<pre><code>func (c *exec.Cmd) Run() error</code></pre>

Expected behavior
Highlighting to method names just like function names.

Additional context
image

image

@RickoNoNo3 RickoNoNo3 added bug help welcome Could use help from community language labels Jan 1, 2025
@joshgoebel
Copy link
Member

To be clear are you suggesting there that "Run" should be highlighted in the second example?

What exactly is (c *exec.Cmd) syntactically here, the type of object the method applies to? Do the type defs always follow this exact pattern... could you provide some additional examples?

@RickoNoNo3
Copy link
Author

RickoNoNo3 commented Jan 3, 2025

Formally speaking, functions in Go can be defined using the following EBNF grammar language:

F := "func" [Receiver] [Pkg.]Name "(" [Params] ")" [Returns]
Receiver := "(" [ReceiverVar] Type ")"
Params := Param {, Param} [, VariadicParam] | VariadicParam
Param := [ParamName {, ParamName}] Type
VariadicParam := [ParamName] "..."Type
Returns := "(" Return {, Return} ")" | Type
Return := [VarName {, VarName}] Type
Type := {"*"}[Pkg.]TypeName

Although the Go documentation does not specifically mention its exact syntax, based on my personal experience, it can be defined in the above manner.
Generally speaking, we call a function that includes a Receiver part a Method, indicating that it can only be called as a "member function" of an instance of the type corresponding to the Receiver. Those without this part are ordinary Functions.

For Example:

func Exit(code int)                       // simplest
func Sleep(d time.Duration)               // a param
func Sleep2(d, d2 time.Duration)          // two params sharing the type
func FormatInt(i int64, base int) string  // two params of different types
func Fatal(v ...interface{})              // the variadic param
func Itoa(i int) string                   // with a return value
func Atoi(s string) (int, error)          // with two return value anonymous
func (m *sync.Mutex) Lock()               // with named receiver
func (*sync.Mutex) Lock()                 // with anonymous receiver

-----

func (mt MyType) MyFunc(int, float32) (int, io.Reader, error) {
	return 0, nil, nil
}

Unfortunately, GitHub is also unable to handle the display of these syntax highlights effectively. Let's see how they are highlighted in VSCode:

image

image

image

image

image


image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help welcome Could use help from community language
Projects
None yet
Development

No branches or pull requests

2 participants