文本“评论”命令无法正常工作的CSS代码 [英] Textmate "comment" command not working properly for css code

查看:227
本文介绍了文本“评论”命令无法正常工作的CSS代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



使用快捷键 CMD + / 束。问题是它为所有种类的语言插入一系列 // 。例如,在CSS文件中,应该插入一个 / ** / 块,但不是。在CSS文件中,我还尝试从源包中的插入块注释命令与奇怪的结果,我得到以下 //

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

而不是我的代码,删除代码并插入。 / p>

我知道我应该从包中修改命令,但我不知道如何和什么。



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

 #! usr / bin / env ruby​​ 

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


#覆盖您的语言的这个commond的操作添加一个首选项
#包项目,定义下面的valiables适合您的
#语言:

#TM_COMMENT_START - 开始注释的字符串,例如/ *
#TM_COMMENT_END - 结束注释的字符串(如果合适),
#。 * /
#TM_COMMENT_MODE - 要使用的注释类型 - 'line'或'block'


要求#{ENV [TM_SUPPORT_PATH]} / lib / escape

def out(* args)
print(* args.map do | arg |
escaped = e_sn(arg)
$ select? gsub(},\\}):escaped.sub(\0,$ {0})
end)
end
$ b b#查找所有可用的注释变量
var_suffixes = []
2.upto(1.0 / 0.0)do | n |
如果ENV.include? TM_COMMENT_START _#{n}
var_suffixes<< _#{n}
else
break
end
end

text = STDIN.read
default = nil#如果没有删除,我们将插入

#维护选择
如果文本== ENV [TM_SELECTED_TEXT]
$ selected = true
print$ {0 :
at_exit {print}}
else
$ selected = false
end

#尝试对每个注释删除...
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]]。
str.gsub(/ [\\ |()\ [\]。?* + {} ^ $] /,'\\\\\&')。
gsub(/ \A\s + | \s + \z /,'(?:\&)?')
end

#保存第一个作为我们的插入默认
default = com如果default.nil?

#尝试删除
case com [:mode]
line#逐行注释
如果文本!〜/ \A [\ t] + \z /&&
text.send(text.respond_to?(:lines)?:lines::to_s)。
map {| l | !!(l =〜/ \A\s *(#{com [:esc_start]} | $)/)} .uniq == [true]
如果$ selected
out文本。 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
out r
exit
end
end
当block#块注释
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
out r
exit
end
end
end
end

#我们的移除工作,所以预成型插入(绑定缩进设置)
text [ENV [TM_LINE_INDEX]。to_i,0] =\0除非$选择或text.empty?
case default [:mode]
line#按行应用注释
如果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})(.*)$(\\\
?)/ then
out $ 1 + default [:start] + $ 2 + default [:end] + $ 3
elsif line =〜/^(.*)$(\\\
?)/,
out indent + default [:start] + $ 1 + default [:end] + $ 2
end
end
end
当block#应用注释选择
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
如果是行。空?
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


解决方案

确保您已安装源捆绑软件。在最新的Textmate 2 Alpha编写的时候,去TextMate - >首选项 - > Bundles - >检查源包安装。 CMD + /快捷方式现在应该可以正常工作。


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

解决方案

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.

这篇关于文本“评论”命令无法正常工作的CSS代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆