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)
| 727 | // - Empty array if formatting produces no changes |
| 728 | // - Error if document not found or formatting fails |
| 729 | func (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 |
| 774 | func formatSQL(sql string, opts FormattingOptions) string { |
no test coverage detected