Changeset View
Changeset View
Standalone View
Standalone View
src/script/data/indentation/cppstyle.js
Show First 20 Lines • Show All 1640 Lines • ▼ Show 20 Line(s) | 1639 | { | |||
---|---|---|---|---|---|
1641 | var line = cursor.line; | 1641 | var line = cursor.line; | ||
1642 | var column = cursor.column; | 1642 | var column = cursor.column; | ||
1643 | 1643 | | |||
1644 | var braceCursor = Cursor.invalid(); | 1644 | var braceCursor = Cursor.invalid(); | ||
1645 | if (ch != '>') | 1645 | if (ch != '>') | ||
1646 | { | 1646 | { | ||
1647 | // TODO Make sure a given `ch` in the gBraceMap | 1647 | // TODO Make sure a given `ch` in the gBraceMap | ||
1648 | braceCursor = document.anchor(line, column - 1, gBraceMap[ch]); | 1648 | braceCursor = document.anchor(line, column - 1, gBraceMap[ch]); | ||
1649 | | ||||
1650 | // make sure we are actually on the character we are expecting | ||||
1651 | // if that's not the case, a brace was probably inserted behind the cursor by auto brackets | ||||
1652 | realCharacter = document.charAt(line, column - 1); | ||||
1653 | if (realCharacter != ch) { | ||||
1654 | braceCursor = document.anchor(line, column, gBraceMap[ch]); | ||||
1655 | } | ||||
1649 | // TODO Otherwise, it seems we have a template parameters list... | 1656 | // TODO Otherwise, it seems we have a template parameters list... | ||
1650 | } | 1657 | } | ||
1651 | 1658 | | |||
1659 | // unindent when starting new block and previous line has lower indentation level | ||||
1660 | // prevents over-indentation when inserting condition or loop headers with their opening brace in the next line | ||||
1661 | var firstPos = document.firstColumn(line); | ||||
1662 | var prevFirstPos = document.firstColumn(line - 1); | ||||
1663 | if (firstPos == column - 1 && ch == '}' && firstPos > prevFirstPos) { | ||||
1664 | result = prevFirstPos; | ||||
1665 | } | ||||
1666 | | ||||
1652 | // Check if a given closing brace is a first char on a line | 1667 | // Check if a given closing brace is a first char on a line | ||
1653 | // (i.e. it is 'dangling' brace)... | 1668 | // (i.e. it is 'dangling' brace)... | ||
1654 | if (justEnteredCharIsFirstOnLine(line, column, ch) && braceCursor.isValid()) | 1669 | if (justEnteredCharIsFirstOnLine(line, column, ch) && braceCursor.isValid()) | ||
1655 | { | 1670 | { | ||
1656 | // Move to one half-TAB right, if anything but not closing '}', else | 1671 | // Move to one half-TAB right, if anything but not closing '}', else | ||
1657 | // align to the corresponding open char | 1672 | // align to the corresponding open char | ||
1658 | result = document.firstColumn(braceCursor.line) + (ch != '}' ? 2 : 0); | 1673 | result = document.firstColumn(braceCursor.line) + (ch != '}' ? 2 : 0); | ||
1659 | dbg("tryCloseBracket: setting result="+result); | 1674 | dbg("tryCloseBracket: setting result="+result); | ||
1660 | } | 1675 | } | ||
1661 | 1676 | | |||
1662 | // Check if ';' required after closing '}' | 1677 | // Check if ';' required after closing '}', but make sure it was inserted by the user | ||
1678 | // and not automatically by auto brackets (behind the cursor) | ||||
1663 | if (ch == '}' && braceCursor.isValid()) | 1679 | if (ch == '}' && braceCursor.isValid()) | ||
1664 | { | 1680 | { | ||
1665 | var is_check_needed = false; | 1681 | var is_check_needed = false; | ||
1666 | // Check if corresponding anchor is a class/struct/union/enum, | 1682 | // Check if corresponding anchor is a class/struct/union/enum, | ||
1667 | // (possible keyword located on same or prev line) | 1683 | // (possible keyword located on same or prev line) | ||
1668 | // and check for trailing ';'... | 1684 | // and check for trailing ';'... | ||
1669 | var anchoredString = document.line(braceCursor.line); | 1685 | var anchoredString = document.line(braceCursor.line); | ||
1670 | dbg("tryCloseBracket: anchoredString='"+anchoredString+"'"); | 1686 | dbg("tryCloseBracket: anchoredString='"+anchoredString+"'"); | ||
1671 | var regex = /^(\s*)(class|struct|union|enum).*$/; | 1687 | var regex = /^(\s*)(class|struct|union|enum).*$/; | ||
1672 | var r = regex.exec(anchoredString); | 1688 | var r = regex.exec(anchoredString); | ||
1673 | if (r != null) | 1689 | if (r != null) | ||
1674 | { | 1690 | { | ||
1675 | dbg("tryCloseBracket: same line"); | 1691 | dbg("tryCloseBracket: same line"); | ||
1676 | is_check_needed = true; | 1692 | is_check_needed = true; | ||
1677 | } | 1693 | } | ||
1678 | else (!is_check_needed && 0 < braceCursor.line) // Is there any line before? | 1694 | else (!is_check_needed && 0 < braceCursor.line) // Is there any line before? | ||
1679 | { | 1695 | { | ||
1680 | dbg("tryCloseBracket: cheking prev line"); | 1696 | dbg("tryCloseBracket: checking prev line"); | ||
1681 | 1697 | | |||
1682 | // Ok, lets check it! | 1698 | // Ok, lets check it! | ||
1683 | anchoredString = document.line(braceCursor.line - 1); | 1699 | anchoredString = document.line(braceCursor.line - 1); | ||
1684 | dbg("tryCloseBracket: anchoredString-1='"+anchoredString+"'"); | 1700 | dbg("tryCloseBracket: anchoredString-1='"+anchoredString+"'"); | ||
1685 | r = regex.exec(anchoredString); | 1701 | r = regex.exec(anchoredString); | ||
1686 | if (r != null) | 1702 | if (r != null) | ||
1687 | { | 1703 | { | ||
1688 | is_check_needed = true; | 1704 | is_check_needed = true; | ||
1689 | dbg("tryCloseBracket: prev line"); | 1705 | dbg("tryCloseBracket: prev line"); | ||
1690 | } | 1706 | } | ||
1691 | } | 1707 | } | ||
1692 | dbg("tryCloseBracket: is_check_needed="+is_check_needed); | 1708 | dbg("tryCloseBracket: is_check_needed="+is_check_needed); | ||
1693 | if (is_check_needed) | 1709 | if (is_check_needed) | ||
1694 | { | 1710 | { | ||
1695 | var is_ok = document.line(line) | 1711 | var is_ok = document.line(line) | ||
1696 | .substring(column, document.lineLength(line)) | 1712 | .substring(column, document.lineLength(line)) | ||
1697 | .ltrim() | 1713 | .ltrim() | ||
1698 | .startsWith(';') | 1714 | .startsWith(';') | ||
1699 | ; | 1715 | ; | ||
1700 | if (!is_ok) | 1716 | if (!is_ok) | ||
1701 | { | 1717 | { | ||
1702 | document.insertText(line, column, ';'); | 1718 | var pos = column; | ||
1703 | view.setCursorPosition(line, column + 1); | 1719 | var newCursorPos = pos + 1; | ||
1720 | if (document.charAt(line, column - 1) != '}' && document.charAt(line, column) == '}') { | ||||
1721 | ++pos; | ||||
1722 | --newCursorPos; | ||||
1723 | } | ||||
1724 | document.insertText(line, pos, ';'); | ||||
1725 | view.setCursorPosition(line, newCursorPos); | ||||
1704 | } | 1726 | } | ||
1705 | } | 1727 | } | ||
1706 | } | 1728 | } | ||
1707 | else if (ch == '>') | 1729 | else if (ch == '>') | ||
1708 | { | 1730 | { | ||
1709 | // If user typed 'some' + '<' + '>', jump over the '>' | 1731 | // If user typed 'some' + '<' + '>', jump over the '>' | ||
1710 | // (which was added by the tryTemplate) | 1732 | // (which was added by the tryTemplate) | ||
1711 | if (document.charAt(line, column) == '>') | 1733 | if (document.charAt(line, column) == '>') | ||
▲ Show 20 Lines • Show All 627 Lines • ▼ Show 20 Line(s) | 2356 | { | |||
2339 | 2361 | | |||
2340 | // TODO Is there any `assert' in JS? | 2362 | // TODO Is there any `assert' in JS? | ||
2341 | if (line != cursor.line) | 2363 | if (line != cursor.line) | ||
2342 | { | 2364 | { | ||
2343 | dbg("ASSERTION FAILURE: line != cursor.line"); | 2365 | dbg("ASSERTION FAILURE: line != cursor.line"); | ||
2344 | return result; | 2366 | return result; | ||
2345 | } | 2367 | } | ||
2346 | 2368 | | |||
2347 | document.editBegin(); | 2369 | document.editBegin(); | ||
2348 | // Check if char under cursor is the same as just entered, | | |||
2349 | // and if so, remove it... to make it behave like "overwrite" mode | | |||
2350 | if (ch != ' ' && document.charAt(cursor) == ch) | | |||
2351 | document.removeText(line, cursor.column, line, cursor.column + 1); | | |||
2352 | | ||||
2353 | switch (ch) | 2370 | switch (ch) | ||
brauch: if you don't want this, remove it and the comment | |||||
2354 | { | 2371 | { | ||
2355 | case '\n': | 2372 | case '\n': | ||
2356 | result = caretPressed(cursor); | 2373 | result = caretPressed(cursor); | ||
2357 | break; | 2374 | break; | ||
2358 | case '/': | 2375 | case '/': | ||
2359 | trySameLineComment(cursor); // Possible user wants to start a comment | 2376 | trySameLineComment(cursor); // Possible user wants to start a comment | ||
2360 | break; | 2377 | break; | ||
2361 | case '<': | 2378 | case '<': | ||
▲ Show 20 Lines • Show All 420 Lines • Show Last 20 Lines |
if you don't want this, remove it and the comment