Feature Request: Refactoring tool

19 次查看(过去 30 天)
Nils G
Nils G 2016-11-21
I would like to have a real refactoring tool for MATLAB like the one e.g. in PyCharm. At the moment I'm using Grep if I'm renaming a Class/Method/Function.

回答(7 个)

Grivault
Grivault 2018-10-17
I agree, the lack of this refactoring capability is making Matlab IDE quite outdated face to other IDEs... As it seems to me impossible to develop Matlab with another IDE it makes Matlab also non-competitive and less attractive face to other languages (python, java, C# etc...) Missing syntactic coloration is already from another age, but since it's intrinsic to Matlab we can accept it. Refactoring though is the minima nowadays.

Adam
Adam 2019-4-2
This is still such a huge frustration all the way into R2019a. Even things that should be so simple can take on epic proportions when it comes to simple changes.
In one of my latest classes I didn't think ahead far enough and named some public functions things like
getData(...)
getScaledData(...)
getTrainingData(...)
Now, of course, a few weeks later I am incorporating a 2nd type of data that I didn't expect originally. So now I have to rename these functions to something more specific than just 'Data'. But they are public functions so I now have to search the whole repository with a Find All. This is not so bad for getTrainingData(...), I don't have many occurences of this, but I have numerous classes with a getData(...) function and loads of places that call it. The class/object of the call gives them their scope so the functions don't need more specific names often.
Except now when I come to have to do a rename and I have to sift through all those cases to make sure I only change those related to the class I am editing.
My design may not be great in terms of having these functions where they are, but isn't that the whole point of refactoring? You don't need to do perfect upfront design, you evolve it as appropriate!
This is one of the most basic tasks of refactoring too. I have no idea how easy or difficult is is for Matlab to link these in an interpreted language, but other languages seem to do it and even Matlab itself I have noticed has improved a lot in terms of its resolution if I 'Go to definition' on a function and it manages to take me to the relevant class.
  3 个评论
Adam
Adam 2019-4-8
Yeah, Ctrl + Shift + F is the method I've always used (it has been around as long as I remember). It helps, but still getting a list of 100+ matches when only 4 of them are related to the specific occurence I want to change makes refactoring a slow risky business!

请先登录,再进行评论。


Frank
Frank 2018-10-8
Dear Don,
Nils asked about refactoring and not how to rename a local variable. The lack of support for refactoring within Matlab is quite annoying.
-Frank

Keith Nguyen
Keith Nguyen 2018-11-9
This is true. If I want to rename a function (to give it an easy-to-understand name instead of test(ptcle1,ptcle2). I'll break my program. Is there a way to rename the function across all files in the same project/folder/workspace?
Other features such as: - Changing function signature also change it across all files. - Moving lines up and down using shortcut. - Syntax highlighting.

Tom Pickering
Tom Pickering 2020-9-23
Hi Nils,
if you rename the function name in the function declaration, a tooltip opens. The tooltip indicates that MATLAB will rename all instances of the function in the file when you press Shift + Enter.
Please also check out the Documentation for more info on Find and Replace in Files.
In addition to this functioncality in both the classic Editor and the Live Editor, the Live Editor also has some additional refactoring features. If you have an existing large live script or function, you can break it into smaller pieces by automatically converting selected areas of code into functions or local functions. Select one or more lines of code and on the Live Editor tab, in the Code section, click Refactor. Then, select from the available options. MATLAB creates a function with the selected code and replaces the original code with a call to the newly created function.
In Projects, when renaming a function, a dependency analysis is performed, notifying you which files and lines are impacted before you make a change.
  3 个评论
Jared MacDonald
Jared MacDonald 2020-11-10
Hi Vasco, this is what we are focused on (migrating features from Live Editor to be available for your .M files) and is planned for a near upcoming release.
Thanks,
Jared
Walter Roberson
Walter Roberson 2021-9-23
As of R2021b, released today, the old editor is retired and the same editor used for Livescript is used for .m files.
... Unfortunately this does mean that the Fix options that used to be available are mostly gone :(

请先登录,再进行评论。


Don Zheng
Don Zheng 2016-12-7
Hi Nils,
Please find the tutorial below for code refactoring in MATLAB Editor:
-Don
  2 个评论
Luke M
Luke M 2018-8-7
编辑:Luke M 2018-8-7
I know this is a fairly old question, but I haven't found anything more recent. I think what Nils is looking for is a tool to refactor a project when renaming, as they said, a Class, Method or Function. The tutorial presented is great when working in a single file, but what about when working on a project with many files?
Utku Kaya
Utku Kaya 2021-10-26
编辑:Utku Kaya 2021-10-26
Agreed. Refactoring still does not work for Classes.

请先登录,再进行评论。


Michael W
Michael W 2021-9-23
It seems some added functionality is now available in the editor since release R2021b:
(from the R2021b release notes)
"Break large scripts or functions into smaller pieces by converting selected code into functions in files or local functions. With one or more lines of code selected, on the Editor tab, in the Code section, click the Refactor button, and then select from the available options. MATLAB creates a function with the selected code and replaces the original code with a call to the newly created function."

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by