Refactor makeEdit so it returns the result node

This commit is contained in:
Misode
2024-10-23 21:41:12 +02:00
parent 6151fbcea4
commit 6e68de01aa
5 changed files with 136 additions and 103 deletions

View File

@@ -18,8 +18,6 @@ import { computeIfAbsent, genPath } from '../Utils.js'
import { fetchBlockStates, fetchRegistries, fetchVanillaMcdoc, getVersionChecksum } from './DataFetcher.js'
import type { VersionId } from './Versions.js'
export type AstEdit = (docAndNode: core.DocAndNode) => void
interface ClientDocument {
doc: TextDocument
undoStack: string[]
@@ -111,7 +109,7 @@ export class SpyglassService {
}
}
public async applyEdit(uri: string, edit: AstEdit) {
public async applyEdit(uri: string, edit: (node: core.FileNode<core.AstNode>) => void) {
const document = this.client.documents.get(uri)
if (document !== undefined) {
document.undoStack.push(document.doc.getText())
@@ -120,7 +118,7 @@ export class SpyglassService {
if (!docAndNode) {
throw new Error(`[Spyglass#openFile] Cannot get doc and node: ${uri}`)
}
edit(docAndNode)
edit(docAndNode.node)
const newText = this.service.format(docAndNode.node, docAndNode.doc, 2, true)
TextDocument.update(document.doc, [{ text: newText }], document.doc.version + 1)
await this.service.project.externals.fs.writeFile(uri, document.doc.getText())