7
7
using System . Reactive ;
8
8
using System . Reactive . Linq ;
9
9
using System . Reactive . Threading . Tasks ;
10
+ using System . Threading ;
10
11
using System . Threading . Tasks ;
11
12
using GitHub . App ;
12
13
using GitHub . Commands ;
17
18
using GitHub . Models ;
18
19
using GitHub . Services ;
19
20
using LibGit2Sharp ;
21
+ using Microsoft . VisualStudio . StaticReviews . Contracts ;
20
22
using ReactiveUI ;
21
23
using ReactiveUI . Legacy ;
22
24
using Serilog ;
@@ -28,7 +30,7 @@ namespace GitHub.ViewModels.GitHubPane
28
30
/// <inheritdoc cref="IPullRequestDetailViewModel"/>
29
31
[ Export ( typeof ( IPullRequestDetailViewModel ) ) ]
30
32
[ PartCreationPolicy ( CreationPolicy . NonShared ) ]
31
- public sealed class PullRequestDetailViewModel : PanePageViewModelBase , IPullRequestDetailViewModel
33
+ public sealed class PullRequestDetailViewModel : PanePageViewModelBase , IPullRequestDetailViewModel , IStaticReviewFileMap
32
34
{
33
35
static readonly ILogger log = LogManager . ForContext < PullRequestDetailViewModel > ( ) ;
34
36
@@ -40,6 +42,7 @@ public sealed class PullRequestDetailViewModel : PanePageViewModelBase, IPullReq
40
42
readonly ISyncSubmodulesCommand syncSubmodulesCommand ;
41
43
readonly IViewViewModelFactory viewViewModelFactory ;
42
44
readonly IGitService gitService ;
45
+
43
46
IModelService modelService ;
44
47
PullRequestDetailModel model ;
45
48
IActorViewModel author ;
@@ -98,6 +101,7 @@ public PullRequestDetailViewModel(
98
101
this . syncSubmodulesCommand = syncSubmodulesCommand ;
99
102
this . viewViewModelFactory = viewViewModelFactory ;
100
103
this . gitService = gitService ;
104
+
101
105
Files = files ;
102
106
103
107
Checkout = ReactiveCommand . CreateFromObservable (
@@ -137,6 +141,9 @@ public PullRequestDetailViewModel(
137
141
ShowAnnotations = ReactiveCommand . Create < IPullRequestCheckViewModel > ( DoShowAnnotations ) ;
138
142
}
139
143
144
+ [ Import ( AllowDefault = true ) ]
145
+ private IStaticReviewFileMapManager StaticReviewFileMapManager { get ; set ; }
146
+
140
147
private void DoOpenDetailsUrl ( )
141
148
{
142
149
usageTracker . IncrementCounter ( measuresModel => measuresModel . NumberOfPRDetailsOpenInGitHub ) . Forget ( ) ;
@@ -490,6 +497,7 @@ public string GetLocalFilePath(IPullRequestFileNode file)
490
497
public override void Activated ( )
491
498
{
492
499
active = true ;
500
+ this . StaticReviewFileMapManager ? . RegisterStaticReviewFileMap ( this ) ;
493
501
494
502
if ( refreshOnActivate )
495
503
{
@@ -499,7 +507,43 @@ public override void Activated()
499
507
}
500
508
501
509
/// <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
+ }
503
547
504
548
/// <inheritdoc/>
505
549
protected override void Dispose ( bool disposing )
0 commit comments