Breaking Error if EditorMFunctionIndentType is originally blank
slee981 opened this issue · 0 comments
Summary
If 'EditorMFunctionIndentType'
is unspecified for the first time using, then originalPreference = com.mathworks.services.Prefs.getStringPref('EditorMFunctionIndentType')
will return an empty Java string. In this case, when the program tries to reset to original preferences, com.mathworks.services.Prefs.setStringPref('EditorMFunctionIndentType', originalPreference);
will try to set an invalid setting in matlab.prf
.
The fix is to check that length(originalPreference) > 0
before resetting to original preferences.
Environment
MATLAB 2019a
Minimum Example of Bug
>> originalPreference = java.lang.String('');
>> com.mathworks.services.Prefs.setStringPref('EditorMFunctionIndentType', originalPreference);
Java exception occurred:
java.lang.IllegalArgumentException: Unrecognized name:
at com.mathworks.widgets.text.matlab.MatlabFunctionIndentingScheme.lookup(MatlabFunctionIndentingScheme.java:64)
at com.mathworks.widgets.text.EditorPrefsAccessor.getMatlabIndentingScheme(EditorPrefsAccessor.java:257)
at com.mathworks.widgets.STPPrefsManager$STPPrefsListener.prefChanged(STPPrefsManager.java:460)
at com.mathworks.services.Prefs.notifyListeners(Prefs.java:1260)
at com.mathworks.services.Prefs.setPrefValue(Prefs.java:1123)
at com.mathworks.services.Prefs.setStringPref(Prefs.java:1021)
This will prevent MATLAB from opening properly in the future. To fix your settings you can either remove the preference or set to a valid option:
>> com.mathworks.services.Prefs.setStringPref('EditorMFunctionIndentType', 'MixedFunctionIndent');
Solution
The following code should prevent the above error
...
originalPreference = com.mathworks.services.Prefs.getStringPref('EditorMFunctionIndentType');
switch lower(indentationStrategy)
case 'allfunctions'
com.mathworks.services.Prefs.setStringPref('EditorMFunctionIndentType', 'AllFunctionIndent');
case 'nestedfunctions'
com.mathworks.services.Prefs.setStringPref('EditorMFunctionIndentType', 'MixedFunctionIndent');
case 'noindent'
com.mathworks.services.Prefs.setStringPref('EditorMFunctionIndentType', 'ClassicFunctionIndent');
end
editorPage.smartIndentContents();
% Restore original settings, if necessary
if (length(originalPreference) > 0 && originalPreference ~= com.mathworks.services.Prefs.getStringPref('EditorMFunctionIndentType'))
com.mathworks.services.Prefs.setStringPref('EditorMFunctionIndentType', originalPreference);
end
...