fixes and cleanups
This commit is contained in:
@@ -74,7 +74,7 @@ func (d Delta) rows() uint {
|
||||
}
|
||||
|
||||
func (d Delta) cols() uint {
|
||||
r := d.End.Column
|
||||
r := d.End.Column - d.Start.Column
|
||||
return r
|
||||
}
|
||||
|
||||
@@ -86,7 +86,8 @@ func (d *Document) Contents() string {
|
||||
}
|
||||
|
||||
type Position struct {
|
||||
Row, Column uint
|
||||
Row uint `json:"row"`
|
||||
Column uint `json:"column"`
|
||||
}
|
||||
|
||||
func (d *Document) InDocument(pos Position) bool {
|
||||
|
||||
@@ -96,7 +96,12 @@ func (c *Client) Error() error {
|
||||
|
||||
// PushDeltas pushes deltas into the document.
|
||||
func (c *Client) PushDeltas(d ...Delta) error {
|
||||
return c.doc.Apply(d...)
|
||||
ds := make([]Delta, len(d))
|
||||
for i := range d {
|
||||
ds[i] = d[i]
|
||||
ds[i].source = c
|
||||
}
|
||||
return c.doc.Apply(ds...)
|
||||
}
|
||||
|
||||
func (c *Client) pullDeltas() []Delta {
|
||||
@@ -125,6 +130,9 @@ func (c *Client) pullDeltas() []Delta {
|
||||
if d.Equal(Delta{}) {
|
||||
continue
|
||||
}
|
||||
if d.source == c {
|
||||
continue
|
||||
}
|
||||
|
||||
ds = append(ds, d)
|
||||
if len(ds) == maxSize {
|
||||
|
||||
@@ -45,10 +45,40 @@ const (
|
||||
DeltaRemove DeltaAction = false
|
||||
)
|
||||
|
||||
func (d DeltaAction) String() string {
|
||||
if d == DeltaInsert {
|
||||
return "insert"
|
||||
}
|
||||
return "remove"
|
||||
}
|
||||
|
||||
func (d DeltaAction) MarshalJSON() ([]byte, error) {
|
||||
if d == DeltaInsert {
|
||||
return []byte(`"insert"`), nil
|
||||
}
|
||||
return []byte(`"remove"`), nil
|
||||
}
|
||||
|
||||
func (d *DeltaAction) UnmarshalJSON(b []byte) error {
|
||||
fmt.Println(string(b))
|
||||
if string(b) == "null" {
|
||||
return nil
|
||||
}
|
||||
|
||||
if string(b) == `"insert"` {
|
||||
*d = DeltaInsert
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type Delta struct {
|
||||
Action DeltaAction
|
||||
Lines []string
|
||||
Start, End Position
|
||||
Action DeltaAction `json:"action"`
|
||||
Lines []string `json:"lines"`
|
||||
Start Position `json:"start"`
|
||||
End Position `json:"end"`
|
||||
|
||||
source *Client
|
||||
}
|
||||
|
||||
func (d Delta) Equal(other Delta) bool {
|
||||
@@ -162,6 +192,9 @@ func (d *Document) Apply(dls ...Delta) error {
|
||||
|
||||
d.deltas = append(d.deltas, dl)
|
||||
for _, c := range d.clients {
|
||||
if dl.source == c {
|
||||
continue
|
||||
}
|
||||
c.ch <- dl
|
||||
}
|
||||
}
|
||||
@@ -184,8 +217,8 @@ func (d *Document) validate(dl Delta) error {
|
||||
|
||||
lastDlLine := dl.line(dl.nRows() - 1)
|
||||
|
||||
if uint(len(lastDlLine)) != dl.cols() {
|
||||
return fmt.Errorf("delta has %d chars on the final line, but positions show range of %d chars", len(lastDlLine), dl.cols())
|
||||
if uint(len(lastDlLine)) != dl.End.Column {
|
||||
return fmt.Errorf("delta has %d chars on the final line, but positions show range of %d chars", len(lastDlLine), dl.End.Column)
|
||||
}
|
||||
|
||||
return nil
|
||||
|
||||
Reference in New Issue
Block a user