11'use strict' ;
22import { Functions , IDeferrable } from './../system' ;
3- import { ConfigurationChangeEvent , Disposable , Event , EventEmitter , TextDocument , TextDocumentChangeEvent , TextEditor , Uri , window , workspace } from 'vscode' ;
3+ import { ConfigurationChangeEvent , Disposable , EndOfLine , Event , EventEmitter , Position , Range , TextDocument , TextDocumentChangeEvent , TextEditor , TextLine , Uri , window , workspace } from 'vscode' ;
44import { configuration } from './../configuration' ;
55import { CommandContext , DocumentSchemes , isActiveDocument , isTextEditor , setCommandContext } from './../constants' ;
66import { GitUri } from '../gitService' ;
@@ -174,10 +174,9 @@ export class DocumentTracker<T> extends Disposable {
174174 // }
175175 // }
176176
177- async add ( fileName : string ) : Promise < TrackedDocument < T > > ;
178177 async add ( document : TextDocument ) : Promise < TrackedDocument < T > > ;
179178 async add ( uri : Uri ) : Promise < TrackedDocument < T > > ;
180- async add ( documentOrId : string | TextDocument | Uri ) : Promise < TrackedDocument < T > > {
179+ async add ( documentOrId : TextDocument | Uri ) : Promise < TrackedDocument < T > > {
181180 return this . _add ( documentOrId ) ;
182181 }
183182
@@ -196,10 +195,9 @@ export class DocumentTracker<T> extends Disposable {
196195 return await this . _get ( documentOrId ) ;
197196 }
198197
199- async getOrAdd ( fileName : string ) : Promise < TrackedDocument < T > > ;
200198 async getOrAdd ( document : TextDocument ) : Promise < TrackedDocument < T > > ;
201199 async getOrAdd ( uri : Uri ) : Promise < TrackedDocument < T > > ;
202- async getOrAdd ( documentOrId : string | TextDocument | Uri ) : Promise < TrackedDocument < T > > {
200+ async getOrAdd ( documentOrId : TextDocument | Uri ) : Promise < TrackedDocument < T > > {
203201 return await this . _get ( documentOrId ) || await this . _add ( documentOrId ) ;
204202 }
205203
@@ -213,12 +211,22 @@ export class DocumentTracker<T> extends Disposable {
213211 return this . _documentMap . has ( key ) ;
214212 }
215213
216- private async _add ( documentOrId : string | TextDocument | Uri ) : Promise < TrackedDocument < T > > {
217- if ( typeof documentOrId === 'string' ) {
218- documentOrId = await workspace . openTextDocument ( documentOrId ) ;
219- }
220- else if ( documentOrId instanceof GitUri ) {
221- documentOrId = await workspace . openTextDocument ( documentOrId . fileUri ( { useVersionedPath : true } ) ) ;
214+ private async _add ( documentOrId : TextDocument | Uri ) : Promise < TrackedDocument < T > > {
215+ if ( documentOrId instanceof GitUri ) {
216+ try {
217+ documentOrId = await workspace . openTextDocument ( documentOrId . fileUri ( { useVersionedPath : true } ) ) ;
218+ }
219+ catch ( ex ) {
220+ if ( ! ex . toString ( ) . includes ( 'File not found' ) ) throw ex ;
221+
222+ // If we can't find the file, assume it is because the file has been renamed or deleted at some point
223+ documentOrId = new MissingRevisionTextDocument ( documentOrId ) ;
224+
225+ // const [fileName, repoPath] = await Container.git.findWorkingFileName(documentOrId, undefined, ref);
226+ // if (fileName === undefined) throw new Error(`Failed to add tracking for document: ${documentOrId}`);
227+
228+ // documentOrId = await workspace.openTextDocument(path.resolve(repoPath!, fileName));
229+ }
222230 }
223231 else if ( documentOrId instanceof Uri ) {
224232 documentOrId = await workspace . openTextDocument ( documentOrId ) ;
@@ -303,3 +311,64 @@ export class DocumentTracker<T> extends Disposable {
303311 this . _dirtyStateChangedDebounced ( e ) ;
304312 }
305313}
314+
315+ class MissingRevisionTextDocument implements TextDocument {
316+
317+ readonly eol : EndOfLine ;
318+ readonly fileName : string ;
319+ readonly isClosed : boolean ;
320+ readonly isDirty : boolean ;
321+ readonly isUntitled : boolean ;
322+ readonly languageId : string ;
323+ readonly lineCount : number ;
324+ readonly uri : Uri ;
325+ readonly version : number ;
326+
327+ constructor (
328+ public readonly gitUri : GitUri
329+ ) {
330+ this . uri = gitUri . fileUri ( { useVersionedPath : true } ) ;
331+
332+ this . eol = EndOfLine . LF ;
333+ this . fileName = this . uri . fsPath ;
334+ this . isClosed = false ;
335+ this . isDirty = false ;
336+ this . isUntitled = false ;
337+ this . lineCount = 0 ;
338+ this . version = 0 ;
339+ }
340+
341+ getText ( range ?: Range | undefined ) : string {
342+ throw new Error ( 'Method not supported.' ) ;
343+ }
344+
345+ getWordRangeAtPosition ( position : Position , regex ?: RegExp | undefined ) : Range | undefined {
346+ throw new Error ( 'Method not supported.' ) ;
347+ }
348+
349+ lineAt ( line : number ) : TextLine ;
350+ lineAt ( position : Position ) : TextLine ;
351+ lineAt ( position : any ) : TextLine {
352+ throw new Error ( 'Method not supported.' ) ;
353+ }
354+
355+ offsetAt ( position : Position ) : number {
356+ throw new Error ( 'Method not supported.' ) ;
357+ }
358+
359+ positionAt ( offset : number ) : Position {
360+ throw new Error ( 'Method not supported.' ) ;
361+ }
362+
363+ save ( ) : Thenable < boolean > {
364+ throw new Error ( 'Method not supported.' ) ;
365+ }
366+
367+ validatePosition ( position : Position ) : Position {
368+ throw new Error ( 'Method not supported.' ) ;
369+ }
370+
371+ validateRange ( range : Range ) : Range {
372+ throw new Error ( 'Method not supported.' ) ;
373+ }
374+ }
0 commit comments