Sublime Forum

Multi-Selection Alignment

#6

I was thinking of adding a mode for a single multi-line selection that would indent all of the lines to the same level. It sounds like this is what you are looking for, correct?

For your example of failure, where are your cursors placed?

0 Likes

#7

Correct!

Beginning of each identifier.

0 Likes

#8

@jbrooksuk

I’ve updated the plugin to be a bit more powerful. I believe the issue you were having with alignment is that the plugin was intended for aligning things like equal signs in the middle of lines, do it always inserted spaces. The way that sublime measures columns of characters, only a single space would have been inserted if you use tabs for indenting.

That said, I’ve updated the plugin to do what I think you were intending to do, which is to align the left edge of each line to the same indent level. If you have a multi-line selection, the plugin will cause all lines to be indented to the same level. As a bonus, if you run it a second time, the first set of equal signs with be aligned using spaces.

The original functionality for multiple selections still exists and functions that same way.

If any of what I described is confusing, please see the readme for examples.

0 Likes

#9

New update:

The multi-line selection mode now properly handles equal signs that do not have a space before them. Equal signs are aligned to the left-most column possible while leaving at least one space to the left of each equal sign/operator.

github.com/wbond/sublime_alignm … all/master

0 Likes

#10

It would be useful for javascript to check not only for ‘=’ but for ‘:’ too. The equal sign could have precedence.

Nice Plugin

0 Likes

#11

I love that plugin!
I really like having my "="s aligned.
As senzo said, “:” alignment would be great for JavaScript and JSON.

0 Likes

#12

A quick modification for “:” alignment

Replace

equal_pt = view.find('=', pt).a

with

import re
if re.search("=", view.substr(view.line(pt))):
  equal_pt = view.find('=', pt).a
elif re.search(":", view.substr(view.line(pt))):
  equal_pt = view.find(':', pt).a
0 Likes

#13

I made it work with this syntax below:

var test = "sadsad", asd = "Asdasd", asdasdasd = "asdasd";

Buy commenting out this part of the code:

# 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)

0 Likes

#14

[quote=“rdougan”]I made it work with this syntax below:

var test = "sadsad", asd = "Asdasd", asdasdasd = "asdasd";

Buy commenting out this part of the code:

# 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)[/quote]

You don’t use this plugin the way it was designed, take a look at the documentation from wbond: https://github.com/wbond/sublime_alignment
The code you commented remove the left alignment and only keep the alignment of “=”, what you probably want.
This plugin must probably be splitted in 2 commands, one for the left alignment and one for the “=” (or “:”) alignment.

0 Likes

#15

I should hopefully be able to add some options for alignment characters and disabling left alignment in the next day or so.

Hopefully I will also figure out how to create a .sublime-package file for those who are so inclined.

0 Likes

#16

I’ve got this error under Ubuntu 11.04 x86_64 kernel 2.6.38-10-generic

Traceback (most recent call last): File "./sublime_plugin.py", line 255, in run_ File "./Alignment.py", line 65, in run AttributeError: 'NoneType' object has no attribute 'a'

0 Likes

#17

Over the past few days I added settings to the plugin for more customization. I also fixed some bugs.

The best way to get the newest version, and to automatically stay up to date with the latest version is to use the Package Control package manager I’ve been working on. Once you manually update Alignment through Package Control, it will automatically check for and install updates whenever Sublime Text 2 starts. You can learn more at wbond.net/sublime_packages/package_control.

@rdougan

The newest version allows disabling indentation alignment on a per-filetype basis.

@xavi

The newest version fixed that bug.

0 Likes

#18

@wbond: thanks for this awesome plugin, my code never looked that good!
There is a way of changing space indent to tab?

Thanks!

0 Likes

#19

@iamntz

The left indent should automatically use tabs or spaces based on the current settings. In terms of mid-line indenting, unfortunately I was not able to figure out last night how to measure the actual displayed width of a tab character via the API. I can get the row and column, but the column is just the number of characters from the beginning of the line. I know ST2 can get the real column number since it is displayed in the status bar, I just couldn’t find a method to translate to that. Without being able to tell how wide each tab is being displayed, I wouldn’t be able to do mid-line tab spacing.

0 Likes

#20

To get the ‘visual’ column of a point, you could replace

view.rowcol(pt)

by

import indentation indentation.line_and_normed_pt(view, pt)
Row is always the same for both.

An addition to the API for it would be nice.

Thanks for your work wbond.

0 Likes

#21

Thanks guys! I’ll work on trying to get mid-line tab alignment done tonight.

0 Likes

#22

I’ve updated to the latest and it doesn’t seem to work at all for me (JavaScript):

var tabBar = me.getTabBar(), initialConfig = card.getInitialConfig(), tabConfig = initialConfig.tab || {}, tabTitle = initialConfig.title, tabIconCls = initialConfig.iconCls, index = card.getIndex(), tabs = tabBar.getItems(), cards = me.getInnerItems(), currentTabInstance = (tabs.length >= cards.length) && tabs.getAt(index), tabInstance;

0 Likes

#23

@rdougan

I just copied and pasted that code into my editor and it all seemed to align properly. Are there any errors in the console? Is anything happening at all? What platform are you running ST2 on, and what build of ST2 are you using?

0 Likes

#24

@wbond

For some reason it is now working. It may have been an issue with another plugin I had. There were no errors in the console.

Thanks for adding the align_indent setting! :smile:

0 Likes

#25

[quote=“iamntz”]@wbond: thanks for this awesome plugin, my code never looked that good!
There is a way of changing space indent to tab? [/quote]

I just released version 1.1.0 which includes the setting mid_line_tabs. When enabled, mid-line indentation will be done via tabs if indentation is set to tabs. This could have some side effects when working with editors that have a different tab width, and changes the way in which multi-character operators are indented.

The original post has been updated with the full changelog.

0 Likes