Skip to content

(Go) Method Highlighting #4196

Open
Open
@RickoNoNo3

Description

@RickoNoNo3

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

Activity

joshgoebel

joshgoebel commented on Jan 2, 2025

@joshgoebel
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

RickoNoNo3 commented on Jan 3, 2025

@RickoNoNo3
Author

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @joshgoebel@RickoNoNo3

        Issue actions

          (Go) Method Highlighting · Issue #4196 · highlightjs/highlight.js