github.com/u-root/u-root@v7.0.1-0.20200915234505-ad7babab0a8e+incompatible/cmds/core/elvish/edit/highlight.go (about)

     1  package edit
     2  
     3  import (
     4  	"os"
     5  
     6  	"github.com/u-root/u-root/cmds/core/elvish/edit/highlight"
     7  	"github.com/u-root/u-root/cmds/core/elvish/eval"
     8  	"github.com/u-root/u-root/cmds/core/elvish/parse"
     9  	"github.com/u-root/u-root/cmds/core/elvish/util"
    10  )
    11  
    12  func doHighlight(n parse.Node, ed *editor) {
    13  	s := &highlight.Emitter{
    14  		func(s string) bool { return goodFormHead(s, ed) },
    15  		ed.styling.Add,
    16  	}
    17  	s.EmitAll(n)
    18  }
    19  
    20  func goodFormHead(head string, ed *editor) bool {
    21  	if eval.IsBuiltinSpecial[head] {
    22  		return true
    23  	} else if util.DontSearch(head) {
    24  		// XXX don't stat twice
    25  		return util.IsExecutable(head) || isDir(head)
    26  	} else {
    27  		ev := ed.evaler
    28  		explode, ns, name := eval.ParseVariableRef(head)
    29  		if !explode {
    30  			switch ns {
    31  			case "":
    32  				if ev.Builtin[name+eval.FnSuffix] != nil || ev.Global[name+eval.FnSuffix] != nil {
    33  					return true
    34  				}
    35  			case "e":
    36  				if ed.isExternal[name] {
    37  					return true
    38  				}
    39  			default:
    40  				mod := ev.Global[ns+eval.NsSuffix]
    41  				if mod == nil {
    42  					mod = ev.Builtin[ns+eval.NsSuffix]
    43  				}
    44  				if mod != nil && mod.Get().(eval.Ns)[name+eval.FnSuffix] != nil {
    45  					return true
    46  				}
    47  			}
    48  		}
    49  		return ed.isExternal[head]
    50  	}
    51  }
    52  
    53  func isDir(fname string) bool {
    54  	stat, err := os.Stat(fname)
    55  	return err == nil && stat.IsDir()
    56  }