Skip to content

Commit

Permalink
unexport document uri and content fields
Browse files Browse the repository at this point in the history
  • Loading branch information
mkideal committed Sep 6, 2024
1 parent 055c671 commit 87d4e44
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 27 deletions.
46 changes: 28 additions & 18 deletions text/document/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type ChangeEvent struct {

// Document represents an indexed text document.
type Document struct {
URI string
Content string
uri string
content string
index *index
}

Expand All @@ -58,8 +58,8 @@ type lineInfo struct {
// NewDocument creates a new Document with the given URI and content.
func NewDocument(uri, content string) *Document {
doc := &Document{
URI: uri,
Content: content,
uri: uri,
content: content,
}
doc.index = buildIndex(content)
return doc
Expand All @@ -85,12 +85,22 @@ func buildIndex(content string) *index {
return &index{lines: lines}
}

// URI returns the URI of the document.
func (d *Document) URI() string {
return d.uri
}

// Content returns the content of the document.
func (d *Document) Content() string {
return d.content
}

func (d *Document) ApplyChanges(changes []ChangeEvent) error {
for _, change := range changes {
if change.Range == nil {
// Full document update
d.Content = change.Text
d.index = buildIndex(d.Content)
d.content = change.Text
d.index = buildIndex(d.content)
continue
}

Expand All @@ -103,17 +113,17 @@ func (d *Document) ApplyChanges(changes []ChangeEvent) error {
return err
}

beforeChange := d.Content[:startOffset]
afterChange := d.Content[endOffset:]
d.Content = beforeChange + change.Text + afterChange
beforeChange := d.content[:startOffset]
afterChange := d.content[endOffset:]
d.content = beforeChange + change.Text + afterChange

d.updateIndex(change.Range.Start.Line, startOffset, len(beforeChange)+len(change.Text))
}
return nil
}

func (d *Document) updateIndex(startLine, startByteOffset, endByteOffset int) {
newContent := d.Content[startByteOffset:endByteOffset]
newContent := d.content[startByteOffset:endByteOffset]
newLines := strings.Count(newContent, "\n")

if newLines == 0 {
Expand All @@ -125,7 +135,7 @@ func (d *Document) updateIndex(startLine, startByteOffset, endByteOffset int) {

func (d *Document) updateSingleLine(line, startByteOffset, endByteOffset int) {
oldRuneCount := d.index.lines[line].RunesInLine
newRuneCount := utf8.RuneCountInString(d.Content[d.index.lines[line].ByteOffset:endByteOffset])
newRuneCount := utf8.RuneCountInString(d.content[d.index.lines[line].ByteOffset:endByteOffset])
runesDiff := newRuneCount - oldRuneCount
bytesDiff := endByteOffset - startByteOffset

Expand All @@ -149,11 +159,11 @@ func (d *Document) replaceLines(startLine, startByteOffset, endByteOffset, newLi
byteOffset := startByteOffset

for i := 1; i < newLineCount; i++ {
nlIndex := strings.IndexByte(d.Content[byteOffset:endByteOffset], '\n')
nlIndex := strings.IndexByte(d.content[byteOffset:endByteOffset], '\n')
if nlIndex == -1 {
break
}
runeOffset += utf8.RuneCountInString(d.Content[byteOffset : byteOffset+nlIndex+1])
runeOffset += utf8.RuneCountInString(d.content[byteOffset : byteOffset+nlIndex+1])
byteOffset += nlIndex + 1

newLines[i] = lineInfo{
Expand All @@ -167,14 +177,14 @@ func (d *Document) replaceLines(startLine, startByteOffset, endByteOffset, newLi
for i := 0; i < newLineCount-1; i++ {
newLines[i].RunesInLine = newLines[i+1].RuneOffset - newLines[i].RuneOffset - 1 // -1 for newline character
}
newLines[newLineCount-1].RunesInLine = utf8.RuneCountInString(d.Content[newLines[newLineCount-1].ByteOffset:endByteOffset])
newLines[newLineCount-1].RunesInLine = utf8.RuneCountInString(d.content[newLines[newLineCount-1].ByteOffset:endByteOffset])

// Replace old lines with new lines
d.index.lines = append(d.index.lines[:startLine], append(newLines, d.index.lines[startLine+oldLineCount:]...)...)

// Update subsequent lines
runesDiff := newLines[newLineCount-1].RuneOffset + newLines[newLineCount-1].RunesInLine -
(d.index.lines[startLine].RuneOffset + utf8.RuneCountInString(d.Content[startByteOffset:endByteOffset]))
(d.index.lines[startLine].RuneOffset + utf8.RuneCountInString(d.content[startByteOffset:endByteOffset]))
bytesDiff := endByteOffset - startByteOffset

for i := startLine + newLineCount; i < len(d.index.lines); i++ {
Expand Down Expand Up @@ -224,7 +234,7 @@ func (d *Document) PositionToOffset(pos Position) (int, error) {

offset := lineInfo.ByteOffset
for i := 0; i < pos.Character; i++ {
_, size := utf8.DecodeRuneInString(d.Content[offset:])
_, size := utf8.DecodeRuneInString(d.content[offset:])
offset += size
}

Expand All @@ -233,7 +243,7 @@ func (d *Document) PositionToOffset(pos Position) (int, error) {

// OffsetToPosition converts a byte offset to a Position in the document.
func (d *Document) OffsetToPosition(offset int) (Position, error) {
if offset < 0 || offset > len(d.Content) {
if offset < 0 || offset > len(d.content) {
return Position{}, ErrInvalidOffset
}

Expand All @@ -248,7 +258,7 @@ func (d *Document) OffsetToPosition(offset int) (Position, error) {
lineInfo := d.index.lines[line]
char := 0
for i := lineInfo.ByteOffset; i < offset; {
_, size := utf8.DecodeRuneInString(d.Content[i:])
_, size := utf8.DecodeRuneInString(d.content[i:])
i += size
char++
}
Expand Down
18 changes: 9 additions & 9 deletions text/document/document_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ func TestNewDocument(t *testing.T) {
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
doc := NewDocument(tt.uri, tt.content)
if doc.URI != tt.uri {
t.Errorf("NewDocument().URI = %v, want %v", doc.URI, tt.uri)
if doc.uri != tt.uri {
t.Errorf("NewDocument().URI = %v, want %v", doc.uri, tt.uri)
}
if doc.Content != tt.content {
t.Errorf("NewDocument().Content = %v, want %v", doc.Content, tt.content)
if doc.content != tt.content {
t.Errorf("NewDocument().Content = %v, want %v", doc.content, tt.content)
}
if got := doc.LineCount(); got != tt.wantLine {
t.Errorf("NewDocument().LineCount() = %v, want %v", got, tt.wantLine)
Expand Down Expand Up @@ -166,8 +166,8 @@ func TestDocument_ApplyChanges(t *testing.T) {
t.Errorf("Document.ApplyChanges() error = %v, wantErr %v", err, tt.wantErr)
return
}
if doc.Content != tt.wantText {
t.Errorf("Document.ApplyChanges() content = %v, want %v", doc.Content, tt.wantText)
if doc.content != tt.wantText {
t.Errorf("Document.ApplyChanges() content = %v, want %v", doc.content, tt.wantText)
}
})
}
Expand Down Expand Up @@ -296,8 +296,8 @@ func TestDocument_RuneCount(t *testing.T) {

func ExampleNewDocument() {
doc := NewDocument("example.txt", "Hello, World!")
fmt.Printf("Document URI: %s\n", doc.URI)
fmt.Printf("Content: %s\n", doc.Content)
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:
Expand All @@ -323,7 +323,7 @@ func ExampleDocument_ApplyChanges() {
fmt.Printf("Error: %v\n", err)
return
}
fmt.Printf("Updated content: %s\n", doc.Content)
fmt.Printf("Updated content: %s\n", doc.content)
// Output:
// Updated content: Hello, Go!
}
Expand Down

0 comments on commit 87d4e44

Please sign in to comment.