Skip to content

Commit 0e826e8

Browse files
committed
First commit.
0 parents  commit 0e826e8

File tree

3 files changed

+150
-0
lines changed

3 files changed

+150
-0
lines changed

MIT-LICENSE

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

README.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
## Synopsis
2+
3+
This is a [Sublime Text 2](http://www.sublimetext.com/2) plugin.
4+
5+
**Highlight trailing spaces and provide a command to delete them.**
6+
7+
ST2 provides a way to automatically delete trailing spaces upon file save.
8+
Depending on your settings, it may be more handy to just highlight them and/or
9+
delete them by hand. This plugin provides just that!
10+
11+
## Installation
12+
13+
Go to your `Packages` subdirectory under ST2's data directory:
14+
15+
Windows: `%APPDATA%\Sublime Text 2`
16+
OS X: `~/Library/Application Support/Sublime Text 2`
17+
Linux: `~/.config/sublime-text-2`
18+
Portable Installation: `Sublime Text 2/Data`
19+
20+
Then clone this repository:
21+
22+
git clone git://github.com/SublimeText/TrailingSpaces.git
23+
24+
That's it!
25+
26+
## Options
27+
28+
Several options are available to customize the plugin look 'n feel. The
29+
config keys goes into config files accessible throught the "Preferences"
30+
menu.
31+
32+
### Bind the deletion command to a shortcut
33+
34+
In order to use the deletion feature, one must add the mapping by hand
35+
(this should probably go into "Key Bindings - User"):
36+
37+
``` js
38+
{ "keys": ["ctrl+shift+t"], "command": "delete_trailing_spaces" }
39+
```
40+
41+
### Change the highlighting color
42+
43+
One may also change the highlighting color, providing a scope name such
44+
as "invalid", "comment"... in "File Settings - User":
45+
46+
``` js
47+
{ "trailing_spaces_highlight_color": "invalid" }
48+
```
49+
50+
Actually, "invalid" is the default value. If you'd like to use a custom color,
51+
it should be defined as a color scope in your theme file. Feel free to ask me
52+
how to do it.
53+
54+
### Disabling highlighting for large files
55+
56+
Highlighting may be disabled for large files. The default threshold is around
57+
1 Mo. This is configurable (in "File Settings - User"); unit is number of chars:
58+
59+
``` js
60+
{ "trailing_spaces_file_max_size": 1000}
61+
```
62+
63+
Even though the trailing spaces are not highlighted, one can still delete them
64+
using the deletion command.

trailing_spaces.py

+66
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
'''
2+
Provides both a trailing spaces highlighter and a deletion command.
3+
4+
Config summary (see README.md for details):
5+
6+
# key binding
7+
{ "keys": ["ctrl+shift+t"], "command": "delete_trailing_spaces" }
8+
9+
# file settings
10+
{
11+
"trailing_spaces_highlight_color": "invalid",
12+
"trailing_spaces_file_max_size": 1000
13+
}
14+
15+
@author: Jean-Denis Vauguet <[email protected]>, Oktay Acikalin <[email protected]>
16+
@license: MIT (http://www.opensource.org/licenses/mit-license.php)
17+
@since: 2011-02-25
18+
'''
19+
20+
import sublime, sublime_plugin
21+
22+
DEFAULT_MAX_FILE_SIZE = 1048576
23+
DEFAULT_COLOR_SCOPE_NAME = "invalid"
24+
25+
# Return an array of regions matching trailing spaces.
26+
def find_trailing_spaces(view):
27+
trails = view.find_all('[ \t]+$')
28+
regions = []
29+
for trail in trails:
30+
regions.append(trail)
31+
return regions
32+
33+
# Highlight matching regions.
34+
class TrailingSpacesHighlightListener(sublime_plugin.EventListener):
35+
def on_modified(self, view):
36+
max_size = view.settings().get('trailing_spaces_file_max_size',
37+
DEFAULT_MAX_FILE_SIZE)
38+
color_scope_name = view.settings().get('trailing_spaces_highlight_color',
39+
DEFAULT_COLOR_SCOPE_NAME)
40+
if view.size() <= max_size:
41+
regions = find_trailing_spaces(view)
42+
view.add_regions('TrailingSpacesHighlightListener',
43+
regions, color_scope_name,
44+
sublime.DRAW_EMPTY)
45+
46+
# Allows to erase matching regions.
47+
class DeleteTrailingSpacesCommand(sublime_plugin.TextCommand):
48+
def run(self, edit):
49+
regions = find_trailing_spaces(self.view)
50+
51+
if regions:
52+
# deleting a region changes the other regions positions, so we
53+
# handle this maintaining an offset
54+
offset = 0
55+
for region in regions:
56+
r = sublime.Region(region.a + offset, region.b + offset)
57+
self.view.erase(edit, sublime.Region(r.a, r.b))
58+
offset -= r.size()
59+
60+
msg_parts = {"nbRegions": len(regions),
61+
"plural": 's' if len(regions) > 1 else ''}
62+
msg = "Deleted %(nbRegions)s trailing spaces region%(plural)s" % msg_parts
63+
else:
64+
msg = "No trailing spaces to delete!"
65+
66+
sublime.status_message(msg)

0 commit comments

Comments
 (0)