-
Notifications
You must be signed in to change notification settings - Fork 5
/
MBeautify.m
459 lines (346 loc) · 22.5 KB
/
MBeautify.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
classdef MBeautify
% Provides static methods to perform code formatting targeting file(s), the currently active editor page or the
% current selection in editor.
% The rules of the formatting are defined in the "MBeautyConfigurationRules.xml" in the resources directory. This
% file can be modified to affect the formatting.
% Important: Runtime, the M equivalent of this XML file is used, which is created whenever the "setup" static
% method is called, therefore always call this method if the XML file has been modified.
% To restore the XML file to the default configuration, the "createDefaultConfiguration" static method can be
% called.
%
% Example usage:
%
% MBeautify.setup(); % Creates the default rules
% MBeautify.formatCurrentEditorPage(); % Formats the current page in editor without saving
% MBeautify.formatCurrentEditorPage(); % Formats the current page in editor with saving
% MBeautify.formatFile('D:\testFile.m', 'D:\testFileNew.m'); % Formats the first file into the second file
% MBeautify.formatFile('D:\testFile.m', 'D:\testFile.m'); % Formats the first file in-place
% MBeautify.formatFiles('D:\mydir', '*.m'); % Formats all files in the specified diretory in-place
properties(Access = private, Constant)
RulesXMLFile = 'MBeautyConfigurationRules.xml';
RulesMFile = 'MBeautyConfigurationRules.m'
SettingDirectory = [fileparts(mfilename('fullpath')), filesep, 'resources', filesep, 'settings'];
RulesMFileFull = [fileparts(mfilename('fullpath')), filesep, 'resources', filesep, 'settings', filesep, 'MBeautyConfigurationRules.m'];
RulesXMLFileFull = [fileparts(mfilename('fullpath')), filesep, 'resources', filesep, 'settings', filesep, 'MBeautyConfigurationRules.xml'];
end
%% Public API
methods(Static = true)
function setup()
% MBeautify.setup() initializes MBeautifier for first use and usable to update the formatting configuration.
% It optionally writes the default settings XML file, reads in the settings XML file and then writes the
% configuration M-file which will be used in runtime.
if ~exist(MBeautify.RulesXMLFileFull, 'file')
MBeautify.writeSettingsXML();
end
MBeautify.writeConfigurationFile(MBeautify.readSettingsXML());
fprintf('Configuration was successfully exported to:\n%s\n', MBeautify.RulesMFileFull);
MBeautify.parsingUpToDate(false);
end
function createDefaultConfiguration()
% Writes the default configuration XML file.
MBeautify.writeSettingsXML();
end
function formatFile(file, outFile)
% Formats the file specified in the first argument. The file is opened in the Matlab Editor. If the second
% argument is also specified, the formatted source is saved to this file. Otherwise the formatted input
% file remains opened in the Matlab Editor. The input and the output file can be the same.
if ~exist(file, 'file')
return;
end
document = matlab.desktop.editor.openDocument(file);
% Format the code
formatter = MFormatter(MBeautify.getConfigurationStruct());
document.Text = formatter.performFormatting(document.Text);
document.smartIndentContents();
if nargin >= 2
if exist(outFile, 'file')
fileattrib(outFile, '+w');
end
document.saveAs(outFile)
document.close();
end
end
function formatFiles(directory, fileFilter)
% Formats the files in-place (files are overwritten) in the specified directory, collected by the specified filter.
% The file filter is a wildcard expression used by the dir command.
files = dir(fullfile(directory, fileFilter));
for iF = 1:numel(files)
file = fullfile(directory, files(iF).name);
MBeautify.formatFile(file, file);
end
end
function formatEditorSelection(doSave)
% Performs formatting on selection of the currently active Matlab Editor page.
% The selection is automatically extended until the first empty line above and below.
% This method can be useful for large files, but using "formatCurrentEditorPage" is always suggested.
% Optionally saves the file (if it is possible) and it is forced on the first argument (true). By default
% the file is not saved.
currentEditorPage = matlab.desktop.editor.getActive();
if isempty(currentEditorPage)
return;
end
currentSelection = currentEditorPage.Selection;
if isempty(currentEditorPage.SelectedText)
return;
end
if nargin == 0
doSave = false;
end
% Expand the selection from the beginnig of the first line to the end of the last line
expandedSelection = [currentSelection(1), 1, currentSelection(3), Inf];
% Search for the first empty line before the selection
if currentSelection(1) > 1
lineBeforePosition = [currentSelection(1) - 1, 1, currentSelection(1) - 1, Inf];
currentEditorPage.Selection = lineBeforePosition;
lineBeforeText = currentEditorPage.SelectedText;
while lineBeforePosition(1) > 1 && ~isempty(strtrim(lineBeforeText))
lineBeforePosition = [lineBeforePosition(1) - 1, 1, lineBeforePosition(1) - 1, Inf];
currentEditorPage.Selection = lineBeforePosition;
lineBeforeText = currentEditorPage.SelectedText;
end
end
expandedSelection = [lineBeforePosition(1), 1, expandedSelection(3), Inf];
% Search for the first empty line after the selection
lineAfterSelection = [currentSelection(3) + 1, 1, currentSelection(3) + 1, Inf];
currentEditorPage.Selection = lineAfterSelection;
lineAfterText = currentEditorPage.SelectedText;
beforeselect = currentSelection(1);
while ~isequal(lineAfterSelection(1), beforeselect) && ~isempty(strtrim(lineAfterText))
beforeselect = lineAfterSelection(1);
lineAfterSelection = [lineAfterSelection(1) + 1, 1, lineAfterSelection(1) + 1, Inf];
currentEditorPage.Selection = lineAfterSelection;
lineAfterText = currentEditorPage.SelectedText;
end
endReached = isequal(lineAfterSelection(1), currentSelection(1));
expandedSelection = [expandedSelection(1), 1, lineAfterSelection(3), Inf];
if isequal(currentSelection(1), 1)
codeBefore = '';
else
codeBeforeSelection = [1, 1, expandedSelection(1), Inf];
currentEditorPage.Selection = codeBeforeSelection;
codeBefore = currentEditorPage.SelectedText;
end
if endReached
codeAfter = '';
else
codeAfterSelection = [expandedSelection(3) + 1, 1, Inf, Inf];
currentEditorPage.Selection = codeAfterSelection;
codeAfter = currentEditorPage.SelectedText;
end
currentEditorPage.Selection = expandedSelection;
codeToFormat = currentEditorPage.SelectedText;
selectedPosition = currentEditorPage.Selection;
% Format the code
formatter = MFormatter(MBeautify.getConfigurationStruct());
formattedSource = formatter.performFormatting(codeToFormat);
% Save back the modified data then use Matlab samrt indent functionality
% Set back the selection
currentEditorPage.Text = [codeBefore, formattedSource, codeAfter];
if ~isempty(selectedPosition)
currentEditorPage.goToLine(selectedPosition(1));
end
currentEditorPage.smartIndentContents();
currentEditorPage.makeActive();
% Save if it is possible
if doSave
fileName = currentEditorPage.Filename;
if exist(fileName, 'file') && numel(fileparts(fileName))
fileattrib(fileName, '+w');
currentEditorPage.saveAs(currentEditorPage.Filename)
end
end
end
function formatCurrentEditorPage(doSave)
% Performs formatting on the currently active Matlab Editor page.
% Optionally saves the file (if it is possible) and it is forced on the first argument (true). By default
% the file is not saved.
currentEditorPage = matlab.desktop.editor.getActive();
if isempty(currentEditorPage)
return;
end
if nargin == 0
doSave = false;
end
selectedPosition = currentEditorPage.Selection;
% Format the code
formatter = MFormatter(MBeautify.getConfigurationStruct());
currentEditorPage.Text = formatter.performFormatting(currentEditorPage.Text);
% Set back the selection
if ~isempty(selectedPosition)
currentEditorPage.goToLine(selectedPosition(1));
end
% Use Smart Indent
currentEditorPage.smartIndentContents();
currentEditorPage.makeActive();
% Save if it is possible
if doSave
fileName = currentEditorPage.Filename;
if exist(fileName, 'file') && numel(fileparts(fileName))
fileattrib(fileName, '+w');
currentEditorPage.saveAs(currentEditorPage.Filename)
end
end
end
end
%% Private helpers
methods(Static = true, Access = private)
% Method to mimic a static data member
% Indicates that the token parsing is up to date or the rules file should be reparsed
function val = parsingUpToDate(val)
persistent currentval;
if isempty(currentval)
currentval = true;
end
if nargin >= 1
currentval = val;
end
val = currentval;
end
function configurationStruct = getConfigurationStruct()
% MBeautify.getConfigurationStruct returns the configuration struct from the rules file
% Persistent variable to store the returned rules
% If MBeautify.setup was not called, the stored struct should be returned
persistent configurationStructStored;
if isempty(configurationStructStored) || ~MBeautify.parsingUpToDate()
currCD = cd();
cd(MBeautify.SettingDirectory);
configurationStruct = eval(MBeautify.RulesMFile(1:end - 2));
cd(currCD)
configurationStructStored = configurationStruct;
MBeautify.parsingUpToDate(true);
else
configurationStruct = configurationStructStored;
end
end
function writeConfigurationFile(resStruct)
% MBeautify.writeConfigurationFile creates the configuration M file from the structure of the configuration XML file.
operetorRules = resStruct.OperatorRules;
opFields = fields(operetorRules);
[pathOfMFile, nameOfMFile] = fileparts(MBeautify.RulesMFileFull); %#ok<ASGLU>
settingMFileString = ['function this = ', nameOfMFile, '()', sprintf('\n'), ...
'this = struct();', sprintf('\n'), sprintf('\n')];
settingMFileString = [settingMFileString, 'this.OperatorRules = struct();', sprintf('\n'), sprintf('\n')];
for iOp = 1:numel(opFields)
settingMFileString = [settingMFileString, sprintf('\n')];
settingMFileString = [settingMFileString, ['this.OperatorRules.', opFields{iOp}, ' = struct();'], sprintf('\n')];
valueFrom = regexptranslate('escape', operetorRules.(opFields{iOp}).ValueFrom);
valueTo = regexptranslate('escape', operetorRules.(opFields{iOp}).ValueTo);
settingMFileString = [settingMFileString, ['this.OperatorRules.', opFields{iOp}, '.ValueFrom = ''', valueFrom, ''';'], sprintf('\n')];
settingMFileString = [settingMFileString, ['this.OperatorRules.', opFields{iOp}, '.ValueTo = ''', valueTo, ''';'], sprintf('\n')];
end
settingMFileString = [settingMFileString, 'this.SpecialRules = struct();', sprintf('\n'), sprintf('\n')];
specialRules = resStruct.SpecialRules;
spFields = fields(specialRules);
for iSp = 1:numel(spFields)
settingMFileString = [settingMFileString, sprintf('\n')]; %#ok<*AGROW>
settingMFileString = [settingMFileString, ['this.SpecialRules.', spFields{iSp}, ' = struct();'], sprintf('\n')];
settingMFileString = [settingMFileString, ['this.SpecialRules.', spFields{iSp}, 'Value = ''', specialRules.(spFields{iSp}).Value, ''';'], sprintf('\n')];
end
settingMFileString = [settingMFileString, 'end'];
if exist(MBeautify.RulesMFileFull, 'file')
fileattrib(MBeautify.RulesMFileFull, '+w');
end
fid = fopen(MBeautify.RulesMFileFull, 'w');
fwrite(fid, settingMFileString);
fclose(fid);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Writes the default settings XML file
function writeSettingsXML()
% MBeautify.writeSettingsXML creates the default configuration XML structure to the configuration XML file.
docNode = com.mathworks.xml.XMLUtils.createDocument('MBeautifyRuleConfiguration');
docRootNode = docNode.getDocumentElement();
operatorPaddings = docNode.createElement('OperatorPadding');
docRootNode.appendChild(operatorPaddings);
specialRules = docNode.createElement('SpecialRules');
docRootNode.appendChild(specialRules);
%% Add operator rules
operatorPaddings = appendOperatorPaddingRule('ShortCircuitAnd', '&&', ' && ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('ShortCircuitOr', '||', ' || ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('LogicalAnd', '&', ' & ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('LogicalOr', '|', ' | ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('LessEquals', '<=', ' <= ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('Less', '<', ' < ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('GreaterEquals', '>=', ' >= ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('Greater', '>', ' > ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('Equals', '==', ' == ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('NotEquals', '~=', ' ~= ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('Assignment', '=', ' = ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('Plus', '+', ' + ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('Minus', '-', ' - ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('ElementWiseMultiplication', '.*', ' .* ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('Multiplication', '*', ' * ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('RightArrayDivision', './', ' ./ ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('LeftArrayDivision', '.\', ' .\ ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('Division', '/', ' / ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('LeftDivision', '\', ' \ ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('ElementWisePower', '.^', '.^', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('Power', '^', '^', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('Not', '~', ' ~', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('Comma', ',', ', ', operatorPaddings, docNode);
operatorPaddings = appendOperatorPaddingRule('SemiColon', ';', '; ', operatorPaddings, docNode);
appendOperatorPaddingRule('Colon', ':', ':', operatorPaddings, docNode);
%% Add special rules
specialRules = appendSpecialRule('MaximalNewLines', '2', specialRules, docNode);
specialRules = appendSpecialRule('AddCommasToMatrices', '1', specialRules, docNode);
specialRules = appendSpecialRule('AddCommasToCellArrays', '1', specialRules, docNode);
specialRules = appendSpecialRule('CellArrayIndexing_ArithmeticOperatorPadding', '0', specialRules, docNode);
appendSpecialRule('MatrixIndexing_ArithmeticOperatorPadding', '0', specialRules, docNode);
xmlwrite(MBeautify.RulesXMLFileFull, docNode);
fprintf('Default configuration XML has been created:\n%s\n', MBeautify.RulesXMLFileFull);
function operatorPaddings = appendOperatorPaddingRule(key, valueFrom, valueTo, operatorPaddings, docNode)
opPaddingRule = docNode.createElement('OperatorPaddingRule');
keyElement = docNode.createElement('Key');
keyElement.appendChild(docNode.createTextNode(key));
valueFromElement = docNode.createElement('ValueFrom');
valueFromElement.appendChild(docNode.createTextNode(valueFrom));
valueToElement = docNode.createElement('ValueTo');
valueToElement.appendChild(docNode.createTextNode(valueTo));
opPaddingRule.appendChild(keyElement);
opPaddingRule.appendChild(valueFromElement);
opPaddingRule.appendChild(valueToElement);
operatorPaddings.appendChild(opPaddingRule);
end
function specialRules = appendSpecialRule(key, value, specialRules, docNode)
specialRule = docNode.createElement('SpecialRule');
keyElement = docNode.createElement('Key');
keyElement.appendChild(docNode.createTextNode(key));
valueElement = docNode.createElement('Value');
valueElement.appendChild(docNode.createTextNode(value));
specialRule.appendChild(keyElement);
specialRule.appendChild(valueElement);
specialRules.appendChild(specialRule);
end
end
% Reads the settings XML file to a structure
function settingsStruct = readSettingsXML()
% MBeautify.readSettingsXML reads the configuration XML file to a structure.
settingsStruct = struct('OperatorRules', struct(), 'SpecialRules', struct());
XMLDoc = xmlread(MBeautify.RulesXMLFileFull);
allOperatorItems = XMLDoc.getElementsByTagName('OperatorPaddingRule');
operatorNode = settingsStruct.OperatorRules;
for iOperator = 0:allOperatorItems.getLength() -1
currentOperator = allOperatorItems.item(iOperator);
key = char(currentOperator.getElementsByTagName('Key').item(0).getTextContent().toString());
operatorNode.(key) = struct();
operatorNode.(key).ValueFrom = removeXMLEscaping(char(currentOperator.getElementsByTagName('ValueFrom').item(0).getTextContent().toString()));
operatorNode.(key).ValueTo = removeXMLEscaping(char(currentOperator.getElementsByTagName('ValueTo').item(0).getTextContent().toString()));
end
settingsStruct.OperatorRules = operatorNode;
allSpecialItems = XMLDoc.getElementsByTagName('SpecialRule');
specialRulesNode = settingsStruct.SpecialRules;
for iSpecRule = 0:allSpecialItems.getLength() -1
currentRule = allSpecialItems.item(iSpecRule);
key = char(currentRule.getElementsByTagName('Key').item(0).getTextContent().toString());
specialRulesNode.(key) = struct();
specialRulesNode.(key).Value = char(currentRule.getElementsByTagName('Value').item(0).getTextContent().toString());
end
settingsStruct.SpecialRules = specialRulesNode;
function escapedValue = removeXMLEscaping(value)
escapedValue = regexprep(value, '<', '<');
escapedValue = regexprep(escapedValue, '&', '&');
escapedValue = regexprep(escapedValue, '>', '>');
end
end
end
end