Skip to content

Commit

Permalink
minor cleanups post #1693 and #1784 (#1788)
Browse files Browse the repository at this point in the history
  • Loading branch information
gayathrir11 authored Dec 22, 2022
1 parent effcc07 commit e772202
Show file tree
Hide file tree
Showing 7 changed files with 320 additions and 536 deletions.
3 changes: 3 additions & 0 deletions .changeset/loud-frogs-grab.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
'@finos/legend-application-studio': patch
---
67 changes: 18 additions & 49 deletions packages/legend-application-studio/src/stores/EditorGraphState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ import { PACKAGEABLE_ELEMENT_TYPE } from './shared/ModelClassifierUtils.js';
import { GlobalTestRunnerState } from './sidebar-state/testable/GlobalTestRunnerState.js';
import { LEGEND_STUDIO_APP_EVENT } from './LegendStudioAppEvent.js';
import { ExplorerTreeState } from './ExplorerTreeState.js';
import { FileGenerationViewerState } from './editor-state/FileGenerationViewerState.js';

export enum GraphBuilderStatus {
SUCCEEDED = 'SUCCEEDED',
Expand Down Expand Up @@ -469,7 +468,9 @@ export class EditorGraphState {
};
}
}
this.editorStore.setGraphEditMode(GRAPH_EDITOR_MODE.GRAMMAR_TEXT);
yield flowResult(
this.editorStore.setGraphEditMode(GRAPH_EDITOR_MODE.GRAMMAR_TEXT),
);
yield flowResult(
this.globalCompileInTextMode({
ignoreBlocking: true,
Expand Down Expand Up @@ -665,13 +666,8 @@ export class EditorGraphState {
);
return FormModeCompilationOutcome.FAILED;
}
this.editorStore.setGraphEditMode(GRAPH_EDITOR_MODE.GRAMMAR_TEXT);
// Stop change detection as we don't need the actual change detection in text mode
this.editorStore.changeDetectionState.stop();
this.editorStore.changeDetectionState.computeLocalChangesInTextMode(
(yield this.editorStore.graphManagerState.graphManager.pureCodeToEntities(
this.editorStore.grammarTextEditorState.graphGrammarText,
)) as Entity[],
yield flowResult(
this.editorStore.setGraphEditMode(GRAPH_EDITOR_MODE.GRAMMAR_TEXT),
);
yield flowResult(
this.globalCompileInTextMode({
Expand Down Expand Up @@ -829,7 +825,9 @@ export class EditorGraphState {
this.editorStore.changeDetectionState.getCurrentGraphHash();
this.editorStore.grammarTextEditorState.setGraphGrammarText('');
this.editorStore.grammarTextEditorState.resetCurrentElementLabelRegexString();
this.editorStore.setGraphEditMode(GRAPH_EDITOR_MODE.FORM);
yield flowResult(
this.editorStore.setGraphEditMode(GRAPH_EDITOR_MODE.FORM),
);
if (this.editorStore.tabManagerState.currentTab) {
this.editorStore.tabManagerState.openTab(
this.editorStore.tabManagerState.currentTab,
Expand Down Expand Up @@ -867,8 +865,13 @@ export class EditorGraphState {
actions: [
{
label: 'Discard Changes',
handler: (): void =>
this.editorStore.setGraphEditMode(GRAPH_EDITOR_MODE.FORM),
handler: () => {
flowResult(
this.editorStore.setGraphEditMode(GRAPH_EDITOR_MODE.FORM),
).catch(
this.editorStore.applicationStore.alertUnhandledError,
);
},
type: ActionAlertActionType.PROCEED_WITH_CAUTION,
},
{
Expand Down Expand Up @@ -919,15 +922,12 @@ export class EditorGraphState {
const systemTreeData = this.editorStore.explorerTreeState.systemTreeData;
const dependencyTreeData =
this.editorStore.explorerTreeState.dependencyTreeData;
const fileGenTreeData =
this.editorStore.explorerTreeState.fileGenerationTreeData;
const selectedNodeId = this.editorStore.explorerTreeState.selectedNode?.id;
this.editorStore.explorerTreeState = new ExplorerTreeState(
this.editorStore,
);
this.editorStore.explorerTreeState.systemTreeData = systemTreeData;
this.editorStore.explorerTreeState.dependencyTreeData = dependencyTreeData;
this.editorStore.explorerTreeState.fileGenerationTreeData = fileGenTreeData;
this.editorStore.explorerTreeState.buildTreeInTextMode();
this.editorStore.explorerTreeState.openExplorerTreeNodes(
mainTreeOpenedNodeIds,
Expand Down Expand Up @@ -1027,14 +1027,9 @@ export class EditorGraphState {
* hold any reference to the actual graph.
*/
const openedTabPaths: string[] = [];
// We can store file genration editor states as is as they don't hold any references to the graph.
const openedGeneratedFileTabStates: FileGenerationViewerState[] = [];
this.editorStore.tabManagerState.tabs.forEach((state: TabState) => {
if (state instanceof ElementEditorState) {
openedTabPaths.push(state.elementPath);
} else if (state instanceof FileGenerationViewerState) {
openedTabPaths.push(state.generatedFilePath);
openedGeneratedFileTabStates.push(state);
}
});
// Only stores editor state for file generation editors as they don't hold any references to the
Expand Down Expand Up @@ -1104,7 +1099,6 @@ export class EditorGraphState {
*/
this.editorStore.tabManagerState.recoverTabs(
openedTabPaths,
openedGeneratedFileTabStates,
currentTabState,
currentTabElementPath,
true,
Expand Down Expand Up @@ -1213,14 +1207,9 @@ export class EditorGraphState {
* hold any reference to the actual graph.
*/
const openedTabPaths: string[] = [];
// We can store file genration editor states as is as they don't hold any references to the graph.
const openedGeneratedFileTabStates: FileGenerationViewerState[] = [];
this.editorStore.tabManagerState.tabs.forEach((state: TabState) => {
if (state instanceof ElementEditorState) {
openedTabPaths.push(state.elementPath);
} else if (state instanceof FileGenerationViewerState) {
openedTabPaths.push(state.generatedFilePath);
openedGeneratedFileTabStates.push(state);
}
});
// Only stores editor state for file generation editors as they don't hold any references to the
Expand Down Expand Up @@ -1287,7 +1276,6 @@ export class EditorGraphState {
*/
this.editorStore.tabManagerState.recoverTabs(
openedTabPaths,
openedGeneratedFileTabStates,
currentTabState,
currentTabElementPath,
false,
Expand All @@ -1306,24 +1294,10 @@ export class EditorGraphState {
LogEvent.create(GRAPH_MANAGER_EVENT.GRAPH_BUILDER_FAILURE),
error,
);
this.editorStore.changeDetectionState.stop(true); // force stop change detection
this.isUpdatingGraph = false;
// Note: in the future this function will probably be ideal to refactor when we have different classes for each mode
// as we would handle this error differently in `text` mode and `form` mode.
if (error instanceof GraphBuilderError && this.editorStore.isInFormMode) {
this.editorStore.applicationStore.setBlockingAlert({
message: `Can't build graph: ${error.message}`,
prompt: 'Refreshing full application...',
showLoading: true,
});
this.editorStore.tabManagerState.closeAllTabs();
this.editorStore.cleanUp();
yield flowResult(this.editorStore.buildGraph(entities));
} else {
this.editorStore.applicationStore.notifyError(
`Can't build graph: ${error.message}`,
);
}
this.editorStore.applicationStore.notifyError(
`Can't build graph: ${error.message}`,
);
} finally {
this.isUpdatingApplication = false;
this.editorStore.applicationStore.setBlockingAlert(undefined);
Expand All @@ -1346,13 +1320,9 @@ export class EditorGraphState {
* Backup and editor states info before resetting
*/
const openedTabEditorPaths: string[] = [];
const openedGeneratedFileTabStates: FileGenerationViewerState[] = [];
this.editorStore.tabManagerState.tabs.forEach((state: TabState) => {
if (state instanceof ElementEditorState) {
openedTabEditorPaths.push(state.elementPath);
} else if (state instanceof FileGenerationViewerState) {
openedTabEditorPaths.push(state.generatedFilePath);
openedGeneratedFileTabStates.push(state);
}
});
const currentTabState =
Expand Down Expand Up @@ -1381,7 +1351,6 @@ export class EditorGraphState {
this.updateExplorerTree();
this.editorStore.tabManagerState.recoverTabs(
openedTabEditorPaths,
openedGeneratedFileTabStates,
currentTabState,
currentTabElementPath,
true,
Expand Down
22 changes: 12 additions & 10 deletions packages/legend-application-studio/src/stores/EditorStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export class EditorStore implements CommandRegistrar {
setActiveAuxPanelMode: action,
cleanUp: action,
reset: action,
setGraphEditMode: action,
setGraphEditMode: flow,
setActiveActivity: action,

initialize: flow,
Expand Down Expand Up @@ -340,10 +340,19 @@ export class EditorStore implements CommandRegistrar {
this.mode = val;
}

setGraphEditMode(graphEditor: GRAPH_EDITOR_MODE): void {
*setGraphEditMode(graphEditor: GRAPH_EDITOR_MODE): GeneratorFn<void> {
this.graphEditMode = graphEditor;
this.changeLocalChangesState();
this.graphState.clearProblems();
if (graphEditor === GRAPH_EDITOR_MODE.GRAMMAR_TEXT) {
// Stop change detection as we don't need the actual change detection in text mode
this.changeDetectionState.stop();
this.changeDetectionState.computeLocalChangesInTextMode(
(yield this.graphManagerState.graphManager.pureCodeToEntities(
this.grammarTextEditorState.graphGrammarText,
)) as Entity[],
);
}
}

cleanUp(): void {
Expand Down Expand Up @@ -1113,20 +1122,13 @@ export class EditorStore implements CommandRegistrar {
return;
}
this.applicationStore.setBlockingAlert(undefined);
this.setGraphEditMode(GRAPH_EDITOR_MODE.GRAMMAR_TEXT);
yield flowResult(this.setGraphEditMode(GRAPH_EDITOR_MODE.GRAMMAR_TEXT));
// navigate to the currently opened element immediately after entering text mode editor
if (this.tabManagerState.currentTab instanceof ElementEditorState) {
this.grammarTextEditorState.setCurrentElementLabelRegexString(
this.tabManagerState.currentTab.element,
);
}
// Stop change detection as we don't need the actual change detection in text mode
this.changeDetectionState.stop();
this.changeDetectionState.computeLocalChangesInTextMode(
(yield this.graphManagerState.graphManager.pureCodeToEntities(
this.grammarTextEditorState.graphGrammarText,
)) as Entity[],
);
} else if (this.isInGrammarTextMode) {
yield flowResult(this.graphState.leaveTextMode());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { makeObservable, action } from 'mobx';
import type { EditorState } from './editor-state/EditorState.js';
import { ElementEditorState } from './editor-state/element-editor-state/ElementEditorState.js';
import { EntityDiffViewerState } from './editor-state/entity-diff-editor-state/EntityDiffEditorState.js';
import type { FileGenerationViewerState } from './editor-state/FileGenerationViewerState.js';
import type { EditorStore } from './EditorStore.js';

export class EditorTabManagerState extends TabManagerState {
Expand Down Expand Up @@ -94,21 +93,10 @@ export class EditorTabManagerState extends TabManagerState {
*/
recoverTabs = (
openedTabEditorPaths: string[],
openedGeneratedFileTabStates: FileGenerationViewerState[],
currentTabState: EditorState | undefined,
currentTabElementPath: string | undefined,
shouldRecoverTabs: boolean,
): void => {
const getElemenetPathFromFilePath = (filePath: string): string => {
const paths = filePath.split('/');
let elementPath = '';
const fileName = paths.slice(-1)[0]?.split('.')[0];
paths
.slice(1, paths.length - 1)
.forEach((path) => (elementPath = `${elementPath + path}::`));
elementPath = `${elementPath + fileName}`;
return elementPath;
};
if (shouldRecoverTabs) {
this.tabs = openedTabEditorPaths
.map((editorPath) => {
Expand All @@ -121,14 +109,7 @@ export class EditorTabManagerState extends TabManagerState {
correspondingElement,
);
}
const fileGenerationViewerEditor = openedGeneratedFileTabStates.find(
(editorState) =>
editorState.generatedFilePath === editorPath &&
this.editorStore.graphManagerState.graph.getNullableElement(
getElemenetPathFromFilePath(editorState.file.path),
) !== undefined,
);
return fileGenerationViewerEditor;
return undefined;
})
.filter(isNonNullable);
this.setCurrentTab(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,10 @@ export class ExplorerTreeState {
this.editorStore.graphManagerState.graph.generationModel.root,
ExplorerTreeRootPackageLabel.MODEL_GENERATION,
);
if (!this.fileGenerationTreeData) {
this.fileGenerationTreeData = getGenerationTreeData(
this.editorStore.graphState.graphGenerationState.rootFileDirectory,
ExplorerTreeRootPackageLabel.FILE_GENERATION,
);
}
this.fileGenerationTreeData = getGenerationTreeData(
this.editorStore.graphState.graphGenerationState.rootFileDirectory,
ExplorerTreeRootPackageLabel.FILE_GENERATION,
);
this.setSelectedNode(undefined);
this.buildState.complete();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -474,7 +474,9 @@ export class ProjectViewerStore {
});
return false;
}
this.editorStore.setGraphEditMode(GRAPH_EDITOR_MODE.GRAMMAR_TEXT);
yield flowResult(
this.editorStore.setGraphEditMode(GRAPH_EDITOR_MODE.GRAMMAR_TEXT),
);
yield flowResult(
this.editorStore.graphState.globalCompileInTextMode({
ignoreBlocking: true,
Expand Down
Loading

0 comments on commit e772202

Please sign in to comment.