document

package
v0.0.0-...-89aa834 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 29, 2024 License: MIT Imports: 8 Imported by: 2

Documentation

Overview

Package document provides efficient document indexing and updating capabilities for text editors and language servers.

Index

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

func FormatPosition(filename string, p Position) string

FormatPosition formats a position as a string.

func URIToPath

func URIToPath(uri string) string

URIToPath converts a URI to a file path.

Types

type ChangeEvent

type ChangeEvent struct {
	Range *Range // nil for full document updates
	Text  string
}

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

func NewDocument(uri, content string) *Document

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) Content

func (d *Document) Content() string

Content returns the content of the document.

func (*Document) Extension

func (d *Document) Extension() string

Extension returns the file extension of the document path.

func (*Document) Filename

func (d *Document) Filename() string

Filename returns the base name of the document path.

func (*Document) LineCount

func (d *Document) LineCount() int

LineCount returns the number of lines in the document.

func (*Document) OffsetToPosition

func (d *Document) OffsetToPosition(offset int) (Position, error)

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) Path

func (d *Document) Path() string

Path returns the file path of the document.

func (*Document) PositionToOffset

func (d *Document) PositionToOffset(pos Position) (int, error)

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

func (*Document) RuneCount

func (d *Document) RuneCount() int

RuneCount returns the number of runes in the document.

func (*Document) URI

func (d *Document) URI() string

URI returns the URI of the document.

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

func PositionFor(content string, offset int) Position

PositionFor returns a Position for the given byte offset in the content.

func (Position) IsBefore

func (p Position) IsBefore(other Position) bool

IsBefore returns true if the position is before the other position.

func (Position) IsValid

func (p Position) IsValid() bool

IsValid returns true if the position is valid.

type Range

type Range struct {
	Start Position
	End   Position
}

Range represents a range in a text document.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL