【问题标题】:Textmate "comment" command not working properly for css codeTextmate“评论”命令不能正常工作的CSS代码
【发布时间】:2012-02-27 15:13:27
【问题描述】:

我在 TextMate 中为 CSS 源代码切换 cmets 时遇到了一些问题。

使用快捷键 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

【问题讨论】:

    标签: css comments textmate bundle


    【解决方案1】:

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

    【讨论】:

    • 禁用了所有不需要的捆绑包,包括 Source... 再次安装 Source 后对我来说效果很好。 Tnx
    【解决方案2】:

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

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

    就我而言,我这样做了。这也应该适合你:

    lines = text.to_a
    

    lines = text.lines.to_a
    

    这应该是所有事情的解决方案。查看图像以查看我最终修复的文件。

    【讨论】:

    • 呃,天哪.. 谢谢你。这让我NUTS!就我而言,这只是对文件的一次更改...line 139。 (我认为是 textmate 1.5.10)
    • 谢谢!...这困扰了我好几个月
    【解决方案3】:

    我遇到了同样的问题,结果发现我安装了一个 SCSS 包,该包的首选项设置为使用 cmets 的“//”以及 source.css 和 source.scss 的范围选择器。

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

    【讨论】:

      【解决方案4】:

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

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

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

      -- EDIT --
      

      【讨论】:

      • 对不起,我忘了说明我使用的是 TM 1.5 我不认为我弄乱了语言定义,正如你建议的那样,我停用了大部分捆绑包,只保留了活动 css source texttextmate 捆绑没有结果。正确识别 Css 文件。
      • 请将该命令的内容(见截图)粘贴到某处,以便有人可以将其与正常的比较。与此同时,你应该重新安装整个东西。
      猜你喜欢
      • 2018-10-18
      • 2021-10-13
      • 1970-01-01
      • 1970-01-01
      • 2016-10-09
      • 2019-04-13
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多