|
1 |
| -function formattedCode = formatCode (codeToFormat, options) |
2 |
| - % FORMATCODE Formats the given MATLAB code according to the specified options. |
| 1 | +function formattedCode = formatCode (code, startLine, endLine, options) |
| 2 | + % FORMATCODE Formats the specifid line range of the given MATLAB |
| 3 | + % code according to the provided options. |
| 4 | + % |
| 5 | + % Note: `startLine` and `endLine` should be provided as 0-based line numbers. |
3 | 6 |
|
4 | 7 | % Copyright 2025 The MathWorks, Inc.
|
5 | 8 |
|
6 |
| - s = settings; |
7 |
| - |
8 | 9 | % Update settings (temporarily) for formatting
|
| 10 | + s = settings; |
9 | 11 | cleanupObj1 = setTemporaryValue(s.matlab.editor.tab.InsertSpaces, options.insertSpaces); %#ok<NASGU>
|
10 | 12 | cleanupObj2 = setTemporaryValue(s.matlab.editor.tab.TabSize, options.tabSize); %#ok<NASGU>
|
11 | 13 | cleanupObj3 = setTemporaryValue(s.matlab.editor.tab.IndentSize, options.tabSize); %#ok<NASGU>
|
12 | 14 |
|
13 |
| - % Format code |
14 |
| - formattedCode = indentcode(codeToFormat); |
| 15 | + % Formatting logic expects 1-based line numbers |
| 16 | + formattedCode = doFormatLines(code, startLine + 1, endLine + 1, options); |
| 17 | +end |
| 18 | + |
| 19 | +function formattedCode = doFormatLines (code, startLine, endLine, options) |
| 20 | + % Standardize line endings to \n |
| 21 | + code = regexprep(code , sprintf('(\r\n)|\r|\n'), newline); |
| 22 | + |
| 23 | + lines = strsplit(code, newline, CollapseDelimiters = false); |
| 24 | + |
| 25 | + % Determine the range of lines to format |
| 26 | + if startLine == 1 |
| 27 | + % Case 1: start is the first code line |
| 28 | + linesToFormat = lines(startLine:endLine); |
| 29 | + else |
| 30 | + % Case 2: start is not the first code line |
| 31 | + % In this case, we include the preceding line when formatting to |
| 32 | + % ensure the indentation of startLine is correct with respect to |
| 33 | + % the previous line. |
| 34 | + linesToFormat = lines(startLine-1:endLine); |
| 35 | + end |
| 36 | + |
| 37 | + % Format the lines using the indentcode function |
| 38 | + formattedCode = indentcode(strjoin(linesToFormat, '\n')); |
| 39 | + formattedLines = strsplit(formattedCode, '\n', CollapseDelimiters = false); |
| 40 | + |
| 41 | + % Replace the respective lines in the original snippet |
| 42 | + if startLine == 1 |
| 43 | + lines(startLine:endLine) = formattedLines; |
| 44 | + else |
| 45 | + % Calculate the difference in indentation |
| 46 | + originalIndent = regexp(lines{startLine-1}, '^\s*', 'match', 'once'); |
| 47 | + newIndent = regexp(formattedLines{1}, '^\s*', 'match', 'once'); |
| 48 | + diff = getWhitespaceLength(originalIndent, options.tabSize) - getWhitespaceLength(newIndent, options.tabSize); |
| 49 | + |
| 50 | + if diff ~= 0 |
| 51 | + % Adjust whitespace indent of formatted lines [startLine:endLine] |
| 52 | + for i = 2:numel(formattedLines) |
| 53 | + if options.insertSpaces |
| 54 | + formattedLines{i} = append(getWhitespaceStringOfLength(diff, true, options.tabSize), formattedLines{i}); |
| 55 | + else |
| 56 | + existingWhitespace = regexp(formattedLines{i}, '^\s*', 'match', 'once'); |
| 57 | + existingWhitespaceLength = getWhitespaceLength(existingWhitespace, options.tabSize); |
| 58 | + targetWhitespaceLength = existingWhitespaceLength + diff; |
| 59 | + |
| 60 | + newWhitespace = getWhitespaceStringOfLength(targetWhitespaceLength, false, options.tabSize); |
| 61 | + formattedLines{i} = append(newWhitespace, strip(formattedLines{i}, 'left')); |
| 62 | + end |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + % Replace the formatted lines |
| 67 | + lines(startLine:endLine) = formattedLines(2:end); |
| 68 | + end |
| 69 | + |
| 70 | + % Join the lines back into a single string |
| 71 | + formattedCode = strjoin(lines, '\n'); |
| 72 | +end |
| 73 | + |
| 74 | +function lengthInSpaces = getWhitespaceLength (whitespaceStr, tabSize) |
| 75 | + lengthInSpaces = 0; |
| 76 | + |
| 77 | + % Iterate through each character in the string |
| 78 | + for i = 1:length(whitespaceStr) |
| 79 | + charAtPos = whitespaceStr(i); |
| 80 | + |
| 81 | + if charAtPos == ' ' |
| 82 | + % Space character - add length of 1 |
| 83 | + lengthInSpaces = lengthInSpaces + 1; |
| 84 | + else |
| 85 | + % Assume tab character |
| 86 | + % The length will now be the next multiple of `tabSize` |
| 87 | + lengthInSpaces = floor(lengthInSpaces / tabSize + 1) * tabSize; |
| 88 | + end |
| 89 | + end |
| 90 | +end |
| 91 | + |
| 92 | +function whitespaceStr = getWhitespaceStringOfLength (length, insertSpaces, tabSize) |
| 93 | + if insertSpaces |
| 94 | + whitespaceStr = blanks(length); |
| 95 | + else |
| 96 | + % Calculate how many tab characters and additional spaces are needed |
| 97 | + nTabs = floor(length / tabSize); |
| 98 | + nSpaces = rem(length, tabSize); |
| 99 | + whitespaceStr = append(repmat(char(9), 1, nTabs), blanks(nSpaces)); |
| 100 | + end |
15 | 101 | end
|
16 | 102 |
|
17 | 103 | function cleanupObj = setTemporaryValue (setting, tempValue)
|
|
0 commit comments