forked from bergercookie/vim-debugstring
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdebugstring.vim
More file actions
229 lines (195 loc) · 6.18 KB
/
Copy pathdebugstring.vim
File metadata and controls
229 lines (195 loc) · 6.18 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
" debugstring.vim - Debug, printf()-style, at the speed of light
" Maintainer: Nikos Koukis <http://bergercookie.github.io/>
" Version: 0.1
""
" @section Introduction, intro
"
"
" @plugin(name) aims to automate standard debugging operations (e.g.,
" segfaults). It does that by facilitating the ubiquitous printf()-debugging
" i.e., scatter logging statements around the various code snippets that you
" want to test.
"
" The form and syntax of the logging statements target the language at hand
" (e.g., use printf() in C/C++ but puts() in Ruby)
"
" vim-debugstring should be compatible with Vim >= 7.4.313
"
""
" @section Configuration, config
" To add a logging statement either use the default normal mode mapping
" (@setting(default_dump_debug_map)) or define your own:
"
" * nmap <your-key-combination> <Plug>DumpDebugStringVar
"
" * nmap <a-second-key-combination> <Plug>DumpDebugStringExpr
""
" @section Functions
"
" Each debug* method supports one (or a family of) programming languages. This
" way it takes care of the different logging directives + different syntaxes.
"
" If you want to extend @plugin(name) for language not yet suppoprted you just
" have to implement a corresponding s:debug* method and also add a line for that
" language in the @setting(debugstring_mappings) group
"
""
" @section About
"
" Original version of this package is written by Nikos Koukis - bergercookie
"
" https://github.com/bergercookie
"
" * Thanks to Steve Losh for writing the excellent book "Learn Vimscript the
" Hard Way". If that helped you as much as it helped me seriously consider
" buying either the e-book or paperback version of it.
"
" http://learnvimscriptthehardway.stevelosh.com/
"
" * Thanks to tpope for providing so much quality vimscript code to work with
" and get constant inspiration from while writing this
"
" https://github.com/tpope/
"
" Introductory moves {{{
if exists('g:loaded_debugstring') || &compatible
finish
endif
let g:loaded_debugstring = 1
let s:save_cpo = &cpoptions
set cpoptions&vim
" }}}
"
""
" Available modes - to be used with the debugFunctionWrapper method
"@setting debugging_modes
"
"
let s:modes = {
\ 'std_debug': 0,
\ 'var_debug': 1,
\}
let g:debugStringCounter = 0
let g:debugStringCounterStep = 1
""
" By default debugging lines should be of the form
" <directive_to_print> <prefix_string><debug_number>
function! g:DebugstringPrefixStr()
let l:debug_str = '[' . fnamemodify(bufname('%'), ':t') . ':'
if getline('.') =~# '^$' " Empty line
let l:debug_str .= line('.')
else
let l:debug_str .= string(str2nr(line('.')) + 1)
endif
let l:debug_str .= '] DEBUGGING STRING ==> '
return l:debug_str
endfunc
" Supplementary functions {{{
""
" Reset the debugging counter.
"
function! s:resetDebugCounter()
let g:debugStringCounter = 0
endfunc
" }}}
""
" Reset the debugging counter
"
command -nargs=0 ResetDebugCounter :call <SID>resetDebugCounter()
""
" Increment the debugging counter
" This is called automaticall every time a debug* method is used
"
function! s:incrDebugCounter()
let g:debugStringCounter += g:debugStringCounterStep
endfunc
if !hasmapto('<Plug>DumpDebugStringVar')
""@setting default_dump_debug_map
"
nmap <unique> <Leader>ds <Plug>DumpDebugStringVar
endif
if !hasmapto('<Plug>DumpDebugStringExpr')
""@setting default_dump_debug_map
"
nmap <unique> <Leader>dS <Plug>DumpDebugStringExpr
endif
""
" Set this to false if you want to print just the logging statement without any
" additional directive like '#include <stsdio.h'
" Applicable only in libraries that the logging directive is part of an external
" library
"
let g:debugstringAlwaysIncludeHeader = 0 " Include Header in place?
""
" Wrapper around the low-level debug* methods.
" It also takes care of incrementing the g:debugCounter
"
" {mode} refers to the type of debugging that is to be done. See
" @setting(debugging_modes) for the available modes
"
" If an additional argument is provided it will be used as the expression - This
" additional argument implies that the debugging mode is 'var_debug'
"
"
function! s:debugFunctionWrapper(mode, ...)
let l:prev_pos = getpos('.')
let l:append_at_same_line = 0
if getline('.') =~# '^$' " Empty line
let l:append_at_same_line = 1
endif
if a:mode ==# s:modes['std_debug']
if !exists(':AddDebugString')
echoerr "Command AddDebugString isn't implemented for filetype \"" . &filetype . "\""
return 0
endif
AddDebugString
if &runtimepath =~# 'vim-repeat'
call repeat#set("\<Plug>DumpDebugStringVar<CR>")
endif
elseif a:mode ==# s:modes['var_debug']
if !exists(':AddDebugStringExpr')
echoerr "Command AddDebugStringExpr isn't implemented for filetype \"" . &filetype . "\""
return 0
endif
let l:expr = ''
if len(a:000) ==# 0
let l:expr = input('Input Expression: ')
else
let l:expr = a:1
endif
if empty(l:expr)
return
endif
AddDebugStringExpr(l:expr)
" Make way for repeat.vim
if &runtimepath =~# 'vim-repeat'
execute 'nnoremap <silent> <Plug>DumpDebugStringSpecExpr :<C-U>call <SID>debugFunctionWrapper(1, "' . l:expr . '")<CR>'
call repeat#set("\<Plug>DumpDebugStringSpecExpr<CR>")
endif
else
echoerr 's:debugFunctionWrapper - Unknown mode: ' a:mode
return 0
endif
" correct indentation level
if indent(line('.') - 1) !=# 0
normal! k0vwhyjP
endif
normal! ==
call s:incrDebugCounter() " increase the counter
let l:new_pos = l:prev_pos
if l:append_at_same_line
" move them to previous line
normal! kJ
else
let l:new_pos[1] += 1 " go directly to the next line
endif
call setpos('.', l:new_pos)
endfunc
""
" For debugging, execute a <Plug> command using execute:
" :execute "normal \<Plug>DumpDebugStringVar"
"
nnoremap <silent> <Plug>DumpDebugStringVar :<C-U>call <SID>debugFunctionWrapper(0)<CR>
nnoremap <silent> <Plug>DumpDebugStringExpr :<C-U>call <SID>debugFunctionWrapper(1)<CR>
let &cpoptions = s:save_cpo
unlet s:save_cpo