[英]Textmate “comment” command not working properly for css code
當我在TextMate中為CSS源代碼切換注釋時,我遇到了一些問題。
使用快捷鍵CMD + / I激活“源”包中的“注釋行/選擇”命令。 問題是它為各種語言插入了一系列//
。 例如,在CSS文件中,它應該插入/**/
塊,但它不會。 在CSS文件中,我還嘗試了源包中的“插入塊注釋”命令,結果很奇怪,我得到以下結果//
。
// ----------------------------------------
而不是我的代碼,刪除代碼並插入。
我知道我應該從捆綁中修改命令,但我無法弄清楚如何以及如何。
這是來自“Source”Bundle的“Comment Line / Selection”命令的代碼:
#!/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
確保安裝了“Source”軟件包。 在撰寫本文時的最新Textmate 2 Alpha中,轉到TextMate - > Preferences - > Bundles - > Check“Source”包進行安裝。 CMD + /快捷方式現在應該可以工作。
如果你使用的是高於1.8.7的Ruby,這是一個很小的語法問題。 你會發現to_a
方法已被刪除。 如果要解決問題,只需修改此文件中的代碼即可。
為了解決問題,您需要搜索他們調用to_a
任何位置並將其替換為Array("string")
。
就我而言,我做到了這一點。 這也應該適合你:
lines = text.to_a
同
lines = text.lines.to_a
這應該是每件事的解決方案。 查看圖像,看看我最終修復了哪個文件。
我遇到了同樣的問題,結果發現我安裝了一個SCSS軟件包,其首選項設置為使用“//”作為帶有source.css和source.scss范圍選擇器的注釋。
我會檢查以確保您沒有相同的SCSS包,如果這樣做,請將注釋首選項的范圍選擇器更改為source.scss。
Cmd /已經工作了多年,現在仍然如此。 好吧,我的TM2 alpha副本被破壞了(與數字鍵盤中的/
不兼容,但是,它是alpha)但是TM 1.5.x可以正常工作。
你不應該在任何地方修改任何東西。 注釋行/選擇命令足夠智能,可以在“任何”類型的文件中添加正確的注釋。
你搞亂了語言定義嗎? 您的文件是否被識別為“CSS”? 刪除所有或某些插件/包時它是否有效?
-- EDIT --
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.