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

35 次查看(过去 30 天)
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 个评论
dpb
dpb 2020-1-16
Probably using EMACS or another smarter editor would be the simpler route...
mahoromax
mahoromax 2020-1-17
编辑:mahoromax 2020-1-17
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".

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2020-1-17
编辑:Image Analyst 2020-1-17
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 个评论
Image Analyst
Image Analyst 2020-1-17
编辑:Image Analyst 2020-1-17
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 2020-1-17
编辑:mahoromax 2020-1-17
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

标签

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by