edit: move utils for evaluation of nodes into nodeutil.go

This commit is contained in:
Qi Xiao 2016-01-25 22:47:43 +01:00
parent 52d3565af1
commit ea2b7e8b92
2 changed files with 50 additions and 47 deletions

View File

@ -43,53 +43,6 @@ func init() {
builtins = append(builtins, eval.BuiltinSpecialNames...)
}
func formHead(n parse.Node) (parse.Node, string) {
if _, ok := n.(*parse.Chunk); ok {
return n, ""
}
if primary, ok := n.(*parse.Primary); ok {
compound, head := simpleCompound(primary)
if form, ok := compound.Parent().(*parse.Form); ok {
if form.Head == compound {
return compound, head
}
}
}
return nil, ""
}
func simpleCompound(pn *parse.Primary) (*parse.Compound, string) {
thisIndexed, ok := pn.Parent().(*parse.Indexed)
if !ok {
return nil, ""
}
thisCompound, ok := thisIndexed.Parent().(*parse.Compound)
if !ok {
return nil, ""
}
head := ""
for _, in := range thisCompound.Indexeds {
if len(in.Indicies) > 0 {
return nil, ""
}
typ := in.Head.Type
if typ != parse.Bareword &&
typ != parse.SingleQuoted &&
typ != parse.DoubleQuoted {
return nil, ""
}
head += in.Head.Value
if in == thisIndexed {
break
}
}
return thisCompound, head
}
func complArg(n parse.Node, ed *Editor) ([]*candidate, int) {
pn, ok := n.(*parse.Primary)
if !ok {

50
edit/nodeutil.go Normal file
View File

@ -0,0 +1,50 @@
package edit
import "github.com/elves/elvish/parse"
func formHead(n parse.Node) (parse.Node, string) {
if _, ok := n.(*parse.Chunk); ok {
return n, ""
}
if primary, ok := n.(*parse.Primary); ok {
compound, head := simpleCompound(primary)
if form, ok := compound.Parent().(*parse.Form); ok {
if form.Head == compound {
return compound, head
}
}
}
return nil, ""
}
func simpleCompound(pn *parse.Primary) (*parse.Compound, string) {
thisIndexed, ok := pn.Parent().(*parse.Indexed)
if !ok {
return nil, ""
}
thisCompound, ok := thisIndexed.Parent().(*parse.Compound)
if !ok {
return nil, ""
}
head := ""
for _, in := range thisCompound.Indexeds {
if len(in.Indicies) > 0 {
return nil, ""
}
typ := in.Head.Type
if typ != parse.Bareword &&
typ != parse.SingleQuoted &&
typ != parse.DoubleQuoted {
return nil, ""
}
head += in.Head.Value
if in == thisIndexed {
break
}
}
return thisCompound, head
}