generated from obsidianmd/obsidian-sample-plugin
-
Notifications
You must be signed in to change notification settings - Fork 2
/
actions.ts
241 lines (224 loc) · 7.4 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
import { Editor } from 'obsidian';
import {
getLineBoundaries,
getCursorAndParagraphText,
forEachSentence,
setCursorAtStartOfLine,
setCursorAtEndOfLine,
setCursorAtNextWordCharacter,
getPrevNonEmptyLine,
getNextNonEmptyLine,
isAtStartOfListItem,
} from './utils';
export const deleteToBoundary = (editor: Editor, boundary: 'start' | 'end') => {
let { cursorPosition, paragraphText } = getCursorAndParagraphText(editor);
const originalCursorPosition = cursorPosition;
if (
paragraphText.charAt(cursorPosition.ch) === ' ' ||
paragraphText.charAt(cursorPosition.ch - 1) === ' '
) {
setCursorAtNextWordCharacter({
editor,
cursorPosition,
paragraphText,
direction: boundary,
});
({ cursorPosition, paragraphText } = getCursorAndParagraphText(editor));
}
let done = false;
forEachSentence(paragraphText, (sentence) => {
if (
!done &&
cursorPosition.ch >= sentence.index &&
cursorPosition.ch <= sentence.index + sentence[0].length
) {
if (boundary === 'start') {
const sentenceTextBeforeCursor =
paragraphText.substring(0, sentence.index) +
paragraphText.substring(originalCursorPosition.ch);
const cutPortionLength =
paragraphText.length - sentenceTextBeforeCursor.length;
const { start, end } = getLineBoundaries(editor, cursorPosition.line);
editor.replaceRange(sentenceTextBeforeCursor, start, end);
editor.setCursor({
line: cursorPosition.line,
ch: originalCursorPosition.ch - cutPortionLength,
});
} else {
const remainingSentenceLength =
sentence.index + sentence[0].length - cursorPosition.ch;
const newParagraph =
paragraphText.substring(0, originalCursorPosition.ch) +
paragraphText.substring(cursorPosition.ch + remainingSentenceLength);
const { start, end } = getLineBoundaries(editor, cursorPosition.line);
editor.replaceRange(newParagraph, start, end);
editor.setCursor(originalCursorPosition);
}
done = true;
}
});
};
/**
* Selects from the cursor to the start or end of a sentence, with the current
* cursor position as an anchor. Intended to be used from inside a sentence, so
* does not handle any leading and trailing space characters.
*/
export const selectToBoundary = (editor: Editor, boundary: 'start' | 'end') => {
const { cursorPosition, paragraphText } = getCursorAndParagraphText(editor);
let done = false;
forEachSentence(paragraphText, (sentence) => {
if (
!done &&
cursorPosition.ch >= sentence.index &&
cursorPosition.ch <= sentence.index + sentence[0].length
) {
// Don't change selection if it currently starts or ends at a boundary
if (
editor.getSelection().length > 0 &&
(cursorPosition.ch === sentence.index ||
cursorPosition.ch === sentence.index + sentence[0].length)
) {
return true;
}
if (boundary === 'start') {
const sentenceTextBeforeCursor =
paragraphText.substring(0, sentence.index) +
paragraphText.substring(cursorPosition.ch);
const precedingLength =
paragraphText.length - sentenceTextBeforeCursor.length;
const start = {
...cursorPosition,
ch: cursorPosition.ch - precedingLength,
};
editor.setSelection(cursorPosition, start);
} else {
const remainingLength =
sentence.index + sentence[0].length - cursorPosition.ch;
const end = {
...cursorPosition,
ch: cursorPosition.ch + remainingLength,
};
editor.setSelection(cursorPosition, end);
}
done = true;
}
});
};
export const moveToStartOfCurrentSentence = (editor: Editor) => {
let { cursorPosition, paragraphText } = getCursorAndParagraphText(editor);
if (
cursorPosition.ch === 0 ||
isAtStartOfListItem(cursorPosition, paragraphText)
) {
// if cursor is already at the start of this paragraph, move to the previous paragraph
setCursorAtEndOfLine(
editor,
getPrevNonEmptyLine(editor, cursorPosition.line),
);
}
({ cursorPosition, paragraphText } = getCursorAndParagraphText(editor));
forEachSentence(paragraphText, (sentence) => {
const startOfSentence = sentence.index;
// handle any spaces behind the cursor
while (
cursorPosition.ch > 0 &&
paragraphText.charAt(cursorPosition.ch - 1) === ' '
) {
editor.setCursor({
line: cursorPosition.line,
ch: cursorPosition.ch - 1,
});
({ cursorPosition, paragraphText } = getCursorAndParagraphText(editor));
}
if (
cursorPosition.ch > startOfSentence &&
startOfSentence + sentence[0].length >= cursorPosition.ch
) {
const newPosition = startOfSentence;
editor.setCursor({
line: cursorPosition.line,
ch: newPosition,
});
if (newPosition >= paragraphText.length) {
setCursorAtStartOfLine(
editor,
getPrevNonEmptyLine(editor, cursorPosition.line),
);
}
}
});
};
export const moveToStartOfNextSentence = (editor: Editor) => {
let { cursorPosition, paragraphText } = getCursorAndParagraphText(editor);
if (cursorPosition.ch === paragraphText.length) {
// if cursor is already at the end of this paragraph, move to the next paragraph
setCursorAtStartOfLine(
editor,
getNextNonEmptyLine(editor, cursorPosition.line),
);
} else {
forEachSentence(paragraphText, (sentence) => {
const startOfSentence = sentence.index;
const endOfSentence = startOfSentence + sentence[0].length;
// handle any spaces in front of the cursor
while (
cursorPosition.ch < paragraphText.length &&
paragraphText.charAt(cursorPosition.ch) === ' '
) {
editor.setCursor({
line: cursorPosition.line,
ch: cursorPosition.ch + 1,
});
({ cursorPosition, paragraphText } = getCursorAndParagraphText(editor));
}
if (
cursorPosition.ch >= startOfSentence &&
cursorPosition.ch <= endOfSentence
) {
const newPosition = {
line: cursorPosition.line,
ch: endOfSentence,
};
setCursorAtNextWordCharacter({
editor,
cursorPosition: newPosition,
paragraphText,
direction: 'end',
});
if (endOfSentence >= paragraphText.length) {
setCursorAtStartOfLine(
editor,
getNextNonEmptyLine(editor, cursorPosition.line),
);
}
}
});
}
};
export const selectSentence = (editor: Editor) => {
const { cursorPosition, paragraphText } = getCursorAndParagraphText(editor);
let found = false;
// Ignore markdown list characters
let paragraphTextProcessed = paragraphText;
let offset = 0;
const matches = paragraphText.match(/^(\d+\.|[-*+]) /);
if (matches) {
offset = matches[0].length;
paragraphTextProcessed = paragraphText.slice(offset);
}
forEachSentence(paragraphTextProcessed, (sentence) => {
if (
!found &&
cursorPosition.ch <= offset + sentence.index + sentence[0].length
) {
editor.setSelection(
{ line: cursorPosition.line, ch: offset + sentence.index },
{
line: cursorPosition.line,
ch: offset + sentence.index + sentence[0].length,
},
);
found = true;
}
});
};