|
|
|
@ -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 |
|
|
|
|