Skip to content

Commit 6814321

Browse files
committed
Initial work on updatable record format keywords
Signed-off-by: worksofliam <[email protected]>
1 parent 6455de6 commit 6814321

File tree

3 files changed

+55
-15
lines changed

3 files changed

+55
-15
lines changed

src/ui/dspf.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -437,14 +437,14 @@ export class DisplayFile {
437437
}
438438

439439
// TODO: test cases
440-
static getLinesForFormat(recordFormat: RecordInfo): string[] {
440+
static getHeaderLinesForFormat(recordFormat: string, keywords: Keyword[]): string[] {
441441
const lines: string[] = [];
442442

443-
if (recordFormat.name !== GLOBAL_RECORD_NAME) {
444-
lines.push(` A R ${recordFormat.name}`);
443+
if (recordFormat) {
444+
lines.push(` A R ${recordFormat}`);
445445
}
446446

447-
for (const keyword of recordFormat.keywords) {
447+
for (const keyword of keywords) {
448448
// TODO: support conditions
449449
lines.push(
450450
` A ${keyword.name}${keyword.value ? `(${keyword.value})` : ``}`,
@@ -454,7 +454,7 @@ export class DisplayFile {
454454
return lines;
455455
}
456456

457-
public getRangeForFormat(recordFormat: string): DdsLineRange|undefined {
457+
public getHeaderRangeForFormat(recordFormat: string): DdsLineRange|undefined {
458458
let range: DdsLineRange|undefined = undefined;
459459
const currentFormatI = this.formats.findIndex(format => format.name === recordFormat);
460460
if (currentFormatI > 0) {
@@ -474,9 +474,9 @@ export class DisplayFile {
474474
}
475475

476476
// TODO: test cases
477-
public updateFormat(originalFormatName: string, newRecordFormat: RecordInfo): DdsUpdate|undefined {
478-
const newLines = DisplayFile.getLinesForFormat(newRecordFormat);
479-
let range = this.getRangeForFormat(originalFormatName);
477+
public updateFormatHeader(originalFormatName: string, keywords: Keyword[]): DdsUpdate|undefined {
478+
const newLines = DisplayFile.getHeaderLinesForFormat(originalFormatName, keywords);
479+
let range = this.getHeaderRangeForFormat(originalFormatName);
480480

481481
if (range) {
482482
range = {

src/ui/index.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { readFile, readFileSync } from "fs";
2-
import { WebviewViewProvider, WebviewView, Uri, CancellationToken, WebviewViewResolveContext, Webview, DiagnosticSeverity, window, WebviewPanel, ViewColumn, ExtensionContext, workspace, TextDocument, TextEdit, Range, WorkspaceEdit, Position } from "vscode";
3-
import { basename } from "path";
4-
import { DisplayFile, FieldInfo } from "./dspf";
1+
import { readFileSync } from "fs";
2+
import { Uri, Webview, window, WebviewPanel, ViewColumn, ExtensionContext, workspace, TextDocument, Range, WorkspaceEdit, Position } from "vscode";
3+
import { DisplayFile, FieldInfo, Keyword, RecordInfo } from "./dspf";
54

65

76
export class RendererWebview {
@@ -126,7 +125,32 @@ export class RendererWebview {
126125
);
127126

128127
await workspace.applyEdit(workspaceEdit);
129-
this.load(false);
128+
this.load(false); //Field is updated on the client
129+
}
130+
}
131+
}
132+
break;
133+
134+
case `updateFormat`:
135+
// This does not update any of the fields in the record format, only the format header
136+
recordFormat = message.recordFormat;
137+
const newKeywords: Keyword[] = message.newKeywords;
138+
139+
if (typeof recordFormat === `string` && Array.isArray(newKeywords)) {
140+
const formatUpdate = this.dds?.updateFormatHeader(recordFormat, newKeywords);
141+
142+
if (formatUpdate) {
143+
if (formatUpdate.range && this.document) {
144+
const workspaceEdit = new WorkspaceEdit();
145+
workspaceEdit.replace(
146+
this.document.uri,
147+
new Range(formatUpdate.range.start, 0, formatUpdate.range.end, 1000),
148+
formatUpdate.newLines.join('\n'), // TOOD: use the correct EOL?
149+
{label: `Update DDS Format`, needsConfirmation: false}
150+
);
151+
152+
await workspace.applyEdit(workspaceEdit);
153+
this.load(true);
130154
}
131155
}
132156
}

webui/main.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ const timeFormats = {
4444
'*JIS': 'hh:mm:ss',
4545
};
4646

47+
const GLOBAL_RECORD_FORMAT = `_GLOBAL`;
48+
4749
const vscode = acquireVsCodeApi();
4850

4951
const pxwPerChar = 8.45;
@@ -92,7 +94,7 @@ function loadDDS(newDoc, type, withRerender = true) {
9294
activeDocumentType = type;
9395

9496
if (withRerender) {
95-
const validFormats = activeDocument.formats.filter(format => format.name !== `GLOBAL`);
97+
const validFormats = activeDocument.formats.filter(format => format.name !== GLOBAL_RECORD_FORMAT);
9698

9799
setTabs(validFormats.map(format => format.name), lastSelectedFormat);
98100

@@ -777,7 +779,9 @@ function updateRecordFormatSidebar(recordInfo, globalInfo) {
777779

778780
sections.push({
779781
title: `Format Keywords`,
780-
html: createKeywordPanel(`keywords-${recordInfo.name}`, recordInfo.keywords),
782+
html: createKeywordPanel(`keywords-${recordInfo.name}`, recordInfo.keywords, (keywords) => {
783+
sendFormatHeaderUpdate(recordInfo.name, keywords);
784+
}),
781785
open: true
782786
});
783787

@@ -985,6 +989,18 @@ function sendFieldUpdate(recordFormat, originalFieldName, newFieldInfo) {
985989
}
986990
}
987991

992+
/**
993+
* @param {string} recordFormat
994+
* @param {Keyword[]} newKeywords
995+
*/
996+
function sendFormatHeaderUpdate(recordFormat, newKeywords) {
997+
vscode.postMessage({
998+
command: `updateFormat`,
999+
recordFormat,
1000+
newKeywords
1001+
});
1002+
}
1003+
9881004
/**
9891005
* Used to create panels for editable key/value lists.
9901006
* @param {string} id

0 commit comments

Comments
 (0)