代码之家  ›  专栏  ›  技术社区  ›  James Almond

Atom-Emmet包:结束评论

  •  2
  • James Almond  · 技术社区  · 9 年前

    Emmet for Atom:在html结束标记后自动注释。

    我似乎在任何地方都找不到解决这个问题的办法,所以我只好在这里询问。

    http://iaintnoextra.tumblr.com/post/68089741466/automatically-add-closing-comments-to-html-using

    在Sublime Text 3中,使用上面链接中的emmet用户首选项文件,emmet会在关闭html标记后自动添加注释;例如:

    div.container
    

    将产生:

    <div class="container"></div><!-- /.container -->
    

    我似乎在Emmet的软件包设置中找不到任何地方可以在Atom V1上实现这一点。有人知道我可以在哪里更改它,使其模仿相同的功能吗?

    3 回复  |  直到 9 年前
        1
  •  8
  •   James Almond    9 年前

    http://www.devstavern.com/emmet/custom-comments-in-emmet-sublime-atom-and-others/

    在上面的链接的帮助下,我自己设法解决了这个问题,下面是答案:

    1. 在Atom上编辑emmet设置。
    2. 将“设置”标题下的扩展路径更新到您希望保存首选项的位置。json文件,我在我的dropbox文件夹中创建了一个文件夹,所以我可以从任何位置访问该文件。
    3. 创建首选项。json文件,添加以下代码

    同一行上的注释:

    {
      "filter.commentAfter": "<!-- /<%= attr('id', '#') %><%= attr('class', '.') %> -->"
    }
    

    新行注释:

    {
      "filter.commentAfter": "\n<!-- /<%= attr('id', '#') %><%= attr('class', '.') %> -->"
    }
    
    1. 转到您的 .atom\packages\emmet\node_modules\emmet\lib 文件夹并编辑snippets.json。
    2. 找到html片段并将过滤器设置更改为“html,c”,这将自动在上面显示的任何自动完成的代码末尾添加注释。

    塔达,它起作用了!

    更新(05/11/15):

    确保在保存更改后重新启动Atom。

        2
  •  0
  •   Mahmut Gundogdu    8 年前

    我改变了html。js文件(标记生成器),位于C:\Users\\AppData\Roaming\Brackets\extensions\user\Brackets emmet\node_modules\emmet\lib\filter

    https://gist.github.com/mgundogdu38/a53af0bccd61bba4cefac56ab705d2b1

    现在: enter image description here

    1-我找到html标记生成器库。html.js。 2-我找到html标记生成器函数。它被称为processTag。 我需要属性生成器功能。它被称为makeAttributesString。在我克隆之后。我调用了“makeAttributesString2”:)

    function makeAttributesString2(node, profile) {
        var attrQuote = profile.attributeQuote();
        var cursor = profile.cursor();
    
        return node.attributeList().map(function(a) {
            var isBoolean = profile.isBoolean(a.name, a.value);
            var attrName = profile.attributeName(a.name);
            var attrValue = isBoolean ? attrName : a.value;
            //i added there. if attribute is id. i added "." on head
            if(attrName == "id")
            {
                return "#"+(attrValue || cursor);
            }
      //if attribute is class i added "." on head
            if(attrName == "class")
            {
                return "."+(attrValue || cursor);
            }
            //else only tagname
            if (isBoolean && profile.allowCompactBoolean()) {
                return ' ' + attrName;
            }
      //end of my code
        }).join('');
    }
    
    1. 然后我使用makeAttributesString2在proccessTag上生成注释标题。

      function processTag(item, profile) {
      if (!item.parent) { // looks like it's root element
          return item;
      }
      
      var attrs = makeAttributesString(item, profile); 
      
      var cursor = profile.cursor();
      var isUnary = abbrUtils.isUnary(item);
      var start = '';
      var end = '';
      
      // define opening and closing tags
      if (!item.isTextNode()) {
           //this is pieace of comment title.
          var attrsComment = makeAttributesString2(item,profile);
          var tagName = profile.tagName(item.name());
          if (isUnary) {
              start = '<' + tagName + attrs + profile.selfClosing() + '>';
              item.end = '';
          } else {
          //there is comment tag. i just add "<!-- "+tagName+attrsComment+" -->\n" and "\n <!-- /"+tagName+attrsComment+" -->"
          start = "<!-- "+tagName+attrsComment+" -->\n"+ '<' + tagName + attrs + '>';
              end = '</' + tagName + '>'+"\n <!-- /"+tagName+attrsComment+" -->";
          }
      }
      
      var placeholder = '%s';
      // We can't just replace placeholder with new value because
      // JavaScript will treat double $ character as a single one, assuming
      // we're using RegExp literal.
      item.start = utils.replaceSubstring(item.start, start,     item.start.indexOf(placeholder), placeholder);
      item.end = utils.replaceSubstring(item.end, end,    item.end.indexOf(placeholder), placeholder);
      
      // should we put caret placeholder after opening tag?
      if (
              !item.children.length 
              && !isUnary 
              && !~item.content.indexOf(cursor)
              && !tabStops.extract(item.content).tabstops.length
          ) {
          item.start += cursor;
      }
      
      return item;
      }
      
        3
  •  0
  •   Dan    7 年前

    如果有人想在2018年用VS Code做这件事,这就是我发现的有效方法。

      "emmet.preferences": {
        "filter.commentAfter": "<!-- /[#ID][.CLASS] -->"
      },
      "emmet.syntaxProfiles": {
        "html": {
          "filters": "html, c"
        }
      }

    将此添加到现有的用户设置中,它应该可以正常工作:)