Shougo / context_filetype.vim

Context filetype library for Vim script
Other
160 stars 20 forks source link

Improve (or revert) cgo support #31

Closed haruyama closed 7 years ago

haruyama commented 7 years ago

Current s:default_filetypes.go definition is here. https://github.com/Shougo/context_filetype.vim/commit/bf0ff1392e4310b413846f12aad2a61626588065

      \ 'go': [
      \   {
      \    'start': '^\s*/\*$',
      \    'end': '^\s*\*/\n\s*import\s\+"C"$', 'filetype': 'c',
      \   },

'end' pattern matches lines after "*/"(?), So context_filetype.vim sets 'c' to filetype in and after(!) C-style comment lines.

I made simple fix,

diff --git a/autoload/context_filetype.vim b/autoload/context_filetype.vim
index 1d3ba8a..b5ebb09 100644
--- a/autoload/context_filetype.vim
+++ b/autoload/context_filetype.vim
@@ -319,7 +319,7 @@ let s:default_filetypes = {
       \ 'go': [
       \   {
       \    'start': '^\s*/\*$',
-      \    'end': '^\s*\*/\n\s*import\s\+"C"$', 'filetype': 'c',
+      \    'end': '^\s*\*/\s*\_.\{1}\s*import\s\+"C"$', 'filetype': 'c',
       \   },
       \ ],
 \}"}}}

but, it does not work for a file with multi C-sytle comments.

package main

`go`

/*

`go`

*/

`go`

/*

`c`

*/
import "C"

`go`

Second and third 'go' are not correctly set(c).

Shougo commented 7 years ago

So context_filetype.vim sets 'c' to filetype in and after(!) C-style comment lines.

I don't understand it. Please upload the example.

haruyama commented 7 years ago
     1  package main
     2  
     3  /*
     4   */
     5  
     6  func hoge() string {
     7      return ""
     8  }

Lines 1, 2, 3 and 8 are go, others are c.

Shougo commented 7 years ago

Oh...

Shougo commented 7 years ago

But why

+      \    'end': '^\s*\*/\s*\_.\{1}\s*import\s\+"C"$', 'filetype': 'c',

fixes the problem?

haruyama commented 7 years ago

\n does not express new-line in Vim regexp. \_. expresses all chars including new-line(\{1} is redundant). cf: [Vim正規表現-特訓#1]Vimで複数行にまたがる条件で検索 - Qiita

haruyama commented 7 years ago

Sorry, My pattern

+      \    'end': '^\s*\*/\s*\_.\{1}\s*import\s\+"C"$', 'filetype': 'c',

is also not correct. It does not work fine.

I retry searching adequate pattern.

haruyama commented 7 years ago

[WIP] Fix cgo support #32

I suppose that curretn range searching scheme is insufficient for cgo support.

haruyama commented 7 years ago

I suppose that cgo support has 2 problems.

1: context_filetype.vim supports a range with start pattern without end pattern.This feature works fine in many situation, such as editing following dein.toml:

[[plugins]]
repo = 'Shougo/vimproc.vim'
`toml`
hook_post_update = '''
`vim`

In golang, a C-style (no cgo) comment has a filetype=c start pattern and does not have filetype=c end pattern. This is why a range between line /*(+1) and EOL(-1) becomes filetype=c.

cf: https://github.com/Shougo/context_filetype.vim/issues/31#issuecomment-319625600

32 disables this feature.

2: Even if disabling without end pattern support, current context_filetype.vim range searching does not work fine for following go code.

1 /*
2 */
3
4 /*
5 */
6 import "C"

In lines between 2 and 3, context_filetype.vim set filetype=c, this is because start pattern matches line 1 and end pattern matches line 5,6.

I suppose that supporting cgo-like range (end pattern is more important than start) needs another search_range(), which searches end pattern first and start pattern second, contrary to existing search_range().

Most golang users do not write cgo and may use C-style comments, so reverting bf0ff1392e4310b413846f12aad2a61626588065 is one solution.