main.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. exports.activate = function () {
  2. if (!atom.grammars.addInjectionPoint) return;
  3. atom.grammars.addInjectionPoint('source.js', {
  4. type: 'call_expression',
  5. language(callExpression) {
  6. const { firstChild } = callExpression;
  7. switch (firstChild.type) {
  8. case 'identifier':
  9. return languageStringForTemplateTag(firstChild.text);
  10. case 'call_expression':
  11. return languageStringForTemplateTag(firstChild.children[0].text);
  12. case 'member_expression':
  13. if (firstChild.startPosition.row === firstChild.endPosition.row) {
  14. return languageStringForTemplateTag(firstChild.text);
  15. }
  16. }
  17. },
  18. content(callExpression) {
  19. const { lastChild } = callExpression;
  20. if (lastChild.type === 'template_string') {
  21. return stringFragmentsOfTemplateString(lastChild);
  22. }
  23. }
  24. });
  25. atom.grammars.addInjectionPoint('source.js', {
  26. type: 'assignment_expression',
  27. language(expression) {
  28. const { firstChild } = expression;
  29. if (firstChild.type === 'member_expression') {
  30. if (firstChild.lastChild.text === 'innerHTML') {
  31. return 'html';
  32. }
  33. }
  34. },
  35. content(expression) {
  36. const { lastChild } = expression;
  37. if (lastChild.type === 'template_string') {
  38. return stringFragmentsOfTemplateString(lastChild);
  39. }
  40. }
  41. });
  42. atom.grammars.addInjectionPoint('source.js', {
  43. type: 'regex_pattern',
  44. language() {
  45. return 'js-regex';
  46. },
  47. content(regex) {
  48. return regex;
  49. },
  50. languageScope: null
  51. });
  52. atom.grammars.addInjectionPoint('source.js', {
  53. type: 'comment',
  54. language(comment) {
  55. if (comment.text.startsWith('/**')) return 'jsdoc';
  56. },
  57. content(comment) {
  58. return comment;
  59. },
  60. languageScope: null,
  61. coverShallowerScopes: true
  62. });
  63. };
  64. exports.consumeHyperlinkInjection = (hyperlink) => {
  65. hyperlink.addInjectionPoint('source.js', {
  66. types: ['comment', 'template_string', 'string_fragment']
  67. });
  68. };
  69. exports.consumeTodoInjection = (todo) => {
  70. todo.addInjectionPoint('source.js', { types: ['comment'] });
  71. };
  72. const CSS_REGEX = /\bstyled\b|\bcss\b/i;
  73. const GQL_REGEX = /\bgraphql\b|\bgql\b/i;
  74. const SQL_REGEX = /\bsql\b/i;
  75. function languageStringForTemplateTag(tag) {
  76. if (CSS_REGEX.test(tag)) {
  77. return 'CSS';
  78. } else if (GQL_REGEX.test(tag)) {
  79. return 'GraphQL';
  80. } else if (SQL_REGEX.test(tag)) {
  81. return 'SQL';
  82. } else {
  83. return tag;
  84. }
  85. }
  86. function stringFragmentsOfTemplateString(templateStringNode) {
  87. return templateStringNode.children.filter(
  88. c => c.type === 'string_fragment'
  89. );
  90. }