How to reformat a single line?


Hi,

I would like to reformat a single line or possibly multiple lines, similar to how Code->AutoIndentLines , except I don't want the caret to move down a line.

I tried using CodeStyleManager.adjustLineIndent(psifile,line) but that didn't do anything.

I tried using CodeStyleManager.reformatRange(psifile,lineOffsetBegin,linePlusOneOffsetBegin);
and that didn't do anything.

I also played with CodeStyleManager.isLineToBeIndented(psifile,line) but it always seems to return false no matter if the caret is on an improperly indented line or not.

Since there aren't any javadocs for com.intellij.psi.codeStyle, it's hard to figure out want some of the parameters or return values are.

-Alex

0
5 comments

I was able to get the reformatRange() command to work, but it required some hacks.
Given something like this:

The indentation is wrong lines 1-4.

I created an EditorAction ReformatLines bound to CTRLSHIFTI. ReformatLines calls CodeStyleManager.reformatRange() on the selection start and end offsets.

I experimented by selecting each of the lines 1-4 and involing ReformatLines

Line1: did nothing
Line2: reindented the line
Line3: reindented the line
Line4: reindented the line

For Line1, I then tried selecting the Line0 and Line1 and invoking ReformatLines. Still did nothing.

Then I selected Line0 through Line4 and invoked ReformatLines. Now all the lines are reindented including Line1.

So, I got the CodeStyleRange.reformatRange() to do something, but it looks like it requires the enclosing block to be included within the start and end offets in order for it to work. So you need to use Psi structure to create proper start and end offsets in order to make it work.

I still can't duplicate the functionality Code->Auto Indent Lines. If you select Line1 and invoke Code->Auto Indent Lines, then Line1 is properly indented.


0


Well,

I just figured out why CodeStyleManager.adjustLineIndent(psifile,line) doesn't work!

It takes an offset not a line number!

So, CodeStyleManager.adjustLineIndent(psifile,offset) will reindent the line containing offset.

Duh!

I hope Jetbrains gets around to putting op JavaDocs for com.intellij.psi package, because it sure would save alot of time by avoiding these misunderstandings of the paramters and return values.

-Alex

0
Avatar
Andrew Houghton

Out of curiosity, does reformatting work by finding the start of the block and reformatting based on that indent? I'm curious, 'cause what I really, really, really want in IDEA right now is emacs-style tab binding. The number of times I want tab to do anything other than properly indent my line is.. well, it's absurdly small.

Please tell me this is all working towards a plugin to do exactly that. :)

- a

0

The number of times I want tab to do anything other > than properly indent my line is.. well, it's absurdly > small.


I agree 1000%. IDEA's advanced refactoring and reformatting is super, but Emacs ccmode still beats em on basic editing IMO mostly because of the default TAB and BACKSPACE behavior.

I have an AutoNewline-0.1 mode working.

It overrides TAB to reindent-line and move caret to indent level.

It adds an AutoNewline mode same as CCMODE with
electric braces {} and semi-colon.
Pressing brace{} or semi-colon reindents line and
auto adds new-lines.

It is working well for me right now, but it only
works if you use hanging braces.

Maybe I should break out the electric TAB into
a separate plugin because it is more basic feature
like HungryBackSpace.



0
Avatar
Andrew Houghton

Cool.. it's great to hear I'm not the only one who wants this. :)

Re: splitting things into various plugins -- it seems to me you're reimplementing a number of emacs-style cc-mode behaviors. It makes sense that they'd all live in one plugin that simply provides the hooks for the functionality, without automatically binding them to any keys (I don't know if that's possible). Ideally, I'd love to see the ideaemacs.jar that implements ^K and ^Y for a kill-ring get folded into a plugin like yours.

Truthfully, this probably comes down to personal choice -- I'd tend to like having a single plugin that provides more functionality, while other people may like splitting that functionality across multiple plugins.

For what it's worth, I'd be more than happy to test this kind of stuff -- any chance of seeing a release soon(ish)?

0

Please sign in to leave a comment.