Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -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
Expand Down
27 changes: 19 additions & 8 deletions src/languages/go.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Category: common, system
*/

export default function(hljs) {
const regex = hljs.regex;
const LITERALS = [
"true",
"false",
Expand Down Expand Up @@ -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' ],
Expand Down Expand Up @@ -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
]
}
]
Expand Down
15 changes: 15 additions & 0 deletions test/markup/go/methods.expect.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(c *exec.Cmd)</span> <span class="hljs-title">Run</span><span class="hljs-params">()</span></span> <span class="hljs-type">error</span>

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(m sync.Mutex)</span> <span class="hljs-title">Lock</span><span class="hljs-params">()</span></span>

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(*sync.Mutex)</span> <span class="hljs-title">Unlock</span><span class="hljs-params">()</span></span>

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(s *Stack[T])</span> <span class="hljs-title">Push</span><span class="hljs-params">(v T)</span></span> {
s.items = <span class="hljs-built_in">append</span>(s.items, v)
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(mt MyType)</span> <span class="hljs-title">MyFunc</span><span class="hljs-params">(<span class="hljs-type">int</span>, <span class="hljs-type">float32</span>)</span></span> (<span class="hljs-type">int</span>, io.Reader, <span class="hljs-type">error</span>) {
<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>, <span class="hljs-literal">nil</span>, <span class="hljs-literal">nil</span>
}

<span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">middleware</span><span class="hljs-params">(h Handler)</span></span> <span class="hljs-function"><span class="hljs-keyword">func</span><span class="hljs-params">(<span class="hljs-type">int32</span>)</span></span> <span class="hljs-type">int32</span>
15 changes: 15 additions & 0 deletions test/markup/go/methods.txt
Original file line number Diff line number Diff line change
@@ -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