diff --git a/CHANGES.md b/CHANGES.md
index 7050702f5f..f329fb9a85 100644
--- a/CHANGES.md
+++ b/CHANGES.md
@@ -14,6 +14,7 @@ Core Grammars:
- fix(c, cpp) stop a raw string's closing delimiter from swallowing quotes, which broke highlighting of everything after the literal, issue #3585 [David Pavlovschii][]
- enh(python) add missing builtins: `aiter` and `anext` (Python 3.10), `frozendict` and `sentinel` (Python 3.15) [Hugo van Kemenade][]
- enh(python) Support t-strings [Nicolas Le Cam][]
+- fix(go) highlight the name of a method with a receiver, e.g. `func (c *exec.Cmd) Run()`, issue #4196 [Ali Asgher][]
Documentation:
@@ -33,6 +34,7 @@ CONTRIBUTORS
[Nicolas Le Cam]: https://github.com/KuSh
[Konstantin Baltsat]: https://github.com/Baltsat
[David Pavlovschii]: https://github.com/davidpavlovschi
+[Ali Asgher]: https://github.com/alliasgher
## Version 11.11.3
diff --git a/src/languages/go.js b/src/languages/go.js
index f1b4d152cb..cf2690562b 100644
--- a/src/languages/go.js
+++ b/src/languages/go.js
@@ -8,6 +8,7 @@ Category: common, system
*/
export default function(hljs) {
+ const regex = hljs.regex;
const LITERALS = [
"true",
"false",
@@ -86,6 +87,22 @@ export default function(hljs) {
literal: LITERALS,
built_in: BUILT_INS
};
+ const PARAMS = {
+ className: 'params',
+ begin: /\(/,
+ end: /\)/,
+ endsParent: true,
+ keywords: KEYWORDS,
+ illegal: /["']/
+ };
+ // a method's receiver, ie the `(c *exec.Cmd)` of `func (c *exec.Cmd) Run()` -
+ // it must not end the function mode, otherwise the method name that follows
+ // it is never seen; only a parenthesized group directly followed by
+ // `name(` qualifies, so a plain function's argument list is unaffected
+ const RECEIVER = hljs.inherit(PARAMS, {
+ begin: regex.concat(/\(/, regex.lookahead(/[^)]+\)\s*(?!func\b)[a-zA-Z_]\w*\s*\(/)),
+ endsParent: false
+ });
return {
name: 'Go',
aliases: [ 'golang' ],
@@ -142,15 +159,9 @@ export default function(hljs) {
end: '\\s*(\\{|$)',
excludeEnd: true,
contains: [
+ RECEIVER,
hljs.TITLE_MODE,
- {
- className: 'params',
- begin: /\(/,
- end: /\)/,
- endsParent: true,
- keywords: KEYWORDS,
- illegal: /["']/
- }
+ PARAMS
]
}
]
diff --git a/test/markup/go/methods.expect.txt b/test/markup/go/methods.expect.txt
new file mode 100644
index 0000000000..61d320ecd7
--- /dev/null
+++ b/test/markup/go/methods.expect.txt
@@ -0,0 +1,15 @@
+func (c *exec.Cmd) Run() error
+
+func (m sync.Mutex) Lock()
+
+func (*sync.Mutex) Unlock()
+
+func (s *Stack[T]) Push(v T) {
+ s.items = append(s.items, v)
+}
+
+func (mt MyType) MyFunc(int, float32) (int, io.Reader, error) {
+ return 0, nil, nil
+}
+
+func middleware(h Handler) func(int32) int32
diff --git a/test/markup/go/methods.txt b/test/markup/go/methods.txt
new file mode 100644
index 0000000000..b13cbb0842
--- /dev/null
+++ b/test/markup/go/methods.txt
@@ -0,0 +1,15 @@
+func (c *exec.Cmd) Run() error
+
+func (m sync.Mutex) Lock()
+
+func (*sync.Mutex) Unlock()
+
+func (s *Stack[T]) Push(v T) {
+ s.items = append(s.items, v)
+}
+
+func (mt MyType) MyFunc(int, float32) (int, io.Reader, error) {
+ return 0, nil, nil
+}
+
+func middleware(h Handler) func(int32) int32