MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / handleFormatting

Method handleFormatting

pkg/lsp/handler.go:729–771  ·  view source on GitHub ↗

handleFormatting formats the SQL document with intelligent indentation. This handler provides SQL code formatting to improve readability and maintain consistent style across SQL files. The formatter applies intelligent rules for clause alignment and keyword positioning. Formatting Features: - Keyw

(params json.RawMessage)

Source from the content-addressed store, hash-verified

727// - Empty array if formatting produces no changes
728// - Error if document not found or formatting fails
729func (h *Handler) handleFormatting(params json.RawMessage) ([]TextEdit, error) {
730 var p DocumentFormattingParams
731 if err := json.Unmarshal(params, &p); err != nil {
732 return nil, err
733 }
734
735 content, ok := h.server.Documents().GetContent(p.TextDocument.URI)
736 if !ok {
737 return nil, nil
738 }
739
740 // Format the SQL using the full AST-based formatter
741 opts := gosqlx.DefaultFormatOptions()
742 if p.Options.InsertSpaces {
743 opts.IndentSize = p.Options.TabSize
744 }
745 formatted, err := gosqlx.Format(content, opts)
746 if err != nil {
747 // If parsing fails, fall back to basic formatting
748 formatted = formatSQL(content, p.Options)
749 }
750 if formatted == content {
751 return []TextEdit{}, nil
752 }
753
754 // Calculate the full document range
755 lines := strings.Split(content, "\n")
756 endLine := len(lines) - 1
757 endChar := 0
758 if endLine >= 0 && endLine < len(lines) {
759 endChar = len(lines[endLine])
760 }
761
762 return []TextEdit{
763 {
764 Range: Range{
765 Start: Position{Line: 0, Character: 0},
766 End: Position{Line: endLine, Character: endChar},
767 },
768 NewText: formatted,
769 },
770 }, nil
771}
772
773// formatSQL provides basic SQL formatting
774func formatSQL(sql string, opts FormattingOptions) string {

Callers 1

HandleRequestMethod · 0.95

Calls 5

DefaultFormatOptionsFunction · 0.92
FormatFunction · 0.92
GetContentMethod · 0.80
DocumentsMethod · 0.80
formatSQLFunction · 0.70

Tested by

no test coverage detected