Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
feat: impl code complete cancellation (#16378)
p0nyaa64ef17d077
diff --git a/backend/api/lsp/handler.go b/backend/api/lsp/handler.goindex 6b58858efb..78225af436 100644--- a/backend/api/lsp/handler.go+++ b/backend/api/lsp/handler.go@@ -72,6 +72,7 @@ type Handler struct {shutDown boolprofile *config.Profile+ cancelF sync.Map // map[jsonrpc2.ID]context.CancelFunc}// ShutDown shuts down the handler.@@ -245,7 +246,28 @@ func (h *Handler) handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2h.ShutDown()return nil, nilcase LSPMethodCancelRequest:- // Do nothing for now.+ var params lsp.CancelParams+ if err := json.Unmarshal(*req.Params, ¶ms); err != nil {+ return nil, errors.Wrapf(err, "failed to unmarshal cancel request")+ }+ var id jsonrpc2.ID+ if v, ok := params.ID.(string); ok {+ id = jsonrpc2.ID{+ Str: v,+ IsString: true,+ }+ } else if v, ok := params.ID.(float64); ok {+ // handle json number+ id = jsonrpc2.ID{+ Num: uint64(v),+ }+ }+ cancelFAny, loaded := h.cancelF.LoadAndDelete(id)+ if loaded {+ if cancelF, ok := cancelFAny.(context.CancelFunc); ok {+ cancelF()+ }+ }return nil, nilcase LSPMethodSetTrace:// Do nothing for now.@@ -281,7 +303,13 @@ func (h *Handler) handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2if err := json.Unmarshal(*req.Params, ¶ms); err != nil {return nil, err}- return h.handleTextDocumentCompletion(ctx, conn, req, params)+ childCtx, cancel := context.WithCancel(ctx)+ h.cancelF.Store(req.ID, cancel)+ defer func() {+ cancel()+ h.cancelF.Delete(req.ID)+ }()+ return h.handleTextDocumentCompletion(childCtx, conn, req, params)default:if isFileSystemRequest(req.Method) {_, _, err := h.handleFileSystemRequest(ctx, conn, req)diff --git a/backend/plugin/parser/base/c3.go b/backend/plugin/parser/base/c3.goindex 8e8c511172..2468fdcb52 100644--- a/backend/plugin/parser/base/c3.go+++ b/backend/plugin/parser/base/c3.go@@ -1,6 +1,7 @@package baseimport (+ "context""sync""github.com/antlr4-go/antlr/v4"@@ -36,6 +37,9 @@ type CodeCompletionCore struct {callStack *RuleListlastQueryRuleContext *RuleContextlastShadowQueryRuleContext *RuleContext++ // Context for cancellation support+ ctx context.Context}type Token struct {@@ -45,6 +49,7 @@ type Token struct {// NewCodeCompletionCore creates a new CodeCompletionCore.func NewCodeCompletionCore(+ ctx context.Context,parser antlr.Parser,ignoredTokens,preferredRules map[int]bool,@@ -64,6 +69,7 @@ func NewCodeCompletionCore(parser: parser,atn: parser.GetATN(),followSetsByState: followSets,+ ctx: ctx,}}@@ -394,6 +400,13 @@ func (c *CodeCompletionCore) CollectCandidates(caretTokenIndex int, context antl}func (c *CodeCompletionCore) fetchEndStatus(startState antlr.ATNState, tokenIndex int, indentation string) RuleEndStatus {+ // Check for context cancellation+ select {+ case <-c.ctx.Done():+ return RuleEndStatus{}+ default:+ }+if len(c.callStack.rules) > maxRecursionDepth {return RuleEndStatus{}}@@ -476,6 +489,14 @@ func (c *CodeCompletionCore) fetchEndStatus(startState antlr.ATNState, tokenInde})for len(statePipeline) != 0 {+ // Check for context cancellation in the main loop+ select {+ case <-c.ctx.Done():+ c.callStack.Pop()+ return RuleEndStatus{}+ default:+ }+currentEntry = statePipeline[len(statePipeline)-1]statePipeline = statePipeline[:len(statePipeline)-1]c.statesProcessed++diff --git a/backend/plugin/parser/mysql/completion.go b/backend/plugin/parser/mysql/completion.goindex 72feb6a32f..5f9ea063c4 100644--- a/backend/plugin/parser/mysql/completion.go+++ b/backend/plugin/parser/mysql/completion.go@@ -239,6 +239,7 @@ func NewStandardCompleter(ctx context.Context, cCtx base.CompletionContext, stat// For all MySQL completers, we use one global follow sets by state.// The FollowSetsByState is the thread-safe struct.core := base.NewCodeCompletionCore(+ ctx,parser,newIgnoredTokens(),newPreferredRules(),@@ -270,6 +271,7 @@ func NewTrickyCompleter(ctx context.Context, cCtx base.CompletionContext, statem// For all MySQL completers, we use one global follow sets by state.// The FollowSetsByState is the thread-safe struct.core := base.NewCodeCompletionCore(+ ctx,parser,newIgnoredTokens(),newPreferredRules(),diff --git a/backend/plugin/parser/partiql/completion.go b/backend/plugin/parser/partiql/completion.goindex 9b745a26aa..398064ab01 100644--- a/backend/plugin/parser/partiql/completion.go+++ b/backend/plugin/parser/partiql/completion.go@@ -207,6 +207,7 @@ func Completion(ctx context.Context, cCtx base.CompletionContext, statement strifunc NewTrickyCompleter(ctx context.Context, cCtx base.CompletionContext, statement string, caretLine int, caretOffset int) *Completer {parser, lexer, scanner := prepareTrickyParserAndScanner(statement, caretLine, caretOffset)core := base.NewCodeCompletionCore(+ ctx,parser,ignoredTokens, /* IgnoredTokens */preferredRules, /* PreferredRules */@@ -235,6 +236,7 @@ func NewTrickyCompleter(ctx context.Context, cCtx base.CompletionContext, statemfunc NewStandardCompleter(ctx context.Context, cCtx base.CompletionContext, statement string, caretLine int, caretOffset int) *Completer {parser, lexer, scanner := prepareParserAndScanner(statement, caretLine, caretOffset)core := base.NewCodeCompletionCore(+ ctx,parser,ignoredTokens, /* IgnoredTokens */preferredRules, /* PreferredRules */diff --git a/backend/plugin/parser/pg/completion.go b/backend/plugin/parser/pg/completion.goindex cb5ea9e6e6..2576acaf00 100644--- a/backend/plugin/parser/pg/completion.go+++ b/backend/plugin/parser/pg/completion.go@@ -176,6 +176,7 @@ func NewTrickyCompleter(ctx context.Context, cCtx base.CompletionContext, statem// For all PostgreSQL completers, we use one global follow sets by state.// The FollowSetsByState is the thread-safe struct.core := base.NewCodeCompletionCore(+ ctx,parser,newIgnoredTokens(),newPreferredRules(),@@ -211,6 +212,7 @@ func NewStandardCompleter(ctx context.Context, cCtx base.CompletionContext, stat// For all PostgreSQL completers, we use one global follow sets by state.// The FollowSetsByState is the thread-safe struct.core := base.NewCodeCompletionCore(+ ctx,parser,newIgnoredTokens(),newPreferredRules(),diff --git a/backend/plugin/parser/plsql/completion.go b/backend/plugin/parser/plsql/completion.goindex cd6c0edf1f..c99bfb6995 100644--- a/backend/plugin/parser/plsql/completion.go+++ b/backend/plugin/parser/plsql/completion.go@@ -186,6 +186,7 @@ type Completer struct {func NewTrickyCompleter(ctx context.Context, cCtx base.CompletionContext, statement string, caretLine int, caretOffset int) *Completer {parser, lexer, scanner := prepareTrickyParserAndScanner(statement, caretLine, caretOffset)core := base.NewCodeCompletionCore(+ ctx,parser,newIgnoredTokens(),newPreferredRules(),@@ -215,6 +216,7 @@ func NewTrickyCompleter(ctx context.Context, cCtx base.CompletionContext, statemfunc NewStandardCompleter(ctx context.Context, cCtx base.CompletionContext, statement string, caretLine int, caretOffset int) *Completer {parser, lexer, scanner := prepareParserAndScanner(statement, caretLine, caretOffset)core := base.NewCodeCompletionCore(+ ctx,parser,newIgnoredTokens(),newPreferredRules(),diff --git a/backend/plugin/parser/trino/completion.go b/backend/plugin/parser/trino/completion.goindex b5cf0b3dcd..52508a276f 100644--- a/backend/plugin/parser/trino/completion.go+++ b/backend/plugin/parser/trino/completion.go@@ -482,6 +482,7 @@ func Completion(ctx context.Context, cCtx base.CompletionContext, statement strifunc NewStandardCompleter(ctx context.Context, cCtx base.CompletionContext, statement string, caretLine int, caretOffset int) *Completer {parser, lexer, scanner := prepareParserAndScanner(statement, caretLine, caretOffset)core := base.NewCodeCompletionCore(+ ctx,parser,ignoredTokens, /* IgnoredTokens */preferredRules, /* PreferredRules */@@ -520,6 +521,7 @@ func NewStandardCompleter(ctx context.Context, cCtx base.CompletionContext, statfunc NewTrickyCompleter(ctx context.Context, cCtx base.CompletionContext, statement string, caretLine int, caretOffset int) *Completer {parser, lexer, scanner := prepareTrickyParserAndScanner(statement, caretLine, caretOffset)core := base.NewCodeCompletionCore(+ ctx,parser,ignoredTokens, /* IgnoredTokens */preferredRules, /* PreferredRules */diff --git a/backend/plugin/parser/tsql/completion.go b/backend/plugin/parser/tsql/completion.goindex 49c07a18a1..b2013e2125 100644--- a/backend/plugin/parser/tsql/completion.go+++ b/backend/plugin/parser/tsql/completion.go@@ -527,6 +527,7 @@ func Completion(ctx context.Context, cCtx base.CompletionContext, statement strifunc NewStandardCompleter(ctx context.Context, cCtx base.CompletionContext, statement string, caretLine int, caretOffset int) *Completer {parser, lexer, scanner := prepareParserAndScanner(statement, caretLine, caretOffset)core := base.NewCodeCompletionCore(+ ctx,parser,ignoredTokens, /* IgnoredTokens */preferredRules, /* PreferredRules */@@ -557,6 +558,7 @@ func NewStandardCompleter(ctx context.Context, cCtx base.CompletionContext, statfunc NewTrickyCompleter(ctx context.Context, cCtx base.CompletionContext, statement string, caretLine int, caretOffset int) *Completer {parser, lexer, scanner := prepareTrickyParserAndScanner(statement, caretLine, caretOffset)core := base.NewCodeCompletionCore(+ ctx,parser,ignoredTokens, /* IgnoredTokens */preferredRules, /* PreferredRules */