-
Notifications
You must be signed in to change notification settings - Fork 77
/
MBeautyShortcuts.m
160 lines (131 loc) · 6.83 KB
/
MBeautyShortcuts.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
classdef MBeautyShortcuts
% Creates and executes MBeautifier related Matlab shortcuts.
properties (Access = private, Constant)
ShorcutModes = {'editorpage', 'editorselection', 'file'};
end
methods (Static)
function createShortcut(mode)
% Creates a shortcut with the selected mode: 'editorpage', 'editorselection', 'file'
% 'editorpage' - Execute MBeauty.formatCurrentEditorPage
% 'editorselection' - Execute MBeauty.formatEditorSelection
% 'file' - Execute MBeauty.formatFile
mode = MBeautyShortcuts.checkMode(mode);
if ~verLessThan('matlab', '8.0')
category = 'MBeautifier';
else
category = 'Toolbar Shortcuts';
end
shortCutStruct = MBeautyShortcuts.getShortcutCategoryStructure(mode);
% Below 2019b shortcuts will be sued
if verLessThan('matlab', '9.5')
shortcutUtils = com.mathworks.mlwidgets.shortcuts.ShortcutUtils();
try
shortcutUtils.removeShortcut(category, shortCutStruct.Name);
catch %#ok<CTCH>
% This command only fails on R2012b
end
shortcutUtils.addShortcutToBottom(shortCutStruct.Name, shortCutStruct.Callback, '', category, 'true');
else
% Above 2019b favourites + quick access toolbar will be
% used
fc = com.mathworks.mlwidgets.favoritecommands.FavoriteCommands.getInstance();
if fc.hasCategory(category)
method = fc.getClass().getDeclaredMethod('getCategories', []);
method.setAccessible(true)
categories = method.invoke(fc,[]);
for catInd = 0:categories.size-1
if strcmp(categories.get(catInd).getLabel, category)
categoryMBeautifier = categories.get(catInd);
end
end
if categoryMBeautifier.hasChildren()
childToBeUpdated = [];
childs = categoryMBeautifier.getChildren();
for childInd = 0:childs.size-1
if strcmp(childs.get(childInd).getLabel, shortCutStruct.Name)
childToBeUpdated = childs.get(childInd);
end
end
if ~isempty(childToBeUpdated)
childToBeUpdated.setCode(shortCutStruct.Callback);
else
MBeautyShortcuts.createFavouriteEntry(fc, category, shortCutStruct);
end
else
MBeautyShortcuts.createFavouriteEntry(fc, category, shortCutStruct);
end
else
MBeautyShortcuts.createFavouriteEntry(fc, category, shortCutStruct);
end
end
end
function executeCallback(mode)
mode = MBeautyShortcuts.checkMode(mode);
if strcmp(mode, 'editorpage')
MBeautyShortcuts.editorPageShortcutCallback();
elseif strcmp(mode, 'editorselection')
MBeautyShortcuts.editorSelectionShortcutCallback();
elseif strcmp(mode, 'file')
MBeautyShortcuts.fileShortcutCallback();
end
end
end
methods (Static, Access = private)
function createFavouriteEntry(fc, category, shortCutStruct)
newMBeautyShortcut = com.mathworks.mlwidgets.favoritecommands.FavoriteCommandProperties();
newMBeautyShortcut.setLabel(shortCutStruct.Name);
newMBeautyShortcut.setCategoryLabel(category);
newMBeautyShortcut.setCode(shortCutStruct.Callback);
newMBeautyShortcut.setIsOnQuickToolBar(true);
newMBeautyShortcut.setIsShowingLabelOnToolBar(true);
fc.addCommand(newMBeautyShortcut);
end
function structure = getShortcutCategoryStructure(mode)
mode = MBeautyShortcuts.checkMode(mode);
if strcmp(mode, 'editorpage')
structure = MBeautyShortcuts.getEditorPageShortcut();
elseif strcmp(mode, 'editorselection')
structure = MBeautyShortcuts.getEditorSelectionShortcut();
elseif strcmp(mode, 'file')
structure = MBeautyShortcuts.getFileShortcut();
end
pathToAdd = eval('fileparts(mfilename(''fullpath''))');
addPathCommand = ['addpath(''', pathToAdd, ''');'];
structure.Callback = [addPathCommand, structure.Callback];
end
function mode = checkMode(mode)
mode = lower(strtrim(mode));
if ~any(strcmp(mode, MBeautyShortcuts.ShorcutModes))
error('MBeautifier:InvalidShortcutMode', 'Unavailable shortcut mode defined!');
end
end
function structure = getEditorPageShortcut()
structure = struct();
structure.Name = 'MBeauty: Format Editor Page';
structure.Callback = MBeautyShortcuts.editorPageShortcutCallback();
end
function command = editorPageShortcutCallback()
command = 'MBeautify.formatCurrentEditorPage();';
end
function structure = getEditorSelectionShortcut()
structure = struct();
structure.Name = 'MBeauty: Format Editor Selection';
structure.Callback = MBeautyShortcuts.editorSelectionShortcutCallback();
end
function command = editorSelectionShortcutCallback()
command = 'MBeautify.formatEditorSelection();';
end
function structure = getFileShortcut()
structure = struct();
structure.Name = 'MBeauty: Format File';
structure.Callback = MBeautyShortcuts.fileShortcutCallback();
end
function command = fileShortcutCallback()
command = ['[sourceFile, sourcePath] = uigetfile(); drawnow(); sourceFile = fullfile(sourcePath, sourceFile);', ...
'if isempty(sourceFile), return; end', MBeautifier.Constants.NewLine, ...
'[destFile, destPath] = uiputfile(); drawnow(); destFile = fullfile(destPath, destFile);', ...
'if isempty(destFile), return; end', MBeautifier.Constants.NewLine, ...
'MBeautify.formatFile(sourceFile, destFile);'];
end
end
end