MCPcopy
hub / github.com/witheve/Eve / findBeginningAndEnd

Function findBeginningAndEnd

src/codemirror.js:12849–12905  ·  view source on GitHub ↗
(cm, head, symb, inclusive)

Source from the content-addressed store, hash-verified

12847 // have identical opening and closing symbols
12848 // TODO support across multiple lines
12849 function findBeginningAndEnd(cm, head, symb, inclusive) {
12850 var cur = copyCursor(head);
12851 var line = cm.getLine(cur.line);
12852 var chars = line.split('');
12853 var start, end, i, len;
12854 var firstIndex = chars.indexOf(symb);
12855
12856 // the decision tree is to always look backwards for the beginning first,
12857 // but if the cursor is in front of the first instance of the symb,
12858 // then move the cursor forward
12859 if (cur.ch < firstIndex) {
12860 cur.ch = firstIndex;
12861 // Why is this line even here???
12862 // cm.setCursor(cur.line, firstIndex+1);
12863 }
12864 // otherwise if the cursor is currently on the closing symbol
12865 else if (firstIndex < cur.ch && chars[cur.ch] == symb) {
12866 end = cur.ch; // assign end to the current cursor
12867 --cur.ch; // make sure to look backwards
12868 }
12869
12870 // if we're currently on the symbol, we've got a start
12871 if (chars[cur.ch] == symb && !end) {
12872 start = cur.ch + 1; // assign start to ahead of the cursor
12873 } else {
12874 // go backwards to find the start
12875 for (i = cur.ch; i > -1 && !start; i--) {
12876 if (chars[i] == symb) {
12877 start = i + 1;
12878 }
12879 }
12880 }
12881
12882 // look forwards for the end symbol
12883 if (start && !end) {
12884 for (i = start, len = chars.length; i < len && !end; i++) {
12885 if (chars[i] == symb) {
12886 end = i;
12887 }
12888 }
12889 }
12890
12891 // nothing found
12892 if (!start || !end) {
12893 return { start: cur, end: cur };
12894 }
12895
12896 // include the symbols
12897 if (inclusive) {
12898 --start; ++end;
12899 }
12900
12901 return {
12902 start: Pos(cur.line, start),
12903 end: Pos(cur.line, end)
12904 };
12905 }
12906

Callers 1

VimFunction · 0.85

Calls 2

copyCursorFunction · 0.85
getLineMethod · 0.80

Tested by

no test coverage detected