comment.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. (function(mod) {
  2. if (typeof exports == "object" && typeof module == "object") // CommonJS
  3. return mod(exports);
  4. if (typeof define == "function" && define.amd) // AMD
  5. return define(["exports"], mod);
  6. mod(tern.comment || (tern.comment = {}));
  7. })(function(exports) {
  8. function isSpace(ch) {
  9. return (ch < 14 && ch > 8) || ch === 32 || ch === 160;
  10. }
  11. function onOwnLine(text, pos) {
  12. for (; pos > 0; --pos) {
  13. var ch = text.charCodeAt(pos - 1);
  14. if (ch == 10) break;
  15. if (!isSpace(ch)) return false;
  16. }
  17. return true;
  18. }
  19. // Gather comments directly before a function
  20. exports.commentsBefore = function(text, pos) {
  21. var found = null, emptyLines = 0, topIsLineComment;
  22. out: while (pos > 0) {
  23. var prev = text.charCodeAt(pos - 1);
  24. if (prev == 10) {
  25. for (var scan = --pos, sawNonWS = false; scan > 0; --scan) {
  26. prev = text.charCodeAt(scan - 1);
  27. if (prev == 47 && text.charCodeAt(scan - 2) == 47) {
  28. if (!onOwnLine(text, scan - 2)) break out;
  29. var content = text.slice(scan, pos);
  30. if (!emptyLines && topIsLineComment) found[0] = content + "\n" + found[0];
  31. else (found || (found = [])).unshift(content);
  32. topIsLineComment = true;
  33. emptyLines = 0;
  34. pos = scan - 2;
  35. break;
  36. } else if (prev == 10) {
  37. if (!sawNonWS && ++emptyLines > 1) break out;
  38. break;
  39. } else if (!sawNonWS && !isSpace(prev)) {
  40. sawNonWS = true;
  41. }
  42. }
  43. } else if (prev == 47 && text.charCodeAt(pos - 2) == 42) {
  44. for (var scan = pos - 2; scan > 1; --scan) {
  45. if (text.charCodeAt(scan - 1) == 42 && text.charCodeAt(scan - 2) == 47) {
  46. if (!onOwnLine(text, scan - 2)) break out;
  47. (found || (found = [])).unshift(text.slice(scan, pos - 2));
  48. topIsLineComment = false;
  49. emptyLines = 0;
  50. break;
  51. }
  52. }
  53. pos = scan - 2;
  54. } else if (isSpace(prev)) {
  55. --pos;
  56. } else {
  57. break;
  58. }
  59. }
  60. return found;
  61. };
  62. exports.commentAfter = function(text, pos) {
  63. while (pos < text.length) {
  64. var next = text.charCodeAt(pos);
  65. if (next == 47) {
  66. var after = text.charCodeAt(pos + 1), end;
  67. if (after == 47) // line comment
  68. end = text.indexOf("\n", pos + 2);
  69. else if (after == 42) // block comment
  70. end = text.indexOf("*/", pos + 2);
  71. else
  72. return;
  73. return text.slice(pos + 2, end < 0 ? text.length : end);
  74. } else if (isSpace(next)) {
  75. ++pos;
  76. }
  77. }
  78. };
  79. exports.ensureCommentsBefore = function(text, node) {
  80. if (node.hasOwnProperty("commentsBefore")) return node.commentsBefore;
  81. return node.commentsBefore = exports.commentsBefore(text, node.start);
  82. };
  83. });