Skip to content

Commit 074d32e

Browse files
authored
[ty] Fix playground crash when renaming file (#23838)
1 parent e7e7f2c commit 074d32e

1 file changed

Lines changed: 19 additions & 29 deletions

File tree

playground/ty/src/Editor/Editor.tsx

Lines changed: 19 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -403,21 +403,13 @@ class PlaygroundServer
403403
// eslint-disable-next-line @typescript-eslint/no-unused-vars
404404
_token: CancellationToken,
405405
): languages.ProviderResult<languages.DocumentHighlight[]> {
406-
const workspace = this.props.workspace;
407-
const selectedFile = this.props.files.selected;
408-
409-
if (selectedFile == null) {
410-
return;
411-
}
412-
413-
const selectedHandle = this.props.files.handles[selectedFile];
414-
415-
if (selectedHandle == null) {
416-
return;
406+
const fileHandle = this.getFileHandleForModel(model);
407+
if (fileHandle == null) {
408+
return undefined;
417409
}
418410

419-
const highlights = workspace.documentHighlights(
420-
selectedHandle,
411+
const highlights = this.props.workspace.documentHighlights(
412+
fileHandle,
421413
new TyPosition(position.lineNumber, position.column),
422414
);
423415

@@ -502,6 +494,14 @@ class PlaygroundServer
502494
this.inVendoredFileCondition.set(isViewingVendoredFile);
503495
}
504496

497+
private getPlaygroundFileIdForUri(uri: Uri): FileId | null {
498+
return (
499+
this.props.files.index.find((file) => {
500+
return Uri.file(file.name).toString() === uri.toString();
501+
})?.id ?? null
502+
);
503+
}
504+
505505
private getOrCreateVendoredFileHandle(vendoredPath: string): FileHandle {
506506
const cachedHandle = this.vendoredFileHandles.get(vendoredPath);
507507
// Check if we already have a handle for this vendored file
@@ -524,13 +524,12 @@ class PlaygroundServer
524524
return this.getOrCreateVendoredFileHandle(vendoredPath);
525525
}
526526

527-
// Handle regular user files
528-
const selectedFile = this.props.files.selected;
529-
if (selectedFile == null) {
527+
const fileId = this.getPlaygroundFileIdForUri(model.uri);
528+
if (fileId == null) {
530529
return null;
531530
}
532531

533-
return this.props.files.handles[selectedFile];
532+
return this.props.files.handles[fileId] ?? null;
534533
}
535534

536535
private formatSignatureHelp(
@@ -805,9 +804,7 @@ class PlaygroundServer
805804
this.props.onVendoredFileChange(fileHandle);
806805
} else {
807806
// Handle regular files
808-
const fileId = files.index.find((file) => {
809-
return Uri.file(file.name).toString() === resource.toString();
810-
})?.id;
807+
const fileId = this.getPlaygroundFileIdForUri(resource);
811808

812809
if (fileId == null) {
813810
return false;
@@ -837,12 +834,7 @@ class PlaygroundServer
837834
provideDocumentFormattingEdits(
838835
model: editor.ITextModel,
839836
): languages.ProviderResult<languages.TextEdit[]> {
840-
if (this.props.files.selected == null) {
841-
return null;
842-
}
843-
844-
const fileHandle = this.props.files.handles[this.props.files.selected];
845-
837+
const fileHandle = this.getFileHandleForModel(model);
846838
if (fileHandle == null) {
847839
return null;
848840
}
@@ -873,9 +865,7 @@ class PlaygroundServer
873865
this.monaco.editor.createModel(content, "python", uri);
874866
} else {
875867
// Handle regular files
876-
const fileId = this.props.files.index.find((file) => {
877-
return Uri.file(file.name).toString() === uri.toString();
878-
})?.id;
868+
const fileId = this.getPlaygroundFileIdForUri(uri);
879869

880870
if (fileId != null) {
881871
const handle = this.props.files.handles[fileId];

0 commit comments

Comments
 (0)