SublimeText / LaTeXTools

LaTeX plugin for Sublime Text
https://latextools.readthedocs.io/
2.01k stars 365 forks source link

$ auto-pairing #508

Open sjoertvv opened 9 years ago

sjoertvv commented 9 years ago

Entering a $-sign right after text yields a single $. That is, the following doesn't autocomplete:

text$

While I think it should complete to text$$, with the cursor in between the signs (except of course when typing a new $-sign after $text).

When I see a single $-sign, I tend to press $ once more, which now does autocomplete, yielding text$$$. Pressing backspace then removes both new $-signs again :)

I'm on Mac OS X, Sublime build 3083, LaTexTools version 2015.04.13.03. My colleague who is running Sublime on Windows had the same issue.

ig0774 commented 9 years ago

Add this to your default keymap (Preferences -> Key Bindings - User):

{ "keys": ["$"], "command": "insert_snippet", "args": {"contents": "\\$$0\\$"}, 
"context":  
    [
        { "key": "setting.command_mode", "operator": "equal", "operand": false },
        {"key": "selector", "operator": "equal", "operand": "text.tex.latex"},
        { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
        { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
        /*{ "key": "following_text", "operator": "regex_contains", "operand": "^(?:\t| |\\)|])", "match_all": true },*/
        /*{ "key": "preceding_text", "operator": "not_regex_contains", "operand": "[\"a-zA-Z0-9_]$", "match_all": true },*/
        { "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.double", "match_all": true }
    ] 
},

I took this from the default LaTeXTools keymap and commented out the preceding_text rule.

sjoertvv commented 9 years ago

Thanks! That (almost) does the trick.

When inserting after $text, I get two signs: $text$$. This is not a big deal, since this should happend rarely. If regex could count the number of $'s in the preceding_text we might be able to fix this? That is, insert a single $ if the preceding number of $'s is odd.

ig0774 commented 9 years ago

Better option than trying to count $s with regex: use the support already builtin to the LaTeX syntax:

{ "keys": ["$"], "command": "insert_snippet", "args": {"contents": "\\$$0\\$"}, 
    "context":  
        [
            { "key": "setting.command_mode", "operator": "equal", "operand": false },
            { "key": "selector", "operator": "equal", "operand": "text.tex.latex"},
            { "key": "selector", "operator": "not_equal", "operand": "string.other.math.block.tex"},
            { "key": "selector", "operator": "not_equal", "operand": "string.other.math.tex"},
            { "key": "setting.auto_match_enabled", "operator": "equal", "operand": true },
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "eol_selector", "operator": "not_equal", "operand": "string.quoted.double", "match_all": true }
        ] 
    },

On a quick test that seems to work, basically by stopping the extra $ from inserting if we are already in math mode. This might actually be a better update to the may keymap files, since it seems to be the intention if not the execution behind the original exclusion.

sjoertvv commented 9 years ago

Hm. Adding this appears to make no difference for me.

ig0774 commented 9 years ago

Here's what I was using to find the current selector value when I was testing this (run these commands in the console):

import sublime
view = sublime.active_window().active_view()
point = view.sel()[0].b
view.scope_name(point)

What do you see for whatever you are testing (and note that the option above isn't perfect, but---for me on Windows and Mac---it works for both the initial case and $text). Note that one of the quirks of this is that this doesn't work when the text following the $ ends on whitespace, i.e., it works for $text but not $text␣. I don't quite know why that is...