Skip to content

Commit

Permalink
Final (?) pane-renaming fix
Browse files Browse the repository at this point in the history
  • Loading branch information
OfekShilon committed Aug 17, 2024
1 parent 12f6bfc commit 898c63e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 20 deletions.
14 changes: 0 additions & 14 deletions static/multifile-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -273,17 +273,7 @@ export class MultifileService {
}
}

private static isValidFile(file: MultifileFile): boolean {
return file.editorId > 0 || !!file.filename;
}

private filterOutNonsense() {
this.files = this.files.filter((file: MultifileFile) => MultifileService.isValidFile(file));
}

public getFiles(): Array<FiledataPair> {
this.filterOutNonsense();

const filtered = this.files.filter((file: MultifileFile) => {
return !file.isMainSource && file.isIncluded;
});
Expand Down Expand Up @@ -435,8 +425,6 @@ export class MultifileService {
}

public forEachOpenFile(callback: (File) => void) {
this.filterOutNonsense();

for (const file of this.files) {
if (file.isOpen && file.editorId > 0) {
callback(file);
Expand All @@ -445,8 +433,6 @@ export class MultifileService {
}

public forEachFile(callback: (File) => void) {
this.filterOutNonsense();

for (const file of this.files) {
callback(file);
}
Expand Down
27 changes: 21 additions & 6 deletions static/panes/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,13 @@ export class Tree {
this.updateButtons(state);
}

private paneRenamedExternally() {
this.multifileService.forEachFile(
(file: MultifileFile) => (file.filename = this.hub.getEditorById(file.editorId)?.getPaneName() ?? ''),
);
this.refresh();
}

private initCallbacks() {
this.container.on('resize', this.resize, this);
this.container.on('shown', this.resize, this);
Expand All @@ -198,7 +205,7 @@ export class Tree {
});
this.container.on('destroy', this.close, this);

this.eventHub.on('renamePane', this.updateState.bind(this));
this.eventHub.on('renamePane', this.paneRenamedExternally, this);

this.eventHub.on('editorOpen', this.onEditorOpen, this);
this.eventHub.on('editorClose', this.onEditorClose, this);
Expand Down Expand Up @@ -281,11 +288,11 @@ export class Tree {

private onEditorOpen(editorId: number) {
const file = this.multifileService.getFileByEditorId(editorId);
this.refresh();
this.sendChangesToAllEditors();
if (file) return;

this.multifileService.addFileForEditorId(editorId);
this.refresh();
this.sendChangesToAllEditors();
}

private onEditorClose(editorId: number) {
Expand Down Expand Up @@ -445,6 +452,13 @@ export class Tree {

dragSource.on('click', () => {
this.hub.addInEditorStackIfPossible(dragConfig.bind(this));
// at this point the editor is initialized with default contents
const mfsState = this.multifileService.getState();
const newFile = mfsState.files.find(file => file.fileId === mfsState.newFileId - 1);
if (newFile) {
newFile.content = this.hub.getEditorById(newFile.editorId)?.getSource() ?? '';
newFile.filename = this.hub.getEditorById(newFile.editorId)?.getPaneName() ?? '';
}
});
}

Expand All @@ -460,16 +474,17 @@ export class Tree {
let editor;
const editorId = this.hub.nextEditorId();

if (file) {
if (!file) {
this.multifileService.addFileForEditorId(editorId);
editor = Components.getEditor(this.multifileService.getLanguageId(), editorId);
} else {
file.editorId = editorId;
editor = Components.getEditor(file.langId, editorId);

editor.componentState.source = file.content;
if (file.filename) {
editor.componentState.filename = file.filename;
}
} else {
editor = Components.getEditor(this.multifileService.getLanguageId(), editorId);
}

return editor;
Expand Down

0 comments on commit 898c63e

Please sign in to comment.