@@ -584,7 +584,7 @@ class PlaygroundServer
584
584
new TyPosition ( position . lineNumber , position . column ) ,
585
585
) ;
586
586
587
- return mapNavigationTargets ( links ) ;
587
+ return this . mapNavigationTargets ( links ) ;
588
588
}
589
589
590
590
provideDeclaration (
@@ -603,7 +603,7 @@ class PlaygroundServer
603
603
new TyPosition ( position . lineNumber , position . column ) ,
604
604
) ;
605
605
606
- return mapNavigationTargets ( links ) ;
606
+ return this . mapNavigationTargets ( links ) ;
607
607
}
608
608
609
609
provideDefinition (
@@ -622,7 +622,7 @@ class PlaygroundServer
622
622
new TyPosition ( position . lineNumber , position . column ) ,
623
623
) ;
624
624
625
- return mapNavigationTargets ( links ) ;
625
+ return this . mapNavigationTargets ( links ) ;
626
626
}
627
627
628
628
provideReferences (
@@ -643,7 +643,7 @@ class PlaygroundServer
643
643
new TyPosition ( position . lineNumber , position . column ) ,
644
644
) ;
645
645
646
- return mapNavigationTargets ( links ) ;
646
+ return this . mapNavigationTargets ( links ) ;
647
647
}
648
648
649
649
openCodeEditor (
@@ -653,36 +653,18 @@ class PlaygroundServer
653
653
) : boolean {
654
654
const files = this . props . files ;
655
655
656
- // Check if this is a vendored file
656
+ // Model should already exist from mapNavigationTargets for both vendored and regular files
657
+ const model = this . monaco . editor . getModel ( resource ) ;
658
+ if ( model == null ) {
659
+ // Model should have been created by mapNavigationTargets
660
+ return false ;
661
+ }
662
+
663
+ // Handle file-specific logic
657
664
if ( resource . scheme === "vendored" ) {
665
+ // Get the file handle to track that we're viewing a vendored file
658
666
const vendoredPath = this . getVendoredPath ( resource ) ;
659
- // Get a file handle for this vendored file
660
667
const fileHandle = this . getOrCreateVendoredFileHandle ( vendoredPath ) ;
661
-
662
- // Create or get the model for the vendored file
663
- let model = this . monaco . editor . getModel ( resource ) ;
664
-
665
- if ( model == null ) {
666
- // Read the vendored file content using the file handle
667
- const content = this . props . workspace . sourceText ( fileHandle ) ;
668
- // Ensure vendored files get proper Python language features
669
- model = this . monaco . editor . createModel ( content , "python" , resource ) ;
670
- }
671
-
672
- // Set the model and reveal the position
673
- source . setModel ( model ) ;
674
-
675
- if ( selectionOrPosition != null ) {
676
- if ( Position . isIPosition ( selectionOrPosition ) ) {
677
- source . setPosition ( selectionOrPosition ) ;
678
- source . revealPositionInCenterIfOutsideViewport ( selectionOrPosition ) ;
679
- } else {
680
- source . setSelection ( selectionOrPosition ) ;
681
- source . revealRangeNearTopIfOutsideViewport ( selectionOrPosition ) ;
682
- }
683
- }
684
-
685
- // Track that we're now viewing a vendored file
686
668
this . props . onVendoredFileChange ( fileHandle ) ;
687
669
} else {
688
670
// Handle regular files
@@ -694,44 +676,21 @@ class PlaygroundServer
694
676
return false ;
695
677
}
696
678
697
- const handle = files . handles [ fileId ] ;
698
- if ( handle == null ) {
699
- return false ;
700
- }
701
-
702
- let model = this . monaco . editor . getModel ( resource ) ;
703
- if ( model == null ) {
704
- const language = isPythonFile ( handle ) ? "python" : undefined ;
705
- model = this . monaco . editor . createModel (
706
- files . contents [ fileId ] ,
707
- language ,
708
- resource ,
709
- ) ;
710
- } else {
711
- // Update model content to match current file state
712
- model . setValue ( files . contents [ fileId ] ) ;
713
- }
714
-
715
- // it's a bit hacky to create the model manually
716
- // but only using `onOpenFile` isn't enough
717
- // because the model doesn't get updated until the next render.
679
+ // Set the model and trigger UI updates
718
680
if ( files . selected !== fileId ) {
719
- source . setModel ( model ) ;
720
-
721
681
this . props . onOpenFile ( fileId ) ;
722
682
}
723
683
}
724
684
685
+ source . setModel ( model ) ;
686
+
725
687
if ( selectionOrPosition != null ) {
726
688
if ( Position . isIPosition ( selectionOrPosition ) ) {
727
689
source . setPosition ( selectionOrPosition ) ;
728
- source . revealPosition ( selectionOrPosition ) ;
690
+ source . revealPositionInCenterIfOutsideViewport ( selectionOrPosition ) ;
729
691
} else {
730
692
source . setSelection ( selectionOrPosition ) ;
731
- source . revealPosition ( {
732
- lineNumber : selectionOrPosition . startLineNumber ,
733
- column : selectionOrPosition . startColumn ,
734
- } ) ;
693
+ source . revealRangeNearTopIfOutsideViewport ( selectionOrPosition ) ;
735
694
}
736
695
}
737
696
@@ -764,6 +723,59 @@ class PlaygroundServer
764
723
return null ;
765
724
}
766
725
726
+ private mapNavigationTargets ( links : any [ ] ) : languages . LocationLink [ ] {
727
+ const result = links . map ( ( link ) => {
728
+ const uri = Uri . parse ( link . path ) ;
729
+
730
+ // Pre-create models to ensure peek definition works
731
+ if ( this . monaco . editor . getModel ( uri ) == null ) {
732
+ if ( uri . scheme === "vendored" ) {
733
+ // Handle vendored files
734
+ const vendoredPath = this . getVendoredPath ( uri ) ;
735
+ const fileHandle = this . getOrCreateVendoredFileHandle ( vendoredPath ) ;
736
+ const content = this . props . workspace . sourceText ( fileHandle ) ;
737
+ this . monaco . editor . createModel ( content , "python" , uri ) ;
738
+ } else {
739
+ // Handle regular files
740
+ const fileId = this . props . files . index . find ( ( file ) => {
741
+ return Uri . file ( file . name ) . toString ( ) === uri . toString ( ) ;
742
+ } ) ?. id ;
743
+
744
+ if ( fileId != null ) {
745
+ const handle = this . props . files . handles [ fileId ] ;
746
+ if ( handle != null ) {
747
+ const language = isPythonFile ( handle ) ? "python" : undefined ;
748
+ this . monaco . editor . createModel (
749
+ this . props . files . contents [ fileId ] ,
750
+ language ,
751
+ uri ,
752
+ ) ;
753
+ }
754
+ }
755
+ }
756
+ }
757
+
758
+ const targetSelection =
759
+ link . selection_range == null
760
+ ? undefined
761
+ : tyRangeToMonacoRange ( link . selection_range ) ;
762
+
763
+ const originSelection =
764
+ link . origin_selection_range == null
765
+ ? undefined
766
+ : tyRangeToMonacoRange ( link . origin_selection_range ) ;
767
+
768
+ return {
769
+ uri : uri ,
770
+ range : tyRangeToMonacoRange ( link . full_range ) ,
771
+ targetSelectionRange : targetSelection ,
772
+ originSelectionRange : originSelection ,
773
+ } as languages . LocationLink ;
774
+ } ) ;
775
+
776
+ return result ;
777
+ }
778
+
767
779
dispose ( ) {
768
780
this . hoverDisposable . dispose ( ) ;
769
781
this . editorOpenerDisposable . dispose ( ) ;
@@ -830,31 +842,6 @@ function generateMonacoTokens(
830
842
return { data : Uint32Array . from ( result ) } ;
831
843
}
832
844
833
- function mapNavigationTargets ( links : any [ ] ) : languages . LocationLink [ ] {
834
- const result = links . map ( ( link ) => {
835
- const targetSelection =
836
- link . selection_range == null
837
- ? undefined
838
- : tyRangeToMonacoRange ( link . selection_range ) ;
839
-
840
- const originSelection =
841
- link . origin_selection_range == null
842
- ? undefined
843
- : tyRangeToMonacoRange ( link . origin_selection_range ) ;
844
-
845
- const locationLink = {
846
- uri : Uri . parse ( link . path ) ,
847
- range : tyRangeToMonacoRange ( link . full_range ) ,
848
- targetSelectionRange : targetSelection ,
849
- originSelectionRange : originSelection ,
850
- } as languages . LocationLink ;
851
-
852
- return locationLink ;
853
- } ) ;
854
-
855
- return result ;
856
- }
857
-
858
845
function mapCompletionKind ( kind : CompletionKind ) : CompletionItemKind {
859
846
switch ( kind ) {
860
847
case CompletionKind . Text :
0 commit comments