Skip to content

Commit abf2878

Browse files
committed
New functions for updating formats
Signed-off-by: worksofliam <mrliamallan@live.co.uk>
1 parent cae38ed commit abf2878

File tree

1 file changed

+67
-5
lines changed

1 file changed

+67
-5
lines changed

src/ui/dspf.ts

Lines changed: 67 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,14 @@
11

2-
export interface DdsLineRange { start: number, end: number };
2+
export interface DdsLineRange { start: number, endHeader?: number, end: number };
3+
export interface DdsUpdate { newLines: string[], range?: DdsLineRange };
4+
5+
const GLOBAL_RECORD_NAME = `GLOBAL`;
36

47
export class DisplayFile {
58
public formats: RecordInfo[] = [];
69
public currentField: FieldInfo | undefined;
710
public currentFields: FieldInfo[] = [];
8-
public currentRecord: RecordInfo|undefined = new RecordInfo(`GLOBAL`);
11+
public currentRecord: RecordInfo|undefined = new RecordInfo(GLOBAL_RECORD_NAME);
912

1013
constructor() { }
1114

@@ -348,7 +351,8 @@ export class DisplayFile {
348351
return result;
349352
}
350353

351-
public getLinesForField(field: FieldInfo): string[] {
354+
// TODO: test cases
355+
public static getLinesForField(field: FieldInfo): string[] {
352356
const newLines: string[] = [];
353357

354358
const FIELD_TYPE: { [name in DisplayType]: string } = {
@@ -387,6 +391,7 @@ export class DisplayFile {
387391
return newLines;
388392
}
389393

394+
// TODO: test cases
390395
public getRangeForField(recordFormat: string, fieldName: string): DdsLineRange|undefined {
391396
let range: DdsLineRange|undefined = undefined;
392397
const currentFormatI = this.formats.findIndex(format => format.name === recordFormat);
@@ -417,8 +422,9 @@ export class DisplayFile {
417422
return range;
418423
}
419424

420-
public updateField(recordFormat: string, originalFieldName: string|undefined, fieldInfo: FieldInfo): { newLines: string[], range?: DdsLineRange }|undefined {
421-
const newLines = this.getLinesForField(fieldInfo);
425+
// TODO: test cases
426+
public updateField(recordFormat: string, originalFieldName: string|undefined, fieldInfo: FieldInfo): DdsUpdate|undefined {
427+
const newLines = DisplayFile.getLinesForField(fieldInfo);
422428

423429
let range = this.getRangeForField(recordFormat, originalFieldName!);
424430

@@ -431,6 +437,62 @@ export class DisplayFile {
431437

432438
return { newLines, range };
433439
}
440+
441+
// TODO: test cases
442+
static getLinesForFormat(recordFormat: RecordInfo): string[] {
443+
const lines: string[] = [];
444+
445+
if (recordFormat.name !== GLOBAL_RECORD_NAME) {
446+
lines.push(` A R ${recordFormat.name}`);
447+
}
448+
449+
for (const keyword of recordFormat.keywords) {
450+
// TODO: support conditions
451+
lines.push(
452+
` A ${keyword.name}${keyword.value ? `(${keyword.value})` : ``}`,
453+
);
454+
}
455+
456+
return lines;
457+
}
458+
459+
// TODO: test cases
460+
public getRangeForFormat(recordFormat: string): DdsLineRange|undefined {
461+
let range: DdsLineRange|undefined = undefined;
462+
const currentFormatI = this.formats.findIndex(format => format.name === recordFormat);
463+
if (currentFormatI > 0) {
464+
range = { start: this.formats[currentFormatI].range.start, end: this.formats[currentFormatI].range.end };
465+
466+
const currentFormat = this.formats[currentFormatI];
467+
const firstField = currentFormat.fields[0];
468+
469+
if (firstField) {
470+
range.endHeader = firstField.startRange-1;
471+
} else {
472+
range.endHeader = range.start;
473+
}
474+
}
475+
476+
return range;
477+
}
478+
479+
// TODO: test cases
480+
public updateFormat(originalFormatName: string, newRecordFormat: RecordInfo): DdsUpdate|undefined {
481+
const newLines = DisplayFile.getLinesForFormat(newRecordFormat);
482+
let range = this.getRangeForFormat(originalFormatName);
483+
484+
if (range) {
485+
range = {
486+
start: range.start,
487+
end: range.endHeader || range.end,
488+
};
489+
}
490+
491+
return {
492+
newLines,
493+
range
494+
};
495+
}
434496
}
435497

436498
export class RecordInfo {

0 commit comments

Comments
 (0)