Documentation
¶
Overview ¶
Package document provides efficient document indexing and updating capabilities for text editors and language servers.
Index ¶
- Variables
- func FormatPosition(filename string, p Position) string
- func URIToPath(uri string) string
- type ChangeEvent
- type Document
- func (d *Document) ApplyChanges(changes []ChangeEvent) error
- func (d *Document) Content() string
- func (d *Document) Extension() string
- func (d *Document) Filename() string
- func (d *Document) LineCount() int
- func (d *Document) OffsetToPosition(offset int) (Position, error)
- func (d *Document) Path() string
- func (d *Document) PositionToOffset(pos Position) (int, error)
- func (d *Document) RuneCount() int
- func (d *Document) URI() string
- type Position
- type Range
Examples ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var ( // ErrInvalidLine indicates an invalid line number. ErrInvalidLine = errors.New("invalid line number") // ErrInvalidCharacter indicates an invalid character offset. ErrInvalidCharacter = errors.New("invalid character offset") // ErrInvalidOffset indicates an invalid byte offset. ErrInvalidOffset = errors.New("invalid offset") )
Functions ¶
func FormatPosition ¶
FormatPosition formats a position as a string.
Types ¶
type ChangeEvent ¶
ChangeEvent represents a change in a text document.
type Document ¶
type Document struct {
// contains filtered or unexported fields
}
Document represents an indexed text document.
func NewDocument ¶
NewDocument creates a new Document with the given URI and content.
Example ¶
doc := NewDocument("example.txt", "Hello, World!") fmt.Printf("Document URI: %s\n", doc.uri) fmt.Printf("Content: %s\n", doc.content) fmt.Printf("Line count: %d\n", doc.LineCount()) fmt.Printf("Rune count: %d\n", doc.RuneCount())
Output: Document URI: example.txt Content: Hello, World! Line count: 1 Rune count: 13
func (*Document) ApplyChanges ¶
func (d *Document) ApplyChanges(changes []ChangeEvent) error
ApplyChanges applies the given changes to the document.
Example ¶
doc := NewDocument("example.txt", "Hello, World!") changes := []ChangeEvent{ { Range: &Range{ Start: Position{Line: 0, Character: 7}, End: Position{Line: 0, Character: 12}, }, Text: "Go", }, } err := doc.ApplyChanges(changes) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Updated content: %s\n", doc.content)
Output: Updated content: Hello, Go!
func (*Document) OffsetToPosition ¶
OffsetToPosition converts a byte offset to a Position in the document.
Example ¶
doc := NewDocument("example.txt", "Hello,\nWorld!") offset := 10 pos, err := doc.OffsetToPosition(offset) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Position for offset 10: {%d, %d}\n", pos.Line, pos.Character)
Output: Position for offset 10: {1, 3}
func (*Document) PositionToOffset ¶
PositionToOffset converts a Position to a byte offset in the document.
Example ¶
doc := NewDocument("example.txt", "Hello,\nWorld!") pos := Position{Line: 1, Character: 3} offset, err := doc.PositionToOffset(pos) if err != nil { fmt.Printf("Error: %v\n", err) return } fmt.Printf("Offset for position {1, 3}: %d\n", offset)
Output: Offset for position {1, 3}: 10
type Position ¶
type Position struct { Line int // Line number (0-based) Character int // Character offset in line (0-based) }
Position represents a position in a text document.
func PositionFor ¶
PositionFor returns a Position for the given byte offset in the content.
Click to show internal directories.
Click to hide internal directories.