Commits

ZyX_I  committed 073b324

@aurum/log/templates: Made generated function look better and be a bit more optimal

  • Participants
  • Parent commits 6e0b200

Comments (0)

Files changed (1)

File plugin/aurum/log/templates.vim

                 \             mapexpr)
     return a:special
 endfunction
+"▶1 strappend :: func, String → + func
+let s:setlstrstr='let lstr=remove(text, -1)'
+let s:addexpr='((eval(submatch(1))!=0)?'.
+            \           '(printf("%+i", eval(submatch(1)))):'.
+            \           '(""))'
+function s:F.strappend(func, s)
+    let func=a:func
+    if func[-1][:13] is# 'let text[-1].='
+        let func[-1].='.'.a:s
+    elseif func[-1][:10] is# 'let text+=['
+        let func[-1]=func[-1][:-2].'.'.a:s.']'
+    else
+        let i=len(func)
+        let spsets=[]
+        while i
+            let i-=1
+            if func[i] is# s:setlstrstr || func[i][:1] is# 'if'
+                break
+            elseif func[i][:11] is# 'let special.'
+                let spsets+=[i-1]
+            elseif func[i] =~# '\V\^let text+=[''\v%([^'']|\''\'')*\V'']'
+                let str=remove(func, i)[11:-2]
+                let lstr=len(eval(str))
+                for i in spsets
+                    let func[i]=substitute(substitute(substitute(substitute(
+                                \func[i],
+                                \'\Vlen(text[-1])', lstr,  'g'),
+                                \'\v\ (\d+%([+\-]\d+)+)',
+                                \       '\='.tr(s:addexpr, '+', '1'),
+                                \                          'g'),
+                                \'\Vlen(text)',     '&+1', 'g'),
+                                \'\Vlen(text)\v(%([+\-]\d+)+)',
+                                \       '\="len(text)".'.s:addexpr,
+                                \                          'g')
+                endfor
+                let func+=['let text+=['.str.'.'.a:s.']']
+                return
+            endif
+        endwhile
+        let func+=['let text[-1].='.a:s]
+    endif
+endfunction
 "▶1 compile :: template, opts → Fref
 let s:add={}
 "▶2 add.ke2 : Add complex multiline statement
 function s:add.ke2(addedif, expr, kw, arg, func)
     let addedif2=0
     let func=a:func
-    let func+=['let lstr=remove(text, -1)']
+    let func+=[s:setlstrstr]
     "▶3 Add missing if’s
     if !a:addedif
         if a:kw is# 'stat'
 function s:add.ke1(addedif, expr, kw, arg, func)
     let addedif2=0
     let func=a:func
-    let func+=['let lstr=remove(text, -1)']
+    let func+=[s:setlstrstr]
     "▶3 Add missing if’s
     if !a:addedif && a:kw is# 'patch'
         let addedif2=1
     "▲3
     let func+=['let special.'.a:kw.'_r='.
                 \  '[[len(text)-1, len(text[-1])], '.
-                \   '[len(text)-1, len(text[-1])+len(estr)-1]]',
-                \'let text[-1].=estr',]
+                \   '[len(text)-1, len(text[-1])-1+len(estr)]]',]
+    call s:F.strappend(func, 'estr')
     if add2if
         let func+=['endif']
     endif
                 if i==0
                     let func[-1]='let text+=['.string(str).']'
                 else
-                    let func+=['let text[-1].='.string(str)]
+                    call s:F.strappend(func, string(str))
                 endif
             endif
             if lmeta>i