Skip to content

Commit

Permalink
https://github.com/davidvarga/MBeautifier/issues/47
Browse files Browse the repository at this point in the history
  • Loading branch information
davidvarga committed Dec 23, 2017
1 parent d64985f commit db2bbea
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
19 changes: 19 additions & 0 deletions MBeautify.m
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -454,6 +471,8 @@ function writeSettingsXML()
end

end


end
end

114 changes: 114 additions & 0 deletions MBeautyShortcuts.m
Original file line number Diff line number Diff line change
@@ -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<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

0 comments on commit db2bbea

Please sign in to comment.