Skip to content

Commit 67bab31

Browse files
committed
Handle renames of current file in VSCode
1 parent 9b6f4dd commit 67bab31

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

packages/app/src/app/components/CodeEditor/VSCode/MonacoReactComponent.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ class MonacoEditor extends React.PureComponent {
114114
const codeEditorService = services.get(ICodeEditorService);
115115
const textFileService = services.get(ITextFileService);
116116
const editorService = services.get(IEditorService);
117-
118117
const lifecycleService = services.get(ILifecycleService);
119118

120119
lifecycleService.phase = 3; // Running

packages/app/src/app/components/CodeEditor/VSCode/index.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
8585

8686
sandbox: $PropertyType<Props, 'sandbox'>;
8787
currentModule: $PropertyType<Props, 'currentModule'>;
88+
currentTitle: string;
89+
currentDirectoryShortid: ?string;
8890
settings: $PropertyType<Props, 'settings'>;
8991
dependencies: ?$PropertyType<Props, 'dependencies'>;
9092
tsconfig: ?$PropertyType<Props, 'tsconfig'>;
@@ -104,6 +106,8 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
104106
};
105107
this.sandbox = props.sandbox;
106108
this.currentModule = props.currentModule;
109+
this.currentTitle = props.currentModule.title;
110+
this.currentDirectoryShortid = props.currentModule.directoryShortid;
107111
this.settings = props.settings;
108112
this.dependencies = props.dependencies;
109113

@@ -172,6 +176,41 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
172176
}
173177
}
174178

179+
updateModules = () => {
180+
if (
181+
this.currentTitle !== this.currentModule.title ||
182+
this.currentDirectoryShortid !== this.currentModule.directoryShortid
183+
) {
184+
const id = this.currentModule.id;
185+
const title = this.currentModule.title;
186+
const directoryShortid = this.currentModule.directoryShortid;
187+
// Rename of current file.
188+
this.currentTitle = this.currentModule.title;
189+
this.currentDirectoryShortid = this.currentModule.directoryShortid;
190+
191+
const editor = this.editor.getActiveCodeEditor();
192+
if (editor && editor.getValue() === (this.currentModule.code || '')) {
193+
const model = editor.model;
194+
const newPath = getModulePath(
195+
this.sandbox.modules,
196+
this.sandbox.directories,
197+
this.currentModule.id
198+
);
199+
this.editor.textFileService
200+
.move(model.uri, this.monaco.Uri.file(newPath))
201+
.then(() => {
202+
if (
203+
this.currentModule.id === id &&
204+
this.currentModule.title === title &&
205+
this.currentModule.directoryShortid === directoryShortid
206+
) {
207+
this.editor.openFile(newPath);
208+
}
209+
});
210+
}
211+
}
212+
};
213+
175214
getPrettierConfig = () => {
176215
try {
177216
const module = resolveModule(
@@ -408,6 +447,8 @@ class MonacoEditor extends React.Component<Props, State> implements Editor {
408447

409448
this.swapDocuments(oldModule, newModule).then(() => {
410449
this.currentModule = newModule;
450+
this.currentTitle = newModule.title;
451+
this.currentDirectoryShortid = newModule.directoryShortid;
411452

412453
if (errors) {
413454
this.setErrors(errors);

0 commit comments

Comments
 (0)