Skip to content

Commit 7558458

Browse files
fatihStabbyCutyou
authored and
StabbyCutyou
committed
syntax: don't pick up chan chains (channel of channels)
This is a very weird error. We want to show an error if the chan statement is in form of: <- chan chan <- The correct form should be of course: <-chan chan<- But for example, if we have a channel of channels (like below), the selected piece of whitespace is also highlighted as an error chan<- chan<- ^ Because it's the part of `<- chan`, whereas we should not treat as an error because we have two `chan<-` and `chan<-` statements. To fix this, we add a `negative lookbehind` regex statement. It will not match a text if it's preceded with other text. First let assemble the current regex for the case: `<- chan` (receive-only annotation), which is: \(<-\)\@<=\s\+\(chan\>\)\@= The first part is: \(<-\)\@<=\s\+\ Here the pattern \@<= matches with zero width if the preceding atom matches just before what follows. `\s\+` means white space of at least one character. So it matches **whitespaces** which precedes the statement `<-` Now the second part is only useful with the first part. I've added the placeholder to make it more readable: PLACEHOLDER(chan\>\)\@=" First the pattern `\@=` means to match the preceding atom with zero width. So it will match the **PLACEHOLDER** till it finds a `chan` statement. Once it finds it, it doesn't proceed anymore. So we only highlight the whitespaces between `<-` and `chan` only if we have a white space of at least one character Finally we can introduce our fix now. We're going to pick the `<-` statemetn if it doesn't follow a `chan`. So `chan<-` should NOT trigger an error anymore, because we're not going to pick arrows in that form. To match an arrow which doesn't follow a chan statement can be done via a `negative lookbehind`, which is in the form of: \(\<chan\>\)\@<!<- This literally means: "Match `<-` only if it doesn't follow `chan`. We're going to use this pattern for the plain `<-` pattern. We also fix the issue of only matching `chan` and not words like `mychan`. Aslo note we're just going to replace this into our current regex: This original regex: \(<-\)\@<=\s\+\(\<chan\>\)\@= will be changed into: \(\(\<chan\>\)\@<!<-\)\@<=\s\+\(\<chan\>\)\@= Fixes fatih#655
1 parent c8e5e4f commit 7558458

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

syntax/go.vim

+10-2
Original file line numberDiff line numberDiff line change
@@ -208,9 +208,17 @@ endif
208208
" Spacing errors around the 'chan' keyword
209209
if g:go_highlight_chan_whitespace_error != 0
210210
" receive-only annotation on chan type
211-
syn match goSpaceError display "\(<-\)\@<=\s\+\(chan\>\)\@="
211+
"
212+
" \(\<chan\>\)\@<!<- (only pick arrow when it doesn't come after a chan)
213+
" this prevents picking up 'chan<- chan<-' but not '<- chan'
214+
syn match goSpaceError display "\(\(\<chan\>\)\@<!<-\)\@<=\s\+\(\<chan\>\)\@="
215+
212216
" send-only annotation on chan type
213-
syn match goSpaceError display "\(\<chan\)\@<=\s\+\(<-\)\@="
217+
"
218+
" \(<-\)\@<!\<chan\> (only pick chan when it doesn't come after an arrow)
219+
" this prevents picking up '<-chan <-chan' but not 'chan <-'
220+
syn match goSpaceError display "\(\(<-\)\@<!\<chan\>\)\@<=\s\+\(<-\)\@="
221+
214222
" value-ignoring receives in a few contexts
215223
syn match goSpaceError display "\(\(^\|[={(,;]\)\s*<-\)\@<=\s\+"
216224
endif

0 commit comments

Comments
 (0)