aboutsummaryrefslogtreecommitdiff
path: root/autoload/hints/plugins.vim
blob: 3f080d74c97b7277743874dbb95adde31398b830 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
let s:ftplugins = {}

let s:vim_plugin = {}
function! s:vim_plugin.Before(file)
endfunction
function! s:vim_plugin.TagLine(linenr, line)
  return a:line =~ '\<\(if\|function\|return\|end\w*\|while\|for\|let\|else\|try\)\>'
endfunction

let s:WHITESPACE_OR_COMMENT='\(^\s*$\)\|\(^\s*//\)\|\(^\s*\*/\)'
let s:java_plugin = {}
function! s:java_plugin.new() dict
  let ret = {}

  function! ret.Before(file) dict
    let self.last_line = ''
    let self.last_indent = ''
  endfunction

  function! ret.TagLine(linenr, line) dict
    if self.last_line =~ s:WHITESPACE_OR_COMMENT
          \ && !(a:line =~ s:WHITESPACE_OR_COMMENT)
      let self.last_line = a:line
      return v:true
    endif

    let self.last_line = a:line
    let indent = matchlist(a:line, '^\s*')
    let indent = len(indent) > 0 ? indent[0] : ""
    if self.last_indent != indent
      let self.last_indent = indent
      return v:true
    endif

    return 
          \ a:line =~ '^\s*}$' ||
          \ a:line =~ '\<\(public\|private\|protected\|class\|static\|try\|while\|for\|if\|else\|catch\)\>'
  endfunction

  return ret
endfunction

function! hints#plugins#registerFt(filetype, plugin) abort
  let s:ftplugins[a:filetype] = a:plugin
endfunction

function! hints#plugins#getPluginForFiletype(filetype) abort
  let plug = get(s:ftplugins, a:filetype, s:default_plugin)
  if has_key(plug, "new")
    let plug = plug.new()
  endif
  return plug
endfunction

let s:default_plugin = {}
function! hints#plugins#getDefaultPlugin() abort
  return s:default_plugin
endfunction


let s:ISSPACE = '^\s*$'
let s:ISCOMMENT = '^\s*[-/#;"(]'
function! s:default_plugin.new()
  let ret = {}

  function! ret.Before(file)
    let self.last_kind = 1
  endfunction

  function! ret.TagLine(linenr, line)
    if a:line =~ s:ISSPACE
      let kind = 1
    elseif a:line =~ s:ISCOMMENT
      let kind = 2
    else
      let kind = 3
    endif

    if self.last_kind != kind && kind != 1
      let self.last_kind = kind
      return v:true
    endif

    let self.last_kind = kind
    return v:false
  endfunction

  return ret
endfunction

call hints#plugins#registerFt("vim", s:vim_plugin)
call hints#plugins#registerFt("java", s:java_plugin)