Is there a way for 'Smart Breaking' of code lines?

30 views (last 30 days)
mahoromax
mahoromax on 16 Jan 2020
Edited: mahoromax on 17 Jan 2020
I am transforming some scripts to use other functions or variables. The function calls are rather long and are therefore split into multiple lines.
To have an easier time transforming them and to not hinder regexp with newlines I do remove those, then swap around variables and exchange other expressions.
I end up with a working long line of code, which I want to again split into multiple lines with (...) after the frist commata that exceeds some "default" code length (~45chars?).
a) Is there already any such function (similar to SmartIndent) ?
b) If I had to program it myself, I would start by finding the whole command (until first semicolon ';'). Then I would cut off the first 45 chars and chain it with the regexp-match until the first commata ',' (which I also cut off the original command). I add the '...' and newline chars. Repeat this as long (while) the original command char array's size is still > 45). Then I can replace the original command with the new command in my text-/.m-file.
Any ideas to improve this? Is there a way to call smartindent as a function to run over the code?
I do realize that reformatting the code manually would improve readability a lot more, but at this point it is more about automatisation.
  2 Comments
dpb
dpb on 16 Jan 2020
Probably using EMACS or another smarter editor would be the simpler route...
mahoromax
mahoromax on 17 Jan 2020
Edited: mahoromax on 17 Jan 2020
I only did a quick check on EMACS. I only found the AutoFill, which works on spaces, and basically does what smarIndent does. But it doesnt mention a feature to insert "programming language specific break chars" = ... ?
I imagine that these options (maybe the adaptive fill) can be modified, but would that really be easier than doing it from scratch on a platform that I'm more familiar with?
I guess it might be worth If I find a major/minor (dont know the difference yet) mode package that already implements matlab style "sentences".

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 17 Jan 2020
Edited: Image Analyst on 17 Jan 2020
Do you want to split these lines into multiple lines manually, or automatically via a script? You can split any line apart as long as the split is not in the middle of a string or character array or a number. So just count the number of ' and " and once you have an even number, and are not in a variable name, you can insert a ... and a line feed. You might need to handle pathological cases (like a single " inside a pair of single quotes) individually but you might not have any cases like that. It might be best to look for commas and closing brackets, braces, or parentheses and put the ... there.
  3 Comments
Image Analyst
Image Analyst on 17 Jan 2020
Edited: Image Analyst on 17 Jan 2020
So you'll want fopen(), a while loop, fgetl(), length(), strfind(), sprintf(), fprintf(), and fclose(), probably in that order.. Did you try to use those? Let's see what you have so far.
mahoromax
mahoromax on 17 Jan 2020
Edited: mahoromax on 17 Jan 2020
I did not yet start coding on this (only some preceding thoughs).
I was hoping to get pointed to a more elegant solution.
In case nothing comes up I will try it and post results as an answer here.

Sign in to comment.

Categories

Find more on Characters and Strings in Help Center and File Exchange

Tags

Products


Release

R2018b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!