Commits

ZyX_I committed 66dab11

@/fwc/topconstructs: Improved {optional} section completion

Comments (0)

Files changed (3)

plugin/frawor/fwc/topconstructs.vim

 "   {lvars} :: nextsub, addedsavemsgs
 function s:r.optional.compile(adescr, idx, caidxstr, largsstr, purgemax, type,
             \                 nextsub, addedsavemsgs)
-    let addedsavemsgs=a:addedsavemsgs
     if a:type is 'complete'
-        let hasnext=s:F.hasnext(a:adescr, 'next')
+        let hasnext=s:F.hasnext(a:adescr, 'optional')
         if !hasnext && len(a:adescr.optional)==1
             call self.compadescr(a:adescr.optional[0], a:idx.'.0(optional)',
                         \        a:type, 0)
-            let oldsub=self.getsub(self.subs[-1])
+            let oldsub=self.getlastsub()
             if oldsub isnot a:caidxstr
                 call self.let(a:caidxstr, oldsub)
             endif
-            return [[a:caidxstr], addedsavemsgs]
+            return [[a:caidxstr], a:addedsavemsgs]
         else
-            " TODO
+            " XXX This implementation does not cover all cases. I won't fix this 
+            " because it is too complicated.
+            let failstr=self.getlvarid('fail')
+            call self.let(failstr, 1)
+            let i=0
+            if hasnext
+                for opt in a:adescr.optional
+                    call self.if(failstr)
+                                \.try()
+                                    \.pushms('throwignore')
+                                    \.compadescr(opt, a:idx.'.'.i.'(optional)',
+                                    \            'check', 0)
+                                    \.popms()
+                                    \.let(failstr, 0)
+                                    \.let(a:caidxstr, self.getlastsub())
+                                \.catch(s:cfreg)
+                            \.endif()
+                    let self.subs[-1]=[a:caidxstr]
+                    let i+=1
+                endfor
+                call self.if(failstr)
+            endif
+            let i=0
+            for opt in a:adescr.optional
+                let savedcaidxstr=self.getlvarid('savedcaidx')
+                call self.let(savedcaidxstr, a:caidxstr)
+                        \.compadescr(opt, a:idx.'.'.i.'(optional)', a:type, 0)
+                        \.let(a:caidxstr, savedcaidxstr)
+                let self.subs[-1]=[a:caidxstr]
+                let i+=1
+            endfor
+            if hasnext
+                call self.endif()
+            endif
+            return [[a:caidxstr], a:addedsavemsgs]
         endif
     endif
+    let addedsavemsgs=a:addedsavemsgs
     " XXX nodefs will be still 1 when compiling next adescr. It is intentional.
     let nodefs=empty(self.defvals)
     let lopt=len(a:adescr.optional)

test/fwccomplete.ok

 ::: Section <Different sections/{actions}>
 ::: Section <Different sections/{next}+{actions}>
 ::: Section <Different sections/{prefixes}+{next}>
+::: Section <Different sections/{optional}+{next}>
 <<< messages

test/fwccompletetests.dat

 
   @- -+
   =sort(keys(s:dict))
+
+`[in list  in list2] [key dict]
+  @+
+  =s:list+sort(keys(s:dict))
+
+  @a+
+  =s:list2
 #▶2 {prefixes}
 `{   abc  in list
 +    def  in list2
 
   @d
   def-b
+#▶2 {optional}+{next}
+`[in list] + in list2
+  @+
+  =s:list+s:list2
+
+  @abc+
+  =s:list2
 # vim: cms=#%s fmr=▶,▲ sw=2 ts=2 sts=2 et