|
| 1 | +package lsp |
| 2 | + |
| 3 | +import ( |
| 4 | + "strings" |
| 5 | + "sync" |
| 6 | +) |
| 7 | + |
| 8 | +// DocumentManager manages open documents |
| 9 | +type DocumentManager struct { |
| 10 | + mu sync.RWMutex |
| 11 | + documents map[string]*Document |
| 12 | +} |
| 13 | + |
| 14 | +// Document represents an open SQL document |
| 15 | +type Document struct { |
| 16 | + URI string |
| 17 | + LanguageID string |
| 18 | + Version int |
| 19 | + Content string |
| 20 | + Lines []string // Cached line splits |
| 21 | +} |
| 22 | + |
| 23 | +// NewDocumentManager creates a new document manager |
| 24 | +func NewDocumentManager() *DocumentManager { |
| 25 | + return &DocumentManager{ |
| 26 | + documents: make(map[string]*Document), |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// Open adds a document to the manager |
| 31 | +func (dm *DocumentManager) Open(uri, languageID string, version int, content string) { |
| 32 | + dm.mu.Lock() |
| 33 | + defer dm.mu.Unlock() |
| 34 | + dm.documents[uri] = &Document{ |
| 35 | + URI: uri, |
| 36 | + LanguageID: languageID, |
| 37 | + Version: version, |
| 38 | + Content: content, |
| 39 | + Lines: splitLines(content), |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +// Update updates a document's content |
| 44 | +func (dm *DocumentManager) Update(uri string, version int, changes []TextDocumentContentChangeEvent) { |
| 45 | + dm.mu.Lock() |
| 46 | + defer dm.mu.Unlock() |
| 47 | + |
| 48 | + doc, ok := dm.documents[uri] |
| 49 | + if !ok { |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + doc.Version = version |
| 54 | + |
| 55 | + for _, change := range changes { |
| 56 | + if change.Range == nil { |
| 57 | + // Full document sync |
| 58 | + doc.Content = change.Text |
| 59 | + doc.Lines = splitLines(change.Text) |
| 60 | + } else { |
| 61 | + // Incremental sync |
| 62 | + doc.Content = applyChange(doc.Content, doc.Lines, change) |
| 63 | + doc.Lines = splitLines(doc.Content) |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +// Close removes a document from the manager |
| 69 | +func (dm *DocumentManager) Close(uri string) { |
| 70 | + dm.mu.Lock() |
| 71 | + defer dm.mu.Unlock() |
| 72 | + delete(dm.documents, uri) |
| 73 | +} |
| 74 | + |
| 75 | +// Get retrieves a document |
| 76 | +func (dm *DocumentManager) Get(uri string) (*Document, bool) { |
| 77 | + dm.mu.RLock() |
| 78 | + defer dm.mu.RUnlock() |
| 79 | + doc, ok := dm.documents[uri] |
| 80 | + return doc, ok |
| 81 | +} |
| 82 | + |
| 83 | +// GetContent retrieves a document's content |
| 84 | +func (dm *DocumentManager) GetContent(uri string) (string, bool) { |
| 85 | + dm.mu.RLock() |
| 86 | + defer dm.mu.RUnlock() |
| 87 | + doc, ok := dm.documents[uri] |
| 88 | + if !ok { |
| 89 | + return "", false |
| 90 | + } |
| 91 | + return doc.Content, true |
| 92 | +} |
| 93 | + |
| 94 | +// splitLines splits content into lines, preserving line endings |
| 95 | +func splitLines(content string) []string { |
| 96 | + if content == "" { |
| 97 | + return []string{""} |
| 98 | + } |
| 99 | + lines := strings.Split(content, "\n") |
| 100 | + return lines |
| 101 | +} |
| 102 | + |
| 103 | +// applyChange applies an incremental change to the document |
| 104 | +func applyChange(content string, lines []string, change TextDocumentContentChangeEvent) string { |
| 105 | + if change.Range == nil { |
| 106 | + return change.Text |
| 107 | + } |
| 108 | + |
| 109 | + startOffset := positionToOffset(lines, change.Range.Start) |
| 110 | + endOffset := positionToOffset(lines, change.Range.End) |
| 111 | + |
| 112 | + // Build new content |
| 113 | + var result strings.Builder |
| 114 | + result.WriteString(content[:startOffset]) |
| 115 | + result.WriteString(change.Text) |
| 116 | + if endOffset < len(content) { |
| 117 | + result.WriteString(content[endOffset:]) |
| 118 | + } |
| 119 | + |
| 120 | + return result.String() |
| 121 | +} |
| 122 | + |
| 123 | +// positionToOffset converts a Position to a byte offset |
| 124 | +func positionToOffset(lines []string, pos Position) int { |
| 125 | + offset := 0 |
| 126 | + for i := 0; i < pos.Line && i < len(lines); i++ { |
| 127 | + offset += len(lines[i]) + 1 // +1 for newline |
| 128 | + } |
| 129 | + if pos.Line < len(lines) { |
| 130 | + lineLen := len(lines[pos.Line]) |
| 131 | + if pos.Character < lineLen { |
| 132 | + offset += pos.Character |
| 133 | + } else { |
| 134 | + offset += lineLen |
| 135 | + } |
| 136 | + } |
| 137 | + return offset |
| 138 | +} |
| 139 | + |
| 140 | +// GetWordAtPosition returns the word at the given position |
| 141 | +func (doc *Document) GetWordAtPosition(pos Position) string { |
| 142 | + if pos.Line >= len(doc.Lines) { |
| 143 | + return "" |
| 144 | + } |
| 145 | + |
| 146 | + line := doc.Lines[pos.Line] |
| 147 | + if pos.Character >= len(line) { |
| 148 | + return "" |
| 149 | + } |
| 150 | + |
| 151 | + // Find word boundaries |
| 152 | + start := pos.Character |
| 153 | + end := pos.Character |
| 154 | + |
| 155 | + // Move start backwards to find word start |
| 156 | + for start > 0 && isWordChar(rune(line[start-1])) { |
| 157 | + start-- |
| 158 | + } |
| 159 | + |
| 160 | + // Move end forwards to find word end |
| 161 | + for end < len(line) && isWordChar(rune(line[end])) { |
| 162 | + end++ |
| 163 | + } |
| 164 | + |
| 165 | + if start == end { |
| 166 | + return "" |
| 167 | + } |
| 168 | + |
| 169 | + return line[start:end] |
| 170 | +} |
| 171 | + |
| 172 | +// isWordChar returns true if c is a valid word character |
| 173 | +func isWordChar(c rune) bool { |
| 174 | + return (c >= 'a' && c <= 'z') || |
| 175 | + (c >= 'A' && c <= 'Z') || |
| 176 | + (c >= '0' && c <= '9') || |
| 177 | + c == '_' |
| 178 | +} |
0 commit comments