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(experiment): add lsp push diagnostic for MySQL contains syntax error only (#13580)
h3n4lae86eb046586
diff --git a/backend/api/lsp/fs_handler.go b/backend/api/lsp/fs_handler.goindex 028c77818a..4a77e7543d 100644--- a/backend/api/lsp/fs_handler.go+++ b/backend/api/lsp/fs_handler.go@@ -6,9 +6,15 @@ import ("encoding/json""os"+ "github.com/antlr4-go/antlr/v4""github.com/pkg/errors""github.com/sourcegraph/go-lsp""github.com/sourcegraph/jsonrpc2"++ parser "github.com/bytebase/mysql-parser"++ "github.com/bytebase/bytebase/backend/plugin/parser/base"+ "github.com/bytebase/bytebase/proto/generated-go/store")// GetFS returns the file system.@@ -31,7 +37,7 @@ func (h *Handler) readFile(_ context.Context, uri lsp.DocumentURI) ([]byte, erro}// handleFileSystemRequest handles textDocument/did* requests.-func (h *Handler) handleFileSystemRequest(ctx context.Context, req *jsonrpc2.Request) (lsp.DocumentURI, bool, error) {+func (h *Handler) handleFileSystemRequest(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2.Request) (lsp.DocumentURI, bool, error) {fs := h.GetFS()do := func(uri lsp.DocumentURI, op func() error) (lsp.DocumentURI, bool, error) {@@ -70,7 +76,26 @@ func (h *Handler) handleFileSystemRequest(ctx context.Context, req *jsonrpc2.Reqreturn "", false, err}return do(params.TextDocument.URI, func() error {- return fs.DidChange(¶ms)+ if err := fs.DidChange(¶ms); err != nil {+ return err+ }+ // Beta: Parse and send diagnostics for MySQL.+ if h.getEngineType(ctx) == store.Engine_MYSQL {+ uri := params.TextDocument.URI+ content, found := fs.get(uri)+ if !found {+ return &os.PathError{Op: "Open", Path: string(uri), Err: os.ErrNotExist}+ }+ err := parseMySQLStatement(string(content))+ diagnostics := collectDiagnostics(err)+ if err := conn.Notify(ctx, string(LSPMethodPublishDiagnostics), &lsp.PublishDiagnosticsParams{+ URI: uri,+ Diagnostics: diagnostics,+ }); err != nil {+ return err+ }+ }+ return nil})case LSPMethodTextDocumentDidClose:var params lsp.DidCloseTextDocumentParams@@ -92,3 +117,58 @@ func (h *Handler) handleFileSystemRequest(ctx context.Context, req *jsonrpc2.Reqreturn "", false, errors.Errorf("unknown file system request method: %q", req.Method)}}++func parseMySQLStatement(statement string) *base.SyntaxError {+ inputStream := antlr.NewInputStream(statement)+ lexer := parser.NewMySQLLexer(inputStream)+ stream := antlr.NewCommonTokenStream(lexer, antlr.TokenDefaultChannel)++ p := parser.NewMySQLParser(stream)+ lexerErrorListener := &base.ParseErrorListener{+ BaseLine: 0,+ }+ lexer.RemoveErrorListeners()+ lexer.AddErrorListener(lexerErrorListener)++ parserErrorListener := &base.ParseErrorListener{+ BaseLine: 0,+ }+ p.RemoveErrorListeners()+ p.AddErrorListener(parserErrorListener)++ p.BuildParseTrees = false++ _ = p.Script()++ if lexerErrorListener.Err != nil {+ return lexerErrorListener.Err+ }++ if parserErrorListener.Err != nil {+ return parserErrorListener.Err+ }+ return nil+}++func collectDiagnostics(err *base.SyntaxError) []lsp.Diagnostic {+ if err == nil {+ return []lsp.Diagnostic{}+ }+ syntaxDiagnostic := lsp.Diagnostic{+ Range: lsp.Range{+ Start: lsp.Position{+ Line: err.Line,+ Character: err.Column,+ },+ End: lsp.Position{+ Line: err.Line,+ Character: err.Column,+ },+ },+ Severity: lsp.Error,+ Source: "Syntax check",+ Message: err.Message,+ }++ return []lsp.Diagnostic{syntaxDiagnostic}+}diff --git a/backend/api/lsp/handler.go b/backend/api/lsp/handler.goindex b273b18e7e..03d8355827 100644--- a/backend/api/lsp/handler.go+++ b/backend/api/lsp/handler.go@@ -34,6 +34,9 @@ const (LSPMethodTextDocumentDidChange Method = "textDocument/didChange"LSPMethodTextDocumentDidClose Method = "textDocument/didClose"LSPMethodTextDocumentDidSave Method = "textDocument/didSave"++ // See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_publishDiagnostics.+ LSPMethodPublishDiagnostics Method = "textDocument/publishDiagnostics")// NewHandler creates a new Language Server Protocol handler.@@ -263,7 +266,7 @@ func (h *Handler) handle(ctx context.Context, conn *jsonrpc2.Conn, req *jsonrpc2return h.handleTextDocumentCompletion(ctx, conn, req, params)default:if isFileSystemRequest(req.Method) {- _, _, err := h.handleFileSystemRequest(ctx, req)+ _, _, err := h.handleFileSystemRequest(ctx, conn, req)return nil, err}return nil, &jsonrpc2.Error{Code: jsonrpc2.CodeMethodNotFound, Message: fmt.Sprintf("method not supported: %s", req.Method)}