{"record":{"id":"e1448352ea80de4a","repo":"charmbracelet/crush","slug":"failed-to-apply-text-edits-w","errorCode":null,"errorMessage":"failed to apply text edits: %w","messagePattern":"failed to apply text edits: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/lsp/util/edit.go","lineNumber":273,"sourceCode":"\n\tvar codepointCount uint32\n\tfor byteOffset := range lineText {\n\t\tif codepointCount >= codepointOffset {\n\t\t\treturn byteOffset\n\t\t}\n\t\tcodepointCount++\n\t}\n\treturn len(lineText)\n}\n\n// ApplyWorkspaceEdit applies the given WorkspaceEdit to the filesystem.\n// The encoding parameter specifies the position encoding used by the LSP server\n// (UTF8, UTF16, or UTF32). This affects how character offsets are interpreted.\nfunc ApplyWorkspaceEdit(edit protocol.WorkspaceEdit, encoding powernap.OffsetEncoding) error {\n\t// Handle Changes field\n\tfor uri, textEdits := range edit.Changes {\n\t\tif err := applyTextEdits(uri, textEdits, encoding); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to apply text edits: %w\", err)\n\t\t}\n\t}\n\n\t// Handle DocumentChanges field\n\tfor _, change := range edit.DocumentChanges {\n\t\tif err := applyDocumentChange(change, encoding); err != nil {\n\t\t\treturn fmt.Errorf(\"failed to apply document change: %w\", err)\n\t\t}\n\t}\n\n\treturn nil\n}\n\n// rangesOverlap checks if two LSP ranges overlap.\n// Per the LSP specification, ranges are half-open intervals [start, end),\n// so adjacent ranges where one's end equals another's start do NOT overlap.\n// See https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#range\nfunc rangesOverlap(r1, r2 protocol.Range) bool {","sourceCodeStart":255,"sourceCodeEnd":291,"githubUrl":"https://github.com/charmbracelet/crush/blob/7944b8e52225d8805e31eacbf7ef24856b0dfb7a/internal/lsp/util/edit.go#L255-L291","documentation":"ApplyWorkspaceEdit iterates the WorkspaceEdit.Changes map (uri -> []TextEdit) and applies each set with applyTextEdits. Any failure there (missing document, bad positions, IO error) is wrapped as \"failed to apply text edits\". The Changes field is the legacy/simpler shape of a WorkspaceEdit as opposed to DocumentChanges.","triggerScenarios":"Server returns WorkspaceEdit.Changes entries and applyTextEdits fails, e.g. the target URI is not open/known so its content cannot be read, or the edit's range exceeds the document bounds.","commonSituations":"Edits referencing a file the client never opened; encoding mismatch (UTF-8 vs UTF-16 offsets from OffsetEncoding) producing out-of-range positions; file changed on disk since the server computed the edit; diagnostics-quickfix applied to a file that was modified.","solutions":["Read the wrapped inner error to see whether it is a range/position or IO problem","Ensure the document is open/registered with the LSP client before applying edits","Pass the correct OffsetEncoding negotiated with the server","Reload the file and re-request the edit if it changed since it was produced"],"exampleFix":"// before\nerr := ApplyWorkspaceEdit(edit, powernap.EncodingUTF16)\n// after\nif err := ApplyWorkspaceEdit(edit, serverNegotiatedEncoding); err != nil {\n    if strings.Contains(err.Error(), \"apply text edits\") {\n        // reload document and retry\n    }\n    return err\n}","handlingStrategy":"try-catch","validationCode":"for uri, edits := range edit.Changes {\n    doc, ok := openDocs[uri]\n    if !ok {\n        return fmt.Errorf(\"document not open: %s\", uri)\n    }\n    for _, e := range edits {\n        if e.Range.End.Line > uint32(len(doc.Lines())) {\n            return fmt.Errorf(\"range out of bounds for %s\", uri)\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := ApplyWorkspaceEdit(edit, enc); err != nil {\n    var inner error\n    for e := err; e != nil; e = errors.Unwrap(e) { inner = e }\n    log.Printf(\"text edits failed for %s: %v\", uriOf(edit), inner)\n}","preventionTips":["Only apply edits to documents your client has opened and holds content for","Use the OffsetEncoding negotiated during LSP initialization","Re-fetch edits if the document changed on disk since they were produced"],"tags":["lsp","workspace-edit","text-edit","encoding"],"backgroundTag":"workspace-edit-apply-failed","analyzedSha":"7944b8e52225d8805e31eacbf7ef24856b0dfb7a","analyzedAt":"2026-08-29T12:48:59.079Z","schemaVersion":2},"datasetVersion":"2026-08-29T17:17:51.833Z"}