├── autoload └── prettyprint.vim ├── doc └── prettyprint.txt └── plugin └── prettyprint.vim /autoload/prettyprint.vim: -------------------------------------------------------------------------------- 1 | " Prettyprint vim variables. 2 | " Version: 0.3.4 3 | " Author : thinca 4 | " License: zlib License 5 | 6 | " options. {{{1 7 | if !exists('g:prettyprint_indent') " {{{2 8 | if exists('*shiftwidth') 9 | let g:prettyprint_indent = 'shiftwidth()' 10 | else 11 | let g:prettyprint_indent = '&l:shiftwidth' 12 | endif 13 | endif 14 | 15 | if !exists('g:prettyprint_width') " {{{2 16 | let g:prettyprint_width = '&columns' 17 | endif 18 | 19 | if !exists('g:prettyprint_string') " {{{2 20 | let g:prettyprint_string = [] 21 | endif 22 | 23 | if !exists('g:prettyprint_show_expression') " {{{2 24 | let g:prettyprint_show_expression = 0 25 | endif 26 | 27 | let s:string_t = type('') 28 | let s:list_t = type([]) 29 | let s:dict_t = type({}) 30 | let s:func_t = type(function('tr')) 31 | 32 | " functions. {{{1 33 | function! s:pp(expr, shift, width, stack) abort 34 | let indent = repeat(s:blank, a:shift) 35 | let indentn = indent . s:blank 36 | 37 | let appear = index(a:stack, a:expr) 38 | call add(a:stack, a:expr) 39 | 40 | let width = s:width - a:width - s:indent * a:shift 41 | 42 | let str = '' 43 | if type(a:expr) == s:list_t 44 | if appear < 0 45 | let result = [] 46 | for Expr in a:expr 47 | call add(result, s:pp(Expr, a:shift + 1, 0, a:stack)) 48 | unlet Expr 49 | endfor 50 | let oneline = '[' . join(result, ', ') . ']' 51 | if strlen(oneline) < width && oneline !~ "\n" 52 | let str = oneline 53 | else 54 | let content = join(map(result, 'indentn . v:val'), ",\n") 55 | let str = printf("[\n%s\n%s]", content, indent) 56 | endif 57 | else 58 | let str = '[nested element ' . appear .']' 59 | endif 60 | 61 | elseif type(a:expr) == s:dict_t 62 | if appear < 0 63 | let result = [] 64 | for key in sort(keys(a:expr)) 65 | let skey = string(strtrans(key)) 66 | let sep = ': ' 67 | let Val = get(a:expr, key) " Do not use a:expr[key] to avoid Partial 68 | let valstr = s:pp(Val, a:shift + 1, strlen(skey . sep), a:stack) 69 | if s:indent < strlen(skey . sep) && 70 | \ width - s:indent < strlen(skey . sep . valstr) && valstr !~ "\n" 71 | let sep = ":\n" . indentn . s:blank 72 | endif 73 | call add(result, skey . sep . valstr) 74 | unlet Val 75 | endfor 76 | let oneline = '{' . join(result, ', ') . '}' 77 | if strlen(oneline) < width && oneline !~ "\n" 78 | let str = oneline 79 | else 80 | let content = join(map(result, 'indentn . v:val'), ",\n") 81 | let str = printf("{\n%s\n%s}", content, indent) 82 | endif 83 | else 84 | let str = '{nested element ' . appear .'}' 85 | endif 86 | 87 | elseif type(a:expr) == s:func_t 88 | silent! let funcstr = string(a:expr) 89 | let matched = matchlist(funcstr, '\C^function(''\(.\{-}\)''\()\?\)') 90 | let funcname = matched[1] 91 | let is_partial = matched[2] !=# ')' 92 | let symbol = funcname =~# '^\%(\)\?\d\+$' ? 93 | \ '{"' . funcname . '"}' : funcname 94 | if &verbose && exists('*' . symbol) 95 | redir => func 96 | " Don't print a definition location if &verbose == 1. 97 | silent! execute (&verbose - 1) 'verbose function' symbol 98 | redir END 99 | let str = func 100 | elseif is_partial 101 | let str = printf("function('%s', {partial})", funcname) 102 | else 103 | let str = printf("function('%s')", funcname) 104 | endif 105 | elseif type(a:expr) == s:string_t 106 | let str = a:expr 107 | if a:expr =~# "\n" && s:string_split 108 | let expr = s:string_raw ? 'string(v:val)' : 'string(strtrans(v:val))' 109 | let str = "join([\n" . indentn . 110 | \ join(map(split(a:expr, '\n', 1), expr), ",\n" . indentn) . 111 | \ "\n" . indent . '], "\n")' 112 | elseif s:string_raw 113 | let str = string(a:expr) 114 | else 115 | let str = string(strtrans(a:expr)) 116 | endif 117 | else 118 | let str = string(a:expr) 119 | endif 120 | 121 | unlet a:stack[-1] 122 | return str 123 | endfunction 124 | 125 | function! s:option(name) abort 126 | let name = 'prettyprint_' . a:name 127 | let opt = has_key(b:, name) ? b:[name] : g:[name] 128 | return type(opt) == type('') ? eval(opt) : opt 129 | endfunction 130 | 131 | function! prettyprint#echo(str, msg, expr) abort 132 | let str = a:str 133 | if g:prettyprint_show_expression 134 | let str = a:expr . ' = ' . str 135 | endif 136 | if a:msg 137 | for s in split(str, "\n") 138 | echomsg s 139 | endfor 140 | else 141 | echo str 142 | endif 143 | endfunction 144 | 145 | function! prettyprint#prettyprint(...) abort 146 | let s:indent = s:option('indent') 147 | let s:blank = repeat(' ', s:indent) 148 | let s:width = s:option('width') - 1 149 | let string = s:option('string') 150 | let strlist = type(string) is type([]) ? string : [string] 151 | let s:string_split = 0 <= index(strlist, 'split') 152 | let s:string_raw = 0 <= index(strlist, 'raw') 153 | let result = [] 154 | for Expr in a:000 155 | call add(result, s:pp(Expr, 0, 0, [])) 156 | unlet Expr 157 | endfor 158 | return join(result, "\n") 159 | endfunction 160 | -------------------------------------------------------------------------------- /doc/prettyprint.txt: -------------------------------------------------------------------------------- 1 | *prettyprint.txt* Prettyprint vim variables. 2 | 3 | Version: 0.3.4 4 | Author : thinca 5 | License: zlib License 6 | 7 | ============================================================================== 8 | CONTENTS *prettyprint-contents* 9 | 10 | INTRODUCTION |prettyprint-introduction| 11 | INTERFACE |prettyprint-interface| 12 | COMMANDS |prettyprint-commands| 13 | FUNCTIONS |prettyprint-functions| 14 | OPTIONS |prettyprint-options| 15 | FORMAT RULES |prettyprint-format-rules| 16 | LIMITATION |prettyprint-limitation| 17 | CHANGELOG |prettyprint-changelog| 18 | 19 | 20 | ============================================================================== 21 | INTRODUCTION *prettyprint-introduction* 22 | 23 | *prettyprint.vim* is a Vim plugin to prettyprint vim variable for debug. 24 | 25 | 26 | Requirements: 27 | - Vim 7.2 or later 28 | 29 | Latest version: 30 | http://github.com/thinca/vim-prettyprint 31 | 32 | 33 | 34 | ============================================================================== 35 | INTERFACE *prettyprint-interface* 36 | 37 | ------------------------------------------------------------------------------ 38 | COMMANDS *prettyprint-commands* 39 | 40 | :PrettyPrint[!] {expr} [, ...] *:PrettyPrint* 41 | The command version of |PrettyPrint()|. When two or 42 | more values are passed, it is necessary to delimit 43 | each value by the comma. If [!] is given, the output 44 | is saved to the |message-history| by using |:echomsg|. 45 | 46 | :PP[!] {expr} [, ...] *:PP* 47 | This is a short version of |:PrettyPrint|. 48 | 49 | ------------------------------------------------------------------------------ 50 | FUNCTIONS *prettyprint-functions* 51 | 52 | PrettyPrint({expr} [, ...]) *PrettyPrint()* 53 | Return {expr} converted to a formatted String. When 54 | two or more values are passed, it is delimited by 55 | "\n". 56 | 57 | PP({expr} [, ...]) *PP()* 58 | This is a short version of |PrettyPrint()|. 59 | 60 | 61 | 62 | ============================================================================== 63 | OPTIONS *prettyprint-options* 64 | 65 | g:prettyprint_indent *g:prettyprint_indent* 66 | b:prettyprint_indent *b:prettyprint_indent* 67 | Width of indent. If the type of value is String, |eval()| is applied. 68 | The default value is "shiftwidth()" or "&l:shiftwidth". 69 | 70 | g:prettyprint_width *g:prettyprint_width* 71 | b:prettyprint_width *b:prettyprint_width* 72 | Width by return. If the type of value is String, |eval()| is applied. 73 | The default value is "&columns". 74 | 75 | g:prettyprint_string *g:prettyprint_string* 76 | b:prettyprint_string *b:prettyprint_string* 77 | Format of string. This is a |List| of the following values. 78 | - "split" 79 | - "raw" 80 | See |prettyprint-format-rules-String| for details. 81 | The default value is [] (empty list). 82 | 83 | g:prettyprint_show_expression *g:prettyprint_show_expression* 84 | Show also expression not only evaluated value. 85 | e.g.: If this variable is true, 86 | ":PP 1 + 1" outputs "1 + 1 = 2". 87 | If false, it just outputs "2". 88 | 89 | 90 | ============================================================================== 91 | FORMAT RULES *prettyprint-format-rules* 92 | 93 | String *prettyprint-format-rules-String* 94 | See also |g:prettyprint_string|. 95 | By default, |strtrans()| and |string()| are sequentially used. 96 | If |g:prettyprint_string| contains "raw", don't use |strtrans()|. 97 | If |g:prettyprint_string| contains "split" and the string has two or 98 | more lines, it is displayed by the following styles. 99 | > 100 | join([ 101 | 'line1', 102 | 'line2' 103 | ], "\n") 104 | 105 | Number *prettyprint-format-rules-Number* 106 | Float *prettyprint-format-rules-Float* 107 | |string()| is used. 108 | 109 | List *prettyprint-format-rules-List* 110 | List is displayed by same style as |string()|. If the limit of 111 | width is exceeded, it is displayed by the following styles. 112 | > 113 | [ 114 | element1, 115 | element2, 116 | element3 117 | ] 118 | < 119 | If nested element is appear, it is displayed by the following styles. 120 | > 121 | [nested element {number}] 122 | < 123 | The {number} is count from top level. 124 | > 125 | let x = [10] 126 | let x += [x] 127 | PP x " => [10, [nested element 0]] 128 | < 129 | Dictionary *prettyprint-format-rules-Dictionary* 130 | Dictionary is displayed by same style as |string()|. If the limit of 131 | width is exceeded, it is displayed by the following styles. 132 | > 133 | { 134 | 'key1': value1, 135 | 'key2': value2, 136 | 'key3': value3 137 | } 138 | < 139 | The keys are sorted by |sort()|. 140 | If nested element is appear, it is displayed by same style as List, 141 | but it use brace instead of bracket. 142 | 143 | Funcref *prettyprint-format-rules-Funcref* 144 | Normally, |string()| is used. If 'verbose' is one, the definition of 145 | function is displayed by using |:function|. If 'verbose' is two or 146 | more, in addition, the file which the function was defined at last is 147 | displayed by |:function-verbose|. 148 | 149 | Partial *prettyprint-format-rules-Partial* 150 | Currently, the {arglist} and {self} are omitted. Only show the 151 | {partial} flag as below. 152 | > 153 | function('MyFunc', {partial}) 154 | < 155 | The option to show more detail may be added in the future. 156 | 157 | 158 | 159 | ============================================================================== 160 | LIMITATION *prettyprint-limitation* 161 | 162 | - Key of dictionary is displayed by |strtrans()|. 163 | 164 | - In internal, the recursive call is used. This is subject to the limitation 165 | of it. See 'maxfuncdepth'. 166 | 167 | - Can't access a script local variable/function via the command. 168 | - This is specification of Vim. Use |PP()| instead. 169 | > 170 | let s:var = 0 171 | function s:dump(var) 172 | let l:var = 0 173 | " OK 174 | PP a:var 175 | " OK 176 | PP l:var 177 | " NG 178 | PP s:var 179 | " OK 180 | echo PP(s:var) 181 | endfunction 182 | 183 | 184 | 185 | ============================================================================== 186 | CHANGELOG *prettyprint-changelog* 187 | 188 | 0.3.4 2016-03-24 189 | - Add Partial support. 190 | 191 | 0.3.3 2016-02-08 192 | - Autoloadize. 193 | - Fix for |g:prettyprint_string| option. 194 | - Fix default value of |g:prettyprint_width|. 195 | 196 | 0.3.2 2011-12-22 197 | - Added |g:prettyprint_string| option. 198 | - Improved Funcref support. 199 | 200 | 0.3.1 2010-09-07 201 | - Added the buffer local version of options. 202 | - |b:prettyprint_indent| 203 | - |b:prettyprint_width| 204 | - |:PrettyPrint| accepts "!" for saving the output. 205 | 206 | 0.3.0 2009-11-20 207 | - Changed the treatment of options. 208 | - Applies the eval() function if the type of value is String. 209 | - Improved the return algorithm. 210 | 211 | 212 | 0.2.2 2009-08-03 213 | - Fixed a bug that doesn't work if the values with different type is 214 | passed at a time. 215 | 216 | 0.2.1 2009-07-09 217 | - Fixed a bug that doesn't work if |Funcref| was put on other than the 218 | dictionary. 219 | 220 | 0.2.0 2009-06-25 221 | - Fix typos in this document. 222 | - Rename the plugin name. 223 | - dumper.vim -> prettyprint.vim 224 | 225 | 0.1.0 2009-06-24 226 | - Initial version. 227 | 228 | 229 | ============================================================================== 230 | vim:tw=78:fo=tcq2mM:ts=8:ft=help:norl 231 | -------------------------------------------------------------------------------- /plugin/prettyprint.vim: -------------------------------------------------------------------------------- 1 | " Prettyprint vim variables. 2 | " Version: 0.3.4 3 | " Author : thinca 4 | " License: zlib License 5 | 6 | if exists('g:loaded_prettyprint') 7 | finish 8 | endif 9 | let g:loaded_prettyprint = 1 10 | 11 | function! PrettyPrint(...) abort 12 | return call('prettyprint#prettyprint', a:000) 13 | endfunction 14 | 15 | function! PP(...) abort 16 | return call('prettyprint#prettyprint', a:000) 17 | endfunction 18 | 19 | " commands. {{{1 20 | command! -nargs=+ -bang -complete=expression PrettyPrint PP 21 | command! -nargs=+ -bang -complete=expression PP 22 | \ call prettyprint#echo(PP(), 0, ) 23 | --------------------------------------------------------------------------------