Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature to preserve inline comment spacing #129

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Jargendas
Copy link

@Jargendas Jargendas commented Nov 1, 2024

In some cases (for me at least) it makes sense to preserve the spacing for inline comments. Consider the following snippet for example:

properties (Dependent, Access=public)
    startFrequency          %> [double] Start Frequency in Hz
    stopFrequency           %> [double] Stop Frequency in Hz
    centerFrequency         %> [double] Center Frequency in Hz
    resolutionBandwidth     %> [double] Resolution Bandwidth in Hz    
    sweepMode               %> [string] Sweep Mode
    refLevOffset            %> [double] Scale the amplitude of reference level in dBm
    nSamples                %> [double] Number of Samplingpoints 
    dBperDiv                %> [double] Scale the amplitude per Division of Y-axis in dB
    attenuationLev          %> [double] Adjust the input attenation in dB
    traceActive             %> [double] Indicates active Trace   
    traceState              %> [string] State of active Trace
end

The formatting is nice and tidy, however after applying the normal formatFile to it, it becomes this, which is much less readable imo:

properties (Dependent, Access=public)
    startFrequency %> [double] Start Frequency in Hz
    stopFrequency %> [double] Stop Frequency in Hz
    centerFrequency %> [double] Center Frequency in Hz
    resolutionBandwidth %> [double] Resolution Bandwidth in Hz
    sweepMode %> [string] Sweep Mode
    refLevOffset %> [double] Scale the amplitude of reference level in dBm
    nSamples %> [double] Number of Samplingpoints
    dBperDiv %> [double] Scale the amplitude per Division of Y-axis in dB
    attenuationLev %> [double] Adjust the input attenation in dB
    traceActive %> [double] Indicates active Trace
    traceState %> [string] State of active Trace
end

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.

@Jargendas
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant