intelligent cursor behavior on switch

This commit is contained in:
celeste 2023-03-14 17:42:17 -07:00
parent c20827ee29
commit e677c2bfbc
3 changed files with 35 additions and 4 deletions

25
main.ts
View file

@ -15,6 +15,7 @@ import {
import GPT3Tokenizer from "gpt3-tokenizer";
import { Configuration, OpenAIApi } from "openai";
import { v4 as uuidv4 } from "uuid";
import * as _ from "lodash";
const tokenizer = new GPT3Tokenizer({ type: "codex" });
@ -460,12 +461,28 @@ export default class LoomPlugin extends Plugin {
(id) => (this.state[file.path].nodes[id].collapsed = false)
);
const cursor = this.editor.getCursor();
const linesBefore = this.editor.getValue().split("\n");
this.editor.setValue(this.fullText(id, this.state[file.path]));
const lines = this.editor.getValue().split("\n");
const line = lines.length - 1;
const ch = lines[line].length;
this.editor.setCursor({ line, ch });
const linesAfter = this.editor.getValue().split("\n").slice(0, cursor.line);
let different = false;
for (let i = 0; i < cursor.line; i++) {
if (linesBefore[i] !== linesAfter[i]) {
different = true;
break;
}
}
if (linesBefore[cursor.line] !== this.editor.getLine(cursor.line))
different = true;
if (different) {
const line = this.editor.lineCount() - 1;
const ch = this.editor.getLine(line).length;
this.editor.setCursor({ line, ch });
} else
this.editor.setCursor(cursor);
})
)
);

12
package-lock.json generated
View file

@ -9,7 +9,9 @@
"version": "0.1.0",
"license": "MIT",
"dependencies": {
"@types/lodash": "^4.14.191",
"gpt3-tokenizer": "^1.1.5",
"lodash": "^4.17.21",
"openai": "^3.1.0",
"uuid": "^9.0.0"
},
@ -182,6 +184,11 @@
"integrity": "sha512-wOuvG1SN4Us4rez+tylwwwCV1psiNVOkJeM3AUWUNWg/jDQY2+HE/444y5gc+jBmRqASOm2Oeh5c1axHobwRKQ==",
"dev": true
},
"node_modules/@types/lodash": {
"version": "4.14.191",
"resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.191.tgz",
"integrity": "sha512-BdZ5BCCvho3EIXw6wUCXHe7rS53AIDPLE+JzwgT+OsJk53oBfbSmZZ7CX4VaRoN78N+TJpFi9QPlfIVNmJYWxQ=="
},
"node_modules/@types/node": {
"version": "16.18.12",
"resolved": "https://registry.npmjs.org/@types/node/-/node-16.18.12.tgz",
@ -1316,6 +1323,11 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/lodash": {
"version": "4.17.21",
"resolved": "https://registry.npmjs.org/lodash/-/lodash-4.17.21.tgz",
"integrity": "sha512-v2kDEe57lecTulaDIuNTPy3Ry4gLGJ6Z1O3vE1krgXZNrsQ+LFTGHVxVjcXPs17LhbZVGedAJv8XZ1tvj5FvSg=="
},
"node_modules/lodash.merge": {
"version": "4.6.2",
"resolved": "https://registry.npmjs.org/lodash.merge/-/lodash.merge-4.6.2.tgz",

View file

@ -24,7 +24,9 @@
"typescript": "4.7.4"
},
"dependencies": {
"@types/lodash": "^4.14.191",
"gpt3-tokenizer": "^1.1.5",
"lodash": "^4.17.21",
"openai": "^3.1.0",
"uuid": "^9.0.0"
}