Skip to content

Commit 55a0fe3

Browse files
committed
[MPQEditor] Implement 'updateDocumentBy'
This commit implements 'updateDocumentBy' and adds tests for it. ONE-vscode-DCO-1.0-Signed-off-by: s.malakhov <[email protected]>
1 parent 0ed653a commit 55a0fe3

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

src/MPQEditor/MPQEditor.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,4 +210,20 @@ export class MPQEditorProvider implements vscode.CustomTextEditorProvider {
210210

211211
//TODO process messages
212212
}
213+
214+
/**
215+
* @brief Update document by text
216+
*/
217+
public static async updateDocumentBy(
218+
document: vscode.TextDocument,
219+
text: string
220+
) {
221+
const edit = new vscode.WorkspaceEdit();
222+
edit.replace(
223+
document.uri,
224+
new vscode.Range(0, 0, document.lineCount, 0),
225+
text
226+
);
227+
await vscode.workspace.applyEdit(edit);
228+
}
213229
}

src/Tests/MPQEditor/MPQEditor.test.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
import * as fs from "fs";
1818
import * as path from "path";
19+
import * as vscode from "vscode";
1920

2021
import { assert } from "chai";
2122
import { MPQEditorProvider } from "../../MPQEditor/MPQEditor";
@@ -131,5 +132,36 @@ suite("MPQEditor", function () {
131132
);
132133
});
133134
});
135+
136+
suite("#updateDocumentBy", function () {
137+
test("update document by", async function () {
138+
const dirPath: string = testBuilder.dirInTemp;
139+
const mpqName: string = "model-test-updateDocumentBy.mpq.json";
140+
const circleName: string = "model-test-updateDocumentBy.circle";
141+
142+
const uri = await MPQEditorProvider.createDefaultMPQ(
143+
mpqName,
144+
dirPath,
145+
circleName
146+
);
147+
assert.isTrue(uri !== undefined);
148+
149+
let document = await vscode.workspace.openTextDocument(uri!);
150+
151+
const newJson = `{"default_quantization_dtype": "int16",
152+
"default_granularity": "layer",
153+
"layers": [],
154+
"model_path": "sample_1.circle"}`;
155+
156+
await MPQEditorProvider.updateDocumentBy(document, newJson);
157+
158+
document.save();
159+
const newJsonText: string = document.getText();
160+
const newCont = JSON.parse(newJsonText);
161+
assert.strictEqual(newCont["default_quantization_dtype"], "int16");
162+
assert.strictEqual(newCont["default_granularity"], "layer");
163+
assert.strictEqual(newCont["model_path"], "sample_1.circle");
164+
});
165+
});
134166
});
135167
});

0 commit comments

Comments
 (0)