Author Topic: How do I modify the current file and move { two spaces left  (Read 5376 times)

ldbader

  • Community Member
  • Posts: 8
  • Hero Points: 0
How do I modify the current file and move { two spaces left
« on: February 12, 2010, 04:58:14 PM »
I joined a company that uses the strangest syntax formatting style I have ever seen and I need my Slickedit to help me compensate.

They use a style that is halfway between style 2 and style 3.

Style 2:
if ()
{
    ++i;
}

Style 3:
if ()
    {
    ++i;
    }

Our style:
if ()
  {
    ++i;
  }

The braces are indented two spaces instead of the full syntax indent.

I have modified the maybe_insert_braces() function in c.e so syntax expansion correctly places the braces for our style when the user selects style 3, but the beautify function will force it back to the standard Style 3 format.

I want to write a function that invokes the standard beautify and then makes a second pass on the file.  For every brace on an otherwise empty line where the brace is in a column greater than or equal to the syntax indent, I want to move the brace to (current column - syntax indent + 2).

I was hoping to find a coding example in the current beautify function, but that function goes off into some language dependent method using a technique that I can't follow.

What should I do?

Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: How do I modify the current file and move { two spaces left
« Reply #1 on: February 12, 2010, 11:53:02 PM »
You might be able to use an external formatter like uncrustify.  I haven't used it but it has a lot of indenting options.
http://sourceforge.net/projects/uncrustify/
See also universal gui
http://sourceforge.net/projects/universalindent/

To use uncrustify, you might be able to use the filter_selection command and call uncrustify. Quote from Lee
Quote
It makes a temporary file of the current selection as input file and output is redirected to another temp file (cmd < in.tmp > out.tmp), and then the output temp file is read into the current selection, clean-up of temp files is done automatically
However, you probably just want to save the current file, call uncrustify, then reload the current file.

If you still want to write your own slick macro but aren't familiar enough with slickc, try writing it in pseudo-code in more detail and post it back here.  I can't quite follow the algorithm you're suggesting.  How about - for every opening brace on an otherwise empty line, align it to the column position equal to the first non blank character on the previous line, plus 2  - then align the closing brace to the opening brace.

Graeme

ldbader

  • Community Member
  • Posts: 8
  • Hero Points: 0
Re: How do I modify the current file and move { two spaces left
« Reply #2 on: February 15, 2010, 06:10:49 PM »
I like your algorithm better than mine but I am a Slick C novice.  I can create and edit keyboard macros, but I don't have much experience with most of the functions.

I know a way to find an open brace on a line where the only other characters are tabs or spaces using the find() function and regular expressions.

I know how to slide the open brace to the correct position.
  go up,
  find first non-blank,
  go down,
  go right,
  go right,
  character mark,
  find open brace again,
  go left,
  character mark,
  delete selection.

I know how to find the matching closing brace but I don't know a good way to align it.

I don't know how to repeat for the whole file, quitting when the last open brace has been found.



Graeme

  • Senior Community Member
  • Posts: 2793
  • Hero Points: 347
Re: How do I modify the current file and move { two spaces left
« Reply #3 on: February 16, 2010, 11:15:53 AM »
You could try this.  It basically works but I've only tested it very briefly so you should check the logic and see if it covers everything and test it well.  One thing it doesn't cope with is a comment on the same line as the { or }
Probably if you want to allow trailing comments you could replace the line with the original line with the appropriate number of characters removed from the start using substr() but you also have to change the code that looks for {} on a line by themselves  (maybe use search() with XC to look for non blanks that aren't in comments - or search for {} preceded only by whitespace)

say() is a useful debugging function.  Another thing you can do is record a macro and inspect the code that gets generated - but note that if you do a cursor_right, you'll see a call to cursor_right() which is not the same as calling the core right() function as I do below.  Also, I'd recommend having backup history enabled so you can recover from any disasters and save the file before beautifying.

Code: [Select]
_command void move_paren() name_info(','VSARG2_REQUIRES_EDITORCTL)
{
   int col1, col2, line1, line2;
   int max = 0;
   typeless pos1, pos2;
   _str line;
   _save_pos2(pos1);
   top();
   for (;;) {
      if (++max > 10)   // increase to 100000 or so some time
         break;  // don't loop forever!
      right();
      if (search('{','@xcs') != 0)
         break;
      _save_pos2(pos2);
      get_line(line);
      if (strip(line) != '{') {
         // there are other non whitespace characters on this line
         continue;
      }
      col1 = p_col;
      line1 = p_line;
      if (up() != 0)
         continue;  // this is the first line
      _begin_line(); // go to col 1
      //say(p_col);
      // search for first non whitespace
      if (search('[~ \t]','r@') != 0)
         break;
      //say('k' :+ p_col);
      if (p_line >= line1) {
         _restore_pos2(pos2);
         continue;
      }
      col2 = p_col + 2; // the column we want { to be on
      if (col2 >= col1) {
         _restore_pos2(pos2);
         continue;
      }
      if (down() != 0)
         break;
      // double check this line has only {
      get_line(line);
      if (strip(line) != '{') {
         // there are other non whitespace characters on this line
         continue;
      }
      // check what happens if col2 is 1
      replace_line(field('',col2-1) :+ '{');
      p_line = line1;   // make sure we're on the correct line
      _begin_line(); // go to col 1
      // find the { again
      if (search('{','@xcs') != 0)
         break;
      _save_pos2(pos2);
      if (find_matching_paren() != 0)
         break;
      get_line(line);
      if (strip(line) != '}') {
         // there are other non whitespace characters on this line
         continue;
      }
      // check what happens if col2 is 1
      replace_line(field('',col2-1) :+ '}');
      _restore_pos2(pos2);    // go back to {
      // and do it again
   }
   _restore_pos2(pos1);
}

Graeme
« Last Edit: February 16, 2010, 11:20:29 AM by Graeme »

jporkkahtc

  • Senior Community Member
  • Posts: 2620
  • Hero Points: 210
  • Text
Re: How do I modify the current file and move { two spaces left
« Reply #4 on: April 11, 2016, 05:25:14 PM »
Does the new beautifier system allow for this?
Half-indent braces?
Weird yes, but annoying when you have to deal with old code like this and you shouldn't reformat it.




Code: [Select]
int f()
{
··printf("First line\n"); // 2
··if (something)
····{ // 4
······printf("\n"); // 6
······printf("more\n");
····} // 4
··return 0; // 2
}



patrick

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1818
  • Hero Points: 151
Re: How do I modify the current file and move { two spaces left
« Reply #5 on: April 11, 2016, 07:49:40 PM »
Well, if it's really a syntax indent of 4 and only the braces are half indented, the beautifier doesn't support that.

If you can look at it as the GNU brace style, with a syntax indent of 2, then the beautifier can do it, but you still have to change a setting that wasn't exposed in the UI, because there's no corresponding support of that brace style in the editing code.  So not that helpful unless you're just beautifying code.

jporkkahtc

  • Senior Community Member
  • Posts: 2620
  • Hero Points: 210
  • Text
Re: How do I modify the current file and move { two spaces left
« Reply #6 on: April 11, 2016, 09:09:38 PM »
It is GNU.
Can you be more specific about that non-UI exposed setting?

patrick

  • SlickEdit Team Member
  • Senior Community Member
  • *
  • Posts: 1818
  • Hero Points: 151
Re: How do I modify the current file and move { two spaces left
« Reply #7 on: April 11, 2016, 09:42:13 PM »
Sure.  You have to set your statement brace styles to "Next Line Indented", and then edit the vusr_beautifier.xml that's in your configuration directory.   Search for your profile name (be sure to pay attention to the lang attribute of the "profile" tags).  You're looking for the "indent_from_brace" setting.  Change the "v" attribute from 0 to 1, and save it.  Once it's changed, it should stay changed - editing your profile shouldn't clobber it.