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 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951
|
;;; kotlin-mode-indent.el --- Major mode for kotlin, indentation -*- lexical-binding: t; -*-
;; Copyright (C) 2019 taku0
;; Authors: taku0 (http://github.com/taku0)
;; This file is not part of GNU Emacs.
;; This program is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Routines for indentation
;;; Code:
(require 'rx)
(require 'cl-lib)
(require 'kotlin-mode-lexer)
;;; Customizations
(defcustom kotlin-tab-width 4
"Amount of indentation for block contents.
Example:
class Foo {
func foo() {} // offset of this line
}"
:type 'integer
:group 'kotlin
:safe 'integerp)
(defcustom kotlin-mode-parenthesized-expression-offset 4
"Amount of indentation inside parentheses and square brackets.
Example:
foo(
1 // offset of this line
)"
:type 'integer
:group 'kotlin
:safe 'integerp)
(defcustom kotlin-mode-multiline-statement-offset 4
"Amount of indentation for continuations of expressions.
Example:
val x = 1 +
2 // offset of this line"
:type 'integer
:group 'kotlin
:safe 'integerp)
(defcustom kotlin-mode-prepend-asterisk-to-comment-line t
"Automatically insert a asterisk to each comment line if non-nil.
Example: if the enter key is pressed when the point is after A below,
/*
* A
*/
an asterisk is inserted to the newline:
/*
* A
*
*/"
:type 'boolean
:group 'kotlin
:safe 'booleanp)
(defcustom kotlin-mode-insert-space-after-asterisk-in-comment t
"Automatically insert a space after asterisk in comment if non-nil.
Example: if an asterisk is inserted before A below,
/*
A
*/
a space is inserted after asterisk:
/*
* A
*/"
:type 'boolean
:group 'kotlin
:safe 'booleanp)
(defcustom kotlin-mode-auto-close-multiline-comment t
"If non-nil, `indent-new-comment-line' automatically close multiline comment.
Example: when the enter key is pressed after unclosed comment below,
/**
a closing delimiter is inserted automatically:
/**
* // cursor is here
*/"
:type 'boolean
:group 'kotlin
:safe 'booleanp)
(defcustom kotlin-mode-fix-comment-close t
"Fix \"* /\" in incomplete multiline comment to \"*/\" if non-nil.
Example:
/*
*
* // when a slash is inserted here
/*
*
*/ // it become like this
/*
*
* / // rather than like this."
:type 'boolean
:group 'kotlin
:safe 'booleanp)
(defcustom kotlin-mode-break-line-before-comment-close t
"If non-nil, break line before the closing delimiter of multiline comments.
Example: if line break is inserted before A below,
/** A */
it becomes like this:
/**
* A
*/
rather than like this:
/**
* A */"
:type 'boolean
:group 'kotlin
:safe 'booleanp)
(defcustom kotlin-mode-indent-nonempty-line-in-multiline-string nil
"If non-nil, indent nonempty line in multiline string.
`indent-according-to-mode' is no-op otherwise."
:type 'boolean
:group 'kotlin
:safe 'booleanp)
(defcustom kotlin-mode-highlight-anchor nil
"Highlight anchor point for indentation if non-nil.
Intended for debugging."
:type 'boolean
:group 'kotlin
:safe 'booleanp)
;;; Constants and variables
(defconst kotlin-mode--statement-parent-tokens
'(\( \[ { anonymous-function-parameter-arrow when-expression-arrow
bare-else \(\)-before-control-structure-body \; implicit-\;)
"Parent tokens for statements.
Parent tokens are tokens before the beginning of statements.")
(defconst kotlin-mode--expression-parent-tokens
(append kotlin-mode--statement-parent-tokens
'(\, "where" string-chunk-before-template-expression))
"Parent tokens for expressions.
Parent tokens are tokens before the beginning of expressions.")
(defvar-local kotlin-mode--anchor-overlay nil)
(defvar-local kotlin-mode--anchor-overlay-timer nil)
;;; Indentation struct
(defclass kotlin-mode--indentation ()
((position :initarg :position
:type number
:accessor kotlin-mode--indentation-position
:documentation "the position of the anchor point, such as
the start of the previous line or the start of the class declaration.")
(offset :initarg :offset
:type number
:initform 0
:accessor kotlin-mode--indentation-offset
:documentation "the offset from the anchor point. For
example, when indenting the first line of a class body, its anchor
point is the start of the class declaration and its offset is
`kotlin-tab-width'."))
"Indentation.")
;;; Indentation logic
(defun kotlin-mode--indent-line ()
"Indent the current line."
(let* ((indentation (save-excursion (kotlin-mode--calculate-indent)))
(indentation-column
(save-excursion
(goto-char (kotlin-mode--indentation-position indentation))
(+ (current-column) (kotlin-mode--indentation-offset indentation))))
(current-indent
(save-excursion (back-to-indentation) (current-column))))
(if (<= (current-column) current-indent)
;; The point is on the left margin.
;; Move the point to the new leftmost non-comment char.
(indent-line-to indentation-column)
;; Keep current relative position from leftmost non-comment char.
(save-excursion (indent-line-to indentation-column)))
(when kotlin-mode-highlight-anchor
(kotlin-mode-highlight-anchor indentation))))
(defun kotlin-mode--calculate-indent ()
"Return the indentation of the current line."
(back-to-indentation)
(let ((parser-state (syntax-ppss)))
(cond
((nth 4 parser-state)
;; If the 4th element of `(syntax-ppss)' is non-nil, the point is on
;; the 2nd or following lines of a multiline comment, because:
;;
;; - The 4th element of `(syntax-ppss)' is nil on the comment starter.
;; - We have called `back-to-indentation`.
(kotlin-mode--calculate-indent-of-multiline-comment))
((eq (nth 3 parser-state) t)
(kotlin-mode--calculate-indent-of-multiline-string))
((looking-at "//")
(kotlin-mode--calculate-indent-of-single-line-comment))
(t
(kotlin-mode--calculate-indent-of-code)))))
(defun kotlin-mode--calculate-indent-of-multiline-comment ()
"Return the indentation of the current line inside a multiline comment."
(back-to-indentation)
(let ((comment-beginning-position (nth 8 (syntax-ppss)))
(starts-with-asterisk (eq (char-after) ?*)))
(forward-line -1)
(back-to-indentation)
(cond
;; 2nd line of the comment
((<= (point) comment-beginning-position)
;; The point was on the 2nd line of the comment.
(goto-char comment-beginning-position)
(forward-char)
;; If there are extra characters or spaces after asterisks, align with
;; the first non-space character or end of line. Otherwise, align with
;; the first asterisk.
(when (and
(looking-at
(rx (seq (zero-or-more "*") (one-or-more (not (any "\n*"))))))
(not (and kotlin-mode-prepend-asterisk-to-comment-line
starts-with-asterisk)))
(skip-chars-forward "*")
(skip-syntax-forward " "))
(make-instance 'kotlin-mode--indentation :position (point) :offset 0))
;; The point was on the 3rd or following lines of the comment.
;; Before closing delimiter
((= (save-excursion
;; Back to the original line.
(forward-line)
(back-to-indentation)
(point))
(save-excursion
;; Goto just before the closing delimiter.
(goto-char comment-beginning-position)
(if (forward-comment 1)
(progn
;; slash
(backward-char)
(skip-chars-backward "*")
(point))
-1)))
;; Before the closing delimiter. Align with the first asterisk of the
;; opening delimiter.
;;
;; TODO: If there are multiple asterisks on the closing
;; delimiter, and the middle lines have no leading asterisks,
;; align with the slash of the opening delimiter
;;
;; Example:
;;
;; /*********
;; aaa
;; **********/
(goto-char comment-beginning-position)
(forward-char)
(make-instance 'kotlin-mode--indentation :position (point) :offset 0))
;; Otherwise, align with a non-empty preceding line.
;; The previous line is empty
((and (bolp) (eolp))
;; Seek a non-empty-line.
(while (and (bolp) (eolp) (not (bobp)))
(forward-line -1))
(forward-line)
(kotlin-mode--calculate-indent-of-multiline-comment))
;; The previous line is not empty
(t
;; Align to this line.
(make-instance 'kotlin-mode--indentation :position (point) :offset 0)))))
(defun kotlin-mode--calculate-indent-of-multiline-string ()
"Return the indentation of the current line inside a multiline string."
(back-to-indentation)
(let ((string-beginning-position
(save-excursion (kotlin-mode--beginning-of-string))))
(if (looking-at "\"\"\"")
;; Indenting the closing delimiter. Align with the start of containing
;; expression with extra indentation.
;;
;; Example:
;;
;; val someString = """
;; Hello.
;; """
;;
;; When aligning to opening delimiter, align without offset:
;; foo("""
;; aaa
;; """)
;;
;; Possible alternative indentation:
;; val someString = """
;; Hello.
;; """
(progn
(goto-char string-beginning-position)
(let ((indentation
(kotlin-mode--calculate-indent-of-expression
kotlin-mode-multiline-statement-offset)))
(if (= (kotlin-mode--indentation-position indentation)
string-beginning-position)
(make-instance 'kotlin-mode--indentation
:position string-beginning-position
:offset 0)
indentation)))
;; Other than the closing delimiter.
(if (and (not (eolp))
(not kotlin-mode-indent-nonempty-line-in-multiline-string))
;; The user prefers to keep indentations inside multiline string.
(make-instance 'kotlin-mode--indentation :position (point) :offset 0)
;; The user prefers to indent lines inside multiline string.
(beginning-of-line)
(backward-char)
(kotlin-mode--goto-non-template-expression-bol)
(back-to-indentation)
(cond
;; 2nd line of string
((<= (point) string-beginning-position)
;; The point was on the 2nd line of the string. Align
;; with the start of containing expression with extra
;; indentation.
(goto-char string-beginning-position)
(kotlin-mode--calculate-indent-of-expression
kotlin-mode-multiline-statement-offset))
;; The point was on the 3rd or following lines of the string.
;; Align with a non-empty preceding line.
;; The previous line is empty
((and (bolp) (eolp))
;; Seek a non-empty-line.
(while (and (bolp) (eolp) (not (bobp)))
(forward-line -1))
(forward-line)
(kotlin-mode--calculate-indent-of-multiline-string))
;; The previous line is not empty
(t
;; Align to this line.
(make-instance 'kotlin-mode--indentation
:position (point)
:offset 0)))))))
(defun kotlin-mode--goto-non-template-expression-bol ()
"Back to the beginning of line that is not inside a template expression."
(let ((chunk-beginning-position (nth 8 (syntax-ppss)))
(matching-bracket t))
(while (and matching-bracket
(< (line-beginning-position) chunk-beginning-position))
(setq matching-bracket
(get-text-property
chunk-beginning-position 'kotlin-property--matching-bracket))
(when matching-bracket
(goto-char matching-bracket)
(setq chunk-beginning-position (nth 8 (syntax-ppss)))))
(beginning-of-line)))
(defun kotlin-mode--calculate-indent-of-single-line-comment ()
"Return the indentation of the current line inside a single-line comment."
(cond
;; When the comment is beginning of the buffer, indent to the column 0.
((save-excursion
(beginning-of-line)
(bobp))
(make-instance 'kotlin-mode--indentation :position (point-min) :offset 0))
;; If the previous line is also a single-line comment, align with it.
((save-excursion
(forward-line -1)
(skip-syntax-forward " ")
(looking-at "//"))
(forward-line -1)
(skip-syntax-forward " ")
(make-instance 'kotlin-mode--indentation :position (point) :offset 0))
;; Otherwise, indent like a expression.
(t
(kotlin-mode--calculate-indent-of-code))))
(defun kotlin-mode--calculate-indent-of-code ()
"Return the indentation of the current line outside comments or string."
(back-to-indentation)
(let* ((previous-token (save-excursion (kotlin-mode--backward-token)))
(previous-type (kotlin-mode--token-type previous-token))
(previous-text (kotlin-mode--token-text previous-token))
(next-token (save-excursion (kotlin-mode--forward-token)))
(next-type (kotlin-mode--token-type next-token))
(next-text (kotlin-mode--token-text next-token))
(next-is-on-current-line
(<= (kotlin-mode--token-start next-token) (line-end-position))))
(cond
;; Beginning of the buffer
((eq previous-type 'outside-of-buffer)
(make-instance 'kotlin-mode--indentation :position (point-min) :offset 0))
;; Before } on the current line.
;; Align with the head of the statement.
((and next-is-on-current-line (eq next-type '}))
(goto-char (kotlin-mode--token-end next-token))
(backward-list)
(kotlin-mode--calculate-indent-after-open-curly-brace 0))
;; Before ) or ] on the current line.
;; Align with the head of the expression.
((and next-is-on-current-line
(memq next-type '(\) \)-before-control-structure-body \])))
(goto-char (kotlin-mode--token-end next-token))
(backward-list)
(kotlin-mode--calculate-indent-of-expression 0))
;; Before close angle bracket for generics (>) on the current line.
((and next-is-on-current-line
(equal next-text ">")
(save-excursion
(goto-char (kotlin-mode--token-end next-token))
(eq (kotlin-mode--token-type (kotlin-mode--backward-token-or-list))
'<>)))
(goto-char (kotlin-mode--token-end next-token))
(kotlin-mode--backward-token-or-list)
(kotlin-mode--calculate-indent-of-expression 0))
;; Before end of a template expression on the current line.
((and next-is-on-current-line
(eq next-type 'string-chunk-after-template-expression))
(goto-char (get-text-property
(kotlin-mode--token-start next-token)
'kotlin-property--matching-bracket))
;; Skip "${"
(forward-char 2)
(kotlin-mode--backward-string-chunk)
(kotlin-mode--calculate-indent-after-beginning-of-template-expression
0))
;; Before { on the current line
((and next-is-on-current-line (eq next-type '{))
(kotlin-mode--calculate-indent-after-open-curly-brace 0))
;; Before "else" on the current line
((and next-is-on-current-line (equal next-text "else"))
(kotlin-mode--calculate-indent-of-else))
;; Before "where" on the current line
((and next-is-on-current-line (equal next-text "where"))
(kotlin-mode--find-parent-and-align-with-next
kotlin-mode--statement-parent-tokens
kotlin-mode-multiline-statement-offset))
;; Before "catch" or "finally" on the current line
((and next-is-on-current-line (member next-text '("catch" "finally")))
(kotlin-mode--find-parent-and-align-with-next
kotlin-mode--statement-parent-tokens
0
'()
'("catch" "finally")
0))
;; Before "=" on the current line
;;
;; fun <T> foo(): T
;; where
;; T: A,
;; T: B
;; = bar()
((and next-is-on-current-line (equal next-text "="))
(kotlin-mode--find-parent-and-align-with-next
(remove '\, (remove "where" kotlin-mode--expression-parent-tokens))
kotlin-mode-multiline-statement-offset))
;; Before "," on the current line
((and next-is-on-current-line (eq next-type '\,))
(kotlin-mode--calculate-indent-of-prefix-comma))
;; After ","
((eq previous-type '\,)
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--calculate-indent-after-comma))
;; After "catch". Align with "try".
((equal previous-text "catch")
(kotlin-mode--find-parent-and-align-with-next
kotlin-mode--statement-parent-tokens
kotlin-mode-multiline-statement-offset))
;; After {
((eq previous-type '{)
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--calculate-indent-after-open-curly-brace
kotlin-tab-width))
;; After ( or [
((memq previous-type '(\( \[))
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--calculate-indent-of-expression
kotlin-mode-parenthesized-expression-offset
kotlin-mode-parenthesized-expression-offset))
;; After open angle bracket for generics (<)
((and (equal previous-text "<")
(save-excursion
(goto-char (kotlin-mode--token-start previous-token))
(eq (kotlin-mode--token-type (kotlin-mode--forward-token-or-list))
'<>)))
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--calculate-indent-of-expression
kotlin-mode-parenthesized-expression-offset
kotlin-mode-parenthesized-expression-offset))
;; After beginning of a template expression
((eq previous-type 'string-chunk-before-template-expression)
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--calculate-indent-after-beginning-of-template-expression
kotlin-mode-parenthesized-expression-offset))
;; After ; or implicit-\;
((memq previous-type '(\; implicit-\;))
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--calculate-indent-after-semicolon))
;; After "->" of lambda parameters
((eq previous-type 'anonymous-function-parameter-arrow)
(goto-char (cdr (kotlin-mode--find-containing-brackets
(kotlin-mode--token-start previous-token))))
(kotlin-mode--calculate-indent-after-open-curly-brace
kotlin-tab-width))
;; Before "->" of lambda parameters on the current line
((and next-is-on-current-line
(eq next-type 'anonymous-function-parameter-arrow))
(if (eq (kotlin-mode--token-type (kotlin-mode--backward-token-or-list))
'{)
(kotlin-mode--calculate-indent-after-open-curly-brace
kotlin-tab-width)
(goto-char (cdr (kotlin-mode--find-containing-brackets
(kotlin-mode--token-start next-token))))
(kotlin-mode--align-with-next-token (kotlin-mode--forward-token))))
;; After "->" of when expression
((eq previous-type 'when-expression-arrow)
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--find-parent-and-align-with-next
(cl-remove-if
(lambda (e)
(memq e '(when-expression-arrow
bare-else \(\)-before-control-structure-body)))
kotlin-mode--statement-parent-tokens)
kotlin-tab-width))
;; Before "->" of when expression on the current line
((and next-is-on-current-line (eq next-type 'when-expression-arrow))
(if (equal (kotlin-mode--token-text previous-token) "else")
(kotlin-mode--align-with-token
previous-token
kotlin-tab-width)
(kotlin-mode--find-parent-and-align-with-next
(cl-remove-if
(lambda (e)
(memq e '(when-expression-arrow
bare-else \(\)-before-control-structure-body)))
kotlin-mode--statement-parent-tokens)
kotlin-tab-width)))
;; After "where"
;;
;; class Foo<T>
;; where
;; T: A // align with "where" with offset
;;
;; class Foo<T> where
;; T: A // align with "class" with offset
((equal previous-text "where")
(goto-char (kotlin-mode--token-start previous-token))
(if (kotlin-mode--bol-other-than-comments-p)
(kotlin-mode--align-with-token
previous-token
kotlin-mode-multiline-statement-offset)
(kotlin-mode--find-parent-and-align-with-next
kotlin-mode--statement-parent-tokens
kotlin-mode-multiline-statement-offset)))
;; After if, when, for, or while
((member previous-text '("if" "when" "for" "while"))
(kotlin-mode--find-parent-and-align-with-next
kotlin-mode--statement-parent-tokens
kotlin-mode-multiline-statement-offset))
;; After do
((equal previous-text "do")
(kotlin-mode--align-with-token
previous-token
(if (equal next-text "while") 0 kotlin-tab-width)))
;; After else
((equal previous-text "else")
(kotlin-mode--align-with-token
previous-token
kotlin-tab-width))
;; After "if ()", "while ()", or "for ()"
((eq previous-type '\)-before-control-structure-body)
(kotlin-mode--backward-token-or-list)
(kotlin-mode--find-parent-and-align-with-next
kotlin-mode--statement-parent-tokens
kotlin-tab-width
'()
'("else")
kotlin-tab-width))
;; Before ; on the current line
((and next-is-on-current-line (eq next-type '\;))
(kotlin-mode--find-parent-and-align-with-next
(remove '\; (remove 'implicit-\; kotlin-mode--statement-parent-tokens))
0
'(\; implicit-\;)))
;; Before "in" on the current line
((and next-is-on-current-line (equal next-text "in"))
(kotlin-mode--calculate-indent-before-in))
;; After "in"
;;
;; Examples:
;; for (
;; x
;; in
;; xs
;; ) {}
;;
;; for (x
;; in
;; xs) {[]}
;;
;; when (x) {
;; in
;; xs -> y in
;; ys
;; in
;; zs -> 1
;; }
;;
;; Foo<
;; in
;; X,
;; @AAA in
;; X
;; >
;;
;; val x = 1 in
;; array
((equal previous-text "in")
(let* ((type-and-parent
(save-excursion
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--find-parent-of-in)))
(type (car type-and-parent)))
(if (or (memq type '(for <>))
(save-excursion
(goto-char (kotlin-mode--token-start previous-token))
(not (kotlin-mode--bol-other-than-comments-p))))
;; for-statement and type parameters, or "in" is not at
;; the beginning of the line.
(progn
;; Indent like a expression
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--calculate-indent-of-expression
kotlin-mode-multiline-statement-offset))
;; After "in" at the beginning of the line.
;;
;; Example:
;;
;; when (x) {
;; in
;; xs -> 1
;; in
;; ys -> 1
;; }
;;
;; Pretend a semicolon exists before "in"
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--align-with-next-token
(kotlin-mode--backward-token)
kotlin-mode-multiline-statement-offset))))
;; Before "while" on the current line
((and next-is-on-current-line (equal next-text "while"))
(let ((do-token (save-excursion (kotlin-mode--find-do-for-while))))
(if do-token
(kotlin-mode--align-with-token do-token)
(kotlin-mode--calculate-indent-after-semicolon))))
;; Inside annotation
((or (and (eq previous-type 'annotation)
(or
;; @file
;; : // ← here
(eq next-type ':)
;; @file:A
;; .B // ← here
;; @file:A
;; <A> // ← here
(member next-text '("." "<"))))
;; @file:
;; [ // ← here
(and (eq previous-type ':)
(eq next-type '\[)
(save-excursion
(goto-char (kotlin-mode--token-start previous-token))
(eq (kotlin-mode--token-type (kotlin-mode--backward-token))
'annotation)))
;; @file:
;; A // ← here
;; @file:A
;; .
;; B // ← here
;; @file:A
;; <
;; B
;; > // ← here
(and (or (eq next-type 'atom) (equal next-text ">"))
(eq (kotlin-mode--token-type
(save-excursion
(kotlin-mode--extend-annotation-token-backward
next-token)))
'annotation)))
(let ((start-of-annotation
(cond
((eq previous-type 'annotation)
(kotlin-mode--token-start previous-token))
((eq previous-type ':)
(save-excursion
(goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--token-start (kotlin-mode--backward-token))))
(t (kotlin-mode--token-start
(save-excursion (kotlin-mode--extend-annotation-token-backward
next-token))))))
(start-of-previous-line
(save-excursion
(kotlin-mode--backward-token-or-list t)
(kotlin-mode--goto-non-comment-bol-with-same-nesting-level t)
(point))))
(goto-char (max start-of-annotation start-of-previous-line))
(kotlin-mode--align-with-current-line
(if (< start-of-annotation start-of-previous-line)
;; 3rd or following lines of the annotation.
;; Align with previous line without offset.
0
;; 2nd line of the annotation.
;; Align with the start of the annotation with offset.
kotlin-mode-multiline-statement-offset))))
;; After annotations or labels
((memq previous-type '(annotation label))
(goto-char (kotlin-mode--token-start previous-token))
;; Align with preceding annotation or label at the beginning of
;; a line, or indent like an expression if the annotations are in
;; middle of a line.
(let ((token previous-token))
(while (and (memq (kotlin-mode--token-type token) '(annotation label))
(not (kotlin-mode--bol-other-than-comments-p)))
(setq token (kotlin-mode--backward-token)))
(unless (memq (kotlin-mode--token-type token) '(annotation label))
;; The annotations are in middle of a line.
(goto-char (kotlin-mode--token-end token))))
(if (kotlin-mode--bol-other-than-comments-p)
;; The annotations are at the beginning of a line.
(kotlin-mode--align-with-current-line)
;; The annotations are in middle of a line.
(kotlin-mode--align-with-next-token
(kotlin-mode--find-parent-of-expression))))
;; Otherwise, it is continuation of the previous line
(t
(goto-char (kotlin-mode--token-end previous-token))
(kotlin-mode--backward-token-or-list)
(kotlin-mode--calculate-indent-of-expression
kotlin-mode-multiline-statement-offset)))))
(defun kotlin-mode--find-parent-and-align-with-next
(parents
&optional
offset
stop-at-eol-token-types
stop-at-bol-token-types
bol-offset)
"Find the parent and return indentation based on it.
A parent is, for example, the open bracket of the containing block or
semicolon of the preceding statement.
PARENTS is a list of token types that precedes the expression or the statement.
OFFSET is the offset. If it is omitted, assumed to be 0.
See `kotlin-mode--backward-sexps-until' for the details of
STOP-AT-EOL-TOKEN-TYPES and STOP-AT-BOL-TOKEN-TYPES.
If scanning stops at STOP-AT-EOL-TOKEN-TYPES, align with the next token with
BOL-OFFSET.
If scanning stops at STOP-AT-BOL-TOKEN-TYPES, align with that token with
BOL-OFFSET.
If STOP-AT-BOL-TOKEN-TYPES or STOP-AT-BOL-TOKEN-TYPES is the symbol
`any', it matches all tokens.
The point is assumed to be on the previous line."
(save-excursion
(let* ((parent (kotlin-mode--backward-sexps-until
parents
stop-at-eol-token-types
stop-at-bol-token-types))
(parent-end (kotlin-mode--token-end parent))
(stopped-at-parent
(or (memq (kotlin-mode--token-type parent) parents)
(member (kotlin-mode--token-text parent) parents)
(eq (kotlin-mode--token-type parent) 'outside-of-buffer)))
(stopped-at-eol
(and
(not stopped-at-parent)
stop-at-eol-token-types
(or
(eq stop-at-eol-token-types 'any)
(memq (kotlin-mode--token-type parent)
stop-at-eol-token-types)
(member (kotlin-mode--token-text parent)
stop-at-eol-token-types)))))
(if stopped-at-parent
(kotlin-mode--align-with-next-token parent offset)
(when stopped-at-eol
(goto-char parent-end)
(forward-comment (point-max)))
(kotlin-mode--align-with-current-line bol-offset)))))
(defun kotlin-mode--calculate-indent-of-expression
(&optional
offset
bol-offset)
"Return indentation of the current expression.
the point is assumed to be on the previous line.
OFFSET is the offset. If it is omitted, assumed to be 0.
If scanning stops at eol, align with the next token with BOL-OFFSET."
(save-excursion
(let* ((pos (point))
(parent-of-previous-line
(save-excursion
(kotlin-mode--goto-non-comment-bol-with-same-nesting-level)
(kotlin-mode--backward-token)))
(parent-of-expression (kotlin-mode--find-parent-of-expression)))
;; Skip annotations
(goto-char (kotlin-mode--token-end parent-of-expression))
(kotlin-mode--forward-annotations)
(kotlin-mode--goto-non-comment-bol-with-same-nesting-level)
(when (or (< (point) (kotlin-mode--token-end parent-of-expression))
(< pos (point)))
(goto-char (kotlin-mode--token-end parent-of-expression)))
(setq parent-of-expression (kotlin-mode--backward-token))
(if (<= (kotlin-mode--token-start parent-of-previous-line)
(kotlin-mode--token-start parent-of-expression))
;; let x =
;; 1 + // indenting here
;; 2 +
;; 3
;;
;; Aligns with the parent of the expression with offset.
(kotlin-mode--align-with-next-token parent-of-expression offset)
;; let x =
;; 1 +
;; 2 + // indenting here
;; 3 // or here
;;
;; Aligns with the previous line.
(kotlin-mode--align-with-next-token parent-of-previous-line
bol-offset)))))
(defun kotlin-mode--forward-annotations ()
"Skip forward comments, whitespaces, labels, and annotations."
(let (token)
(while (progn
(forward-comment (point-max))
(setq token (kotlin-mode--forward-token))
(memq (kotlin-mode--token-type token) '(annotation label))))
(goto-char (kotlin-mode--token-start token))))
(defun kotlin-mode--backward-annotations ()
"Skip backward comments, whitespaces, labels, and annotations."
(let (token)
(while (progn
(forward-comment (- (point)))
(setq token (kotlin-mode--backward-token))
(memq (kotlin-mode--token-type token) '(annotation label))))
(goto-char (kotlin-mode--token-end token))))
(defun kotlin-mode--calculate-indent-after-open-curly-brace (offset)
"Return indentation after open curly braces.
Assuming the point is on the open brace.
OFFSET is the offset of the contents.
This function is also used for close curly braces."
;; If the statement is multiline expression, aligns with the start of
;; the line on which the open brace is:
;;
;; foo()
;; .then { x ->
;; foo()
;; foo()
;; }
;; .then { x ->
;; foo()
;; foo()
;; }
;;
;; rather than
;;
;; foo()
;; .then { x ->
;; foo()
;; foo()
;; }
;; .then { x ->
;; foo()
;; foo()
;; }
;;
;; Otherwise, aligns with the start of the whole statement:
;;
;; class Foo:
;; Bar {
;; fun foo() {}
;; }
;;
;; rather than
;;
;; class Foo:
;; Bar {
;; fun foo() {}
;; }
;;
;; POSSIBLE IMPROVEMENT: those behavior should be configurable.
;;
;; FIXME: curly braces after binary operator is a part of a
;; multiline expression:
;;
;; class Foo:
;; Bar by xs someInfixOperator { x ->
;; // this is not the body of the class.
;; } {
;; // The body of the class.
;; }
(let* ((brace-type-and-keyword-token (kotlin-mode--curly-brace-type))
(brace-type (car brace-type-and-keyword-token))
(keyword-token (cdr brace-type-and-keyword-token)))
(cond
((memq brace-type
'(object-literal
lambda-literal
if-expression
try-expression
catch-block
finally-block
when-expression))
(goto-char (kotlin-mode--token-start keyword-token))
(kotlin-mode--backward-annotations)
(unless (eq brace-type 'lambda-literal)
(forward-comment (point-max)))
(kotlin-mode--calculate-indent-of-expression offset offset))
((eq brace-type 'else-block)
(goto-char (kotlin-mode--token-start keyword-token))
(kotlin-mode--calculate-indent-of-else offset))
((eq brace-type 'when-entry)
(goto-char (kotlin-mode--token-start keyword-token))
(if (equal (kotlin-mode--token-text
(save-excursion (kotlin-mode--backward-token)))
"else")
(kotlin-mode--align-with-token (kotlin-mode--backward-token) offset)
(kotlin-mode--find-parent-and-align-with-next
(remove 'when-expression-arrow kotlin-mode--statement-parent-tokens)
offset)))
((memq brace-type '(getter setter))
(goto-char (kotlin-mode--token-start keyword-token))
(kotlin-mode--try-backward-modifiers)
(kotlin-mode--align-with-next-token (kotlin-mode--backward-token) offset))
(t
(goto-char (kotlin-mode--token-start keyword-token))
(kotlin-mode--find-parent-and-align-with-next
kotlin-mode--statement-parent-tokens
offset)))))
(defun kotlin-mode--curly-brace-type ()
"Return information about curly brace.
Return a cons (TYPE . KEYWORD-TOKEN) where TYPE is a symbol, and KEYWORD-TOKEN
is the keyword token:
- TYPE: class-declaration, KEYWORD-TOKEN: \"class\"
- TYPE: interface-declaration, KEYWORD-TOKEN: \"interface\"
- TYPE: object-declaration, KEYWORD-TOKEN: \"object\"
- TYPE: enum-entry, KEYWORD-TOKEN: identifier
- TYPE: object-literal, KEYWORD-TOKEN: \"object\"
- TYPE: anonymous-initializer, KEYWORD-TOKEN: \"init\"
- TYPE: function-declaration, KEYWORD-TOKEN: \"fun\"
- TYPE: getter, KEYWORD-TOKEN: \"get\"
- TYPE: setter, KEYWORD-TOKEN: \"set\"
- TYPE: secondary-constructor, KEYWORD-TOKEN: \"constructor\"
- TYPE: for-statement, KEYWORD-TOKEN: \"for\"
- TYPE: while-statement, KEYWORD-TOKEN: \"while\"
- TYPE: do-while-statement, KEYWORD-TOKEN: \"do\"
- TYPE: if-expression, KEYWORD-TOKEN: \"if\"
- TYPE: else-block, KEYWORD-TOKEN: \"else\"
- TYPE: when-entry, KEYWORD-TOKEN: \"->\"
- TYPE: try-expression, KEYWORD-TOKEN: \"try\"
- TYPE: catch-block, KEYWORD-TOKEN: \"catch\"
- TYPE: finally-block, KEYWORD-TOKEN: \"finally\"
- TYPE: lambda-literal, KEYWORD-TOKEN: \"{\"
- TYPE: when-expression, KEYWORD-TOKEN: \"when\"
Assuming the point is just before the open curly brace."
;; Other braces may appear between the keyword and the open brace:
;;
;; class Foo: Bar by if (aaa) {bbb} else {ccc} {
;; }
;;
;; So we maintain stack of braces and match them with keywords.
(save-excursion
(let* ((pos (point))
(open-brace-points (list pos))
(containing-brackets (kotlin-mode--find-containing-brackets (point)))
previous-token
previous-type
previous-text
brace-type
result)
;; Special handling for enumEntry.
(goto-char (cdr containing-brackets))
(when (eq (car containing-brackets) '{)
(let ((token (kotlin-mode--backward-sexps-until
(append kotlin-mode--statement-parent-tokens
'("enum")))))
(when (equal (kotlin-mode--token-text token) "enum")
(goto-char pos)
(when (eq (kotlin-mode--token-type
(kotlin-mode--backward-sexps-until
kotlin-mode--statement-parent-tokens))
'{)
;; This must be a enumEntry.
(goto-char pos)
(setq previous-token (kotlin-mode--backward-token-or-list))
(when (eq (kotlin-mode--token-type previous-token) '\(\))
(setq previous-token (kotlin-mode--backward-token-or-list)))
(setq result (cons 'enum-entry previous-token))))))
(unless result
;; Other cases
(goto-char pos)
(while open-brace-points
(setq previous-token (kotlin-mode--backward-token-or-list))
(setq previous-type (kotlin-mode--token-type previous-token))
(setq previous-text (kotlin-mode--token-text previous-token))
(cond
;; whenEntry.
;; Stop immediately.
((eq previous-type 'when-expression-arrow)
(setq result (cons 'when-entry previous-token))
(setq open-brace-points nil))
;; Hit beginning of the statement without seeing keyword.
;; Assume it is a lambda expression and stop the loop.
((or (eq previous-type 'outside-of-buffer)
(memq previous-type kotlin-mode--statement-parent-tokens)
(member previous-text kotlin-mode--statement-parent-tokens))
(setq result (cons 'lambda-literal
(progn
(goto-char pos)
(kotlin-mode--forward-token))))
(setq open-brace-points nil))
;; Found another braces.
;; Push it to the stack and continue the loop.
((eq previous-type '{})
(push (point) open-brace-points))
;; Simple cases.
;; Those cannot be a part of an expression.
;; Stop immediately.
((member previous-text
'("class" "interface" "init" "fun" "get" "set"
"for" "while" "do"))
(setq brace-type (assoc-default
previous-text
'(("class" . class-declaration)
("interface" . interface-declaration)
("init" . anonymous-initializer)
;; FIXME handle anonymous function
("fun" . function-declaration)
("get" . getter)
("set" . setter)
("for" . for-statement)
("while" . while-statement)
("do" . do-while-statement))))
(setq result (cons brace-type previous-token))
(setq open-brace-points nil))
;; Semi-simple cases.
;; Those can be a part of an expression and the braces are mandatory.
((member previous-text '("try" "catch" "finally" "when"))
(pop open-brace-points)
(when (null open-brace-points)
(setq brace-type (assoc-default
previous-text
'(("try" . try-expression)
("catch" . catch-block)
("finally" . finally-block)
("when" . when-expression))))
(setq result (cons brace-type previous-token))))
;; If and else.
;; Those can be a part of an expression and the braces are optional.
((member previous-text '("if" "else"))
(save-excursion
(goto-char (kotlin-mode--token-end previous-token))
(when (equal previous-text "if")
(kotlin-mode--forward-token-or-list))
(forward-comment (point-max))
(when (= (point) (car open-brace-points))
(pop open-brace-points)
(when (null open-brace-points)
(setq brace-type
(if (equal previous-text "if")
'if-expression
'else-block))
(setq result (cons brace-type previous-token))))))
;; Object declaration or object literal.
((equal previous-text "object")
(setq brace-type
(cond
((save-excursion
(equal (kotlin-mode--token-text
(kotlin-mode--backward-token))
"companion"))
'object-declaration)
((save-excursion
(goto-char (kotlin-mode--token-end previous-token))
(eq (kotlin-mode--token-type (kotlin-mode--forward-token))
'atom))
'object-declaration)
(t 'object-literal)))
(pop open-brace-points)
(when (or (null open-brace-points)
(eq brace-type 'object-declaration))
(setq result (cons brace-type previous-token))
(setq open-brace-points nil)))
;; Primary constructor or secondary constructor.
((equal previous-text "constructor")
(let ((parent-token
(kotlin-mode--backward-sexps-until
(append kotlin-mode--statement-parent-tokens '("class")))))
(if (equal (kotlin-mode--token-text parent-token) "class")
;; primary constructor
(setq result (cons 'class-declaration parent-token))
;; secondary constructor
(setq result (cons 'secondary-constructor previous-token)))
(setq open-brace-points nil))))))
result)))
(defun kotlin-mode--calculate-indent-of-else (&optional offset)
"Return indentation for \"else\" token with OFFSET."
;; Align it with "if" token.
;; Since if-else can be nested, we keep nesting level to find matching "if".
(let ((nesting-level 1)
previous-token)
(while (< 0 nesting-level)
(setq previous-token (kotlin-mode--backward-token-or-list))
(cond
((equal (kotlin-mode--token-text previous-token) "else")
(setq nesting-level (1+ nesting-level)))
((equal (kotlin-mode--token-text previous-token) "if")
(if (save-excursion
;; "else if" on the same line
(and
(equal (kotlin-mode--token-text (kotlin-mode--backward-token))
"else")
(= (progn (kotlin-mode--goto-non-comment-bol)
(point))
(progn (goto-char (kotlin-mode--token-start previous-token))
(kotlin-mode--goto-non-comment-bol)
(point)))))
;; Skip "else-if" without changing nesting-level.
(kotlin-mode--backward-token)
(setq nesting-level (1- nesting-level))))
((memq (kotlin-mode--token-type previous-token)
'({ \( outside-of-buffer))
;; Unmatched if-else
(setq nesting-level 0)))))
(kotlin-mode--forward-token)
(kotlin-mode--calculate-indent-of-expression (or offset 0) (or offset 0)))
(defun kotlin-mode--calculate-indent-of-prefix-comma ()
"Return indentation for prefix comma.
Example:
foo( 1
, 2
, 3
)
class Foo: A
, B
, C
class D<A, B, C>
where A: AAA
, B: BBB
, C: CCC
This is also known as Utrecht-style in the Haskell community."
(let ((parent (kotlin-mode--find-parent-of-list-element t)))
(if (eq (kotlin-mode--token-type parent) '\,)
;; The comma was the 2nd or the following commas.
;; Align with the previous comma.
(kotlin-mode--align-with-current-line)
;; The comma was the 1st comma.
;; Align with the end of the parent.
(goto-char (kotlin-mode--token-end parent))
(backward-char)
(make-instance 'kotlin-mode--indentation :position (point) :offset 0))))
(defun kotlin-mode--calculate-indent-after-comma ()
"Return indentation after comma.
Assuming the point is on the comma."
(kotlin-mode--align-with-next-token
(kotlin-mode--find-parent-of-list-element nil)))
(defun kotlin-mode--find-parent-of-list-element (&optional utrecht-style)
"Move point backward to the parent token of the comma under the point.
If UTRECHT-STYLE is non-nil, stop at a comma at bol. Otherwise, stop at a
comma at eol."
;; Various examples:
;;
;; val x = foo( // simple case
;; 1,
;; 2,
;; 3
;; )
;;
;; val x = xs[
;; 1,
;; 2,
;; 3
;; ]
;;
;; class Foo<A>: A1(),
;; B1 by object: C1,
;; C2 {
;; },
;; @AAA D1
;; where A: B,
;; A: C,
;; A: D {
;; }
;;
;; when (foo) {
;; object: B1,
;; B2 {
;; },
;; object: B3 by object: B4 {
;; },
;; B5 {
;; } ->
;; 1
;; }
;;
;; fun <T>foo(x: T): Int
;; where
;; T: A,
;; T: B,
;; T: C {
;; }
;;
;; val <T> A<T>.foo: Int
;; where
;; T: A,
;; T: B,
;; T: C
;; get() = aaa
;;
;; enum class Foo(x: Int) {
;; A(1), B(2) {},
;; C(3)
;; }
(let ((parent (kotlin-mode--backward-sexps-until
(append
(remove '\, kotlin-mode--expression-parent-tokens)
'("<" : {}))
(if utrecht-style nil '(\,))
(if utrecht-style '(\,) nil))))
(cond
((equal (kotlin-mode--token-text parent) "<")
(if (save-excursion
(eq (kotlin-mode--token-type (kotlin-mode--forward-token-or-list))
'<>))
parent
(kotlin-mode--find-parent-of-list-element utrecht-style)))
((eq (kotlin-mode--token-type parent) '{})
(let* ((brace-type-and-keyword-token (kotlin-mode--curly-brace-type))
(keyword-token (cdr brace-type-and-keyword-token)))
(goto-char (kotlin-mode--token-start keyword-token)))
(kotlin-mode--find-parent-of-list-element utrecht-style))
((eq (kotlin-mode--token-type parent) ':)
(if (kotlin-mode--colon-before-delegation-specifiers-p parent)
parent
(kotlin-mode--find-parent-of-list-element utrecht-style)))
(t
parent))))
(defun kotlin-mode--find-parent-of-expression ()
"Move point backward to the parent token of the expression under the point."
;; TODO Unify with kotlin-mode--find-parent-of-list-element
(let ((parent (kotlin-mode--backward-sexps-until
(append kotlin-mode--expression-parent-tokens '("<" : {})))))
(cond
((equal (kotlin-mode--token-text parent) "<")
(if (save-excursion
(eq (kotlin-mode--token-type (kotlin-mode--forward-token-or-list))
'<>))
parent
(kotlin-mode--find-parent-of-expression)))
((eq (kotlin-mode--token-type parent) '{})
(let* ((brace-type-and-keyword-token (kotlin-mode--curly-brace-type))
(keyword-token (cdr brace-type-and-keyword-token)))
(goto-char (kotlin-mode--token-start keyword-token)))
(kotlin-mode--find-parent-of-expression))
((eq (kotlin-mode--token-type parent) ':)
(if (kotlin-mode--colon-before-delegation-specifiers-p parent)
parent
(kotlin-mode--find-parent-of-expression)))
(t
parent))))
(defun kotlin-mode--colon-before-delegation-specifiers-p (token)
"Return non-nil if TOKEN is a colon before delegationSpecifiers."
(and
(eq (kotlin-mode--token-type token) ':)
(save-excursion
(goto-char (kotlin-mode--token-start token))
(let (previous-token)
;; Try backward primaryConstructor.
(forward-comment (- (point)))
(when (eq (char-before) ?\))
(kotlin-mode--backward-token-or-list)
(setq previous-token (kotlin-mode--backward-token))
(if (equal (kotlin-mode--token-text previous-token) "constructor")
(kotlin-mode--try-backward-modifiers)
(goto-char (kotlin-mode--token-end previous-token))))
(kotlin-mode--try-backward-type-parameters)
(setq previous-token (kotlin-mode--backward-token-or-list))
(or
(equal (kotlin-mode--token-text previous-token) "object")
(and
(eq (kotlin-mode--token-type previous-token) 'atom)
(member (kotlin-mode--token-text (kotlin-mode--backward-token-or-list))
'("class" "interface" "object"))))))))
(defun kotlin-mode--calculate-indent-after-beginning-of-template-expression
(offset)
"Return indentation after the beginning of a template expression.
It has offset specified with OFFSET.
Assuming the point is before the string chunk."
(let ((pos (point)))
(kotlin-mode--forward-string-chunk)
(if (< pos (line-beginning-position))
;; The chunk has multiple lines. Align with this line with offset.
(progn
(back-to-indentation)
(make-instance 'kotlin-mode--indentation
:position (point)
:offset offset))
;; The chunk is single line. Indent like a expression.
(goto-char pos)
(kotlin-mode--calculate-indent-of-expression offset offset))))
(defun kotlin-mode--calculate-indent-after-semicolon ()
"Return indentation after semicolon.
Assuming the point is after the semicolon."
(while (save-excursion
(memq (kotlin-mode--token-type (kotlin-mode--backward-token))
'(\; implicit-\;)))
(kotlin-mode--backward-token))
(kotlin-mode--find-parent-and-align-with-next
(cl-remove-if
(lambda (e)
(member e '(\; implicit-\; bare-else
\(\)-before-control-structure-body when-expression-arrow)))
kotlin-mode--statement-parent-tokens)
0
'(\; implicit-\;)))
(defun kotlin-mode--calculate-indent-before-in ()
"Return indentation after \"in\" token.
Assuming the point is before the token.
It is also assumed that the point is not just after \"{\" or \"<\"."
(let* ((type-and-parent (kotlin-mode--find-parent-of-in))
(type (car type-and-parent))
(parent (cdr type-and-parent)))
(if (memq type '(for when <>))
(kotlin-mode--align-with-next-token parent)
(kotlin-mode--calculate-indent-after-semicolon))))
(defun kotlin-mode--find-parent-of-in ()
"Return parent token of \"in\" token.
Return a cons (TYPE . PARENT) where TYPE is one of symbol `for',
`when', `<>', or `other' and PARENT is the parent token, one of
`;', `implicit-;', `(', `{', or \"<\".
Assuming the point is before the token."
;; Examples:
;; for (
;; x
;; in // Align with token after "("
;; xs
;; ) {}
;;
;; for (x
;; in
;; xs) {[]}
;;
;; when (x) {
;; in xs -> 1
;; in ys -> 1
;; }
;;
;; Foo<
;; in X,
;; @AAA
;; in X
;; >
;;
;; val x = 1 // Line breaks are prohibited before infix "in" operator.
;; in array
(let ((parent (save-excursion (kotlin-mode--backward-sexps-until
'(\; implicit-\; \( { "<")))))
(cond
;; Inside "for ()"
((and
(eq (kotlin-mode--token-type parent) '\()
(save-excursion
(goto-char (kotlin-mode--token-start parent))
(equal (kotlin-mode--token-text (kotlin-mode--backward-token)) "for")))
(cons 'for parent))
;; Inside "when () {}"
((and
(eq (kotlin-mode--token-type parent) '{)
(save-excursion
(goto-char (kotlin-mode--token-start parent))
(eq (kotlin-mode--token-type (kotlin-mode--backward-token-or-list))
'\(\))
(equal (kotlin-mode--token-text (kotlin-mode--backward-token))
"when")))
(cons 'when parent))
;; Inside type parameters
((equal (kotlin-mode--token-text parent) "<")
(if (save-excursion
(goto-char (kotlin-mode--token-start parent))
(eq (kotlin-mode--token-type
(kotlin-mode--forward-token-or-list))
'<>))
(cons '<> parent)
(save-excursion
(goto-char (kotlin-mode--token-start parent))
(kotlin-mode--find-parent-of-in))))
(t
(cons 'other parent)))))
(defun kotlin-mode--backward-sexps-until (token-types
&optional
stop-at-eol-token-types
stop-at-bol-token-types)
"Backward sexps until a token with one of given token types appears.
Return the token.
When this function returns, the point is at the start of the token.
TOKEN-TYPES is a list of guard token types. This function backs to a
token with one of those token types.
STOP-AT-EOL-TOKEN-TYPES is a list of token types that if we skipped
the end of a line just after a token with one of given token type, the
function returns. Typically, this is a list of token types that
separates list elements (e.g. ',', ';'). If STOP-AT-EOL-TOKEN-TYPES
is the symbol `any', it matches all tokens.
STOP-AT-BOL-TOKEN-TYPES is a list of token types that if we hit the
beginning of a line just before a token with one of given token types,
the function returns. Typically, this is a list of token types that
starts list element . If STOP-AT-BOL-TOKEN-TYPES is the symbol `any',
it matches all tokens."
(let*
((parent (kotlin-mode--backward-token-or-list))
(type (kotlin-mode--token-type parent))
(text (kotlin-mode--token-text parent)))
(while (not
;; Stop loop when...
(or
;; Hit a guard token.
(memq type token-types)
(member text token-types)
;; Beginning of the buffer.
(eq type 'outside-of-buffer)
;; When this function is called on "," token before position (1),
;; this function stops just before the "," token after "Foo".
;;
;; Foo,
;; Bar, Baz, // (1)
;; AAA
(and stop-at-eol-token-types
(or (eq stop-at-eol-token-types 'any)
(member type stop-at-eol-token-types)
(member text stop-at-eol-token-types))
(save-excursion
(kotlin-mode--forward-token-or-list)
(forward-comment (- (point)))
(kotlin-mode--eol-other-than-comments-p)))
;; When this function is called on "," token before position
;; (1), this function stops just before ", Bar".
;;
;; , Foo
;; , Bar, Baz:
;; , AAA // (1)
(and stop-at-bol-token-types
(and
(or
(eq stop-at-bol-token-types 'any)
(member type stop-at-bol-token-types)
(member text stop-at-bol-token-types))
(kotlin-mode--bol-other-than-comments-p)))))
(setq parent (kotlin-mode--backward-token-or-list))
(setq type (kotlin-mode--token-type parent))
(setq text (kotlin-mode--token-text parent)))
parent))
(defun kotlin-mode--align-with-next-token (parent &optional offset)
"Return indentation with the next token of PARENT with OFFSET.
Example:
Suppose indenting \"B\":
foo {
/* */ A()
B()
}
The parent is \"{\". We align with the comment before \"A\"."
(let ((parent-end (kotlin-mode--token-end parent)))
(goto-char parent-end)
(forward-comment (point-max))
(kotlin-mode--goto-non-comment-bol)
(when (< (point) parent-end)
(goto-char parent-end))
(kotlin-mode--skip-whitespaces)
(make-instance 'kotlin-mode--indentation
:position (point)
:offset (or offset 0))))
(defun kotlin-mode--align-with-token (token &optional offset)
"Return indentation with the TOKEN with OFFSET.
If the token is preceded by comments on the same line, align with that
comments instead.
Example:
Suppose indenting \"B\":
foo {
/* */ A()
B()
}
We align with the comment before \"A\"."
(goto-char (kotlin-mode--token-start token))
(forward-comment (- (point)))
(kotlin-mode--align-with-next-token (kotlin-mode--backward-token) offset))
(defun kotlin-mode--align-with-current-line (&optional offset)
"Return indentation of the current line with OFFSET."
(kotlin-mode--goto-non-comment-bol)
(kotlin-mode--skip-whitespaces)
(make-instance 'kotlin-mode--indentation
:position (point)
:offset (or offset 0)))
(defun kotlin-mode--goto-non-comment-bol-with-same-nesting-level
(&optional use-backward-token-simple)
"Back to the beginning of line that is not inside a comment.
See `kotlin-mode--backward-token-or-list' for USE-BACKWARD-TOKEN-SIMPLE."
(while (not (kotlin-mode--bol-other-than-comments-p))
(kotlin-mode--backward-token-or-list use-backward-token-simple)))
;;; indent-new-comment-line
(defun kotlin-mode--indent-new-comment-line (&optional soft)
"Break the line at the point and indent the new line.
If the point is inside a comment, continue the comment. If the comment is a
multiline comment, close the previous comment and start new one if
`comment-multi-line' is nil.
See `indent-new-comment-line' for SOFT."
(interactive)
(let* ((chunk (kotlin-mode--chunk-after))
(comment-beginning-position (kotlin-mode--chunk-start chunk)))
(if soft (insert-and-inherit ?\n) (newline 1))
(delete-horizontal-space)
;; Insert a prefix and indent the line.
(cond
((not (kotlin-mode--chunk-comment-p chunk))
(indent-according-to-mode))
((kotlin-mode--chunk-single-line-comment-p chunk)
(insert-and-inherit
(save-excursion
(goto-char comment-beginning-position)
(looking-at "/+\\s *")
(match-string-no-properties 0)))
(indent-according-to-mode))
((not comment-multi-line)
(insert-and-inherit
(save-excursion
(goto-char comment-beginning-position)
(looking-at "/\\*+\\s *")
(match-string-no-properties 0)))
;; Clean up and close the previous line.
(save-excursion
(beginning-of-line)
(backward-char)
(delete-horizontal-space)
(insert-and-inherit " */"))
(indent-according-to-mode))
(t
(kotlin-mode--format-multiline-comment-line-after-newline chunk soft)))
;; Clean up the previous line.
(save-excursion
(beginning-of-line)
(backward-char)
(delete-horizontal-space))))
(defun kotlin-mode--format-multiline-comment-line-after-newline (chunk soft)
"Insert prefix and indent current line in multiline comment.
The point is assumed inside multiline comment and just after newline.
The closing delimiter is also inserted and/or formatted depending on custom
variables `kotlin-mode-auto-close-multiline-comment' and
`kotlin-mode-break-line-before-comment-close'.
CHUNK is the comment chunk.
See `indent-new-comment-line' for SOFT."
(let ((comment-beginning-position (kotlin-mode--chunk-start chunk)))
(cond
((save-excursion
(forward-line -1)
(<= (point) comment-beginning-position))
;; The point was on the 2nd line of the comment.
;; If the comment have only one line, delete a space after asterisk.
;;
;; Example:
;; /** aaa */
;; ↓
;; /**
;; * aaa
;; */
;;
;; /** aaa */
;; ↓
;; /**
;; * aaa
;; */
;;
;;
;; If the comment spans several lines, keep spaces.
;;
;; /** aaa
;; */
;; ↓
;; /**
;; * aaa
;; */
(when (= (line-beginning-position)
(save-excursion
(goto-char comment-beginning-position)
(forward-comment 1)
(line-beginning-position)))
(save-excursion
(goto-char comment-beginning-position)
(forward-char)
(skip-chars-forward "*")
(when (looking-at " [ \t]*$")
(delete-char 1))))
;; If the point is just before the closing delimiter, break the line.
(when (and kotlin-mode-break-line-before-comment-close
(= (point)
(save-excursion
(goto-char comment-beginning-position)
(if (forward-comment 1)
(progn
(backward-char)
(skip-chars-backward "*")
(point))
-1))))
(save-excursion
(if soft (insert-and-inherit ?\n) (newline 1))
(indent-according-to-mode)))
;; Invoke `kotlin-mode--indent-line`.
(indent-according-to-mode)
;; Insert or replace a space to an asterisk.
(when kotlin-mode-prepend-asterisk-to-comment-line
(let ((columns-from-end (- (line-end-position) (point))))
(move-to-column
(save-excursion
(goto-char comment-beginning-position)
(forward-char)
(current-column)))
(insert-and-inherit "*")
(when (eq (char-after) ?\s)
(delete-char 1))
(when (and
kotlin-mode-insert-space-after-asterisk-in-comment
(not (eq (char-after) ?\s)))
(insert-and-inherit " "))
(goto-char (- (line-end-position) columns-from-end)))))
;; The point was on the 3nd or following lines of
;; the comment.
;; Use the prefix of the previous line.
((and
kotlin-mode-prepend-asterisk-to-comment-line
(save-excursion
(forward-line -1)
(looking-at "\\s *\\(\\*+\\s *\\)")))
;; The previous line has a prefix. Use it.
(insert-and-inherit (match-string-no-properties 1))
(indent-according-to-mode))
(t
;; Use the default indentation.
(indent-according-to-mode)))
;; Close incomplete multiline comment.
(when (and kotlin-mode-auto-close-multiline-comment
(kotlin-mode--incomplete-comment-p chunk))
(save-excursion
(end-of-line)
(when comment-multi-line
(if soft (insert-and-inherit ?\n) (newline 1)))
(insert-and-inherit "*/")
(indent-according-to-mode)))
;; Make sure the closing delimiter is on its own line.
(when kotlin-mode-break-line-before-comment-close
(save-excursion
(goto-char comment-beginning-position)
(when (forward-comment 1)
(backward-char)
(skip-chars-backward "*")
(skip-syntax-backward " ")
(when (not (bolp))
(if soft (insert-and-inherit ?\n) (newline 1))
(indent-according-to-mode)))))))
(defun kotlin-mode--post-self-insert ()
"Miscellaneous logic for electric indentation."
(cond
;; Indent electrically and insert a space when "*" is inserted at the
;; beginning of a line inside a multiline comment.
((and
kotlin-mode-prepend-asterisk-to-comment-line
(eq last-command-event ?*)
(kotlin-mode--chunk-comment-p (kotlin-mode--chunk-after))
(save-excursion (backward-char) (skip-syntax-backward " ") (bolp)))
(when kotlin-mode-insert-space-after-asterisk-in-comment
(insert-and-inherit " "))
(when electric-indent-mode
(indent-according-to-mode)))
;; Fixe "* /" at the end of a multiline comment to "*/".
((and
kotlin-mode-fix-comment-close
(eq last-command-event ?/)
(let ((chunk (kotlin-mode--chunk-after))
(pos (point)))
(and
(kotlin-mode--chunk-comment-p chunk)
(save-excursion
(beginning-of-line)
(and
(looking-at "^\\s *\\*\\s +/")
(eq (match-end 0) pos)
(kotlin-mode--incomplete-comment-p chunk))))))
(backward-char)
(delete-horizontal-space)
(forward-char))
;; Indent electrically when "}" is inserted at bol as the end of a string
;; interpolation.
((and
electric-indent-mode
(eq last-command-event ?\})
(save-excursion (backward-char) (skip-syntax-backward " ") (bolp))
(eq (kotlin-mode--chunk-start (kotlin-mode--chunk-after)) (1- (point))))
(indent-according-to-mode))
;; Indent electrically after newline inside strings and comments.
;; Unlike `electric-indent-mode', the previous line is not indented.
((and
electric-indent-mode
(eq last-command-event ?\n))
(let ((chunk (kotlin-mode--chunk-after)))
(if (kotlin-mode--chunk-multiline-comment-p chunk)
(progn
(delete-horizontal-space)
(kotlin-mode--format-multiline-comment-line-after-newline
chunk
(not use-hard-newlines)))
(indent-according-to-mode)))
(save-excursion
(beginning-of-line)
(backward-char)
(delete-horizontal-space)))))
(defun kotlin-mode-highlight-anchor (indentation)
"Highlight the anchor point of the INDENTATION."
(move-overlay
kotlin-mode--anchor-overlay
(kotlin-mode--indentation-position indentation)
(1+ (kotlin-mode--indentation-position indentation)))
(overlay-put kotlin-mode--anchor-overlay 'face 'highlight)
(when kotlin-mode--anchor-overlay-timer
(cancel-timer kotlin-mode--anchor-overlay-timer))
(let ((buffer (current-buffer)))
(setq kotlin-mode--anchor-overlay-timer
(run-at-time
"1 sec"
nil
(lambda ()
(when (buffer-live-p buffer)
(with-current-buffer buffer
(delete-overlay kotlin-mode--anchor-overlay)
(setq kotlin-mode--anchor-overlay-timer nil))))))))
(provide 'kotlin-mode-indent)
;;; kotlin-mode-indent.el ends here
|