Is there a way for 'Smart Breaking' of code lines?
44 views (last 30 days)
Show older comments
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
Answers (1)
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
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.
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!