mirror of
https://github.com/esm7/obsidian-vimrc-support.git
synced 2026-07-22 05:00:25 +00:00
commit
9ccf52a590
1 changed files with 19 additions and 29 deletions
48
main.ts
48
main.ts
|
|
@ -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); });
|
||||
|
|
|
|||
Loading…
Reference in a new issue