Skip to content

Commit ff2c37b

Browse files
committed
Implemented auto select mode
1 parent 5558355 commit ff2c37b

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

src/core.ts

+14-11
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ function getSelnRange(editor: Editor, settings: PluginSettings) {
5757
} else {
5858
switch (settings.nothingSelected) {
5959
case NothingSelected.autoSelect:
60-
replaceRange = getWordBoundaries(editor);
60+
replaceRange = getWordBoundaries(editor, settings);
6161
selectedText = editor.getRange(replaceRange.from, replaceRange.to);
6262
break;
6363
case NothingSelected.insertInline:
@@ -162,16 +162,20 @@ function getCbText(cb: string | ClipboardEvent): string | null {
162162
return clipboardText.trim();
163163
}
164164

165-
function getWordBoundaries(editor: Editor): EditorRange {
165+
function getWordBoundaries(editor: Editor, settings: PluginSettings): EditorRange {
166166
const cursor = editor.getCursor();
167-
let wordBoundaries: EditorRange;
168-
169-
// if (editor.getTokenTypeAt(cursor) === "url") {
170-
// const { start: startCh, end: endCh } = editor.getTokenAt(cursor);
171-
// const line = cursor.line;
172-
// wordBoundaries = { from: { line, ch: startCh }, to: { line, ch: endCh } };
173-
// } else
174-
wordBoundaries = findWordAt(editor.getLine(cursor.ch), cursor);;
167+
const line = editor.getLine(cursor.line);
168+
let wordBoundaries = findWordAt(line, cursor);;
169+
170+
// If the token the cursor is on is a url, grab the whole thing instead of just parsing it like a word
171+
let start = wordBoundaries.from.ch;
172+
let end = wordBoundaries.to.ch;
173+
while (start > 0 && !/\s/.test(line.charAt(start - 1))) --start;
174+
while (end < line.length && !/\s/.test(line.charAt(end))) ++end;
175+
if (isUrl(line.slice(start, end), settings)) {
176+
wordBoundaries.from.ch = start;
177+
wordBoundaries.to.ch = end;
178+
}
175179
return wordBoundaries;
176180
}
177181

@@ -189,7 +193,6 @@ const findWordAt = (() => {
189193
let end = pos.ch;
190194
(end === line.length) ? --start : ++end;
191195
const startChar = line.charAt(pos.ch);
192-
193196
if (isWordChar(startChar)) {
194197
check = (ch: string) => isWordChar(ch);
195198
} else if (/\s/.test(startChar)) {

0 commit comments

Comments
 (0)