github.com/kolbycrouch/elvish@v0.14.1-0.20210614162631-215b9ac1c423/pkg/edit/ns_helper.go (about)

     1  package edit
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  
     7  	"src.elv.sh/pkg/eval"
     8  	"src.elv.sh/pkg/fsutil"
     9  )
    10  
    11  // Calls the passed function for each variable name in namespace ns that can be
    12  // found from the top context.
    13  func eachVariableInTop(builtin, global *eval.Ns, ns string, f func(s string)) {
    14  	switch ns {
    15  	case "", ":":
    16  		global.IterateNames(f)
    17  		builtin.IterateNames(f)
    18  	case "e:":
    19  		fsutil.EachExternal(func(cmd string) {
    20  			f(cmd + eval.FnSuffix)
    21  		})
    22  	case "E:":
    23  		for _, s := range os.Environ() {
    24  			if i := strings.IndexByte(s, '='); i > 0 {
    25  				f(s[:i])
    26  			}
    27  		}
    28  	default:
    29  		segs := eval.SplitQNameSegs(ns)
    30  		mod := global.IndexName(segs[0])
    31  		if mod == nil {
    32  			mod = builtin.IndexName(segs[0])
    33  		}
    34  		for _, seg := range segs[1:] {
    35  			if mod == nil {
    36  				return
    37  			}
    38  			mod = mod.Get().(*eval.Ns).IndexName(seg)
    39  		}
    40  		if mod != nil {
    41  			mod.Get().(*eval.Ns).IterateNames(f)
    42  		}
    43  	}
    44  }
    45  
    46  // Calls the passed function for each namespace that can be used from the top
    47  // context.
    48  func eachNsInTop(builtin, global *eval.Ns, f func(s string)) {
    49  	f("e:")
    50  	f("E:")
    51  
    52  	global.IterateNames(func(name string) {
    53  		if strings.HasSuffix(name, eval.NsSuffix) {
    54  			f(name)
    55  		}
    56  	})
    57  
    58  	builtin.IterateNames(func(name string) {
    59  		if strings.HasSuffix(name, eval.NsSuffix) {
    60  			f(name)
    61  		}
    62  	})
    63  }