import * as interfaces from './interfaces' ; import * as vscode from 'vscode' ; export class DocumentMergeConflict implements interfaces . IDocumentMergeConflict { public range : vscode . Range ; public current : interfaces . IMergeRegion ; public incoming : interfaces . IMergeRegion ; public commonAncestors : interfaces . IMergeRegion [ ] ; public splitter : vscode . Range ; constructor ( descriptor ) { this . range = descriptor . range ; this . current = descriptor . current ; this . incoming = descriptor . incoming ; this . commonAncestors = descriptor . commonAncestors ; this . splitter = descriptor . splitter ; } public commitEdit ( type , editor , edit ? ) { if ( edit ) { this . applyEdit ( type , editor . document , edit ) ; return Promise . resolve ( true ) ; } return editor . edit ( ( edit ) => this . applyEdit ( type , editor . document , edit ) ) ; } public applyEdit ( type , document , edit ) { if ( type === interfaces . CommitType . Current ) { let content = document . getText ( this . current . content ) ; this . replaceRangeWithContent ( content , edit ) ; } else if ( type === interfaces . CommitType . Incoming ) { let content = document . getText ( this . incoming . content ) ; this . replaceRangeWithContent ( content , edit ) ; } else if ( type === interfaces . CommitType . Both ) { const currentContent = document . getText ( this . current . content ) ; const incomingContent = document . getText ( this . incoming . content ) ; edit . replace ( this . range , currentContent . concat ( incomingContent ) ) ; } } private replaceRangeWithContent ( content , edit ) { if ( this . isNewlineOnly ( content ) ) { edit . replace ( this . range , '' ) ; return ; } edit . replace ( this . range , content ) ; } private isNewlineOnly ( text ) { return text === '\n' || text === '\r\n' ; } }