Skip to content

Commit

Permalink
fix: Comment toggle when region includes empty lines (#2386)
Browse files Browse the repository at this point in the history
  • Loading branch information
slavek-kucera authored Jul 19, 2024
1 parent a99147e commit 0997bc2
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,42 @@ describe("Validate ToggleComments", () => {
);
});

test("toggle mixed selection", () => {
// selection[0]
// 4 >
// 5: 01 WS-POINT.
// 6: 05 WS-POINTER USAGE IS POINTER.
// 7: <
// selection[1]
// 13: >
// 14: * DISPLAY 'From P1:' BAR of FOOBAR.
// 15: <
const mixedTextEditor = {
selections: [getRange(4, 12, 7, 25), getRange(13, 20, 15, 30)],
document: {
lineAt: jest.fn().mockImplementation(
(line: number) =>
documentLines[line as keyof typeof documentLines] ?? {
text: "",
range: getRange(line, 0, line, 0),
},
),
eol: vscode.EndOfLine.LF,
},
edit: jest.fn().mockImplementation((callback) => callback(editBuilder)),
} as unknown as vscode.TextEditor;
new ToggleComments(mixedTextEditor, CommentAction.TOGGLE).doIt();
expect(editBuilder.replace).toBeCalledWith(
getRange(5, 0, 6, 45),
" *01 WS-POINT.\n" +
" * 05 WS-POINTER USAGE IS POINTER.",
);
expect(editBuilder.replace).toBeCalledWith(
getRange(14, 0, 14, 44),
" DISPLAY 'From P1:' BAR of FOOBAR.",
);
});

function checkCommonCalls() {
expect(textEditor.document.lineAt).toBeCalledTimes(5);
expect(textEditor.document.lineAt).toBeCalledWith(5);
Expand Down
44 changes: 38 additions & 6 deletions clients/cobol-lsp-vscode-extension/src/commands/CommentCommand.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,29 @@ export function commentCommand(actionType: CommentAction) {
new ToggleComments(activeEditor, actionType).doIt();
}

const allWhitespace = /^ *$/;

function withoutSeqNum(s: string) {
return s.substring(6, 72);
}

function identifyNonEmptyRegion(text: string[]): [number, number] {
let nonEmptyStart = text.findIndex(
(x) => !allWhitespace.test(withoutSeqNum(x)),
);
let nonEmptyEnd = text.length;

if (nonEmptyStart === -1) {
nonEmptyStart = 0;
} else {
for (; nonEmptyEnd > nonEmptyStart; --nonEmptyEnd) {
if (!allWhitespace.test(withoutSeqNum(text[nonEmptyEnd - 1]))) break;
}
}

return [nonEmptyStart, nonEmptyEnd];
}

/**
* Toggles comments in the active text editor based on selection and action type.
*/
Expand Down Expand Up @@ -56,15 +79,24 @@ export class ToggleComments {
const textLines = selectedLines
.map((it) => it.text)
.map(ensureIndicatorArea);
const processedLines = textLines.map(this.evaluateAction(textLines));

const [nonEmptyStart, nonEmptyEnd] = identifyNonEmptyRegion(textLines);
const toProcess = textLines.slice(nonEmptyStart, nonEmptyEnd);
const processedLines = toProcess.map(this.evaluateAction(toProcess));

const lineSeparator =
this.textEditor.document.eol === vscode.EndOfLine.LF ? "\n" : "\r\n";
const selectionRange = new vscode.Range(
new vscode.Position(selectedLines[0].range.start.line, 0),
new vscode.Position(
selectedLines[selectedLines.length - 1].range.end.line,
selectedLines[selectedLines.length - 1].range.end.character,
),
new vscode.Position(selectedLines[0].range.start.line + nonEmptyStart, 0),
nonEmptyEnd == textLines.length
? new vscode.Position(
selectedLines[selectedLines.length - 1].range.end.line,
selectedLines[selectedLines.length - 1].range.end.character,
)
: new vscode.Position(
selectedLines[0].range.start.line + nonEmptyEnd - 1,
textLines[nonEmptyEnd - 1].length,
),
);
return {
selection: selectionRange,
Expand Down

0 comments on commit 0997bc2

Please sign in to comment.