Home Download Buy Blog Forum Support

new Erase Blank Chars Plugin

Re: new Erase Blank Chars Plugin

Postby EJ12N on Wed Jun 17, 2009 3:19 pm

jaydee wrote:Sorry, but does this plug in strip all trailing white spaces during file saving? I'm looking for a plug in that does that and not sure if this is what I need.


Nope, not during save. But I'm sure someone can make a plugin using onSave with findall() :D

EDIT: Looks like your in for some luck =]
STRIP TRAILING ON SAVE
viewtopic.php?p=1540#p1540

Now let's see if sublimator wants to update it to use findall()
EJ12N
 
Posts: 281
Joined: Tue Apr 21, 2009 5:23 pm

Re: new Erase Blank Chars Plugin

Postby gpfsmurf on Wed Jun 17, 2009 5:55 pm

I think you could do something like this:
Code: Select all
import sublime, sublimeplugin

class RemoveTrailingWhiteSpaceOnSave(sublimeplugin.Plugin):
   def onPreSave(self, view):
      region = sublime.Region(0L, view.size())
      s = view.substr(region)
      out = u"\n".join([u.rstrip() for u in s.splitlines()])
      view.replace(region, out)
gpfsmurf
 
Posts: 211
Joined: Mon Jun 23, 2008 6:31 pm

Re: new Erase Blank Chars Plugin

Postby EJ12N on Thu Jun 18, 2009 3:01 pm

sublimator wrote:http://pastie.org/516119

The version in pastie above has an onPreSave and a run.

The run() version forcefully destroys *all* trailing space

The onPreSave() will use an heuristic to determine if you have recently pressed enter, and if so it won't delete that indented space. It will also not strip any trailing space inside any regions scoped as `string` ( which might get annoying for some source code ) Use run() to forcefully strip all or modify onPreSave as you see fit.


Nice one! Thx :)
EJ12N
 
Posts: 281
Joined: Tue Apr 21, 2009 5:23 pm

Re: new Erase Blank Chars Plugin

Postby lawlist on Tue Mar 12, 2013 5:22 pm

I tried updating the example plugin posted by eric1235711 so that it works with ST2 2.0.1/2217, and got most of it working, but am still receiving an error: TypeError: run() takes exactly 3 arguments (2 given). Here is the updated code so far:

Code: Select all
{ "keys": ["f1"], "command": "left_erase_by_char_class" },
{ "keys": ["f2"], "command": "right_erase_by_char_class" },


Code: Select all
import sublime, sublime_plugin, re


class RightEraseByCharClassCommand(sublime_plugin.TextCommand):
        def run(self, view, args):
           
            # patterns
          pt_s = re.compile(r"\s")
          pt_w = re.compile(r"\w")
          pt_o = re.compile(r"[^\w\s]")
         
          sz = view.size()
           
          for region in view.sel():
             pos = region.begin()
             
             # check first char
             if pt_w.match(view.substr(pos)) :
                pt = pt_w
             elif pt_s.match(view.substr(pos)) :
                pt = pt_s
             else :
                pt = pt_o
             
             # removes according to first char
             while pt.match(view.substr(pos)) and pos < sz :
                view.erase(sublime.Region(pos,pos+1))


class LeftEraseByCharClassCommand(sublime_plugin.TextCommand):
       def run(self, view, args):
         
          # patterns
          pt_s = re.compile(r"\s")
          pt_w = re.compile(r"\w")
          pt_o = re.compile(r"[^\w\s]")
         
          for region in view.sel():
             pos = region.end()-1
             
             # check last char
             if pt_w.match(view.substr(pos)) :
                pt = pt_w
             elif pt_s.match(view.substr(pos)) :
                pt = pt_s
             else :
                pt = pt_o
             
             # removes according to last char
             while pos > 1 and pt.match(view.substr(pos)) :
                view.erase(sublime.Region(pos,pos+1))
                pos -= 1
lawlist
 
Posts: 54
Joined: Thu Feb 21, 2013 6:47 pm

Re: new Erase Blank Chars Plugin

Postby facelessuser on Tue Mar 12, 2013 10:01 pm

The API documentation only lists args to let you know that is where you add your custom args if you are not requiring it, do not add any arguments:

Here is an example of a command that has a custom argument called "option". It is required (you can't run the command without it):
class HexViewerOptionsCommand(sublime_plugin.WindowCommand):

...

def run(self, option):


Here is an example that uses custom arguments that are optional (you can call the command without the arguments and it will still work; they are defaulted to a value if they aren't used):
class HexViewerCommand(sublime_plugin.WindowCommand):

...

def run(self, bits=None, bytearray=None):


Here is an example that can take an infinite number of arguments of any name. No need to define them you can read them from the kwargs which is a dictionary:
class ExportHtmlCommand(sublime_plugin.WindowCommand):
def run(self, **kwargs):
facelessuser
 
Posts: 1575
Joined: Tue Apr 05, 2011 7:38 pm

Re: new Erase Blank Chars Plugin

Postby lawlist on Sun Mar 17, 2013 4:19 pm

This plugin by decap is an excellent solution for deleting whitespace.

https://github.com/dacap/sublime-shrink-whitespaces

For whatever the reason, "super+d" aka "⌘+d" aka "find_under_expand" do not work with macros. "find_under_expand" does not appear to be a plugin. So, here is a sample select-entire-word plugin that does work with macros:

Code: Select all
import sublime, sublime_plugin

class SelectEntireWord(sublime_plugin.TextCommand):
    def run(self, edit):
        regions = []
        for s in self.view.sel():
            word = self.view.word(sublime.Region(s.begin(), s.end()))
            if word.end() == s.end():
                # this next part deals with an end of line issue
                word = self.view.word(sublime.Region(s.end(), s.end() + 1))
            regions.append(word)
        for r in regions:
            self.view.sel().add(r)


The two plugins can be linked together with a macro, or chained with another plugin.
lawlist
 
Posts: 54
Joined: Thu Feb 21, 2013 6:47 pm

Previous

Return to Plugin Announcements

Who is online

Users browsing this forum: No registered users and 11 guests