Sublime Forum

Function change request

#1

Running sublime Text 3

Possibly the wrong place to ask, but…

There are 3 functions that I would like to have work differently from how they do. These three are ‘title_case’, ‘lower_case’, and ‘upper_case’.

I’d do it myself, but do not know where or what to change. If you point me in the right direction I’ll handle it…

Here is the way I would like these to work.
After invoking either of these commands, if character(s) after cursor is(are) whitespace, advance thru the whitespace to the first character of a word.

For ‘lower_case’, lower case the word from the beginning of the word and advance the cursor to the end of the word, as above.
For ‘upper_case’, upper case the word from the beginning of the word and advance the cursor to the end of the word, as above.
So if I have a set of words (separated by whitespace), I could place the cursor before or in the first word and press alt-u 4 times and have 4 words upper-cased.
neat.

For ‘title_case’, capitalize the first character from the position of the cursor and advance the cursor to the end of the word.
Note, do not go back to the beginning of the word, just perform the operation from the current cursor position to the end of the word.
This is the reason for starting at the cursor position and then advancing to the end of the word.
Suppose I have a variable ‘myfirSTstriNg’.
I could position the cursor between the ‘y’ and ‘f’ and do title_case. I would get
myFirststring
with the cursor at the end.
back arrow the cursor to between the ‘t’ of first and the ‘s’ of string. and do title_case again.
I get the desired result:
myFirstString

If I wanted MyFirstString, I would place the cursor before the first character and start from there.

So how can I make these changes?

Thanks,
Love Sublime Text

chuckles

0 Likes

#2

As first: ‘title_case’
Here i’ve made it as ‘my_capitalize’.

import sublime
import sublime_plugin

class MyCapitalizeCommand(sublime_plugin.TextCommand):
    def run(self, edit):
        caret = self.view.sel()[0].begin()
        region = self.view.word(caret)

        start = region.begin()
        end = region.end()
        word = self.view.substr(region)

        # set character at caret to uppercase and if it's not the first position _
        # _ all charaters up to the end as lowercase
        l = list(word)
        index = caret-start
        l[index] = word[index].upper()
        if index != 0:
            for i in range(index+1, len(word)):
                l* = word*.lower()

        word = ''.join(l)
        self.view.replace(edit, region, word)

        # set cursor to the end of word
        self.view.sel().clear()
        self.view.sel().add(sublime.Region(end))

Here you see in the last two lines, how to set the cursor position. If you wrap the funtion calls for ‘lower_case’ and ‘upper_case’ in a function like above, you can do the same. Try it.**

0 Likes

#3

Looks very good. Thanks!
Just one little question: Where does that code go? How to make ST3 know about it?

ok, 2 questions.

chuckles

0 Likes

#4

OK, you must create a plugin. ( “Tools” - “New Plugin” ). Insert the code in this file. Store the file as “…\User\PLUGIN_FOLDER\PLUGIN_FILE.py”.
Normally will used same names for PLUGIN_FOLDER and PLUGIN_FILE. i.e. “…\User\MyTools\MyTools.py”.

On ST - start all folders will scanned for plugins. So the new plugin will detect and you can bind commands from this in your keymap and/or in the sublime-menu.
From the camel-case class name (MyCapitalizeCommand) automatically will build the snake-case command name (my_capitalize) without “Command”.

0 Likes

#5

Thanks! Works like a charm, BugFix.

I have been educated. I am dangerous now…

chuckles

0 Likes