First commit.

This commit is contained in:
Jean-Denis Vauguet 2011-10-17 23:15:03 +02:00
commit 0e826e824e
3 changed files with 150 additions and 0 deletions

20
MIT-LICENSE Normal file
View File

@ -0,0 +1,20 @@
Copyright 2010 Jean-Denis Vauguet
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

64
README.md Normal file
View File

@ -0,0 +1,64 @@
## Synopsis
This is a [Sublime Text 2](http://www.sublimetext.com/2) plugin.
**Highlight trailing spaces and provide a command to delete them.**
ST2 provides a way to automatically delete trailing spaces upon file save.
Depending on your settings, it may be more handy to just highlight them and/or
delete them by hand. This plugin provides just that!
## Installation
Go to your `Packages` subdirectory under ST2's data directory:
Windows: `%APPDATA%\Sublime Text 2`
OS X: `~/Library/Application Support/Sublime Text 2`
Linux: `~/.config/sublime-text-2`
Portable Installation: `Sublime Text 2/Data`
Then clone this repository:
git clone git://github.com/SublimeText/TrailingSpaces.git
That's it!
## Options
Several options are available to customize the plugin look 'n feel. The
config keys goes into config files accessible throught the "Preferences"
menu.
### Bind the deletion command to a shortcut
In order to use the deletion feature, one must add the mapping by hand
(this should probably go into "Key Bindings - User"):
``` js
{ "keys": ["ctrl+shift+t"], "command": "delete_trailing_spaces" }
```
### Change the highlighting color
One may also change the highlighting color, providing a scope name such
as "invalid", "comment"... in "File Settings - User":
``` js
{ "trailing_spaces_highlight_color": "invalid" }
```
Actually, "invalid" is the default value. If you'd like to use a custom color,
it should be defined as a color scope in your theme file. Feel free to ask me
how to do it.
### Disabling highlighting for large files
Highlighting may be disabled for large files. The default threshold is around
1 Mo. This is configurable (in "File Settings - User"); unit is number of chars:
``` js
{ "trailing_spaces_file_max_size": 1000}
```
Even though the trailing spaces are not highlighted, one can still delete them
using the deletion command.

66
trailing_spaces.py Normal file
View File

@ -0,0 +1,66 @@
'''
Provides both a trailing spaces highlighter and a deletion command.
Config summary (see README.md for details):
# key binding
{ "keys": ["ctrl+shift+t"], "command": "delete_trailing_spaces" }
# file settings
{
"trailing_spaces_highlight_color": "invalid",
"trailing_spaces_file_max_size": 1000
}
@author: Jean-Denis Vauguet <jd@vauguet.fr>, Oktay Acikalin <ok@ryotic.de>
@license: MIT (http://www.opensource.org/licenses/mit-license.php)
@since: 2011-02-25
'''
import sublime, sublime_plugin
DEFAULT_MAX_FILE_SIZE = 1048576
DEFAULT_COLOR_SCOPE_NAME = "invalid"
# Return an array of regions matching trailing spaces.
def find_trailing_spaces(view):
trails = view.find_all('[ \t]+$')
regions = []
for trail in trails:
regions.append(trail)
return regions
# Highlight matching regions.
class TrailingSpacesHighlightListener(sublime_plugin.EventListener):
def on_modified(self, view):
max_size = view.settings().get('trailing_spaces_file_max_size',
DEFAULT_MAX_FILE_SIZE)
color_scope_name = view.settings().get('trailing_spaces_highlight_color',
DEFAULT_COLOR_SCOPE_NAME)
if view.size() <= max_size:
regions = find_trailing_spaces(view)
view.add_regions('TrailingSpacesHighlightListener',
regions, color_scope_name,
sublime.DRAW_EMPTY)
# Allows to erase matching regions.
class DeleteTrailingSpacesCommand(sublime_plugin.TextCommand):
def run(self, edit):
regions = find_trailing_spaces(self.view)
if regions:
# deleting a region changes the other regions positions, so we
# handle this maintaining an offset
offset = 0
for region in regions:
r = sublime.Region(region.a + offset, region.b + offset)
self.view.erase(edit, sublime.Region(r.a, r.b))
offset -= r.size()
msg_parts = {"nbRegions": len(regions),
"plural": 's' if len(regions) > 1 else ''}
msg = "Deleted %(nbRegions)s trailing spaces region%(plural)s" % msg_parts
else:
msg = "No trailing spaces to delete!"
sublime.status_message(msg)