function Matlab_extendEditorFunctionality(activateFlg)
if nargin < 1 || isempty(activateFlg)
activateFlg = false;
activateFlg = true;
end
jMainPane = [];
jEditor = com.mathworks.mde.desk.MLDesktop.getInstance.getGroupContainer('Editor');
for childIdx = 1 : jEditor.getComponentCount
componentClassName = regexprep(jEditor.getComponent(childIdx-1).class,'.*\.','');
if any(strcmp(componentClassName,{'DTMaximizedPane','DTFloatingPane','DTTiledPane'}))
jMainPane = jEditor.getComponent(childIdx-1);
break;
end
end
if isa(jMainPane,'com.mathworks.mwswing.desk.DTFloatingPane')
jMainPane = jMainPane.getComponent(0);
end
for jj = 1:jMainPane.getComponentCount
hEditorPane = [];
jSyntaxTextPaneView = getDescendent(jMainPane.getComponent(jj-1),[0,0,1,0,0,0,0]);
if isa(jSyntaxTextPaneView,'com.mathworks.widgets.SyntaxTextPaneMultiView$1')
hEditorPane(1) = handle(getDescendent(jSyntaxTextPaneView.getComponent(1),[1,0,0]),'CallbackProperties');
hEditorPane(2) = handle(getDescendent(jSyntaxTextPaneView.getComponent(2),[1,0,0]),'CallbackProperties');
else
jEditorPane = getDescendent(jSyntaxTextPaneView,[1,0,0]);
hEditorPane = handle(jEditorPane,'CallbackProperties');
end
if activateFlg
set(hEditorPane ,'KeyTypedCallback',@(src,evnt)Editor_KeyTypedCallback(src,evnt))
else
set(hEditorPane ,'KeyTypedCallback','')
end
hEditorPaneArray(jj) = hEditorPane;
end
function child = getDescendent(child,listOfChildrenIdx)
if ~isempty(listOfChildrenIdx)
child = getDescendent(child.getComponent(listOfChildrenIdx(1)),listOfChildrenIdx(2:end));
end
function Editor_KeyTypedCallback(~,evnt, varargin)
keyPress = get(evnt,'keyChar');
if strcmp(keyPress,'(') || strcmp(keyPress,'"') || ...
strcmp(keyPress,'[') || strcmp(keyPress,'''') || ...
strcmp(keyPress,'{')
aE = matlab.desktop.editor.getActive;
pos = aE.Selection;
switch keyPress
case '('
putIn = ')';
case '"'
putIn = '"';
case '['
putIn = ']';
case '{'
putIn = '}';
case ''''
putIn = '''';
end
cursorPos = matlab.desktop.editor.positionInLineToIndex(aE, pos(1),pos(2));
if any(strcmp(aE.Text(cursorPos-1),{'(','[','{','''','"'})) && ...
~isempty(regexp(aE.Text(cursorPos),['(\s|',char(13),')'],'match'))
else
putIn = '';
end
aE.insertTextAtPositionInLine(putIn,pos(1),pos(2));
aE.goToPositionInLine(pos(1),pos(2));
end
Best Answer