Skip to content

Commit

Permalink
wip: fixing highlighting
Browse files Browse the repository at this point in the history
  • Loading branch information
topi314 committed Dec 2, 2024
1 parent e320775 commit 4d3e7ad
Show file tree
Hide file tree
Showing 13 changed files with 1,048 additions and 1,031 deletions.
14 changes: 9 additions & 5 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
.idea/
dist/
/test/
.gopad/

/dist/
/test/
/config/grammars/

go.work
go.work.sum
/debug.log
/lsp.log
/config/grammars/

debug.log
lsp.log
trace.log
panic.log
1 change: 1 addition & 0 deletions config/languages.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ line_comment_tokens = ['//']
block_comment_tokens = [{ start = '/*', end = '*/' }]
auto_pairs = [{ open = '(', close = ')' }, { open = '{', close = '}' }, { open = '[', close = ']' }, { open = '"', close = '"' }, { open = "'", close = "'" }, { open = '`', close = '`' }]
grammar = { name = 'go', symbol_name = 'go', install = { git = '/~https://github.com/tree-sitter/tree-sitter-go', rev = '7ee8d928db5202f6831a78f8112fd693bf69f98b', ref = 'master', ref_type = 'commit' } }
formatter = { command = 'gofmt', args = ['-s'] }

[languages.go-mod]
alt_names = ['go.mod']
Expand Down
8 changes: 4 additions & 4 deletions gopad/editor/buffer/buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type Buffer interface {
// Bytes returns the buffer as a byte slice. This uses \n as the line ending.
Bytes() []byte
// BytesRange returns the buffer as a byte slice from the given range. This uses \n as the line ending.
BytesRange(r Range) []byte
BytesRange(r ByteRange) []byte
// Rune returns the rune at the given index.
Rune(i int) rune

Expand Down Expand Up @@ -90,11 +90,11 @@ type Buffer interface {
LineLen(l int) int

// Insert inserts text at the given position.
Insert(p Point, text []byte)
Insert(i uint, text []byte)
// Replace replaces the text in the given range with the given text.
Replace(r Range, text []byte)
Replace(r ByteRange, text []byte)
// Delete deletes the range of text between the two positions.
Delete(r Range)
Delete(r ByteRange)
}

// Line represents a line in a buffer.
Expand Down
47 changes: 47 additions & 0 deletions gopad/editor/buffer/byte_range.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package buffer

import (
"github.com/tree-sitter/go-tree-sitter"
)

func NewByteRange(startByte uint, endByte uint) ByteRange {
return ByteRange{
StartByte: startByte,
EndByte: endByte,
}
}

func ParseByteRange(r tree_sitter.Range) ByteRange {
return ByteRange{
StartByte: r.StartByte,
EndByte: r.EndByte,
}
}

type ByteRange struct {
StartByte uint
EndByte uint
}

func (r ByteRange) Contains(i uint) bool {
return i >= r.StartByte && i <= r.EndByte
}

func (r ByteRange) ContainsRange(other ByteRange) bool {
return r.StartByte <= other.StartByte && r.EndByte >= other.EndByte
}

func (r ByteRange) IsEmpty() bool {
return r.StartByte == r.EndByte
}

func (r ByteRange) Bytes() uint {
return r.EndByte - r.StartByte
}

func (r ByteRange) ToTreeSitter() tree_sitter.Range {
return tree_sitter.Range{
StartByte: r.StartByte,
EndByte: r.EndByte,
}
}
130 changes: 0 additions & 130 deletions gopad/editor/doc/autocomplete.go

This file was deleted.

53 changes: 40 additions & 13 deletions gopad/editor/doc/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package doc

import (
"iter"
"slices"

"go.gopad.dev/gopad/gopad/editor/buffer"
"go.gopad.dev/gopad/internal/xbytes"
Expand Down Expand Up @@ -141,18 +142,16 @@ func (c ChangeSet) Invert(originalBuf buffer.Buffer) ChangeSet {
LenAfter: 0,
}

var pos int
var pos uint
for _, change := range c.Changes {
switch change := change.(type) {
case Move:
changes.Move(change.N)
pos += change.N
pos = uint(int(pos) + change.N)
case Delete:
start := originalBuf.Position(pos)
end := originalBuf.Position(pos + change.N)
text := originalBuf.BytesRange(buffer.Range{Start: start, End: end})
text := originalBuf.BytesRange(buffer.ByteRange{StartByte: pos, EndByte: uint(int(pos) + change.N)})
changes.Insert(text)
pos += change.N
pos = uint(int(pos) + change.N)
case Insert:
changes.Delete(xbytes.RuneCount(change.Text))
}
Expand All @@ -166,18 +165,16 @@ func (c ChangeSet) Apply(buf buffer.Buffer) bool {
return false
}

var pos int
var pos uint
for _, change := range c.Changes {
switch change := change.(type) {
case Move:
pos += change.N
pos = uint(int(pos) + change.N)
case Delete:
start := buf.Position(pos)
end := buf.Position(pos + change.N)
buf.Delete(buffer.Range{Start: start, End: end})
buf.Delete(buffer.ByteRange{StartByte: pos, EndByte: uint(int(pos) + change.N)})
case Insert:
buf.Insert(buf.Position(pos), change.Text)
pos += xbytes.RuneCount(change.Text)
buf.Insert(pos, change.Text)
pos += uint(xbytes.RuneCount(change.Text))
}
}

Expand Down Expand Up @@ -317,6 +314,36 @@ func (c ChangeSet) Merge(other ChangeSet) ChangeSet {
}
}

func (c ChangeSet) UpdatePosition(selections []Selection) []Selection {
slices.SortFunc(selections, func(a, b Selection) int {
return int(a.Anchor) - int(b.Anchor)
})

for i, selection := range selections {
anchor := selection.Anchor
head := selection.Head
for _, change := range c.Changes {
switch change := change.(type) {
case Move:
anchor = uint(int(anchor) + change.N)
head = uint(int(head) + change.N)
case Delete:
anchor = uint(int(anchor) + change.N)
head += uint(int(head) + change.N)
case Insert:
anchor += uint(xbytes.RuneCount(change.Text))
head += uint(xbytes.RuneCount(change.Text))
}
}
selections[i] = Selection{
Anchor: anchor,
Head: head,
}
}

return selections
}

func newChangeIterator(changeSet ChangeSet) *changeIterator {
return &changeIterator{
changes: changeSet.Changes,
Expand Down
Loading

0 comments on commit 4d3e7ad

Please sign in to comment.