forked from davidvarga/MBeautifier
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMBeautyShortcuts.m
113 lines (87 loc) · 4.13 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
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);
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');
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 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', sprintf('\n'), ...
'[destFile, destPath] = uiputfile(); drawnow(); destFile = fullfile(destPath, destFile);', ...
'if isempty(destFile), return; end', sprintf('\n'), ...
'MBeautify.formatFile(sourceFile, destFile);'];
end
end
end