Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

:GoDoc (K) does not highlight the syntax of the method declaration. #3365

Closed
juanmaia opened this issue Feb 27, 2022 · 4 comments
Closed

:GoDoc (K) does not highlight the syntax of the method declaration. #3365

juanmaia opened this issue Feb 27, 2022 · 4 comments

Comments

@juanmaia
Copy link

Title is very straightforward. Is this a "bug" or a feature?
As you can see in the screenshot, in the very first part of the window, the window created with the :GoDoc command, is not syntax highlighted. This is very nit of course, but I was wondering if is something I can clearly accomplish with some config.

Screenshot 2022-02-27 at 19 04 16

Vim version (first three lines from :version):

NVIM v0.7.0-dev+971-g4b719e4a1
Build type: Release
LuaJIT 2.1.0-beta3

Go version (go version):

go version go1.17.7 darwin/amd64

Go environment

go env Output:
GO111MODULE=""
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/juanmaia/Library/Caches/go-build"
GOENV="/Users/juanmaia/Library/Application Support/go/env"
GOEXE=""
GOEXPERIMENT=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOMODCACHE="/Users/juanmaia/go/pkg/mod"
GONOPROXY=""
GONOSUMDB=""
GOOS="darwin"
GOPATH="/Users/juanmaia/go"
GOPRIVATE=""
GOPROXY="https://proxy.golang.org,direct"
GOROOT="/usr/local/go"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOTOOLDIR="/usr/local/go/pkg/tool/darwin_amd64"
GOVCS=""
GOVERSION="go1.17.7"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/Users/juanmaia/Prog/go/channels/go.mod"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/k0/xcqk49bj5gx63kmmytd3q5qh0000gn/T/go-build1216738895=/tmp/go-build -gno-record-gcc-switches -fno-common"

gopls version

gopls version Output:
golang.org/x/tools/gopls v0.7.5
    golang.org/x/tools/gopls@v0.7.5 h1:8Az52YwcFXTWPvrRomns1C0N+zlgTyyPKWvRazO9GG8=

vim-go configuration:

vim-go configuration
g:go_highlight_operators = 1
g:go_highlight_function_calls = 1
g:go_highlight_variable_declarations = 1
g:go_highlight_variable_assignments = 1
g:go_highlight_function_parameters = 1
g:go_auto_sameids = 0
g:go_highlight_functions = 1
g:go_highlight_types = 1
g:go_jump_to_error = 1
g:go_loaded_gosnippets = 1
g:go_loaded_install = 1

filetype detection configuration:

filetype detection
filetype detection:ON  plugin:ON  indent:ON
@bhcleek
Copy link
Collaborator

bhcleek commented Feb 27, 2022

The difficulting with doing something like this is that Vim's syntax highlighting is based purely on regular expressions, and the syntax highlighting in godoc that you may expect wouldn't be possible to fully achieve in all cases; there would be edge cases where it wouldn't be possible to highlight correctly.

It does appear that something like this used to exist, but was removed about 6 years ago, though I don't fully understand why (see #48 and #782)

@juanmaia
Copy link
Author

the syntax highlighting in godoc that you may expect wouldn't be possible to fully achieve in all cases;

Why do you think so? What I have in mind would be just the very first line, which is usually the Method/Type/Constant declaration. I wouldn't bother highlighting the rest of the docs.

Although I don't have much experience with vim plug-in development, I would be eager to help on this one.

@bhcleek
Copy link
Collaborator

bhcleek commented Feb 28, 2022

Why do you think so?

Because it won't take long for people to want that highlighting in any examples and package indexes. (e.g. the examples in :GoDoc net/http and the index in any and every package's godoc).

What I have in mind would be just the very first line, which is usually the Method/Type/Constant declaration. I wouldn't bother highlighting the rest of the docs.

Highlighting in vim is driven by regular expressions; I don't know of a way to restrict to only a particular set of lines within a buffer. It's probably possible to do this based on the presence of leading whitespace or not; it appears that godoc uses spaces for leading whitespace in doc, and tabs in the types that are being documented, and of course no whitespace for package level identifiers.

One would think one could merely reuse the syntax for go files, but that would surely be incorrect (especially if one wanted to highlight examples due to the way some examples are written).

@Erickson400
Copy link

I found a way to highlight the function names by editing the go.vim file that comes with vim. I don't know how to write regex's so i just copied someone else's for a C highlight. All i did was check if a word has a "(" at the end, If it does then draw it yellow.
The drawback to this is that it also paints the imports, multi line constants and multi line variable declamations because they also have a "(" at the end.

import (
. . .
)
var (
. . .
)

I could fix this by checking if it starts with the word "func" or a "."
But I don't know how regex's work so i think this is my dead end.

This is what i put at the bottom of the go.vim file

"highlight function names
syn match Func "\zs(\k\w*)\s\ze("
hi link Func Statement

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants