Auto-enforce matlab editor auto indentation?

조회 수: 4 (최근 30일)
Nathan Fitzgerald
Nathan Fitzgerald 2018년 5월 4일
댓글: Nathan Fitzgerald 2019년 7월 23일
I'm trying to build a system for enforcing coding standards on m-files in our code repository. To accomplish this, I'm creating a Matlab script that can run from the command line that will check all the files and report anomalies to the standard (exiting with exit(0) if everything is ok and with exit(1) if there are problems... will be part of a continuous integration runner).
One of the elements of our standard is that the mlint must be clean. This is pretty easily checked with Matlab's build-in checkcode function.
Another element of the standard is that indentation follows the recommendations of the Matlab editor smart indentation tool. This is proving harder to check programatically. I'd like to find a way to see if the standard indentation is being followed, i.e. if I were to run the smart indentation from the Matlab editor ( ctrl-a, ctrl-i ), would any part of the file change?
Are there ways to programatically run Matlab editor commands on m-files, something that could be used to recursively run smart indentation on all files in a folder structure?
Also, I'm barking up the wrong wrong tree entirely? Are there existing coding standard enforcement packages for Matlab (something like clang-tidy in c++) that I could be using instead?
  댓글 수: 2
Rian Koja
Rian Koja 2019년 7월 23일
By the way, did you suceed in your standard enforment endevour? Would you be able to share your script on the FileExchange repository?
Nathan Fitzgerald
Nathan Fitzgerald 2019년 7월 23일
I half succeeded. The warning detection based on checkcode works fine. The part about enforcing the editor autoindentation worked perfectly when running on my laptop. However, the whole point of the exercise was to run a script from the command line with the "--no-display" option in a continous integration environment linked to our Git repository. This could allow merge/pull requests to be denied if the files were not properly formatted. Unfortunately, the matlab.desktop.editor package is not loaded when matlab is run in "--no-display" mode (at least in 2015b), so my script did not run properly in the CI environment.
I don't think I can share the script outright, but it was basically (in psuedocode):
for file = cellArrayOfEveryFileInTheRepo
results = checkcode( file{1} );
if ~isempty(results)
exit(1);
end
end
exit(0);
with some extra stuff for printing the offending file info to the screen.

댓글을 달려면 로그인하십시오.

채택된 답변

Steven Lord
Steven Lord 2018년 5월 4일
There is an API that allows you to interact with documents in the Editor. For example, I have a file named example54.m in my current directory.
theDocument = matlab.desktop.editor.openDocument(fullfile(pwd, 'example54.m'));
smartIndentContents(theDocument);
save(theDocument);
close(theDocument);
See the help for the matlab.desktop.editor package for more information.
  댓글 수: 1
Nathan Fitzgerald
Nathan Fitzgerald 2018년 5월 4일
Thanks! That's exactly what I was looking for.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by