| 1 |
package lsp |
| 2 |
|
| 3 |
import ( |
| 4 |
"cmp" |
| 5 |
"fmt" |
| 6 |
"path/filepath" |
| 7 |
"strings" |
| 8 |
|
| 9 |
"github.com/charmbracelet/x/powernap/pkg/lsp/protocol" |
| 10 |
) |
| 11 |
|
| 12 |
// TextDocumentSyncManager manages text document synchronization with the language server. |
| 13 |
type TextDocumentSyncManager struct { |
| 14 |
client *Client |
| 15 |
documents map[string]*Document |
| 16 |
syncKind protocol.TextDocumentSyncKind |
| 17 |
} |
| 18 |
|
| 19 |
// Document represents an open text document. |
| 20 |
type Document struct { |
| 21 |
URI string |
| 22 |
LanguageID string |
| 23 |
Version int |
| 24 |
Content string |
| 25 |
} |
| 26 |
|
| 27 |
// NewTextDocumentSyncManager creates a new text document sync manager. |
| 28 |
func NewTextDocumentSyncManager(client *Client) *TextDocumentSyncManager { |
| 29 |
syncKind := protocol.Full // Default to full sync |
| 30 |
|
| 31 |
// Extract sync kind from capabilities |
| 32 |
switch v := client.capabilities.TextDocumentSync.(type) { |
| 33 |
case float64: |
| 34 |
syncKind = protocol.TextDocumentSyncKind(int(v)) |
| 35 |
case int: |
| 36 |
syncKind = protocol.TextDocumentSyncKind(v) |
| 37 |
case map[string]any: |
| 38 |
// It's a TextDocumentSyncOptions object |
| 39 |
if change, ok := v["change"].(float64); ok { |
| 40 |
syncKind = protocol.TextDocumentSyncKind(int(change)) |
| 41 |
} |
| 42 |
case *protocol.TextDocumentSyncOptions: |
| 43 |
syncKind = v.Change |
| 44 |
} |
| 45 |
|
| 46 |
return &TextDocumentSyncManager{ |
| 47 |
client: client, |
| 48 |
documents: make(map[string]*Document), |
| 49 |
syncKind: syncKind, |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
// Open opens a new text document. |
| 54 |
func (m *TextDocumentSyncManager) Open(uri, languageID, content string) error { |
| 55 |
if _, exists := m.documents[uri]; exists { |
| 56 |
return fmt.Errorf("document already open: %s", uri) |
| 57 |
} |
| 58 |
|
| 59 |
doc := &Document{ |
| 60 |
URI: uri, |
| 61 |
LanguageID: languageID, |
| 62 |
Version: 1, |
| 63 |
Content: content, |
| 64 |
} |
| 65 |
|
| 66 |
m.documents[uri] = doc |
| 67 |
|
| 68 |
return m.client.NotifyDidOpenTextDocument(m.client.ctx, uri, languageID, doc.Version, content) |
| 69 |
} |
| 70 |
|
| 71 |
// Change applies changes to an open document. |
| 72 |
func (m *TextDocumentSyncManager) Change(uri string, changes []protocol.TextDocumentContentChangeEvent) error { |
| 73 |
doc, exists := m.documents[uri] |
| 74 |
if !exists { |
| 75 |
return fmt.Errorf("document not open: %s", uri) |
| 76 |
} |
| 77 |
|
| 78 |
// Apply changes based on sync kind |
| 79 |
switch m.syncKind { |
| 80 |
case protocol.Full: |
| 81 |
// For full sync, we expect a single change with the full content |
| 82 |
if len(changes) > 0 { |
| 83 |
m.applyFullDocumentChange(doc, changes[0]) |
| 84 |
} |
| 85 |
case protocol.Incremental: |
| 86 |
// Apply incremental changes |
| 87 |
for _, change := range changes { |
| 88 |
if err := m.applyIncrementalChange(doc, change); err != nil { |
| 89 |
return err |
| 90 |
} |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
doc.Version++ |
| 95 |
|
| 96 |
return m.client.NotifyDidChangeTextDocument(m.client.ctx, uri, doc.Version, changes) |
| 97 |
} |
| 98 |
|
| 99 |
// Close closes a text document. |
| 100 |
func (m *TextDocumentSyncManager) Close(uri string) error { |
| 101 |
if _, exists := m.documents[uri]; !exists { |
| 102 |
return fmt.Errorf("document not open: %s", uri) |
| 103 |
} |
| 104 |
|
| 105 |
delete(m.documents, uri) |
| 106 |
|
| 107 |
// Send didClose notification |
| 108 |
params := map[string]any{ |
| 109 |
"textDocument": map[string]any{ |
| 110 |
"uri": uri, |
| 111 |
}, |
| 112 |
} |
| 113 |
|
| 114 |
return m.client.conn.Notify(m.client.ctx, MethodTextDocumentDidClose, params) |
| 115 |
} |
| 116 |
|
| 117 |
// Save notifies the server that a document was saved. |
| 118 |
func (m *TextDocumentSyncManager) Save(uri string, includeText bool) error { |
| 119 |
doc, exists := m.documents[uri] |
| 120 |
if !exists { |
| 121 |
return fmt.Errorf("document not open: %s", uri) |
| 122 |
} |
| 123 |
|
| 124 |
params := map[string]any{ |
| 125 |
"textDocument": map[string]any{ |
| 126 |
"uri": uri, |
| 127 |
}, |
| 128 |
} |
| 129 |
|
| 130 |
if includeText { |
| 131 |
params["text"] = doc.Content |
| 132 |
} |
| 133 |
|
| 134 |
return m.client.conn.Notify(m.client.ctx, MethodTextDocumentDidSave, params) |
| 135 |
} |
| 136 |
|
| 137 |
// GetDocument returns the document for the given URI. |
| 138 |
func (m *TextDocumentSyncManager) GetDocument(uri string) (*Document, bool) { |
| 139 |
doc, exists := m.documents[uri] |
| 140 |
return doc, exists |
| 141 |
} |
| 142 |
|
| 143 |
func (m *TextDocumentSyncManager) applyFullDocumentChange(doc *Document, change protocol.TextDocumentContentChangeEvent) { |
| 144 |
full, ok := change.Value.(protocol.TextDocumentContentChangeWholeDocument) |
| 145 |
if !ok { |
| 146 |
return |
| 147 |
} |
| 148 |
doc.Content = full.Text |
| 149 |
} |
| 150 |
|
| 151 |
// applyIncrementalChange applies an incremental change to a document. |
| 152 |
func (m *TextDocumentSyncManager) applyIncrementalChange(doc *Document, change protocol.TextDocumentContentChangeEvent) error { |
| 153 |
partial, ok := change.Value.(protocol.TextDocumentContentChangePartial) |
| 154 |
if !ok { |
| 155 |
return nil |
| 156 |
} |
| 157 |
if partial.Range == nil { |
| 158 |
// Full document change |
| 159 |
doc.Content = partial.Text |
| 160 |
return nil |
| 161 |
} |
| 162 |
|
| 163 |
// Convert content to lines for easier manipulation |
| 164 |
lines := strings.Split(doc.Content, "\n") |
| 165 |
lineC := uint32(len(lines)) |
| 166 |
|
| 167 |
// Validate range |
| 168 |
if partial.Range.Start.Line >= lineC { |
| 169 |
return fmt.Errorf("invalid start line: %d", partial.Range.Start.Line) |
| 170 |
} |
| 171 |
if partial.Range.End.Line >= lineC { |
| 172 |
return fmt.Errorf("invalid end line: %d", partial.Range.End.Line) |
| 173 |
} |
| 174 |
|
| 175 |
// Calculate the start and end positions in the document |
| 176 |
var startPos uint32 |
| 177 |
for i := uint32(0); i < partial.Range.Start.Line; i++ { |
| 178 |
startPos += uint32(len(lines[i])) + 1 // +1 for newline |
| 179 |
} |
| 180 |
startPos += partial.Range.Start.Character |
| 181 |
|
| 182 |
var endPos uint32 |
| 183 |
for i := uint32(0); i < partial.Range.End.Line; i++ { |
| 184 |
endPos += uint32(len(lines[i])) + 1 |
| 185 |
} |
| 186 |
endPos += partial.Range.End.Character |
| 187 |
|
| 188 |
// Apply the partial |
| 189 |
newContent := doc.Content[:startPos] + partial.Text + doc.Content[endPos:] |
| 190 |
doc.Content = newContent |
| 191 |
|
| 192 |
return nil |
| 193 |
} |
| 194 |
|
| 195 |
// CreateFullDocumentChange creates a change event for full document sync. |
| 196 |
func CreateFullDocumentChange(content string) []protocol.TextDocumentContentChangeEvent { |
| 197 |
return []protocol.TextDocumentContentChangeEvent{ |
| 198 |
{ |
| 199 |
Value: protocol.TextDocumentContentChangeWholeDocument{ |
| 200 |
Text: content, |
| 201 |
}, |
| 202 |
}, |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
// OpenFile opens a new text document from a file path. |
| 207 |
func (m *TextDocumentSyncManager) OpenFile(filePath, content string) error { |
| 208 |
// Convert to absolute path |
| 209 |
absPath, err := filepath.Abs(filePath) |
| 210 |
if err != nil { |
| 211 |
return fmt.Errorf("failed to get absolute path: %w", err) |
| 212 |
} |
| 213 |
|
| 214 |
// Detect language from file path |
| 215 |
languageID := cmp.Or(string(DetectLanguage(absPath)), "plaintext") |
| 216 |
// Create file URI |
| 217 |
uri := FilePathToURI(absPath) |
| 218 |
|
| 219 |
return m.Open(uri, languageID, content) |
| 220 |
} |
| 221 |
|
| 222 |
// FilePathToURI converts a file path to a file URI. |
| 223 |
// |
| 224 |
// Deprecated: use [protocol.URIFromPath]. |
| 225 |
func FilePathToURI(path string) string { |
| 226 |
return string(protocol.URIFromPath(path)) |
| 227 |
} |
| 228 |
|
| 229 |
// URIToFilePath converts a file URI to a file path. |
| 230 |
func URIToFilePath(uri string) string { |
| 231 |
// Remove file:// prefix |
| 232 |
path := strings.TrimPrefix(uri, "file://") |
| 233 |
path = strings.TrimPrefix(path, "file:///") |
| 234 |
|
| 235 |
// Convert to native path separators |
| 236 |
return filepath.FromSlash(path) |
| 237 |
} |