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
162 lines (138 loc) · 4.63 KB
/
Copy pathdebugstring.vim
File metadata and controls
162 lines (138 loc) · 4.63 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
" debugstring.vim - Debug, printf()-style, at the speed of light
" Maintainer: Nikos Koukis <http://bergercookie.github.io/>
" Version: 0.1
" TODO: Add option for variable debugging
" Make the counter buffer-specific
" Escape double single quotes vimscript variable printing
""
" @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)
""
" @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>DumpDebugString
""
" @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
" }}}
"
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') .
\ ':' . getcurpos()[1] .
\ '] 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>DumpDebugString')
""@setting default_dump_debug_map
"
nmap <unique> <Leader>ds <Plug>DumpDebugString
""@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
"
function! s:debugFunctionWrapper(mode)
let l:prev_pos = getcurpos()
if a:mode ==# s:modes['std_debug']
if !exists(':AddDebugString')
echoerr "Command AddDebugString isn't implemented for filetype \"" . &filetype . "\""
return 0
endif
AddDebugString
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 = input("Input Expression: ")
AddDebugStringExpr(l:expr)
else
echoerr 's:debugFunctionWrapper - Unknown mode: ' a:mode
return 0
endif
call s:incrDebugCounter() " increase the counter
let l:new_pos = l:prev_pos
let l:new_pos[1] += 2 " go directly to the next line
call setpos('.', l:new_pos)
endfunc
nnoremap <silent> <Plug>DumpDebugString :<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