From afbdf644bf5d3bde950551be4ae24cd5a3ceaf02 Mon Sep 17 00:00:00 2001 From: sawka Date: Thu, 10 Nov 2022 16:00:51 -0800 Subject: [PATCH] checkpoint on comp --- pkg/cmdrunner/cmdrunner.go | 4 +-- pkg/comp/comp.go | 53 ++++++++++++++++++++++++-------------- pkg/comp/comp_test.go | 26 ++++--------------- pkg/comp/simplecomp.go | 16 ++++++------ 4 files changed, 49 insertions(+), 50 deletions(-) diff --git a/pkg/cmdrunner/cmdrunner.go b/pkg/cmdrunner/cmdrunner.go index ac9041b3..71d9aef4 100644 --- a/pkg/cmdrunner/cmdrunner.go +++ b/pkg/cmdrunner/cmdrunner.go @@ -35,8 +35,8 @@ const ( ) func init() { - comp.RegisterSimpleCompFn("meta", simpleCompMeta) - comp.RegisterSimpleCompFn("command+meta", simpleCompCommandMeta) + comp.RegisterSimpleCompFn(comp.CGTypeMeta, simpleCompMeta) + comp.RegisterSimpleCompFn(comp.CGTypeCommandMeta, simpleCompCommandMeta) } const DefaultUserId = "sawka" diff --git a/pkg/comp/comp.go b/pkg/comp/comp.go index 0e29d0e5..7714c474 100644 --- a/pkg/comp/comp.go +++ b/pkg/comp/comp.go @@ -20,14 +20,19 @@ import ( const MaxCompQuoteLen = 5000 const ( - SimpleCompGenTypeFile = "file" - SimpleCompGenTypeDir = "dir" - SimpleCompGenTypeCommand = "command" - SimpleCompGenTypeRemote = "remote" - SimpleCompGenTypeRemoteInstance = "remoteinstance" - SimpleCompGenTypeMetaCmd = "metacmd" - SimpleCompGenTypeGlobalCmd = "globalcmd" - SimpleCompGenTypeVariable = "variable" + // local to simplecomp + CGTypeCommand = "command" + CGTypeFile = "file" + CGTypeDir = "directory" + CGTypeVariable = "variable" + + // implemented in cmdrunner + CGTypeMeta = "metacmd" + CGTypeCommandMeta = "command+meta" + + CGTypeRemote = "remote" + CGTypeRemoteInstance = "remoteinstance" + CGTypeGlobalCmd = "globalcmd" ) const ( @@ -48,13 +53,6 @@ type StrWithPos struct { Pos int } -type fullCompPrefix struct { - RawStr string - RawPos int - CompPrefix string - QuoteTypePref string -} - type ParsedWord struct { Offset int Word *syntax.Word @@ -285,7 +283,9 @@ func splitInitialWhitespace(str string) (string, string) { 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("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) { @@ -387,8 +387,23 @@ func splitCompWord(p *CompPoint) { p.Words = newWords } -func DoCompGen(ctx context.Context, point CompPoint, rptr sstore.RemotePtrType, state *packet.ShellState) (*CompReturn, error) { - return nil, nil +func DoCompGen(ctx context.Context, sp StrWithPos, compCtx CompContext) (*CompReturn, *StrWithPos, error) { + 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) { diff --git a/pkg/comp/comp_test.go b/pkg/comp/comp_test.go index 0d01342f..31b4ed75 100644 --- a/pkg/comp/comp_test.go +++ b/pkg/comp/comp_test.go @@ -16,11 +16,7 @@ func parseToSP(s string) StrWithPos { func testParse(cmdStr string, pos int) { fmt.Printf("cmd: %s\n", strWithCursor(cmdStr, pos)) - p, err := ParseCompPoint(cmdStr, pos) - if err != nil { - fmt.Printf("err: %v\n", err) - return - } + p := ParseCompPoint(StrWithPos{Str: cmdStr, Pos: pos}) p.dump() } @@ -52,19 +48,13 @@ func testMiniExtend(t *testing.T, p *CompPoint, newWord string, complete bool, e } func Test2(t *testing.T) { - p, err := ParseCompPoint("ls f", 4) - if err != nil { - t.Fatalf("error: %v", err) - } + p := ParseCompPoint(parseToSP("ls f[*]")) testMiniExtend(t, p, "foo", false, "foo[*]") testMiniExtend(t, p, "foo", true, "foo [*]") testMiniExtend(t, p, "foo bar", true, "'foo bar' [*]") testMiniExtend(t, p, "foo'bar", true, `$'foo\'bar' [*]`) - p, err = ParseCompPoint("ls fmore", 4) - if err != nil { - t.Fatalf("error: %v", err) - } + p = ParseCompPoint(parseToSP("ls f[*]more")) testMiniExtend(t, p, "foo", false, "foo[*]more") testMiniExtend(t, p, "foo bar", false, `'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) { - p, err := ParseCompPoint(origSP.Str, origSP.Pos) - if err != nil { - t.Fatalf("error: %v", err) - } + p := ParseCompPoint(origSP) newSP := StrWithPos{Str: p.getOrigStr(), Pos: p.getOrigPos()} if 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) { origSP := parseToSP(origStr) expectedSP := parseToSP(expectedStr) - p, err := ParseCompPoint(origSP.Str, origSP.Pos) - if err != nil { - t.Fatalf("error: %v", err) - } + p := ParseCompPoint(origSP) crtn := compsToCompReturn(compStrs, false) newSP := p.FullyExtend(crtn) if newSP != expectedSP { diff --git a/pkg/comp/simplecomp.go b/pkg/comp/simplecomp.go index ddae3f46..bdc0c5c4 100644 --- a/pkg/comp/simplecomp.go +++ b/pkg/comp/simplecomp.go @@ -13,10 +13,10 @@ import ( var globalLock = &sync.Mutex{} var simpleCompMap = map[string]SimpleCompGenFnType{ - "file": simpleCompFile, - "directory": simpleCompDir, - "variable": simpleCompVar, - "command": simpleCompCommand, + CGTypeCommand: simpleCompCommand, + CGTypeFile: simpleCompFile, + CGTypeDir: simpleCompDir, + CGTypeVariable: simpleCompVar, } 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) { - 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) { - 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) { - 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) { - return doCompGen(ctx, prefix, "command", compCtx) + return doCompGen(ctx, prefix, CGTypeCommand, compCtx) }