-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathrun_indent_code.m
112 lines (89 loc) · 2.46 KB
/
run_indent_code.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
function run_indent_code()
% Auto indent all the source code.
%
% Find all the MATLAB files.
% Correct the indentation.
%
% (c) 2019-2020, ETH Zurich, Power Electronic Systems Laboratory, T. Guillod
% path of the root of the project
folder = '../..';
% find the MATLAB files
file_list = get_matlab_file(folder);
% find the MATLAB files
get_matlab_indent(file_list);
end
function file_list = get_matlab_file(folder)
% Find all the MATLAB file in a folder (recursive).
%
% Parameters:
% folder (str): folder to parse (including subfolders)
%
% Returns:
% file_list (cell): cell with the MATLAB files
% init the list
file_list = {};
% get the MATLAB file (at the root, not in the subfolders)
file_list_tmp = get_matlab_file_sub(folder);
file_list = [file_list file_list_tmp];
% get the subfolders and apply recursively
folder_list = get_m_folder(folder);
for i=1:length(folder_list)
file_list_tmp = get_matlab_file(folder_list{i});
file_list = [file_list file_list_tmp];
end
end
function file_list = get_matlab_file_sub(folder)
% Find all the MATLAB file in a folder (non-recursive).
%
% Parameters:
% folder (str): folder to parse (without subfolders)
%
% Returns:
% file_list (cell): cell with the MATLAB files
data = dir(fullfile(folder, '*.m'));
file_list = {};
for i=1:length(data)
file_list{end+1} = [data(i).folder filesep() data(i).name];
end
end
function folder_list = get_m_folder(folder)
% Find the subfolders on a folder.
%
% Parameters:
% folder (str): folder to parse
%
% Returns:
% folder_list (cell): cell with the subfolders
% find all the elements
data = dir(fullfile(folder, '*'));
% keep only the subfolders
folder_list = {};
for i=1:length(data)
if data(i).isdir==true
% ignore the hidden folder (and the current and parent folder)
if isempty(regexp(data(i).name, '^\.*', 'ONCE'))
folder_list{end+1} = [data(i).folder filesep() data(i).name];
end
end
end
end
function get_matlab_indent(file_list)
% Indent a list of MATLAB files.
%
% Parameters:
% file_list (cell): cell with the files to indent
for i=1:length(file_list)
fprintf('%d / %d / %s\n', i, length(file_list), file_list{i})
indent_file(file_list{i})
end
end
function indent_file(filename)
% Indent a MATLAB file.
%
% Parameters:
% filename (str): MATLAB file to indent
h = matlab.desktop.editor.openDocument(filename);
h.smartIndentContents();
h.save();
h.close();
end