Skip to content

[MPQEditor] Implement 'updateDocumentBy' #1520

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/MPQEditor/MPQEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -210,4 +210,20 @@ export class MPQEditorProvider implements vscode.CustomTextEditorProvider {

//TODO process messages
}

/**
* @brief Update document by text
*/
public static async updateDocumentBy(
document: vscode.TextDocument,
text: string
) {
const edit = new vscode.WorkspaceEdit();
edit.replace(
document.uri,
new vscode.Range(0, 0, document.lineCount, 0),
text
);
await vscode.workspace.applyEdit(edit);
}
}
32 changes: 32 additions & 0 deletions src/Tests/MPQEditor/MPQEditor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import * as fs from "fs";
import * as path from "path";
import * as vscode from "vscode";

import { assert } from "chai";
import { MPQEditorProvider } from "../../MPQEditor/MPQEditor";
Expand Down Expand Up @@ -131,5 +132,36 @@ suite("MPQEditor", function () {
);
});
});

suite("#updateDocumentBy", function () {
test("update document by", async function () {
const dirPath: string = testBuilder.dirInTemp;
const mpqName: string = "model-test-updateDocumentBy.mpq.json";
const circleName: string = "model-test-updateDocumentBy.circle";

const uri = await MPQEditorProvider.createDefaultMPQ(
mpqName,
dirPath,
circleName
);
assert.isTrue(uri !== undefined);

let document = await vscode.workspace.openTextDocument(uri!);

const newJson = `{"default_quantization_dtype": "int16",
"default_granularity": "layer",
"layers": [],
"model_path": "sample_1.circle"}`;

await MPQEditorProvider.updateDocumentBy(document, newJson);

document.save();
const newJsonText: string = document.getText();
const newCont = JSON.parse(newJsonText);
assert.strictEqual(newCont["default_quantization_dtype"], "int16");
assert.strictEqual(newCont["default_granularity"], "layer");
assert.strictEqual(newCont["model_path"], "sample_1.circle");
});
});
});
});