Repository: wbond/sublime_alignment Branch: master Commit: 7be063c43687 Files: 15 Total size: 16.5 KB Directory structure: gitextract_43knxiwh/ ├── .gitignore ├── Alignment.py ├── Base File.sublime-settings ├── CSS.sublime-settings ├── Default (Linux).sublime-keymap ├── Default (OSX).sublime-keymap ├── Default (Windows).sublime-keymap ├── Default.sublime-commands ├── JSON.sublime-settings ├── Javascript.sublime-settings ├── Main.sublime-menu ├── messages/ │ ├── 2.0.0.txt │ └── 2.1.0.txt ├── messages.json └── readme.creole ================================================ FILE CONTENTS ================================================ ================================================ FILE: .gitignore ================================================ *.pyc *.sublime-project *.sublime-workspace ================================================ FILE: Alignment.py ================================================ import sublime import sublime_plugin import re import math import os import sys try: from Default.indentation import line_and_normed_pt as normed_rowcol except ImportError: # This is necessary due to load order of packages in Sublime Text 2 sys.path.append(os.path.join(sublime.packages_path(), 'Default')) indentation = __import__('indentation') reload(indentation) del sys.path[-1] normed_rowcol = indentation.line_and_normed_pt def convert_to_mid_line_tabs(view, edit, tab_size, pt, length): spaces_end = pt + length spaces_start = spaces_end while view.substr(spaces_start-1) == ' ': spaces_start -= 1 spaces_len = spaces_end - spaces_start normed_start = normed_rowcol(view, spaces_start)[1] normed_mod = normed_start % tab_size tabs_len = 0 diff = 0 if normed_mod != 0: diff = tab_size - normed_mod tabs_len += 1 tabs_len += int(math.ceil(float(spaces_len - diff) / float(tab_size))) view.replace(edit, sublime.Region(spaces_start, spaces_end), '\t' * tabs_len) return tabs_len - spaces_len class AlignmentCommand(sublime_plugin.TextCommand): def run(self, edit): view = self.view sel = view.sel() max_col = 0 settings = view.settings() tab_size = int(settings.get('tab_size', 8)) use_spaces = settings.get('translate_tabs_to_spaces') # This handles aligning single multi-line selections if len(sel) == 1: points = [] line_nums = [view.rowcol(line.a)[0] for line in view.lines(sel[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) # 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) perform_mid_line = max_length == 0 else: perform_mid_line = True 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 = [] alignment_pattern = '|'.join([re.escape(ch) for ch in alignment_chars]) if perform_mid_line and alignment_chars: points = [] max_col = 0 for row in line_nums: pt = view.text_point(row, 0) matching_region = view.find(alignment_pattern, pt) if not matching_region: continue matching_char_pt = matching_region.a 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 alignment_prefix_chars: insert_pt -= 1 space_pt = insert_pt while view.substr(space_pt-1) in [' ', '\t']: space_pt -= 1 # Replace tabs with spaces for consistent indenting if view.substr(space_pt) == '\t': view.replace(edit, sublime.Region(space_pt, space_pt+1), ' ' * tab_size) matching_char_pt += tab_size - 1 insert_pt += tab_size - 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(matching_char_pt)[0] != row: continue points.append(insert_pt) max_col = max([max_col, normed_rowcol(view, space_pt)[1]]) # The adjustment takes care of correcting point positions # since spaces are being inserted, which changes the points adjustment = 0 for pt in points: pt += adjustment length = max_col - normed_rowcol(view, pt)[1] adjustment += length if length >= 0: view.insert(edit, pt, ' ' * length) else: view.erase(edit, sublime.Region(pt + length, pt)) if settings.get('mid_line_tabs') and not use_spaces: adjustment += convert_to_mid_line_tabs(view, edit, tab_size, pt, length) # This handles aligning multiple selections else: max_col = max([normed_rowcol(view, region.b)[1] for region in sel]) for region in sel: length = max_col - normed_rowcol(view, region.b)[1] view.insert(edit, region.b, ' ' * length) if settings.get('mid_line_tabs') and not use_spaces: convert_to_mid_line_tabs(view, edit, tab_size, region.b, length) ================================================ FILE: Base File.sublime-settings ================================================ { // If the indent level of a multi-line selection should be aligned "align_indent": true, // If indentation is done via tabs, set this to true to also align // mid-line characters via tabs. This may cause alignment issues when // viewing the file in an editor with different tab width settings. This // will also cause multi-character operators to be left-aligned to the // first character in the operator instead of the character from the // "alignment_chars" setting. "mid_line_tabs": false, // 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": [ "+", "-", "&", "|", "<", ">", "!", "~", "%", "/", "*", "." ] } ================================================ FILE: CSS.sublime-settings ================================================ { "alignment_chars": ["=", ":"] } ================================================ FILE: Default (Linux).sublime-keymap ================================================ [ { "keys": ["ctrl+alt+a"], "command": "alignment" } ] ================================================ FILE: Default (OSX).sublime-keymap ================================================ [ { "keys": ["super+ctrl+a"], "command": "alignment" } ] ================================================ FILE: Default (Windows).sublime-keymap ================================================ [ { "keys": ["ctrl+alt+a"], "command": "alignment" } ] ================================================ FILE: Default.sublime-commands ================================================ [ { "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 – Default", "command": "open_file", "args": { "file": "${packages}/Alignment/Default (OSX).sublime-keymap", "platform": "OSX" } }, { "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" } } ] ================================================ FILE: JSON.sublime-settings ================================================ { "alignment_chars": ["=", ":"] } ================================================ FILE: Javascript.sublime-settings ================================================ { "alignment_chars": ["=", ":"] } ================================================ FILE: Main.sublime-menu ================================================ [ { "caption": "Preferences", "mnemonic": "n", "id": "preferences", "children": [ { "caption": "Package Settings", "mnemonic": "P", "id": "package-settings", "children": [ { "caption": "Alignment", "children": [ { "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": { "file": "${packages}/Alignment/Default (OSX).sublime-keymap", "platform": "OSX" }, "caption": "Key Bindings – Default" }, { "command": "open_file", "args": { "file": "${packages}/Alignment/Default (Linux).sublime-keymap", "platform": "Linux" }, "caption": "Key Bindings – Default" }, { "command": "open_file", "args": { "file": "${packages}/User/Default (Windows).sublime-keymap", "platform": "Windows" }, "caption": "Key Bindings – User" }, { "command": "open_file", "args": { "file": "${packages}/User/Default (OSX).sublime-keymap", "platform": "OSX" }, "caption": "Key Bindings – User" }, { "command": "open_file", "args": { "file": "${packages}/User/Default (Linux).sublime-keymap", "platform": "Linux" }, "caption": "Key Bindings – User" }, { "caption": "-" } ] } ] } ] } ] ================================================ FILE: messages/2.0.0.txt ================================================ Sublime Alignment 2.0.0 Changelog: - Removed the ctrl+shift+a shortcut on Windows/Linux and the cmd+shift+a shortcut on OS X to prevent conflict with the shortcut for Expand to Tag. The plugin is now triggered via ctrl+alt+a on Windows/Linux and ctrl+cmd+a on OS X. ================================================ FILE: messages/2.1.0.txt ================================================ Sublime Alignment 2.1.0 Changelog: - Added support for Sublime Text 3 thanks to kevinsperrine ================================================ FILE: messages.json ================================================ { "2.1.0": "messages/2.1.0.txt", "2.0.0": "messages/2.0.0.txt" } ================================================ FILE: readme.creole ================================================ = Sublime Alignment A simple key-binding for aligning multi-line and multiple selections in [[http://sublimetext.com/2|Sublime Text 2]]. Please see http://wbond.net/sublime_packages/alignment for install instructions, screenshots and documentation. == License All of Sublime Alignment is licensed under the MIT license. Copyright (c) 2011 Will Bond 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.