| 1842 | // have identical opening and closing symbols |
| 1843 | // TODO support across multiple lines |
| 1844 | function findBeginningAndEnd(cm, symb, inclusive) { |
| 1845 | var cur = cm.getCursor(); |
| 1846 | var line = cm.getLine(cur.line); |
| 1847 | var chars = line.split(''); |
| 1848 | var start, end, i, len; |
| 1849 | var firstIndex = chars.indexOf(symb); |
| 1850 | |
| 1851 | // the decision tree is to always look backwards for the beginning first, |
| 1852 | // but if the cursor is in front of the first instance of the symb, |
| 1853 | // then move the cursor forward |
| 1854 | if (cur.ch < firstIndex) { |
| 1855 | cur.ch = firstIndex; |
| 1856 | // Why is this line even here??? |
| 1857 | // cm.setCursor(cur.line, firstIndex+1); |
| 1858 | } |
| 1859 | // otherwise if the cursor is currently on the closing symbol |
| 1860 | else if (firstIndex < cur.ch && chars[cur.ch] == symb) { |
| 1861 | end = cur.ch; // assign end to the current cursor |
| 1862 | --cur.ch; // make sure to look backwards |
| 1863 | } |
| 1864 | |
| 1865 | // if we're currently on the symbol, we've got a start |
| 1866 | if (chars[cur.ch] == symb && !end) { |
| 1867 | start = cur.ch + 1; // assign start to ahead of the cursor |
| 1868 | } else { |
| 1869 | // go backwards to find the start |
| 1870 | for (i = cur.ch; i > -1 && !start; i--) { |
| 1871 | if (chars[i] == symb) { |
| 1872 | start = i + 1; |
| 1873 | } |
| 1874 | } |
| 1875 | } |
| 1876 | |
| 1877 | // look forwards for the end symbol |
| 1878 | if (start && !end) { |
| 1879 | for (i = start, len = chars.length; i < len && !end; i++) { |
| 1880 | if (chars[i] == symb) { |
| 1881 | end = i; |
| 1882 | } |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | // nothing found |
| 1887 | if (!start || !end) { |
| 1888 | return { start: cur, end: cur }; |
| 1889 | } |
| 1890 | |
| 1891 | // include the symbols |
| 1892 | if (inclusive) { |
| 1893 | --start; ++end; |
| 1894 | } |
| 1895 | |
| 1896 | return { |
| 1897 | start: { line: cur.line, ch: start }, |
| 1898 | end: { line: cur.line, ch: end } |
| 1899 | }; |
| 1900 | } |
| 1901 | |