1. ZyX_I
  2. frawor

Commits

ZyX_I  committed 6d92630

@/resources: Added {copyfunc} argument to postresource

  • Participants
  • Parent commits 8b5bdfe
  • Branches default

Comments (0)

Files changed (8)

File doc/frawor.txt

View file
  • Ignore whitespace
             let s:_commands+=["Foo"]
 
 ------------------------------------------------------------------------------
-3.10. plugin/frawor/resources features              *frawor-f-frawor/resources*
+3.10. plugin/frawor/resources features             *frawor-f-frawor/resources*
 
-postresource : function ({rid}, {resource})            *frawor-f-postresource*
+                                                       *frawor-f-postresource*
+postresource : function ({rid}, {resource}[, {copyfunc}])
              + register, unload
         Create resource (|deepcopy()| of {resource}) with given {rid}. This 
         feature is now used only to provide access to plugin functions that 
-        don't have to know anything about a caller.
+        don't have to know anything about a caller. Additional argument 
+        determines what should be done when resource is requested:
+        1. If it is `0' (default), then resource is deepcopied (|deepcopy()|) 
+           for each plugin.
+        2. If it is a reference to a function, then {resource} is deepcopied 
+           when resource is created and on each request {copyfunc}({resource}) 
+           is used to obtain value which will actually be added to plugin 
+           dictionary.
+        3. If it is `1' then no copying is performed at all. Use this to save 
+           space if you are sharing large structures or need to give access to 
+           your plugin's state. Note that only |Lists| and |Dictionaries| are 
+           passed by references, so it won't have any affect when you share 
+           strings, numbers or function references.
 
 addresource : load + s:_r                               *frawor-f-addresource*
         Automatically adds resources that are defined by plugins listed in 

File plugin/frawor/resources.vim

View file
  • Ignore whitespace
                 \               'имя ресурсо пусто',
                 \  'ridexists': 'Ошибка создания ресурса %s дополнения %s: '.
                 \               'данный ресурс уже создан дополнением %s',
+                \  'invrcpfun': 'Ошибка создания ресурса %s дополнения %s: '.
+                \               'дополнительный аргумент неверен',
             \}
 else
     let s:_messages={
                 \  'ridexists': 'Error while posting resource %s '.
                 \               'for plugin %s: resource was already defined '.
                 \               'by plugin %s',
+                \  'invrcpfun': 'Error while posting resource %s '.
+                \               'for plugin %s: invalid copy function',
             \}
 endif
+"▶1 id
+function s:F.id(val)
+    return a:val
+endfunction
 "▶1 resource feature
-"▶2 postresource :: {f}, rid, resource → + s:postedress, s:plugresources
+"▶2 postresource :: {f}, rid, resource[, cpf] → + s:postedress, s:plugresources
 let s:plugresources={}
 let s:postedress={}
-function s:F.postresource(plugdict, fdict, rid, resource)
+function s:F.postresource(plugdict, fdict, rid, resource, ...)
     "▶3 Check arguments
-    if type(a:rid)!=type("")
+    if type(a:rid)!=type('')
         call s:_f.throw('ridnstr', a:plugdict.id)
     elseif empty(a:rid)
         call s:_f.throw('ridempty', a:plugdict.id)
     elseif has_key(s:postedress, a:rid)
         call s:_f.throw('ridexists', a:plugdict.id, a:rid,
                     \                s:postedress[a:rid].plid)
+    elseif a:0 && a:1 isnot 1 && a:1 isnot 0 && !exists('*a:1')
+        call s:_f.throw('invrcpfun')
     endif
     "▶3 Add fdict to plugresources
     if !has_key(s:plugresources, a:plugdict.id)
     endif
     "▲3
     let s:postedress[a:rid]={'id': a:rid,
-                \          'plid': a:plugdict.id,
-                \      'resource': deepcopy(a:resource),}
+                \          'plid': a:plugdict.id,}
+    if a:0 && a:1 isnot 0
+        if a:1 is 1
+            let s:postedress[a:rid].resource=a:resource
+            let s:postedress[a:rid].copyfunc=s:F.id
+        else
+            let s:postedress[a:rid].resource=deepcopy(a:resource)
+            let s:postedress[a:rid].copyfunc=a:1
+        endif
+    else
+        let s:postedress[a:rid].resource=deepcopy(a:resource)
+        let s:postedress[a:rid].copyfunc=function('deepcopy')
+    endif
     let a:fdict[a:rid]=s:postedress[a:rid]
 endfunction
 "▶2 delresources :: {f} → + s:postedress, s:plugresources
                 \'has_key(s:plugresources, v:val)'),
                 \'s:plugresources[v:val]'),
                 \'map(values(v:val), '.
-                \    '"extend(r, {v:val.id : deepcopy(v:val.resource)})")')
+                \    '"extend(r,{v:val.id : v:val.copyfunc(v:val.resource)})")')
     let a:plugdict.g._r=r
 endfunction
 call s:_f.newfeature('addresource', {'load': s:F.addresource, 'ignoredeps': 1,})

File test/invalid-postresource.ok

View file
  • Ignore whitespace
 .1: plugin/frawor/resources:ridnstr
 .2: plugin/frawor/resources:ridnstr
 .3: plugin/frawor/resources:ridexists
-.4: plugin/frawor/resources:ridempty
+.4: plugin/frawor/resources:ridempty
+.5: plugin/frawor/resources:invrcpfun
+.6: plugin/frawor/resources:invrcpfun

File test/postresource.ok

View file
  • Ignore whitespace
 3: {'single': {}}
 4: ['m1', 0] ['m2', 1] ['m3', 3]
 4: ['m1', 0] ['m2', 1] ['m3', 3]
+5: ['sd0', {}] ['sd1', {'def': 1}] ['sdf', {'abc': 1}]
+5: ['sd0', {}] ['sd1', {'def': 1}] ['sdf', {'abc': 1}]
 1: {}
 3: {'single': {}}
 2: {'single': {}}

File test/rtp/plugin/invalid-postresource.5.vim

View file
  • Ignore whitespace
+execute frawor#Setup('0.0', {'plugin/frawor/resources': '0.0'}, 1)
+call s:_f.postresource('r', {}, 2)
+

File test/rtp/plugin/invalid-postresource.6.vim

View file
  • Ignore whitespace
+execute frawor#Setup('0.0', {'plugin/frawor/resources': '0.0'}, 1)
+call s:_f.postresource('r', {}, function('s:Eval'))
+

File test/rtp/plugin/postresource-copyfunc.vim

View file
  • Ignore whitespace
+execute frawor#Setup('0.0', {'@/resources': '0.0'}, 1)
+let s:d={}
+call s:_f.postresource('sd0', s:d, 0)
+call s:_f.postresource('sd1', s:d, 1)
+let s:d.def=1
+call s:_f.postresource('sdf', '{"abc": 1}', function('eval'))
+

File test/rtp/plugin/postresource.5.vim

View file
  • Ignore whitespace
+execute frawor#Setup('0.0', {'plugin/postresource-copyfunc': '0.0'}, 1)
+call WriteFile(expand('<sfile>:t:r:e').': '.
+            \join(sort(map(items(s:_r), 'string(v:val)'))))