Skip to content

Commit

Permalink
Implement Structure Definition Load
Browse files Browse the repository at this point in the history
  • Loading branch information
LightFLP committed Jan 15, 2024
1 parent c283f9d commit da7fdf4
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 16 deletions.
25 changes: 23 additions & 2 deletions src/extension/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export class EditorProvider implements vscode.CustomTextEditorProvider {
type: message_type,
text: document.getText(),
rules: fs.existsSync(rulesFile) ? JSON.parse(fs.readFileSync(rulesFile, {encoding: 'utf8'})) : [],
structureDefinitionFile: fs.existsSync(structureDefinitionFile) ? JSON.parse(fs.readFileSync(structureDefinitionFile, {encoding: 'utf8'})) : []
});
}

Expand All @@ -61,7 +60,7 @@ export class EditorProvider implements vscode.CustomTextEditorProvider {

// Receive message from the webview.
webviewPanel.webview.onDidReceiveMessage(e => {

if (e.type === 'readFile') {
updateWebview('readFile');
} else if (e.type === 'saveRules') {
Expand All @@ -82,6 +81,28 @@ export class EditorProvider implements vscode.CustomTextEditorProvider {
}
});
}
else if (e.type === 'loadStructureDefinition') {
const options: vscode.OpenDialogOptions = {
title: 'Load Structure Definition',
canSelectMany: false,
openLabel: 'Load',
filters: {
'Stucture files': ['structure']
}
};
vscode.window.showOpenDialog(options).then(fileUri => {

if (fileUri && fileUri[0]) {
const filePath = fileUri[0].fsPath;
console.log('Selected file: ' + filePath);
webviewPanel.webview.postMessage({
type: 'loadedStructureDefinition',
structureDefinition: fs.existsSync(filePath) ? JSON.parse(fs.readFileSync(filePath, {encoding: 'utf8'})) : []
});
}

});
}
else if (e.type === 'exportData') {
const filename = document.fileName.split(".tracy")[0].split("_Tracy_export_")[0]
const _date = new Date().toISOString().slice(0,10).replace(/-/g, "");
Expand Down
19 changes: 16 additions & 3 deletions src/viewer/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import LogFile from "./LogFile";
import LogView from "./log/LogView";
import MinimapView from "./minimap/MinimapView";
import Tooltip from "@mui/material/Tooltip";
import { LogViewState, StructureMatchId, RowProperty, Segment, LogEntryCharMaps } from "./types";
import { LogViewState, StructureMatchId, RowProperty, Segment, LogEntryCharMaps, StructureDefinition } from "./types";
import {
COLUMN_0_HEADER_STYLE,
COLUMN_2_HEADER_STYLE,
Expand Down Expand Up @@ -57,6 +57,7 @@ interface State {

// Structure related
logFileAsString: string;
loadedStructureDefinition: StructureDefinition | null;
logEntryCharIndexMaps: LogEntryCharMaps | null;
selectedLogRows: string[][];
rowProperties: RowProperty[];
Expand Down Expand Up @@ -141,6 +142,10 @@ export default class App extends React.Component<Props, State> {
const showStructureDialog = this.previousSession.showStructureDialog;
this.setState({ showFlagsDialog, showStatesDialog, showStructureDialog });
}
} else if (message.type === "loadedStructureDefinition") {
const loadedStructureDefinition = message.structureDefinition;
console.log("loadedStructureDefinition - in App", loadedStructureDefinition);
this.setState({loadedStructureDefinition});
}
}

Expand Down Expand Up @@ -277,6 +282,12 @@ export default class App extends React.Component<Props, State> {
this.vscode.postMessage({ type: "saveStructureDefinition", structureDefinition: structureDefinition});
}

handleLoadingStructureDefinition() {
console.log("Loading definition - in App");
// console.log(structureDefinition);
this.vscode.postMessage({ type: "loadStructureDefinition"});
}

handleRowCollapse(rowIndex: number, isRendered: boolean) {
const newRowProps = this.state.rowProperties;
newRowProps[rowIndex].isRendered = isRendered;
Expand Down Expand Up @@ -335,6 +346,7 @@ export default class App extends React.Component<Props, State> {
this.setState({
showStructureDialog: !isClosing,
rowProperties: clearedSelectedRows,
loadedStructureDefinition: null,
structureMatches: [],
currentStructureMatchIndex: null,
currentStructureMatch: [],
Expand Down Expand Up @@ -796,13 +808,14 @@ export default class App extends React.Component<Props, State> {
logHeaderColumns={this.state.logFile.headers}
logHeaderColumnsTypes={logHeaderColumnTypes}
logSelectedRows={this.state.selectedLogRows}
loadedStructureDefinition={this.state.loadedStructureDefinition}
currentStructureMatchIndex={this.state.currentStructureMatchIndex}
numberOfMatches={this.state.structureMatches.length}
onClose={() => this.handleStructureDialog(true)}
onStructureUpdate={() => this.handleStructureUpdate(false)}
onMatchStructure={(expression) => this.handleStructureMatching(expression)}
onStructureDefinitionSave={(structureDefinition) => this.handleSavingStuctureDefinition(structureDefinition)}
onStructureDefinitionLoad={() => {}}
onStructureDefinitionSave={(structureDefinitionString) => this.handleSavingStuctureDefinition(structureDefinitionString)}
onStructureDefinitionLoad={() => {this.handleLoadingStructureDefinition()}}
onDefineSegment={(expression) => this.handleSegmentation(expression)}
onNavigateStructureMatches={(isGoingForward) =>
this.handleNavigation(isGoingForward, true)
Expand Down
1 change: 1 addition & 0 deletions src/viewer/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const defaultAppState = {
rowProperties: [],
logEntryCharIndexMaps: null,
showStructureDialog: false,
loadedStructureDefinition: null,
structureMatches: [],
currentStructureMatchIndex: null,
currentStructureMatch: [],
Expand Down
47 changes: 37 additions & 10 deletions src/viewer/structures/StructureDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Tooltip, { TooltipProps, tooltipClasses } from "@mui/material/Tooltip";
import CloseIcon from "@mui/icons-material/Close";
import IconButton from '@mui/material/IconButton';
import StructureTable from "./StructureTable";
import { ContextMenuItem, Header, StructureEntry, Wildcard } from "../types";
import { ContextMenuItem, Header, StructureDefinition, StructureEntry, Wildcard } from "../types";
import { StructureHeaderColumnType } from "../constants";
import { VSCodeButton } from "@vscode/webview-ui-toolkit/react";
import { useStructureQueryConstructor } from "../hooks/useStructureRegularExpressionManager";
Expand Down Expand Up @@ -38,6 +38,7 @@ interface Props {
logHeaderColumns: Header[];
logHeaderColumnsTypes: StructureHeaderColumnType[];
logSelectedRows: string[][];
loadedStructureDefinition: StructureDefinition | null;
currentStructureMatchIndex: number | null;
numberOfMatches: number;
onClose: () => void;
Expand All @@ -54,21 +55,25 @@ interface State {
wildcards: Wildcard[];
structureEntries: StructureEntry[];
isRemovingStructureEntries: boolean;
isLoadingStructureDefintion: boolean;
isStructureMatching: boolean;
structureHeaderColumns: Header[];
structureHeaderColumnsTypes: StructureHeaderColumnType[];
}

export default class StructureDialog extends React.Component<Props, State> {

constructor(props: Props) {
super(props);
const { logHeaderColumnsTypes, logSelectedRows } = this.props;
const { logHeaderColumns, logHeaderColumnsTypes, logSelectedRows } = this.props;
let structureEntries = constructStructureEntriesArray(logHeaderColumnsTypes, logSelectedRows);
structureEntries = removeLastStructureLink(structureEntries);

this.state = {
isRemovingStructureEntries: false,
isLoadingStructureDefintion: false,
isStructureMatching: this.props.numberOfMatches > 0 ? true : false,
structureHeaderColumns: logHeaderColumns,
structureHeaderColumnsTypes: logHeaderColumnsTypes,
structureEntries: structureEntries,
wildcards: [],
Expand All @@ -77,6 +82,7 @@ export default class StructureDialog extends React.Component<Props, State> {
//bind context for all functions used by the context and dropdown menus:
this.createWildcard = this.createWildcard.bind(this);
this.saveStructureDefinition = this.saveStructureDefinition.bind(this);
this.loadStructureDefinition = this.loadStructureDefinition.bind(this);
}

componentDidMount(): void {
Expand All @@ -90,6 +96,9 @@ export default class StructureDialog extends React.Component<Props, State> {
nextState: Readonly<State>,
_nextContext: any,
): boolean {
const isLoadingStructureDefinition = (
nextProps.loadedStructureDefinition !== null
);
const arelogHeaderColumnsUpdating = !isEqual(
this.props.logHeaderColumns,
nextProps.logHeaderColumns,
Expand Down Expand Up @@ -129,7 +138,10 @@ export default class StructureDialog extends React.Component<Props, State> {
nextState.isStructureMatching,
);

console.log("isLoadingStructureDefinition", isLoadingStructureDefinition);

if (
isLoadingStructureDefinition ||
arelogHeaderColumnsUpdating ||
arelogHeaderColumnTypesUpdating ||
arelogSelectedRowsUpdating ||
Expand All @@ -148,7 +160,20 @@ export default class StructureDialog extends React.Component<Props, State> {
}

componentDidUpdate(prevProps: Readonly<Props>, _prevState: Readonly<State>): void {
if (this.props.logSelectedRows !== prevProps.logSelectedRows) {
const {loadedStructureDefinition, logSelectedRows} = this.props;

if(this.state.isLoadingStructureDefintion && loadedStructureDefinition !== null) {
this.setState({
isLoadingStructureDefintion: false,
isStructureMatching: false,
structureHeaderColumns: loadedStructureDefinition.headerColumns,
structureHeaderColumnsTypes: loadedStructureDefinition.headerColumnsTypes,
structureEntries: loadedStructureDefinition.entries,
wildcards: loadedStructureDefinition.wildcards});

this.props.onStructureUpdate();
}
else if (logSelectedRows !== prevProps.logSelectedRows) {
this.updateStructure();
}
}
Expand Down Expand Up @@ -278,7 +303,7 @@ export default class StructureDialog extends React.Component<Props, State> {
matchStructure() {
// pass list of wildcards and use those in regular expression construction
const structureRegExp = useStructureQueryConstructor(
this.props.logHeaderColumns,
this.state.structureHeaderColumns,
this.state.structureHeaderColumnsTypes,
this.state.structureEntries,
this.state.wildcards,
Expand All @@ -290,7 +315,7 @@ export default class StructureDialog extends React.Component<Props, State> {

defineSegment() {
const segmentRegExp = useStructureQueryConstructor(
this.props.logHeaderColumns,
this.state.structureHeaderColumns,
this.state.structureHeaderColumnsTypes,
this.state.structureEntries,
this.state.wildcards,
Expand Down Expand Up @@ -470,17 +495,19 @@ export default class StructureDialog extends React.Component<Props, State> {

saveStructureDefinition() {
console.log("Saving structure definition");
const { structureEntries, wildcards} = this.state;
const {logHeaderColumns, onStructureDefinitionSave} = this.props;
const {structureHeaderColumns, structureHeaderColumnsTypes, structureEntries, wildcards} = this.state;
const {onStructureDefinitionSave} = this.props;

let structureDefiniton = { headerColumns: logHeaderColumns, entries: structureEntries, wildcards: wildcards};
let structureDefinitonJSON = JSON.stringify(structureDefiniton);
const structureDefiniton: StructureDefinition = { headerColumns: structureHeaderColumns, headerColumnsTypes: structureHeaderColumnsTypes, entries: structureEntries, wildcards: wildcards};
const structureDefinitonJSON = JSON.stringify(structureDefiniton);

onStructureDefinitionSave(structureDefinitonJSON);
}

loadStructureDefinition() {
console.log("Loading structure definition");
this.props.onStructureDefinitionLoad();
this.setState({isLoadingStructureDefintion:true});
}

render() {
Expand Down Expand Up @@ -584,7 +611,7 @@ export default class StructureDialog extends React.Component<Props, State> {
</div>
</div>
<StructureTable
headerColumns={this.props.logHeaderColumns}
headerColumns={this.state.structureHeaderColumns}
structureEntries={structureEntriesCopy}
wildcards={wildcardsCopy}
isRemovingStructureEntries={isRemovingStructureEntries}
Expand Down
9 changes: 8 additions & 1 deletion src/viewer/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { SelectedRowType, StructureLinkDistance } from "./constants";
import { SelectedRowType, StructureHeaderColumnType, StructureLinkDistance } from "./constants";

export interface LogViewState {
height: number;
Expand All @@ -16,6 +16,13 @@ export interface Header {
type: "string" | "number";
}

export interface StructureDefinition {
headerColumns: Header[],
headerColumnsTypes: StructureHeaderColumnType[],
entries: StructureEntry[],
wildcards: Wildcard[]
}

export interface StructureEntry {
row: CellContents[][];
cellSelection: boolean[];
Expand Down

0 comments on commit da7fdf4

Please sign in to comment.