Skip to content

Commit

Permalink
Added settings to allow for tweaking of alignment, tweaked handling o…
Browse files Browse the repository at this point in the history
…f blank lines when the user has their settings set to trim trailing white space
  • Loading branch information
wbond committed Aug 8, 2011
1 parent 93f279e commit 29412f5
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 69 deletions.
128 changes: 81 additions & 47 deletions Alignment.py
Original file line number Diff line number Diff line change
@@ -1,84 +1,118 @@
import sublime
import sublime_plugin
import re

class AlignmentCommand(sublime_plugin.TextCommand):
operator_chars = ['+', '-', '&', '|', '<', '>', '!', '~', '%', '/', '*',
'.']

def run(self, edit):
view = self.view
sel = view.sel()
max_col = 0

settings = view.settings()

# This handles aligning single multi-line selections
if len(sel) == 1:
tab_size = int(view.settings().get('tab_size', 8))
use_spaces = view.settings().get('translate_tabs_to_spaces')
tab_size = int(settings.get('tab_size', 8))
use_spaces = settings.get('translate_tabs_to_spaces')
points = []
line_nums = [view.rowcol(line.a)[0] for line in view.lines(sel[0])]

# Align the left edges by first finding the left edge
for row in line_nums:
pt = view.text_point(row, 0)
trim_trailing_white_space = \
settings.get('trim_trailing_white_space_on_save')

if settings.get('align_indent'):
# Align the left edges by first finding the left edge
for row in line_nums:
pt = view.text_point(row, 0)

# Skip blank lines when the user times trailing whitespace
line = view.line(pt)
if trim_trailing_white_space and line.a == line.b:
continue

char = view.substr(pt)
while char == ' ' or char == '\t':
# Turn tabs into spaces when the preference is spaces
if use_spaces and char == '\t':
view.replace(edit, sublime.Region(pt, pt+1), ' ' *
tab_size)
char = view.substr(pt)
while char == ' ' or char == '\t':
# Turn tabs into spaces when the preference is spaces
if use_spaces and char == '\t':
view.replace(edit, sublime.Region(pt, pt+1), ' ' *
tab_size)

# Turn spaces into tabs when tabs are the preference
if not use_spaces and char == ' ':
max_pt = pt + tab_size
end_pt = pt
while view.substr(end_pt) == ' ' and end_pt < \
max_pt:
end_pt += 1
view.replace(edit, sublime.Region(pt, end_pt),
'\t')

pt += 1

# Rollback if the left edge wraps to the next line
if view.rowcol(pt)[0] != row:
pt -= 1
break

char = view.substr(pt)

points.append(pt)
max_col = max([max_col, view.rowcol(pt)[1]])

# Adjust the left edges based on the maximum that was found
adjustment = 0
max_length = 0
for pt in points:
pt += adjustment
length = max_col - view.rowcol(pt)[1]
max_length = max([max_length, length])
adjustment += length
view.insert(edit, pt, (' ' if use_spaces else '\t') *
length)

# Turn spaces into tabs when the preference is to use tabs
if not use_spaces and char == ' ':
max_pt = pt + tab_size
end_pt = pt
while view.substr(end_pt) == ' ' and end_pt < max_pt:
end_pt += 1
view.replace(edit, sublime.Region(pt, end_pt), '\t')
perform_mid_line = max_length == 0

pt += 1
else:
perform_mid_line = True

# Rollback if the left edge wraps to the next line
if view.rowcol(pt)[0] != row:
pt -= 1
break
alignment_chars = settings.get('alignment_chars')
if alignment_chars == None:
alignment_chars = []
alignment_prefix_chars = settings.get('alignment_prefix_chars')
if alignment_prefix_chars == None:
alignment_prefix_chars = []
alignment_space_chars = settings.get('alignment_space_chars')
if alignment_space_chars == None:
alignment_space_chars = []

char = view.substr(pt)
alignment_pattern = '|'.join([re.escape(ch) for ch in
alignment_chars])

points.append(pt)
max_col = max([max_col, view.rowcol(pt)[1]])

# Adjust the left edges based on the maximum that was found
adjustment = 0
max_length = 0
for pt in points:
pt += adjustment
length = max_col - view.rowcol(pt)[1]
max_length = max([max_length, length])
adjustment += length
view.insert(edit, pt, (' ' if use_spaces else '\t') * length)

# If the left-edges are aligned, align the equal signs
if max_length == 0:
if perform_mid_line and alignment_chars:
points = []
max_col = 0
for row in line_nums:
pt = view.text_point(row, 0)
equal_pt = view.find('=', pt).a
matching_region = view.find(alignment_pattern, pt)
if not matching_region:
continue
matching_char_pt = matching_region.a

insert_pt = equal_pt
insert_pt = matching_char_pt
# If the equal sign is part of a multi-character
# operator, bring the first character forward also
if view.substr(insert_pt-1) in self.operator_chars:
if view.substr(insert_pt-1) in alignment_prefix_chars:
insert_pt -= 1

space_pt = insert_pt
while view.substr(space_pt-1) == ' ':
space_pt -= 1
space_pt += 1

if view.substr(matching_char_pt) in alignment_space_chars:
space_pt += 1

# If the next equal sign is not on this line, skip the line
if view.rowcol(equal_pt)[0] != row:
if view.rowcol(matching_char_pt)[0] != row:
continue

points.append(insert_pt)
Expand Down
20 changes: 20 additions & 0 deletions Base File.sublime-settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
// If the indent level of a multi-line selection should be aligned
"align_indent": true,

// The mid-line characters to align in a multi-line selection, changing
// this to an empty array will disable mid-line alignment
"alignment_chars": ["="],

// If the following character is matched for alignment, insert a space
// before it in the final alignment
"alignment_space_chars": ["="],

// The characters to align along with "alignment_chars"
// For instance if the = is to be aligned, there are a number of
// symbols that can be combined with the = to make an operator, and all
// of those must be kept next to the = for the operator to be parsed
"alignment_prefix_chars": [
"+", "-", "&", "|", "<", ">", "!", "~", "%", "/", "*", "."
]
}
62 changes: 52 additions & 10 deletions Default.sublime-commands
Original file line number Diff line number Diff line change
@@ -1,26 +1,68 @@
[
{
"caption": "Preferences: Alignment Key Bindings",
"command": "open_file", "args":
{
"caption": "Preferences: Alignment File Settings – Default",
"command": "open_file",
"args": {
"file": "${packages}/Terminal/Base File.sublime-settings"
}
},
{
"caption": "Preferences: Alignment File Settings – User",
"command": "open_file",
"args": {
"file": "${packages}/User/Base File.sublime-settings"
}
},
{
"caption": "Preferences: Alignment File Settings – Syntax Specific – User",
"command": "open_file_settings"
},
{
"caption": "Preferences: Alignment Key Bindings – Default",
"command": "open_file",
"args": {
"file": "${packages}/Alignment/Default (Windows).sublime-keymap",
"platform": "Windows"
}
},
{
"caption": "Preferences: Alignment Key Bindings",
"command": "open_file", "args":
{
"caption": "Preferences: Alignment Key Bindings – Default",
"command": "open_file",
"args": {
"file": "${packages}/Alignment/Default (OSX).sublime-keymap",
"platform": "OSX"
}
},
{
"caption": "Preferences: Alignment Key Bindings",
"command": "open_file", "args":
{
"caption": "Preferences: Alignment Key Bindings – Default",
"command": "open_file",
"args": {
"file": "${packages}/Alignment/Default (Linux).sublime-keymap",
"platform": "Linux"
}
},
{
"caption": "Preferences: Alignment Key Bindings – User",
"command": "open_file",
"args": {
"file": "${packages}/User/Default (Windows).sublime-keymap",
"platform": "Windows"
}
},
{
"caption": "Preferences: Alignment Key Bindings – User",
"command": "open_file",
"args": {
"file": "${packages}/User/Default (OSX).sublime-keymap",
"platform": "OSX"
}
},
{
"caption": "Preferences: Alignment Key Bindings – User",
"command": "open_file",
"args": {
"file": "${packages}/User/Default (Linux).sublime-keymap",
"platform": "Linux"
}
}
]
]
3 changes: 3 additions & 0 deletions JSON.sublime-settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"alignment_chars": ["=", ":"]
}
3 changes: 3 additions & 0 deletions Javascript.sublime-settings
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"alignment_chars": ["=", ":"]
}
38 changes: 26 additions & 12 deletions Main.sublime-menu
Original file line number Diff line number Diff line change
Expand Up @@ -16,48 +16,62 @@
"children":
[
{
"command": "open_file", "args":
{
"command": "open_file",
"args": {"file": "${packages}/Alignment/Base File.sublime-settings"},
"caption": "Settings – Default"
},
{
"command": "open_file",
"args": {"file": "${packages}/User/Base File.sublime-settings"},
"caption": "Settings – User"
},
{
"command": "open_file_settings",
"caption": "Settings – Syntax Specific – User"
},
{
"command": "open_file",
"args": {
"file": "${packages}/Alignment/Default (Windows).sublime-keymap",
"platform": "Windows"
},
"caption": "Key Bindings – Default"
},
{
"command": "open_file", "args":
{
"command": "open_file",
"args": {
"file": "${packages}/Alignment/Default (OSX).sublime-keymap",
"platform": "OSX"
},
"caption": "Key Bindings – Default"
},
{
"command": "open_file", "args":
{
"command": "open_file",
"args": {
"file": "${packages}/Alignment/Default (Linux).sublime-keymap",
"platform": "Linux"
},
"caption": "Key Bindings – Default"
},
{
"command": "open_file", "args":
{
"command": "open_file",
"args": {
"file": "${packages}/User/Default (Windows).sublime-keymap",
"platform": "Windows"
},
"caption": "Key Bindings – User"
},
{
"command": "open_file", "args":
{
"command": "open_file",
"args": {
"file": "${packages}/User/Default (OSX).sublime-keymap",
"platform": "OSX"
},
"caption": "Key Bindings – User"
},
{
"command": "open_file", "args":
{
"command": "open_file",
"args": {
"file": "${packages}/User/Default (Linux).sublime-keymap",
"platform": "Linux"
},
Expand Down
10 changes: 10 additions & 0 deletions readme.creole
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,16 @@ array(
)
}}}

== Settings

Using the //Preferences > Package Settings > Alignment// menu or the command
palette it is possible to customize the plugin to:

* Disable indent alignment
* Perform mid-line alignment on characters other than an {{{=}}}
* Tweak what characters are aligned with the {{{=}}}
* Set the alignment settings per syntax, e.g. to align {{{:}}} in JSON
== License

All of Sublime Alignment is licensed under the MIT license.
Expand Down

0 comments on commit 29412f5

Please sign in to comment.