[TxMt] Reformat comment command
James Edward Gray II
james at grayproductions.net
Wed Aug 23 22:20:05 UTC 2006
On Aug 23, 2006, at 4:33 PM, Alan Schussman wrote:
>> Probably better to grab the comment character from the start of
>> the input. Otherwise there is a more full mapping in the “Continue
>> Line Comment” snippet in the Source bundle.
>
> Grabbing the start of the input works well, and it means setting
> the comment parameter based on what actually sits at the beginning
> of the comment scope, so that it will catch "%comment" or "%
> comment" or "%% comment". Nice.
Yeah, this seems clever. I think we need to make your regex for this
a little more lenient though...
>> The line can be:
>>
>> text = `ruby "#{ENV["TM_SUPPORT_PATH"]}/bin/rubywrap.rb" #
>> {flags}`
>
> Here's where I go "huh." That works, but 1) I don't at all get how
> the input is passed to the command; and 2) if I call on STDIN.read
> prior to this line (by using it to get the beginning of the input),
> then that line fails to get any input, and I have to manually pipe
> the text back into it. What's happening there?
Well, if you call read(), you consume the input, so it's not there to
pass on to the child process.
>>> I also added a gsub to the command because it was eating latex
>>> math and R symbols ($). There's probably a better solution to that.
>>
>> Likely because of the Insert as Snippet -- but the e_sn should
>> take care of that.
>>
>> I am however not sure inserting as snippet is ideal -- I reckon
>> this was to preserve the indent, but maybe better to Replace Text
>> and have the command indent properly.
>
> Excellent -- thanks for pointing out what e_as was doing. When
> inserting as a snippet, e_sn does the job, and replacing the text
> instead of inserting as snippet also works, though you're right
> that it doesn't re-indent before the comment.
Hmm, I think you are still having trouble understanding where to use
these:
e_sh(): for data headed to the shell
e_sn(): for data about to be shown as a TextMate snippet
Here's the code I just moved into the Source bundle. Let me know if
this is working please:
#!/usr/bin/env ruby
$LOAD_PATH << "#{ENV["TM_SUPPORT_PATH"]}/lib"
require "escape"
require "exit_codes"
ctext = STDIN.read
if ctext =~ /^\s*([^\s\w]+\s*)/
cstring = $1
else
TextMate.exit_show_tool_tip("Unable to determine comment character.")
end
flags = %Q{ -p "#{cstring}"}
flags += " --retabify" if ENV["TM_SOFT_TABS"] == "NO"
command = "ruby #{e_sh(ENV["TM_SUPPORT_PATH"])}/bin/rubywrap.rb #
{flags}"
text = open("| #{command}", "r+") do |wrapper|
wrapper << ctext
wrapper.close_write
wrapper.read
end
print e_sn(text)
__END__
James Edward Gray II
More information about the textmate
mailing list