简体   繁体   中英

How do I stop unwanted text from appearing when I write multi-line comments in Sublime Text (Ruby)?

In Ruby multi-line comments are written with =begin and =end .

When I type =begin , default text pops up after what I wrote regarding an error. Sublime Version 3.2.1. The automatic text that pops up is

rescue Exception => e

referring to an exception in Ruby. I am looking to stop this text from appearing every time I create a multi-line comment.

I have tried looking at the Sublime Text Preferences (version 3) and google searching for related questions, and I have reviewed Sublime documentation but I have not found anything that will help. There is a file for Ruby preferences on sublime text, but mine is empty so I am not sure what to add.

=begin

rescue Exception => e

=end

The reason this happens is that the Ruby package that ships with Sublime includes a series of snippets and one of them inserts a begin-rescue-end block in response to the text begin followed by the autocomplete completion key. There are a couple of ways to stop this from happening that involve changing how the snippet is triggered or what the snippet contains.

First, you can tweak the setting of auto_complete_commit_on_tab :

    // By default, auto complete will commit the current completion on enter.
    // This setting can be used to make it complete on tab instead.
    // Completing on tab is generally a superior option, as it removes
    // ambiguity between committing the completion and inserting a newline.
    "auto_complete_commit_on_tab": false,

As the comment describes, this setting controls whether Enter or Tab is used to select an autocomplete entry from the popup, with the default being Enter .

From your problem description, you may be running with this setting set at the default; thus whenever you type begin the autocomplete entry for the snippet appears in the list, and pressing Enter selects the item and expands it.

If that's the case, then flipping the value of the setting to true will stop this from happening; when you type begin the popup will still offer the snippet, but when you press Enter it will be interpreted as a "normal" enter, and the snippet won't expand.

This also has the benefit that you can still utilize the snippet for it's intended purpose outside of comments to quickly set up a rescue block. Note however that if you make use of the autocomplete popup in other cases, this would require you to train your brain that you need to press Tab where you used to press Enter .

Alternatively, you can disable the snippet or alter what it inserts to make your life a little better. To do that, you would select Preferences: Browse Packages from the command palette or the main menu to open the Packages folder.

Inside of that folder, create a folder named Ruby , and then inside of that folder create a folder named Snippets , and inside of that folder a file named Wrap-in-Begin-Rescue-End.sublime-snippet (the case is important; the names should be exact for all of the files and folders here).

If you create an empty file when you do this, then the snippet in the Ruby package will be complete disabled and can no longer be triggered.

Alternately, you can instead give the file you create content that looks something like this:

<snippet>
    <content><![CDATA[
begin
    $0
=end
]]></content>
    <tabTrigger>begin</tabTrigger>
    <scope>source.ruby - comment</scope>
    <description>complete block comment</description>
</snippet>

That will make the text begin expand out to be the text in the snippet content area, which will add the text begin followed by the close of the block comment, and leave the cursor in the middle where the $0 is located.

You can alter that as needed, such as altering the indent or changing where the cursor is placed, etc.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM