77using System . Reactive ;
88using System . Reactive . Linq ;
99using System . Reactive . Threading . Tasks ;
10+ using System . Threading ;
1011using System . Threading . Tasks ;
1112using GitHub . App ;
1213using GitHub . Commands ;
1718using GitHub . Models ;
1819using GitHub . Services ;
1920using LibGit2Sharp ;
21+ using Microsoft . VisualStudio . StaticReviews . Contracts ;
2022using ReactiveUI ;
2123using ReactiveUI . Legacy ;
2224using Serilog ;
@@ -28,7 +30,7 @@ namespace GitHub.ViewModels.GitHubPane
2830 /// <inheritdoc cref="IPullRequestDetailViewModel"/>
2931 [ Export ( typeof ( IPullRequestDetailViewModel ) ) ]
3032 [ PartCreationPolicy ( CreationPolicy . NonShared ) ]
31- public sealed class PullRequestDetailViewModel : PanePageViewModelBase , IPullRequestDetailViewModel
33+ public sealed class PullRequestDetailViewModel : PanePageViewModelBase , IPullRequestDetailViewModel , IStaticReviewFileMap
3234 {
3335 static readonly ILogger log = LogManager . ForContext < PullRequestDetailViewModel > ( ) ;
3436
@@ -40,6 +42,7 @@ public sealed class PullRequestDetailViewModel : PanePageViewModelBase, IPullReq
4042 readonly ISyncSubmodulesCommand syncSubmodulesCommand ;
4143 readonly IViewViewModelFactory viewViewModelFactory ;
4244 readonly IGitService gitService ;
45+
4346 IModelService modelService ;
4447 PullRequestDetailModel model ;
4548 IActorViewModel author ;
@@ -98,6 +101,7 @@ public PullRequestDetailViewModel(
98101 this . syncSubmodulesCommand = syncSubmodulesCommand ;
99102 this . viewViewModelFactory = viewViewModelFactory ;
100103 this . gitService = gitService ;
104+
101105 Files = files ;
102106
103107 Checkout = ReactiveCommand . CreateFromObservable (
@@ -137,6 +141,9 @@ public PullRequestDetailViewModel(
137141 ShowAnnotations = ReactiveCommand . Create < IPullRequestCheckViewModel > ( DoShowAnnotations ) ;
138142 }
139143
144+ [ Import ( AllowDefault = true ) ]
145+ private IStaticReviewFileMapManager StaticReviewFileMapManager { get ; set ; }
146+
140147 private void DoOpenDetailsUrl ( )
141148 {
142149 usageTracker . IncrementCounter ( measuresModel => measuresModel . NumberOfPRDetailsOpenInGitHub ) . Forget ( ) ;
@@ -490,6 +497,7 @@ public string GetLocalFilePath(IPullRequestFileNode file)
490497 public override void Activated ( )
491498 {
492499 active = true ;
500+ this . StaticReviewFileMapManager ? . RegisterStaticReviewFileMap ( this ) ;
493501
494502 if ( refreshOnActivate )
495503 {
@@ -499,7 +507,43 @@ public override void Activated()
499507 }
500508
501509 /// <inheritdoc/>
502- public override void Deactivated ( ) => active = false ;
510+ public override void Deactivated ( )
511+ {
512+ this . StaticReviewFileMapManager ? . UnregisterStaticReviewFileMap ( this ) ;
513+ active = false ;
514+ }
515+
516+ /// <inheritdoc/>
517+ public Task < string > GetLocalPathFromObjectishAsync ( string objectish , CancellationToken cancellationToken )
518+ {
519+ if ( this . pullRequestsService != null )
520+ {
521+ string commitId = objectish . Substring ( 0 , objectish . IndexOf ( ':' ) ) ;
522+ string relativePath = objectish . Substring ( objectish . IndexOf ( ':' ) + 1 ) . TrimStart ( '/' ) ;
523+
524+ return this . pullRequestsService . ExtractToTempFile (
525+ this . Session . LocalRepository ,
526+ this . Session . PullRequest ,
527+ relativePath ,
528+ commitId ,
529+ this . pullRequestsService . GetEncoding ( this . Session . LocalRepository , relativePath ) ) ;
530+ }
531+
532+ return Task . FromResult < string > ( null ) ;
533+ }
534+
535+ /// <inheritdoc/>
536+ public Task < string > GetObjectishFromLocalPathAsync ( string localPath , CancellationToken cancellationToken )
537+ {
538+ // We rely on pull request service's global map here instead of trying to get it from IPullRequestSessionManager via ITextBuffer
539+ // because it is possible that the file queried wasn't opened by GitHub extension and instead was opened by LSP
540+ if ( this . pullRequestsService is IStaticReviewFileMap staticReviewFileMap )
541+ {
542+ return staticReviewFileMap . GetObjectishFromLocalPathAsync ( localPath , cancellationToken ) ;
543+ }
544+
545+ return Task . FromResult < string > ( null ) ;
546+ }
503547
504548 /// <inheritdoc/>
505549 protected override void Dispose ( bool disposing )
0 commit comments