Textmate “comment” command not working properly for css code

假如想象 提交于 2019-12-06 11:04:48

问题


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

回答1:


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.




回答2:


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

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




回答3:


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.




回答4:


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 --



来源:https://stackoverflow.com/questions/9155137/textmate-comment-command-not-working-properly-for-css-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!