fix(go) highlight the name of a method with a receiver - #4458
Open
alliasgher wants to merge 1 commit into
Open
Conversation
The function mode's params sub-mode has endsParent, so the first parenthesised group closed the whole mode. For a method that group is the receiver, so the mode ended at the receiver's closing paren and TITLE_MODE never reached the method name. Add a receiver mode that matches only a parenthesised group directly followed by an identifier and another open paren, and which does not end its parent. A plain function's argument list does not match that shape, so it keeps the old behaviour, and a func-typed return such as func(int32) int32 is excluded explicitly so it is still treated as a return type. Fixes highlightjs#4196
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #4196.
Describe the issue
func (c *exec.Cmd) Run() errorgets nohljs-titleonRun, while a plainfunc deferFunc(err *error)highlights fine.The function mode contains a
paramssub-mode withendsParent: true, so the first parenthesised group ends the whole function mode. For a method, that first group is the receiver, so the mode terminates at the receiver's)andTITLE_MODEnever gets as far as the method name.The fix
A separate receiver mode, placed before
TITLE_MODE, which does not end its parent:I kept the grammar deliberately narrow rather than loosening
funcgenerally, since widening it risks auto-detection. The lookahead only fires for a non-empty parenthesised group that is directly followed by an identifier and another open paren, which is the method-declaration shape. A plain function's argument list is not followed byname(, so it never matches and behaves exactly as before.The
(?!func\b)is there forfunc middleware(h Handler) func(int32) int32: a return type can be followed by(, and without the guard that argument list would be misread as a receiver.Testing
New markup test
test/markup/go/methods.txtcovering a pointer receiver, a value receiver, an anonymous receiver(*sync.Mutex), a generic receiver(s *Stack[T]), multiple return values, and thefunc-typed return above as a negative case. It fails onmainand passes with this change.Full suite:
1594 passing, and no existingtest/markup/go/*.expect.txtchanged, which is the check that plain functions are untouched.npm run lintandnpm run lint-languagesare clean.Two limitations I did not try to fix, both pre-existing and equally true of plain functions today:
paramsmode, so a string in a parameter default/array-length expression loseshljs-string— this makes methods consistent with plain functions rather than introducing a new inconsistencyTITLE_MODEis ASCII-onlyChecklist