Merge pull request #184 from twio142/patch-1

Improve `surround`
This commit is contained in:
esm7 2023-07-01 15:30:59 +03:00 committed by GitHub
commit 9ccf52a590
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

48
main.ts
View file

@ -446,9 +446,9 @@ export default class VimrcPlugin extends Plugin {
let beginning = newArgs[0].replace("\\\\", "\\").replace("\\ ", " "); // Get the beginning surround text
let ending = newArgs[1].replace("\\\\", "\\").replace("\\ ", " "); // Get the ending surround text
let currentSelections = this.currentSelection;
var chosenSelection = currentSelections && currentSelections.length > 0 ? currentSelections[0] : null;
if (this.currentSelection && currentSelections?.length > 1) {
let currentSelections = this.currentSelection;
var chosenSelection = currentSelections?.[0] ? currentSelections[0] : {anchor: editor.getCursor(), head: editor.getCursor()};
if (currentSelections?.length > 1) {
console.log("WARNING: Multiple selections in surround. Attempt to select matching cursor. (obsidian-vimrc-support)")
const cursorPos = editor.getCursor();
for (const selection of currentSelections) {
@ -459,35 +459,25 @@ export default class VimrcPlugin extends Plugin {
}
}
}
if (JSON.stringify(chosenSelection.anchor) === JSON.stringify(chosenSelection.head)) {
if (editor.posToOffset(chosenSelection.anchor) === editor.posToOffset(chosenSelection.head)) {
// No range of selected text, so select word.
var line = editor.getLine(chosenSelection.anchor.line);
if (line.length === 0)
throw new Error("can't surround on an empty line");
// Go to the beginning of the word
let wordStart = chosenSelection.anchor.ch;
for ( ; wordStart >= 0 ; wordStart--)
if (line[wordStart].match(/\s/))
break;
wordStart++;
let wordEnd = chosenSelection.anchor.ch;
for ( ; wordEnd < line.length ; wordEnd++)
if (line[wordEnd].match(/\s/))
break;
var word = line.substring(wordStart, wordEnd);
chosenSelection.anchor.ch = wordStart;
chosenSelection.head.ch = wordEnd;
chosenSelection = {
anchor: {line: chosenSelection.anchor.line, ch: wordStart},
head: {line: chosenSelection.head.line, ch: wordEnd}
};
let wordAt = editor.wordAt(chosenSelection.head);
if (wordAt) {
chosenSelection = {anchor: wordAt.from, head: wordAt.to};
}
}
// If the selection is reverse, switch the variables
if (chosenSelection.anchor.line > chosenSelection.head.line ||
(chosenSelection.anchor.line == chosenSelection.head.line && chosenSelection.anchor.ch > chosenSelection.head.ch))
[chosenSelection.anchor, chosenSelection.head] = [chosenSelection.head, chosenSelection.anchor];
let currText = editor.getRange(chosenSelection.anchor, chosenSelection.head);
let currText;
if (editor.posToOffset(chosenSelection.anchor) > editor.posToOffset(chosenSelection.head)) {
currText = editor.getRange(chosenSelection.head, chosenSelection.anchor);
} else {
currText = editor.getRange(chosenSelection.anchor, chosenSelection.head);
}
editor.replaceRange(beginning + currText + ending, chosenSelection.anchor, chosenSelection.head);
// If no selection, place cursor between beginning and ending
if (editor.posToOffset(chosenSelection.anchor) === editor.posToOffset(chosenSelection.head)) {
chosenSelection.head.ch += beginning.length;
editor.setCursor(chosenSelection.head);
}
}
vimObject.defineEx("surround", "", (cm: any, params: any) => { surroundFunc(params.args); });