diff --git a/MBeautify.m b/MBeautify.m index 4a9fec9..eac6614 100644 --- a/MBeautify.m +++ b/MBeautify.m @@ -16,6 +16,13 @@ % 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 + % + % Shortcuts: + % + % Shortcuts can be automatically created for "formatCurrentEditorPage", "formatEditorSelection" and + % "formatFile" methods by executing MBeautify.createShortcut() in order with the parameter 'editorpage', + % 'editorselection' or 'file'. + % The created shortcuts add MBeauty to the Matlab path also (therefore no preparation of the path is needed additionally). properties(Access = private, Constant) RulesXMLFile = 'MBeautyConfigurationRules.xml'; @@ -228,6 +235,16 @@ function formatCurrentEditorPage(doSave) end end + function createShortcut(mode) + % Creates a shortcut with the selected mode: 'editorpage', 'editorselection', 'file'. The shortcut adds + % MBeauty to the Matlab path and executes the following command: + % 'editorpage' - MBeauty.formatCurrentEditorPage + % 'editorselection' - MBeauty.formatEditorSelection + % 'file' - MBeauty.formatFile + + MBeautyShortcuts.createShortcut(mode); + end + end %% Private helpers @@ -454,6 +471,8 @@ function writeSettingsXML() end end + + end end diff --git a/MBeautyShortcuts.m b/MBeautyShortcuts.m new file mode 100644 index 0000000..c59b916 --- /dev/null +++ b/MBeautyShortcuts.m @@ -0,0 +1,114 @@ +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 + % 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 +