Skip to content

Commit faacb38

Browse files
committed
Updated build, fixed undo/redo after format
1 parent 8c54f7e commit faacb38

5 files changed

Lines changed: 810 additions & 732 deletions

File tree

dist/main.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "acode-plugin-prettier",
3-
"version": "1.0.2",
3+
"version": "1.0.5",
44
"description": "Acode plugin to format code",
55
"main": "dist/main.js",
66
"repository": "https://github.com/deadlyjack/acode-plugin-prettier.git",
@@ -10,18 +10,18 @@
1010
"prettier": "^2.7.1"
1111
},
1212
"devDependencies": {
13-
"@babel/cli": "^7.17.10",
14-
"@babel/core": "^7.18.5",
15-
"@babel/preset-env": "^7.18.2",
16-
"babel-loader": "^8.2.5",
17-
"webpack": "^5.73.0",
13+
"@babel/cli": "^7.19.3",
14+
"@babel/core": "^7.19.6",
15+
"@babel/preset-env": "^7.19.4",
16+
"babel-loader": "^9.0.0",
17+
"webpack": "^5.74.0",
1818
"webpack-cli": "^4.10.0"
1919
},
2020
"scripts": {
2121
"build": "node ./scripts/build.js"
2222
},
2323
"resolutions": {
24-
"terser": ">=5.14.2"
24+
"terser": ">=5.15.1"
2525
},
2626
"browserslist": [
2727
"> 0.25%, not dead"

plugin.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "acode.plugin.prettier",
33
"name": "Prettier",
44
"main": "dist/main.js",
5-
"version": "1.0.4",
5+
"version": "1.0.5",
66
"readme": "readme.md",
77
"icon": "icon.png",
88
"type": "free",

src/main.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ class AcodePrettier {
7979
await this.loadScript();
8080
}
8181

82-
const config = appSettings.value[pluginId];
8382
const extensions = [
8483
"html",
8584
"htm",
@@ -107,6 +106,7 @@ class AcodePrettier {
107106

108107
async run() {
109108
const { editor, activeFile } = editorManager;
109+
const { session } = activeFile;
110110
const code = editor.getValue();
111111
const cursorPos = editor.getCursorPosition();
112112
const parser = AcodePrettier.inferParser(activeFile.name);
@@ -129,9 +129,7 @@ class AcodePrettier {
129129
const { prettier, plugins } = window.acodePluginPrettier;
130130
cursorOptions.plugins = plugins;
131131
const res = prettier.formatWithCursor(code, cursorOptions);
132-
editor.setValue(res.formatted);
133-
const { row, column } = this.#cursorOffsetTocursorPos(res.cursorOffset);
134-
editor.gotoLine(row + 1, column - 1);
132+
this.#setValue(session, res);
135133
}
136134

137135
destroy() {
@@ -181,10 +179,7 @@ class AcodePrettier {
181179
if (!file) return;
182180

183181
const { session } = file;
184-
session.setValue(res.formatted);
185-
const { row, column } = this.#cursorOffsetTocursorPos(res.cursorOffset);
186-
187-
session.selection.moveCursorTo(row, column);
182+
this.#setValue(session, res);
188183
}
189184
};
190185

@@ -193,6 +188,18 @@ class AcodePrettier {
193188
scriptUrl: acode.joinUrl(this.baseUrl, "vendor.js"),
194189
});
195190
}
191+
192+
#setValue(session, formattedCode) {
193+
const { $undoStack, $redoStack, $rev, $mark } = Object.assign({}, session.getUndoManager());
194+
session.setValue(formattedCode.formatted);
195+
const undoManager = session.getUndoManager();
196+
undoManager.$undoStack = $undoStack;
197+
undoManager.$redoStack = $redoStack;
198+
undoManager.$rev = $rev;
199+
undoManager.$mark = $mark;
200+
const { row, column } = this.#cursorOffsetTocursorPos(formattedCode.cursorOffset);
201+
session.selection.moveCursorTo(row, column);
202+
}
196203
}
197204

198205
if (window.acode) {

0 commit comments

Comments
 (0)