Skip to content

Commit 6559cc2

Browse files
bobbrowdinhtam2c
andauthored
Fix custom comment continuation for multiple follow-up lines (#14076)
* Fix custom comment continuation for multiple follow-up lines (#14074) * Add a unit test for the scenario * formatting hasn't been done on this file yet. Match the existing style. --------- Co-authored-by: dinhtam2c <[email protected]>
1 parent 9ccb63f commit 6559cc2

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

Extension/src/LanguageServer/languageConfig.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ function getMLSplitAfterPattern(): string {
5555

5656
function getMLPreviousLinePattern(insert: string): string | undefined {
5757
if (insert.startsWith("/*")) {
58-
return `(?=^(\\s*(\\/\\*\\*|\\*)).*)(?=(?!(\\s*\\*\\/)))`;
58+
const match: string = escape(insert);
59+
return `(?=^(\\s*(${match}|\\*)).*)(?=(?!(\\s*\\*\\/)))`;
5960
}
6061
return undefined;
6162
}
@@ -238,8 +239,9 @@ export function getLanguageConfig(languageId: string): vscode.LanguageConfigurat
238239
}
239240

240241
export function getLanguageConfigFromPatterns(languageId: string, patterns?: (string | CommentPattern)[]): vscode.LanguageConfiguration {
241-
const beginPatterns: string[] = []; // avoid duplicate rules
242-
const continuePatterns: string[] = []; // avoid duplicate rules
242+
const beginPatterns: string[] = []; // avoid duplicate begin rules
243+
const continuePatterns: string[] = []; // avoid duplicate continue rules
244+
const endPatterns: string[] = []; // avoid duplicate end rules
243245
let duplicates: boolean = false;
244246
let beginRules: vscode.OnEnterRule[] = [];
245247
let continueRules: vscode.OnEnterRule[] = [];
@@ -258,14 +260,17 @@ export function getLanguageConfigFromPatterns(languageId: string, patterns?: (st
258260
} else {
259261
duplicates = true;
260262
}
261-
if (continuePatterns.indexOf(c.continue) < 0) {
263+
if (continuePatterns.indexOf(`${c.begin}\0${c.continue}`) < 0) {
262264
if (r.continue && r.continue.length > 0) {
263265
continueRules = continueRules.concat(r.continue);
264266
}
267+
continuePatterns.push(`${c.begin}\0${c.continue}`);
268+
}
269+
if (endPatterns.indexOf(c.continue) < 0) {
265270
if (r.end && r.end.length > 0) {
266271
endRules = endRules.concat(r.end);
267272
}
268-
continuePatterns.push(c.continue);
273+
endPatterns.push(c.continue);
269274
}
270275
});
271276
if (duplicates) {

Extension/test/scenarios/SimpleCppProject/tests/languageServer.integration.test.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,27 @@ suite("multiline comment setting tests", function(): void {
4545
action: { indentAction: vscode.IndentAction.None, removeText: 1 }
4646
}
4747
];
48+
const multipleMLRules: vscode.OnEnterRule[] = [
49+
defaultMLRules[0], // e.g. /** | */
50+
defaultMLRules[1], // e.g. /** ...|
51+
{ // e.g. /*! | */
52+
beforeText: /^\s*\/\*\!(?!\/)([^\*]|\*(?!\/))*$/,
53+
afterText: /^\s*\*\/$/,
54+
action: { indentAction: vscode.IndentAction.IndentOutdent, appendText: ' * ' }
55+
},
56+
{ // e.g. /*! ...|
57+
beforeText: /^\s*\/\*\!(?!\/)([^\*]|\*(?!\/))*$/,
58+
action: { indentAction: vscode.IndentAction.None, appendText: ' * ' }
59+
},
60+
defaultMLRules[2], // e.g. * ...|
61+
{ // e.g. * ...|
62+
beforeText: /^(\t|[ ])*\ \*(\ ([^\*]|\*(?!\/))*)?$/,
63+
previousLineText: /(?=^(\s*(\/\*\!|\*)).*)(?=(?!(\s*\*\/)))/,
64+
action: { indentAction: vscode.IndentAction.None, appendText: '* ' }
65+
},
66+
defaultMLRules[3], // e.g. */|
67+
defaultMLRules[4] // e.g. *-----*/|
68+
];
4869
const defaultSLRules: vscode.OnEnterRule[] = [
4970
{
5071
beforeText: /^\s*\/\/\/.+$/,
@@ -72,6 +93,11 @@ suite("multiline comment setting tests", function(): void {
7293
assert.deepStrictEqual(rules, defaultMLRules);
7394
});
7495

96+
test("Check the OnEnterRules for C++ with an additional option", () => {
97+
const rules = getLanguageConfigFromPatterns('cpp', [ "/**", "/*!" ]).onEnterRules;
98+
assert.deepStrictEqual(rules, multipleMLRules);
99+
});
100+
75101
test("Make sure duplicate rules are removed", () => {
76102
const rules = getLanguageConfigFromPatterns('cpp', [ "/**", { begin: "/**", continue: " * " }, "/**" ]).onEnterRules;
77103
assert.deepStrictEqual(rules, defaultMLRules);

0 commit comments

Comments
 (0)