Textmate“评论”命令对于 CSS 代码无法正常工作

发布于 2025-01-02 22:43:59 字数 5062 浏览 0 评论 0原文

当我在 TextMate 中切换 CSS 源代码的注释时遇到一些问题。

使用快捷键 CMD + / 我从“源”包中激活“注释行/选择”命令。问题在于它为各种语言插入了一系列//。例如,在 CSS 文件中,应该插入 /**/ 块,但事实并非如此。在 CSS 文件中,我还尝试了源包中的“插入块注释”命令,但奇怪的结果是我得到以下 //

// ----------------------------------------

而不是我的代码,删除代码并插入它。

我知道我应该修改捆绑包中的命令,但我不知道如何修改以及修改内容。

这是“源”包中“注释行/选择”命令的代码:

#!/usr/bin/env ruby

# by James Edward Gray II <james (at) grayproductions.net>

# 
# To override the operation of this commond for your language add a Preferences
# bundle item that defines the following valiables as appropriate for your
# language:
# 
#   TM_COMMENT_START - the character string that starts comments, e.g. /*
#   TM_COMMENT_END   - the character string that ends comments (if appropriate),
#                      e.g. */
#   TM_COMMENT_MODE  - the type of comment to use - either 'line' or 'block'
# 

require "#{ENV["TM_SUPPORT_PATH"]}/lib/escape"

def out(*args)
  print( *args.map do |arg|
    escaped = e_sn(arg)
    $selected ? escaped.gsub("}", "\\}") : escaped.sub("\0", "${0}")
  end )
end

# find all available comment variables
var_suffixes = [""]
2.upto(1.0/0.0) do |n|
  if ENV.include? "TM_COMMENT_START_#{n}"
    var_suffixes << "_#{n}"
  else
    break
  end
end

text    = STDIN.read
default = nil  # the comment we will insert, if none are removed

# maintain selection
if text == ENV["TM_SELECTED_TEXT"]
  $selected = true
  print "${0:"
  at_exit { print "}" }
else
  $selected = false
end

# try a removal for each comment...
var_suffixes.each do |suffix|
  # build comment
  com = { :start     => ENV["TM_COMMENT_START#{suffix}"] || "# ",
          :end       => ENV["TM_COMMENT_END#{suffix}"]   || "",
          :mode      => ENV["TM_COMMENT_MODE#{suffix}"]  ||
                        (ENV["TM_COMMENT_END#{suffix}"] ? "block" : "line"),
          :no_indent => ENV["TM_COMMENT_DISABLE_INDENT#{suffix}"] }

  com[:esc_start], com[:esc_end] = [com[:start], com[:end]].map do |str|
    str.gsub(/[\\|()\[\].?*+{}^$]/, '\\\\\&').
        gsub(/\A\s+|\s+\z/, '(?:\&)?')
  end

  # save the first one as our insertion default
  default = com if default.nil?

  # try a removal
  case com[:mode]
  when "line"  # line by line comment
    if text !~ /\A[\t ]+\z/ &&
       text.send(text.respond_to?(:lines) ? :lines : :to_s).
            map { |l| !!(l =~ /\A\s*(#{com[:esc_start]}|$)/) }.uniq == [true]
      if $selected
        out text.gsub( /^(\s*)#{com[:esc_start]}(.*?)#{com[:esc_end]}(\s*)$/,
                       '\1\2\3' )
        exit
      else
        r = text.sub( /^(\s*)#{com[:esc_start]}(.*?)#{com[:esc_end]}(\s*)$/,
                      '\1\2\3' )
        i = ENV["TM_LINE_INDEX"].to_i
        i = i > text.index(/#{com[:esc_start]}/)            ?
            [[0, i - com[:start].length].max, r.length].min :
            [i, r.length].min
        r[i, 0] = "\0"
        out r
        exit
      end
    end
  when "block" # block comment
    regex = /\A(\s*)#{com[:esc_start]}(.*?)#{com[:esc_end]}(\s*)\z/m
    if text =~ regex
      if $selected
        out text.sub(regex, '\1\2\3')
        exit
      else
        r = text.sub(regex, '\1\2\3')
        i = ENV["TM_LINE_INDEX"].to_i
        i = i > text.index(/#{com[:esc_start]}/)            ?
            [[0, i - com[:start].length].max, r.length].min :
            [i, r.length].min
        r[i, 0] = "\0"
        out r
        exit
      end
    end
  end
end

# none of our removals worked, so preform an insert (minding indent setting)
text[ENV["TM_LINE_INDEX"].to_i, 0] = "\0" unless $selected or text.empty?
case default[:mode]
when "line"  # apply comment line by line
  if text.empty?
    out "#{default[:start]}\0#{default[:end]}"
  elsif default[:no_indent]
    out text.gsub(/^.*$/, "#{default[:start]}\\&#{default[:end]}")
  elsif text =~ /\A([\t ]*)\0([\t ]*)\z/
    out text.gsub(/^.*$/, "#{$1}#{default[:start]}#{$2}#{default[:end]}")
  else
    indent = text.scan(/^[\t \0]*(?=\S)/).
                  min { |a, b| a.length <=> b.length } || ""
    text.send(text.respond_to?(:lines) ? :lines : :to_s).map do |line|
      if line =~ /^(#{indent})(.*)$(\n?)/ then
        out $1 + default[:start] + $2 + default[:end] + $3
      elsif line =~ /^(.*)$(\n?)/ then
        out indent + default[:start] + $1 + default[:end] + $2
      end
    end
  end
when "block" # apply comment around selection
  if text.empty?
    out default[:start]
    print "${0}"
    out default[:end]
  elsif text =~ /\A([\t ]*)\0([\t ]*)\z/
    out $1, default[:start]
    print "${0}"
    out $2, default[:end]
  elsif default[:no_indent]
    out default[:start], text, default[:end]
  else
    lines = text.to_a
    if lines.empty?
      out default[:start], default[:end]
    else
      lines[-1].sub!(/^(.*)$/, "\\1#{default[:end]}")
      out lines.shift.sub(/^([\s\0]*)(.*)$/, "\\1#{default[:start]}\\2")
      out(*lines) unless lines.empty?
    end
  end
end

I'm having some problems when I toggle the comments in TextMate for CSS source code.

Using the shortcut CMD + / I activate the "Comment Line/Selection" command from the "source" bundle. The problem is that it inserts a series of // for all kinds of languages. For example, in CSS files it is supposed to insert a /**/ block, but it doesn't. In CSS files I also tried the "Insert Block Comment" command from the source bundle with the weird result that I get the following //.

// ----------------------------------------

instead of my code, deleting the code and inserting that.

I know I am supposed to modify the command from the bundle, but I can't figure out how and what.

This is the code of the "Comment Line/Selection" command from the "Source" Bundle:

#!/usr/bin/env ruby

# by James Edward Gray II <james (at) grayproductions.net>

# 
# To override the operation of this commond for your language add a Preferences
# bundle item that defines the following valiables as appropriate for your
# language:
# 
#   TM_COMMENT_START - the character string that starts comments, e.g. /*
#   TM_COMMENT_END   - the character string that ends comments (if appropriate),
#                      e.g. */
#   TM_COMMENT_MODE  - the type of comment to use - either 'line' or 'block'
# 

require "#{ENV["TM_SUPPORT_PATH"]}/lib/escape"

def out(*args)
  print( *args.map do |arg|
    escaped = e_sn(arg)
    $selected ? escaped.gsub("}", "\\}") : escaped.sub("\0", "${0}")
  end )
end

# find all available comment variables
var_suffixes = [""]
2.upto(1.0/0.0) do |n|
  if ENV.include? "TM_COMMENT_START_#{n}"
    var_suffixes << "_#{n}"
  else
    break
  end
end

text    = STDIN.read
default = nil  # the comment we will insert, if none are removed

# maintain selection
if text == ENV["TM_SELECTED_TEXT"]
  $selected = true
  print "${0:"
  at_exit { print "}" }
else
  $selected = false
end

# try a removal for each comment...
var_suffixes.each do |suffix|
  # build comment
  com = { :start     => ENV["TM_COMMENT_START#{suffix}"] || "# ",
          :end       => ENV["TM_COMMENT_END#{suffix}"]   || "",
          :mode      => ENV["TM_COMMENT_MODE#{suffix}"]  ||
                        (ENV["TM_COMMENT_END#{suffix}"] ? "block" : "line"),
          :no_indent => ENV["TM_COMMENT_DISABLE_INDENT#{suffix}"] }

  com[:esc_start], com[:esc_end] = [com[:start], com[:end]].map do |str|
    str.gsub(/[\\|()\[\].?*+{}^$]/, '\\\\\&').
        gsub(/\A\s+|\s+\z/, '(?:\&)?')
  end

  # save the first one as our insertion default
  default = com if default.nil?

  # try a removal
  case com[:mode]
  when "line"  # line by line comment
    if text !~ /\A[\t ]+\z/ &&
       text.send(text.respond_to?(:lines) ? :lines : :to_s).
            map { |l| !!(l =~ /\A\s*(#{com[:esc_start]}|$)/) }.uniq == [true]
      if $selected
        out text.gsub( /^(\s*)#{com[:esc_start]}(.*?)#{com[:esc_end]}(\s*)$/,
                       '\1\2\3' )
        exit
      else
        r = text.sub( /^(\s*)#{com[:esc_start]}(.*?)#{com[:esc_end]}(\s*)$/,
                      '\1\2\3' )
        i = ENV["TM_LINE_INDEX"].to_i
        i = i > text.index(/#{com[:esc_start]}/)            ?
            [[0, i - com[:start].length].max, r.length].min :
            [i, r.length].min
        r[i, 0] = "\0"
        out r
        exit
      end
    end
  when "block" # block comment
    regex = /\A(\s*)#{com[:esc_start]}(.*?)#{com[:esc_end]}(\s*)\z/m
    if text =~ regex
      if $selected
        out text.sub(regex, '\1\2\3')
        exit
      else
        r = text.sub(regex, '\1\2\3')
        i = ENV["TM_LINE_INDEX"].to_i
        i = i > text.index(/#{com[:esc_start]}/)            ?
            [[0, i - com[:start].length].max, r.length].min :
            [i, r.length].min
        r[i, 0] = "\0"
        out r
        exit
      end
    end
  end
end

# none of our removals worked, so preform an insert (minding indent setting)
text[ENV["TM_LINE_INDEX"].to_i, 0] = "\0" unless $selected or text.empty?
case default[:mode]
when "line"  # apply comment line by line
  if text.empty?
    out "#{default[:start]}\0#{default[:end]}"
  elsif default[:no_indent]
    out text.gsub(/^.*$/, "#{default[:start]}\\&#{default[:end]}")
  elsif text =~ /\A([\t ]*)\0([\t ]*)\z/
    out text.gsub(/^.*$/, "#{$1}#{default[:start]}#{$2}#{default[:end]}")
  else
    indent = text.scan(/^[\t \0]*(?=\S)/).
                  min { |a, b| a.length <=> b.length } || ""
    text.send(text.respond_to?(:lines) ? :lines : :to_s).map do |line|
      if line =~ /^(#{indent})(.*)$(\n?)/ then
        out $1 + default[:start] + $2 + default[:end] + $3
      elsif line =~ /^(.*)$(\n?)/ then
        out indent + default[:start] + $1 + default[:end] + $2
      end
    end
  end
when "block" # apply comment around selection
  if text.empty?
    out default[:start]
    print "${0}"
    out default[:end]
  elsif text =~ /\A([\t ]*)\0([\t ]*)\z/
    out $1, default[:start]
    print "${0}"
    out $2, default[:end]
  elsif default[:no_indent]
    out default[:start], text, default[:end]
  else
    lines = text.to_a
    if lines.empty?
      out default[:start], default[:end]
    else
      lines[-1].sub!(/^(.*)$/, "\\1#{default[:end]}")
      out lines.shift.sub(/^([\s\0]*)(.*)$/, "\\1#{default[:start]}\\2")
      out(*lines) unless lines.empty?
    end
  end
end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

弥枳 2025-01-09 22:43:59

确保您已安装“源”包。在撰写本文时的最新 Textmate 2 Alpha 中,转到 TextMate ->首选项->捆绑 ->检查“源”捆绑包进行安装。 CMD + / 快捷键现在应该可以使用了。

Ensure you have the "Source" bundle installed. In the latest Textmate 2 Alpha at the time of writing, go to TextMate -> Preferences -> Bundles -> Check "Source" bundle to install. The CMD + / shortcut should now work.

爱她像谁 2025-01-09 22:43:59

如果您使用高于 1.8.7 的 Ruby,这只是一个小语法问题。您会发现 to_a 方法已被删除。如果您想解决该问题,您只需修改此文件中的代码即可。

为了解决这个问题,您需要搜索他们调用 to_a 的任何位置,并将其替换为 Array("string")

就我而言,我就是这样做的。这也应该适合您:

lines = text.to_a

使用

lines = text.lines.to_a

在此处输入图像描述

这应该是解决所有问题的方法。查看图片看看我最终修复了哪个文件。

It is small syntax problem if you're using a Ruby higher then 1.8.7. You will find that to_a method has been removed. If you want to fix the problem all you need to do is modify the code found in this file.

In order to fix the problem you need to search for any location that they call to_a and replace it with Array("string").

In my case I did this. This also should work for you:

lines = text.to_a

with

lines = text.lines.to_a

enter image description here

This should be a solution for every thing. Look to the image to see what file I ended up fixing.

沉溺在你眼里的海 2025-01-09 22:43:59

我遇到了同样的问题,结果发现我安装了一个 SCSS 包,它的首选项设置为使用“//”作为注释,并带有 source.css 和 source.scss 的范围选择器。

我会检查以确保您没有相同的 SCSS 捆绑包,如果有,请将注释首选项的范围选择器更改为 source.scss。

I had the same problem and it turns out that I had an SCSS bundle installed that had a preference set to use "//" for comments with a scope selector for source.css as well as source.scss.

I would check to make sure that you don't have the same SCSS bundle and if you do, change the scope selector of the comments preference to be just source.scss.

温柔嚣张 2025-01-09 22:43:59

Cmd/ 已经工作多年,现在仍然如此。好吧,我的 TM2 alpha 副本已损坏(无法与数字键盘中的 / 一起使用,但好吧,它是 alpha),但 TM 1.5.x 可以正常工作。

不应该在任何地方修改任何内容。 注释行/选择命令足够智能,可以在“任何”类型的文件中放置正确类型的注释。

你是否搞乱了语言定义?您的文件被识别为“CSS”吗?删除所有或某些插件/捆绑包时它是否有效?

-- EDIT --

在此处输入图像描述

Cmd/ has been working for years and still is. Well, my copy of TM2 alpha is broken (doesn't work with the / in the numeric pad but, well, it's alpha) but TM 1.5.x works as it should.

You are not supposed to modify anything anywhere. The Comment Line/Selection command is smart enough to put the right kind of comment in "any" kind of file.

Did you mess with language definitions? Is your file recognized as "CSS"? Does it work when removing all or certain plugins/bundles?

-- EDIT --

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文