@@ -19,6 +19,10 @@ export class CSharpProjectedDocument implements IProjectedDocument {
1919 private resolveProvisionalEditAt : number | undefined ;
2020 private ProvisionalDotPosition : Position | undefined ;
2121 private hostDocumentVersion : number | null = null ;
22+ private updates : CSharpDocumentUpdate [ ] | null = null ;
23+ private _checksum : string = '' ;
24+ private _checksumAlgorithm : number = 1 ; // Default to Sha1
25+ private _encodingCodePage : number | null = null ;
2226
2327 public constructor ( public readonly uri : vscode . Uri ) {
2428 this . path = getUriPath ( uri ) ;
@@ -36,22 +40,78 @@ export class CSharpProjectedDocument implements IProjectedDocument {
3640 this . setContent ( '' ) ;
3741 }
3842
39- public update ( edits : ServerTextChange [ ] , hostDocumentVersion : number ) {
40- this . removeProvisionalDot ( ) ;
43+ public get checksum ( ) : string {
44+ return this . _checksum ;
45+ }
4146
42- this . hostDocumentVersion = hostDocumentVersion ;
47+ public get checksumAlgorithm ( ) : number {
48+ return this . _checksumAlgorithm ;
49+ }
4350
44- if ( edits . length === 0 ) {
45- return ;
51+ public get encodingCodePage ( ) : number | null {
52+ return this . _encodingCodePage ;
53+ }
54+
55+ public update (
56+ hostDocumentIsOpen : boolean ,
57+ edits : ServerTextChange [ ] ,
58+ hostDocumentVersion : number ,
59+ checksum : string ,
60+ checksumAlgorithm : number ,
61+ encodingCodePage : number | null
62+ ) {
63+ if ( hostDocumentIsOpen ) {
64+ this . removeProvisionalDot ( ) ;
65+
66+ // Apply any stored edits if needed
67+ if ( this . updates ) {
68+ for ( const update of this . updates ) {
69+ this . updateContent ( update . changes ) ;
70+ }
71+
72+ this . updates = null ;
73+ }
74+
75+ this . updateContent ( edits ) ;
76+ this . _checksum = checksum ;
77+ this . _checksumAlgorithm = checksumAlgorithm ;
78+ this . _encodingCodePage = encodingCodePage ;
79+ } else {
80+ const update = new CSharpDocumentUpdate ( edits , checksum , checksumAlgorithm , encodingCodePage ) ;
81+
82+ if ( this . updates ) {
83+ this . updates = this . updates . concat ( update ) ;
84+ } else {
85+ this . updates = [ update ] ;
86+ }
4687 }
4788
48- let content = this . content ;
49- for ( const edit of edits . reverse ( ) ) {
50- // TODO: Use a better data structure to represent the content, string concatenation is slow.
51- content = this . getEditedContent ( edit . newText , edit . span . start , edit . span . start + edit . span . length , content ) ;
89+ this . hostDocumentVersion = hostDocumentVersion ;
90+ }
91+
92+ public applyEdits ( ) : ApplyEditsResponse {
93+ const updates = this . updates ;
94+ this . updates = null ;
95+
96+ const originalChecksum = this . _checksum ;
97+ const originalChecksumAlgorithm = this . _checksumAlgorithm ;
98+ const originalEncodingCodePage = this . _encodingCodePage ;
99+
100+ if ( updates ) {
101+ for ( const update of updates ) {
102+ this . updateContent ( update . changes ) ;
103+ this . _checksum = update . checksum ;
104+ this . _checksumAlgorithm = update . checksumAlgorithm ;
105+ this . _encodingCodePage = update . encodingCodePage ;
106+ }
52107 }
53108
54- this . setContent ( content ) ;
109+ return {
110+ edits : updates ,
111+ originalChecksum : originalChecksum ,
112+ originalChecksumAlgorithm : originalChecksumAlgorithm ,
113+ originalEncodingCodePage : originalEncodingCodePage ,
114+ } ;
55115 }
56116
57117 public getContent ( ) {
@@ -140,8 +200,8 @@ export class CSharpProjectedDocument implements IProjectedDocument {
140200 }
141201
142202 private getEditedContent ( newText : string , start : number , end : number , content : string ) {
143- const before = content . substr ( 0 , start ) ;
144- const after = content . substr ( end ) ;
203+ const before = content . substring ( 0 , start ) ;
204+ const after = content . substring ( end ) ;
145205 content = `${ before } ${ newText } ${ after } ` ;
146206
147207 return content ;
@@ -150,4 +210,34 @@ export class CSharpProjectedDocument implements IProjectedDocument {
150210 private setContent ( content : string ) {
151211 this . content = content ;
152212 }
213+
214+ private updateContent ( edits : ServerTextChange [ ] ) {
215+ if ( edits . length === 0 ) {
216+ return ;
217+ }
218+
219+ let content = this . content ;
220+ for ( const edit of edits . reverse ( ) ) {
221+ // TODO: Use a better data structure to represent the content, string concatenation is slow.
222+ content = this . getEditedContent ( edit . newText , edit . span . start , edit . span . start + edit . span . length , content ) ;
223+ }
224+
225+ this . setContent ( content ) ;
226+ }
227+ }
228+
229+ export class CSharpDocumentUpdate {
230+ constructor (
231+ public readonly changes : ServerTextChange [ ] ,
232+ public readonly checksum : string ,
233+ public readonly checksumAlgorithm : number ,
234+ public readonly encodingCodePage : number | null
235+ ) { }
236+ }
237+
238+ export interface ApplyEditsResponse {
239+ edits : CSharpDocumentUpdate [ ] | null ;
240+ originalChecksum : string ;
241+ originalChecksumAlgorithm : number ;
242+ originalEncodingCodePage : number | null ;
153243}
0 commit comments