Open Jargendas opened 1 month ago
Okay, I did actually find a way of replacing tabs with spaces correctly, but it is quite a beast...
if numel(commentSpacing) > 0 && actComment ~= "" && obj.Configuration.specialRule('PreserveInlineCommentSpacing').ValueAsDouble() ~= 0
spacing = commentSpacing{1};
if obj.Configuration.specialRule('IndentationCharacter').Value == 'white-space'
tabs = regexp(spacing, '\t');
addedChars = 0;
for tabIdx = tabs
numSpaces = obj.Configuration.specialRule('IndentationCount').ValueAsDouble() - mod(numel(actCode) + addedChars + tabIdx -1, obj.Configuration.specialRule('IndentationCount').ValueAsDouble());
leadSpacing = '';
trailSpacing = '';
if (tabIdx+addedChars > 1)
leadSpacing = spacing(1:tabIdx+addedChars-1);
end
if (tabIdx+addedChars < length(spacing))
trailSpacing = spacing(tabIdx+addedChars+1:end);
end
spacing = [leadSpacing, repelem(' ', numSpaces), trailSpacing];
addedChars = addedChars + numSpaces - 1;
end
end
actComment = [spacing, actComment];
end
In some cases (for me at least) it makes sense to preserve the spacing for inline comments. Consider the following snippet for example:
The formatting is nice and tidy, however after applying the normal formatFile to it, it becomes this, which is much less readable imo:
Therefore, I added a new special rule
PreserveInlineCommentSpacing
to keep the original spacing in these cases.The downside of the solution is that the type of spacing (tabs, spaces) used by the user will also be preserved. I don't really see an easy way to change that, as tabs can represent a variable number of spaces.