
function propgrp = getPropertyGroups(obj)
% getPropertyGroups provides custom display for BuildInfo

% Copyright 2021 The MathWorks, Inc.

propgrp = getPropertyGroups@matlab.mixin.CustomDisplay(obj);
    
if isscalar(obj)
    % Customize display for LinkObj and ModelRefs to avoid
    % loading child buildInfo objects. Achieve this by printing
    % names in LinksObjsDirect, ModelRefsDirect followed by an
    % ellipsis.
    linkObjDisplay = i_getLinkableDisplay(obj.LinkObjsDirect);
    modelRefsDisplay = i_getLinkableDisplay(obj.ModelRefsDirect);
    
    propgrp.PropertyList.LinkObj = linkObjDisplay;
    propgrp.PropertyList.ModelRefs = modelRefsDisplay;
end



function linkObjDisplay = i_getLinkableDisplay(linkObjs)
if isempty(linkObjs)
    % No customization needed
    linkObjDisplay = linkObjs;
else
    linkObjNames = char(join(string({linkObjs.Name}), ", "));
    maxWidth = 60;
    commaIdx = find(linkObjNames==',');
    truncateIdx = [];
    if length(linkObjNames) > maxWidth
        inRangeIdx = commaIdx < maxWidth;
        commaIdxInRange = commaIdx(inRangeIdx);
        commaIdxOutOfRange = commaIdx(~inRangeIdx);
        if ~isempty(commaIdxInRange)
            truncateIdx = commaIdxInRange(end);
        elseif ~isempty(commaIdxOutOfRange)
            % There might be a single linkable with very long name
            truncateIdx = commaIdxOutOfRange(1);
        end
        if ~isempty(truncateIdx)
            linkObjNames = linkObjNames(1:truncateIdx-1);
        end
    end
    % Always add an ellipsis to indicate possibility further linkables
    % would be found when the hierarchy is loaded
    linkObjDisplay = [linkObjNames ' ...'];
end
