checkpoint on comp

This commit is contained in:
sawka
2022-11-10 16:00:51 -08:00
parent f81bea6658
commit afbdf644bf
4 changed files with 49 additions and 50 deletions
+2 -2
View File
@@ -35,8 +35,8 @@ const (
) )
func init() { func init() {
comp.RegisterSimpleCompFn("meta", simpleCompMeta) comp.RegisterSimpleCompFn(comp.CGTypeMeta, simpleCompMeta)
comp.RegisterSimpleCompFn("command+meta", simpleCompCommandMeta) comp.RegisterSimpleCompFn(comp.CGTypeCommandMeta, simpleCompCommandMeta)
} }
const DefaultUserId = "sawka" const DefaultUserId = "sawka"
+34 -19
View File
@@ -20,14 +20,19 @@ import (
const MaxCompQuoteLen = 5000 const MaxCompQuoteLen = 5000
const ( const (
SimpleCompGenTypeFile = "file" // local to simplecomp
SimpleCompGenTypeDir = "dir" CGTypeCommand = "command"
SimpleCompGenTypeCommand = "command" CGTypeFile = "file"
SimpleCompGenTypeRemote = "remote" CGTypeDir = "directory"
SimpleCompGenTypeRemoteInstance = "remoteinstance" CGTypeVariable = "variable"
SimpleCompGenTypeMetaCmd = "metacmd"
SimpleCompGenTypeGlobalCmd = "globalcmd" // implemented in cmdrunner
SimpleCompGenTypeVariable = "variable" CGTypeMeta = "metacmd"
CGTypeCommandMeta = "command+meta"
CGTypeRemote = "remote"
CGTypeRemoteInstance = "remoteinstance"
CGTypeGlobalCmd = "globalcmd"
) )
const ( const (
@@ -48,13 +53,6 @@ type StrWithPos struct {
Pos int Pos int
} }
type fullCompPrefix struct {
RawStr string
RawPos int
CompPrefix string
QuoteTypePref string
}
type ParsedWord struct { type ParsedWord struct {
Offset int Offset int
Word *syntax.Word Word *syntax.Word
@@ -285,7 +283,9 @@ func splitInitialWhitespace(str string) (string, string) {
return str, "" return str, ""
} }
func ParseCompPoint(fullCmdStr string, pos int) (*CompPoint, error) { func ParseCompPoint(cmdStr StrWithPos) *CompPoint {
fullCmdStr := cmdStr.Str
pos := cmdStr.Pos
// fmt.Printf("---\n") // fmt.Printf("---\n")
// fmt.Printf("cmd: %s\n", strWithCursor(fullCmdStr, pos)) // fmt.Printf("cmd: %s\n", strWithCursor(fullCmdStr, pos))
@@ -367,7 +367,7 @@ func ParseCompPoint(fullCmdStr string, pos int) (*CompPoint, error) {
} }
} }
} }
return &rtnPoint, nil return &rtnPoint
} }
func splitCompWord(p *CompPoint) { func splitCompWord(p *CompPoint) {
@@ -387,8 +387,23 @@ func splitCompWord(p *CompPoint) {
p.Words = newWords p.Words = newWords
} }
func DoCompGen(ctx context.Context, point CompPoint, rptr sstore.RemotePtrType, state *packet.ShellState) (*CompReturn, error) { func DoCompGen(ctx context.Context, sp StrWithPos, compCtx CompContext) (*CompReturn, *StrWithPos, error) {
return nil, nil compPoint := ParseCompPoint(sp)
compType := CGTypeFile
if compPoint.CompWord == 0 {
compType = CGTypeCommandMeta
}
// TODO lookup special types
compPrefix := compPoint.getCompPrefix()
crtn, err := DoSimpleComp(ctx, compType, compPrefix, compCtx, nil)
if err != nil {
return nil, nil, err
}
if compCtx.ForDisplay {
return crtn, nil, nil
}
rtnSP := compPoint.FullyExtend(crtn)
return crtn, &rtnSP, nil
} }
func SortCompReturnEntries(c *CompReturn) { func SortCompReturnEntries(c *CompReturn) {
+5 -21
View File
@@ -16,11 +16,7 @@ func parseToSP(s string) StrWithPos {
func testParse(cmdStr string, pos int) { func testParse(cmdStr string, pos int) {
fmt.Printf("cmd: %s\n", strWithCursor(cmdStr, pos)) fmt.Printf("cmd: %s\n", strWithCursor(cmdStr, pos))
p, err := ParseCompPoint(cmdStr, pos) p := ParseCompPoint(StrWithPos{Str: cmdStr, Pos: pos})
if err != nil {
fmt.Printf("err: %v\n", err)
return
}
p.dump() p.dump()
} }
@@ -52,19 +48,13 @@ func testMiniExtend(t *testing.T, p *CompPoint, newWord string, complete bool, e
} }
func Test2(t *testing.T) { func Test2(t *testing.T) {
p, err := ParseCompPoint("ls f", 4) p := ParseCompPoint(parseToSP("ls f[*]"))
if err != nil {
t.Fatalf("error: %v", err)
}
testMiniExtend(t, p, "foo", false, "foo[*]") testMiniExtend(t, p, "foo", false, "foo[*]")
testMiniExtend(t, p, "foo", true, "foo [*]") testMiniExtend(t, p, "foo", true, "foo [*]")
testMiniExtend(t, p, "foo bar", true, "'foo bar' [*]") testMiniExtend(t, p, "foo bar", true, "'foo bar' [*]")
testMiniExtend(t, p, "foo'bar", true, `$'foo\'bar' [*]`) testMiniExtend(t, p, "foo'bar", true, `$'foo\'bar' [*]`)
p, err = ParseCompPoint("ls fmore", 4) p = ParseCompPoint(parseToSP("ls f[*]more"))
if err != nil {
t.Fatalf("error: %v", err)
}
testMiniExtend(t, p, "foo", false, "foo[*]more") testMiniExtend(t, p, "foo", false, "foo[*]more")
testMiniExtend(t, p, "foo bar", false, `'foo bar[*]more`) testMiniExtend(t, p, "foo bar", false, `'foo bar[*]more`)
testMiniExtend(t, p, "foo bar", true, `'foo bar[*]more`) testMiniExtend(t, p, "foo bar", true, `'foo bar[*]more`)
@@ -72,10 +62,7 @@ func Test2(t *testing.T) {
} }
func testParseRT(t *testing.T, origSP StrWithPos) { func testParseRT(t *testing.T, origSP StrWithPos) {
p, err := ParseCompPoint(origSP.Str, origSP.Pos) p := ParseCompPoint(origSP)
if err != nil {
t.Fatalf("error: %v", err)
}
newSP := StrWithPos{Str: p.getOrigStr(), Pos: p.getOrigPos()} newSP := StrWithPos{Str: p.getOrigStr(), Pos: p.getOrigPos()}
if origSP != newSP { if origSP != newSP {
t.Fatalf("not equal: [%s] != [%s]", origSP, newSP) t.Fatalf("not equal: [%s] != [%s]", origSP, newSP)
@@ -92,10 +79,7 @@ func Test3(t *testing.T) {
func testExtend(t *testing.T, origStr string, compStrs []string, expectedStr string) { func testExtend(t *testing.T, origStr string, compStrs []string, expectedStr string) {
origSP := parseToSP(origStr) origSP := parseToSP(origStr)
expectedSP := parseToSP(expectedStr) expectedSP := parseToSP(expectedStr)
p, err := ParseCompPoint(origSP.Str, origSP.Pos) p := ParseCompPoint(origSP)
if err != nil {
t.Fatalf("error: %v", err)
}
crtn := compsToCompReturn(compStrs, false) crtn := compsToCompReturn(compStrs, false)
newSP := p.FullyExtend(crtn) newSP := p.FullyExtend(crtn)
if newSP != expectedSP { if newSP != expectedSP {
+8 -8
View File
@@ -13,10 +13,10 @@ import (
var globalLock = &sync.Mutex{} var globalLock = &sync.Mutex{}
var simpleCompMap = map[string]SimpleCompGenFnType{ var simpleCompMap = map[string]SimpleCompGenFnType{
"file": simpleCompFile, CGTypeCommand: simpleCompCommand,
"directory": simpleCompDir, CGTypeFile: simpleCompFile,
"variable": simpleCompVar, CGTypeDir: simpleCompDir,
"command": simpleCompCommand, CGTypeVariable: simpleCompVar,
} }
type SimpleCompGenFnType = func(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) type SimpleCompGenFnType = func(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error)
@@ -79,17 +79,17 @@ func doCompGen(ctx context.Context, prefix string, compType string, compCtx Comp
} }
func simpleCompFile(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) { func simpleCompFile(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) {
return doCompGen(ctx, prefix, "file", compCtx) return doCompGen(ctx, prefix, CGTypeFile, compCtx)
} }
func simpleCompDir(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) { func simpleCompDir(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) {
return doCompGen(ctx, prefix, "directory", compCtx) return doCompGen(ctx, prefix, CGTypeDir, compCtx)
} }
func simpleCompVar(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) { func simpleCompVar(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) {
return doCompGen(ctx, prefix, "variable", compCtx) return doCompGen(ctx, prefix, CGTypeVariable, compCtx)
} }
func simpleCompCommand(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) { func simpleCompCommand(ctx context.Context, prefix string, compCtx CompContext, args []interface{}) (*CompReturn, error) {
return doCompGen(ctx, prefix, "command", compCtx) return doCompGen(ctx, prefix, CGTypeCommand, compCtx)
} }