Skip to content

Commit e5b0587

Browse files
committed
[ty] Fix definition peek
1 parent 7a541f5 commit e5b0587

File tree

1 file changed

+71
-84
lines changed

1 file changed

+71
-84
lines changed

playground/ty/src/Editor/Editor.tsx

Lines changed: 71 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ class PlaygroundServer
584584
new TyPosition(position.lineNumber, position.column),
585585
);
586586

587-
return mapNavigationTargets(links);
587+
return this.mapNavigationTargets(links);
588588
}
589589

590590
provideDeclaration(
@@ -603,7 +603,7 @@ class PlaygroundServer
603603
new TyPosition(position.lineNumber, position.column),
604604
);
605605

606-
return mapNavigationTargets(links);
606+
return this.mapNavigationTargets(links);
607607
}
608608

609609
provideDefinition(
@@ -622,7 +622,7 @@ class PlaygroundServer
622622
new TyPosition(position.lineNumber, position.column),
623623
);
624624

625-
return mapNavigationTargets(links);
625+
return this.mapNavigationTargets(links);
626626
}
627627

628628
provideReferences(
@@ -643,7 +643,7 @@ class PlaygroundServer
643643
new TyPosition(position.lineNumber, position.column),
644644
);
645645

646-
return mapNavigationTargets(links);
646+
return this.mapNavigationTargets(links);
647647
}
648648

649649
openCodeEditor(
@@ -653,36 +653,18 @@ class PlaygroundServer
653653
): boolean {
654654
const files = this.props.files;
655655

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
657664
if (resource.scheme === "vendored") {
665+
// Get the file handle to track that we're viewing a vendored file
658666
const vendoredPath = this.getVendoredPath(resource);
659-
// Get a file handle for this vendored file
660667
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
686668
this.props.onVendoredFileChange(fileHandle);
687669
} else {
688670
// Handle regular files
@@ -694,44 +676,21 @@ class PlaygroundServer
694676
return false;
695677
}
696678

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
718680
if (files.selected !== fileId) {
719-
source.setModel(model);
720-
721681
this.props.onOpenFile(fileId);
722682
}
723683
}
724684

685+
source.setModel(model);
686+
725687
if (selectionOrPosition != null) {
726688
if (Position.isIPosition(selectionOrPosition)) {
727689
source.setPosition(selectionOrPosition);
728-
source.revealPosition(selectionOrPosition);
690+
source.revealPositionInCenterIfOutsideViewport(selectionOrPosition);
729691
} else {
730692
source.setSelection(selectionOrPosition);
731-
source.revealPosition({
732-
lineNumber: selectionOrPosition.startLineNumber,
733-
column: selectionOrPosition.startColumn,
734-
});
693+
source.revealRangeNearTopIfOutsideViewport(selectionOrPosition);
735694
}
736695
}
737696

@@ -764,6 +723,59 @@ class PlaygroundServer
764723
return null;
765724
}
766725

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+
767779
dispose() {
768780
this.hoverDisposable.dispose();
769781
this.editorOpenerDisposable.dispose();
@@ -830,31 +842,6 @@ function generateMonacoTokens(
830842
return { data: Uint32Array.from(result) };
831843
}
832844

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-
858845
function mapCompletionKind(kind: CompletionKind): CompletionItemKind {
859846
switch (kind) {
860847
case CompletionKind.Text:

0 commit comments

Comments
 (0)