generated from obsidianmd/obsidian-sample-plugin
-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
actions.ts
690 lines (630 loc) · 18.5 KB
/
actions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
import type {
App,
Editor,
EditorChange,
EditorPosition,
EditorSelection,
} from 'obsidian';
import {
CASE,
SEARCH_DIRECTION,
MATCHING_BRACKETS,
MATCHING_QUOTES,
MATCHING_QUOTES_BRACKETS,
MatchingCharacterMap,
CODE_EDITOR,
LIST_CHARACTER_REGEX,
} from './constants';
import { SettingsState } from './state';
import {
CheckCharacter,
EditorActionCallbackNewArgs,
findAllMatchPositions,
findNextMatchPosition,
findPosOfNextCharacter,
formatRemainingListPrefixes,
getLeadingWhitespace,
getLineEndPos,
getLineStartPos,
getNextCase,
toTitleCase,
getSelectionBoundaries,
wordRangeAtPos,
getSearchText,
getNextListPrefix,
isNumeric,
} from './utils';
export const insertLineAbove = (
editor: Editor,
selection: EditorSelection,
args: EditorActionCallbackNewArgs,
) => {
const { line } = selection.head;
const startOfCurrentLine = getLineStartPos(line);
const contentsOfCurrentLine = editor.getLine(line);
const indentation = getLeadingWhitespace(contentsOfCurrentLine);
let listPrefix = '';
if (
SettingsState.autoInsertListPrefix &&
line > 0 &&
// If inside a list, only insert prefix if within the same list
editor.getLine(line - 1).trim().length > 0
) {
listPrefix = getNextListPrefix(contentsOfCurrentLine, 'before');
if (isNumeric(listPrefix)) {
formatRemainingListPrefixes(editor, line, indentation);
}
}
const changes: EditorChange[] = [
{ from: startOfCurrentLine, text: indentation + listPrefix + '\n' },
];
const newSelection = {
from: {
...startOfCurrentLine,
// Offset by iteration
line: startOfCurrentLine.line + args.iteration,
ch: indentation.length + listPrefix.length,
},
};
return {
changes,
newSelection,
};
};
export const insertLineBelow = (
editor: Editor,
selection: EditorSelection,
args: EditorActionCallbackNewArgs,
) => {
const { line } = selection.head;
const startOfCurrentLine = getLineStartPos(line);
const endOfCurrentLine = getLineEndPos(line, editor);
const contentsOfCurrentLine = editor.getLine(line);
const indentation = getLeadingWhitespace(contentsOfCurrentLine);
let listPrefix = '';
if (SettingsState.autoInsertListPrefix) {
listPrefix = getNextListPrefix(contentsOfCurrentLine, 'after');
// Performing this action on an empty list item should delete it
if (listPrefix === null) {
const changes: EditorChange[] = [
{ from: startOfCurrentLine, to: endOfCurrentLine, text: '' },
];
const newSelection = {
from: {
line,
ch: 0,
},
};
return {
changes,
newSelection,
};
}
if (isNumeric(listPrefix)) {
formatRemainingListPrefixes(editor, line + 1, indentation);
}
}
const changes: EditorChange[] = [
{ from: endOfCurrentLine, text: '\n' + indentation + listPrefix },
];
const newSelection = {
from: {
// Offset by iteration
line: line + 1 + args.iteration,
ch: indentation.length + listPrefix.length,
},
};
return {
changes,
newSelection,
};
};
// Note: don't use the built-in exec method for 'deleteLine' as there is a bug
// where running it on a line that is long enough to be wrapped will focus on
// the previous line instead of the next line after deletion
let numLinesDeleted = 0;
export const deleteLine = (
editor: Editor,
selection: EditorSelection,
args: EditorActionCallbackNewArgs,
) => {
const { from, to, hasTrailingNewline } = getSelectionBoundaries(selection);
if (to.line === editor.lastLine()) {
// There is no 'next line' when cursor is on the last line
const previousLine = Math.max(0, from.line - 1);
const endOfPreviousLine = getLineEndPos(previousLine, editor);
const changes: EditorChange[] = [
{
from: from.line === 0 ? getLineStartPos(0) : endOfPreviousLine,
to:
// Exclude line starting at trailing newline at end of document from being deleted
to.ch === 0
? getLineStartPos(to.line)
: getLineEndPos(to.line, editor),
text: '',
},
];
const newSelection = {
from: {
line: previousLine,
ch: Math.min(from.ch, endOfPreviousLine.ch),
},
};
return {
changes,
newSelection,
};
}
// Reset offset at the start of a new bulk delete operation
if (args.iteration === 0) {
numLinesDeleted = 0;
}
// Exclude line starting at trailing newline from being deleted
const toLine = hasTrailingNewline ? to.line - 1 : to.line;
const endOfNextLine = getLineEndPos(toLine + 1, editor);
const changes: EditorChange[] = [
{
from: getLineStartPos(from.line),
to: getLineStartPos(toLine + 1),
text: '',
},
];
const newSelection = {
from: {
// Offset by the number of lines deleted in all previous iterations
line: from.line - numLinesDeleted,
ch: Math.min(to.ch, endOfNextLine.ch),
},
};
// This needs to be calculated after setting the new selection as it only
// applies for subsequent iterations
numLinesDeleted += toLine - from.line + 1;
return {
changes,
newSelection,
};
};
export const deleteToStartOfLine = (
editor: Editor,
selection: EditorSelection,
) => {
const pos = selection.head;
let startPos = getLineStartPos(pos.line);
if (pos.line === 0 && pos.ch === 0) {
// We're at the start of the document so do nothing
return selection;
}
if (pos.line === startPos.line && pos.ch === startPos.ch) {
// We're at the start of the line so delete the preceding newline
startPos = getLineEndPos(pos.line - 1, editor);
}
editor.replaceRange('', startPos, pos);
return {
anchor: startPos,
};
};
export const deleteToEndOfLine = (
editor: Editor,
selection: EditorSelection,
) => {
const pos = selection.head;
let endPos = getLineEndPos(pos.line, editor);
if (pos.line === endPos.line && pos.ch === endPos.ch) {
// We're at the end of the line so delete just the newline
endPos = getLineStartPos(pos.line + 1);
}
editor.replaceRange('', pos, endPos);
return {
anchor: pos,
};
};
export const joinLines = (editor: Editor, selection: EditorSelection) => {
const { from, to } = getSelectionBoundaries(selection);
const { line } = from;
let endOfCurrentLine = getLineEndPos(line, editor);
const joinRangeLimit = Math.max(to.line - line, 1);
const selectionLength = editor.posToOffset(to) - editor.posToOffset(from);
let trimmedChars = '';
for (let i = 0; i < joinRangeLimit; i++) {
if (line === editor.lineCount() - 1) {
break;
}
endOfCurrentLine = getLineEndPos(line, editor);
const endOfNextLine = getLineEndPos(line + 1, editor);
const contentsOfCurrentLine = editor.getLine(line);
const contentsOfNextLine = editor.getLine(line + 1);
const charsToTrim = contentsOfNextLine.match(LIST_CHARACTER_REGEX) ?? [];
trimmedChars += charsToTrim[0] ?? '';
const newContentsOfNextLine = contentsOfNextLine.replace(
LIST_CHARACTER_REGEX,
'',
);
if (
newContentsOfNextLine.length > 0 &&
contentsOfCurrentLine.charAt(endOfCurrentLine.ch - 1) !== ' '
) {
editor.replaceRange(
' ' + newContentsOfNextLine,
endOfCurrentLine,
endOfNextLine,
);
} else {
editor.replaceRange(
newContentsOfNextLine,
endOfCurrentLine,
endOfNextLine,
);
}
}
if (selectionLength === 0) {
return {
anchor: endOfCurrentLine,
};
}
return {
anchor: from,
head: {
line: from.line,
ch: from.ch + selectionLength - trimmedChars.length,
},
};
};
export const copyLine = (
editor: Editor,
selection: EditorSelection,
direction: 'up' | 'down',
) => {
const { from, to, hasTrailingNewline } = getSelectionBoundaries(selection);
const fromLineStart = getLineStartPos(from.line);
// Exclude line starting at trailing newline from being duplicated
const toLine = hasTrailingNewline ? to.line - 1 : to.line;
const toLineEnd = getLineEndPos(toLine, editor);
const contentsOfSelectedLines = editor.getRange(fromLineStart, toLineEnd);
if (direction === 'up') {
editor.replaceRange('\n' + contentsOfSelectedLines, toLineEnd);
return selection;
} else {
editor.replaceRange(contentsOfSelectedLines + '\n', fromLineStart);
// This uses `to.line` instead of `toLine` to avoid a double adjustment
const linesSelected = to.line - from.line + 1;
return {
anchor: { line: toLine + 1, ch: from.ch },
head: { line: toLine + linesSelected, ch: to.ch },
};
}
};
/*
Properties used to distinguish between selections that are programmatic
(expanding from a cursor selection) vs. manual (using a mouse / Shift + arrow
keys). This controls the match behaviour for selectWordOrNextOccurrence.
*/
let isManualSelection = true;
export const setIsManualSelection = (value: boolean) => {
isManualSelection = value;
};
export let isProgrammaticSelectionChange = false;
export const setIsProgrammaticSelectionChange = (value: boolean) => {
isProgrammaticSelectionChange = value;
};
export const selectWordOrNextOccurrence = (editor: Editor) => {
setIsProgrammaticSelectionChange(true);
const allSelections = editor.listSelections();
const { searchText, singleSearchText } = getSearchText({
editor,
allSelections,
autoExpand: false,
});
if (searchText.length > 0 && singleSearchText) {
const { from: latestMatchPos } = getSelectionBoundaries(
allSelections[allSelections.length - 1],
);
const nextMatch = findNextMatchPosition({
editor,
latestMatchPos,
searchText,
searchWithinWords: isManualSelection,
documentContent: editor.getValue(),
});
const newSelections = nextMatch
? allSelections.concat(nextMatch)
: allSelections;
editor.setSelections(newSelections);
const lastSelection = newSelections[newSelections.length - 1];
editor.scrollIntoView(getSelectionBoundaries(lastSelection));
} else {
const newSelections = [];
for (const selection of allSelections) {
const { from, to } = getSelectionBoundaries(selection);
// Don't modify existing range selections
if (from.line !== to.line || from.ch !== to.ch) {
newSelections.push(selection);
} else {
newSelections.push(wordRangeAtPos(from, editor.getLine(from.line)));
setIsManualSelection(false);
}
}
editor.setSelections(newSelections);
}
};
export const selectAllOccurrences = (editor: Editor) => {
const allSelections = editor.listSelections();
const { searchText, singleSearchText } = getSearchText({
editor,
allSelections,
autoExpand: true,
});
if (!singleSearchText) {
return;
}
const matches = findAllMatchPositions({
editor,
searchText,
searchWithinWords: true,
documentContent: editor.getValue(),
});
editor.setSelections(matches);
};
export const selectLine = (_editor: Editor, selection: EditorSelection) => {
const { from, to } = getSelectionBoundaries(selection);
const startOfCurrentLine = getLineStartPos(from.line);
// if a line is already selected, expand the selection to the next line
const startOfNextLine = getLineStartPos(to.line + 1);
return { anchor: startOfCurrentLine, head: startOfNextLine };
};
export const addCursorsToSelectionEnds = (
editor: Editor,
emulate: CODE_EDITOR = CODE_EDITOR.VSCODE,
) => {
// Only apply the action if there is exactly one selection
if (editor.listSelections().length !== 1) {
return;
}
const selection = editor.listSelections()[0];
const { from, to, hasTrailingNewline } = getSelectionBoundaries(selection);
const newSelections = [];
// Exclude line starting at trailing newline from having cursor added
const toLine = hasTrailingNewline ? to.line - 1 : to.line;
for (let line = from.line; line <= toLine; line++) {
const head = line === to.line ? to : getLineEndPos(line, editor);
let anchor: EditorPosition;
if (emulate === CODE_EDITOR.VSCODE) {
anchor = head;
} else {
anchor = line === from.line ? from : getLineStartPos(line);
}
newSelections.push({
anchor,
head,
});
}
editor.setSelections(newSelections);
};
export const goToLineBoundary = (
editor: Editor,
selection: EditorSelection,
boundary: 'start' | 'end',
) => {
const { from, to } = getSelectionBoundaries(selection);
if (boundary === 'start') {
return { anchor: getLineStartPos(from.line) };
} else {
return { anchor: getLineEndPos(to.line, editor) };
}
};
export const navigateLine = (
editor: Editor,
selection: EditorSelection,
position: 'next' | 'prev' | 'first' | 'last',
) => {
const pos = selection.head;
let line: number;
let ch: number;
if (position === 'prev') {
line = Math.max(pos.line - 1, 0);
const endOfLine = getLineEndPos(line, editor);
ch = Math.min(pos.ch, endOfLine.ch);
}
if (position === 'next') {
line = Math.min(pos.line + 1, editor.lineCount() - 1);
const endOfLine = getLineEndPos(line, editor);
ch = Math.min(pos.ch, endOfLine.ch);
}
if (position === 'first') {
line = 0;
ch = 0;
}
if (position === 'last') {
line = editor.lineCount() - 1;
const endOfLine = getLineEndPos(line, editor);
ch = endOfLine.ch;
}
return { anchor: { line, ch } };
};
export const moveCursor = (
editor: Editor,
direction: 'up' | 'down' | 'left' | 'right',
) => {
switch (direction) {
case 'up':
editor.exec('goUp');
break;
case 'down':
editor.exec('goDown');
break;
case 'left':
editor.exec('goLeft');
break;
case 'right':
editor.exec('goRight');
break;
}
};
export const moveWord = (editor: Editor, direction: 'left' | 'right') => {
switch (direction) {
case 'left':
// @ts-expect-error - command not defined in Obsidian API
editor.exec('goWordLeft');
break;
case 'right':
// @ts-expect-error - command not defined in Obsidian API
editor.exec('goWordRight');
break;
}
};
export const transformCase = (
editor: Editor,
selection: EditorSelection,
caseType: CASE,
) => {
let { from, to } = getSelectionBoundaries(selection);
let selectedText = editor.getRange(from, to);
// apply transform on word at cursor if nothing is selected
if (selectedText.length === 0) {
const pos = selection.head;
const { anchor, head } = wordRangeAtPos(pos, editor.getLine(pos.line));
[from, to] = [anchor, head];
selectedText = editor.getRange(anchor, head);
}
let replacementText = selectedText;
switch (caseType) {
case CASE.UPPER: {
replacementText = selectedText.toUpperCase();
break;
}
case CASE.LOWER: {
replacementText = selectedText.toLowerCase();
break;
}
case CASE.TITLE: {
replacementText = toTitleCase(selectedText);
break;
}
case CASE.NEXT: {
replacementText = getNextCase(selectedText);
break;
}
}
editor.replaceRange(replacementText, from, to);
return selection;
};
const expandSelection = ({
editor,
selection,
openingCharacterCheck,
matchingCharacterMap,
}: {
editor: Editor;
selection: EditorSelection;
openingCharacterCheck: CheckCharacter;
matchingCharacterMap: MatchingCharacterMap;
}) => {
let { anchor, head } = selection;
// in case user selects upwards
if (anchor.line >= head.line && anchor.ch > anchor.ch) {
[anchor, head] = [head, anchor];
}
const newAnchor = findPosOfNextCharacter({
editor,
startPos: anchor,
checkCharacter: openingCharacterCheck,
searchDirection: SEARCH_DIRECTION.BACKWARD,
});
if (!newAnchor) {
return selection;
}
const newHead = findPosOfNextCharacter({
editor,
startPos: head,
checkCharacter: (char: string) =>
char === matchingCharacterMap[newAnchor.match],
searchDirection: SEARCH_DIRECTION.FORWARD,
});
if (!newHead) {
return selection;
}
return { anchor: newAnchor.pos, head: newHead.pos };
};
export const expandSelectionToBrackets = (
editor: Editor,
selection: EditorSelection,
) =>
expandSelection({
editor,
selection,
openingCharacterCheck: (char: string) => /[([{]/.test(char),
matchingCharacterMap: MATCHING_BRACKETS,
});
export const expandSelectionToQuotes = (
editor: Editor,
selection: EditorSelection,
) =>
expandSelection({
editor,
selection,
openingCharacterCheck: (char: string) => /['"`]/.test(char),
matchingCharacterMap: MATCHING_QUOTES,
});
export const expandSelectionToQuotesOrBrackets = (editor: Editor) => {
const selections = editor.listSelections();
const newSelection = expandSelection({
editor,
selection: selections[0],
openingCharacterCheck: (char: string) => /['"`([{]/.test(char),
matchingCharacterMap: MATCHING_QUOTES_BRACKETS,
});
editor.setSelections([...selections, newSelection]);
};
const insertCursor = (editor: Editor, lineOffset: number) => {
const selections = editor.listSelections();
const newSelections = [];
for (const selection of selections) {
const { line, ch } = selection.head;
if (
(line === 0 && lineOffset < 0) ||
(line === editor.lastLine() && lineOffset > 0)
) {
break;
}
const targetLineLength = editor.getLine(line + lineOffset).length;
newSelections.push({
anchor: {
line: selection.anchor.line + lineOffset,
ch: Math.min(selection.anchor.ch, targetLineLength),
},
head: {
line: line + lineOffset,
ch: Math.min(ch, targetLineLength),
},
});
}
editor.setSelections([...editor.listSelections(), ...newSelections]);
};
export const insertCursorAbove = (editor: Editor) => insertCursor(editor, -1);
export const insertCursorBelow = (editor: Editor) => insertCursor(editor, 1);
export const goToHeading = (
app: App,
editor: Editor,
boundary: 'prev' | 'next',
) => {
const file = app.metadataCache.getFileCache(app.workspace.getActiveFile());
if (!file.headings || file.headings.length === 0) {
return;
}
const { line } = editor.getCursor('from');
let prevHeadingLine = 0;
let nextHeadingLine = editor.lastLine();
file.headings.forEach(({ position }) => {
const { end: headingPos } = position;
if (line > headingPos.line && headingPos.line > prevHeadingLine) {
prevHeadingLine = headingPos.line;
}
if (line < headingPos.line && headingPos.line < nextHeadingLine) {
nextHeadingLine = headingPos.line;
}
});
editor.setSelection(
boundary === 'prev'
? getLineEndPos(prevHeadingLine, editor)
: getLineEndPos(nextHeadingLine, editor),
);
};