Repository navigation
Conversation
A field of a struct is now fontified with `font-lock-property-name-face', the face Emacs 29 added for exactly this and the one `go-ts-mode' uses for field declarations. The names of a field are the identifiers at the start of the field declaration, followed by its type on the same line. A field that consists of a type alone is embedded and has no name, so it is left alone, and so are qualified names such as "io.Reader". Selector expressions such as "film.Title" are not affected: there the field is a use rather than a declaration. The new face is only used when it exists, so Emacs 26 still works.
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.
Part of #453 (the declaration half): the use side is left alone, see below.
What changes
Struct field names are fontified with
font-lock-property-name-face:This is the face Emacs 29 added for the purpose (
NEWS.29: "New facesfor font-lock ... primarily meant for use with tree-sitter") and the one
go-ts-modealready uses:How it works
A field name is an identifier that starts a field declaration -- it is
at the beginning of a line, or follows a comma or a semicolon -- and is
followed by the type of the field on the same line. Requiring the type
is what keeps
io.Reader, unfontified;io.Readerafter a name) as they were;foo: 1) and comments untouched.Names of a field list are matched one by one, so the commas between them
keep their own face.
Tests
go--should-fontifygained aPmarker for the new face. Three testscovered struct fields (
go--fontify-struct,go--fontify-type-switchand
go--fontify-var-decl); they now expect the name to be fontified,and the struct test additionally covers a name list, a field whose type
starts with a keyword (
chan), a slice type and an embedded field.All 31 tests pass, and byte-compilation reports the same 13 warnings as
master.
Not in this PR
The use side of #453 --
film.Titlein an expression -- is left alone.Unlike a declaration, the field of a selector cannot be told apart from
a qualified type (
foo.Zebra) without looking at the context, andgo-mode fontifies
foo.Zebraas a type today. That is a separatedecision, and this PR does not change any existing fontification: over
95 Go files the only difference is 435 characters gaining the new face,
with nothing losing a face.
Performance
Fontifying those 95 files (10 passes, best pass,
font-lock-ensurealone) goes from 0.348s to 0.387s, about 11%. The matcher fires for
every identifier that starts a field declaration and checks
go--in-struct-definition-ponce per candidate -- the same shape asgo--match-declandgo--containing-decl.