Fix indentation preservation in JSDoc (#37717)
This fixes two bugs in the parseJSDocCommentWorker(). 1. The initial indent was calculated wrongly. It was set to the difference between the index of the last newline or beginning of file and the current start marker (position of /**). By calculating it this way, the newline character itself is counted as indentation character as well. The initial indent is used as margin for the whole comment. The margin contains the amount of characters to skip before the actual content or payload of a comment line. The algorithm does not skip non-whitespace characters at the beginning of the content, but it would strip away one whitespace character for indented content (which does matter, if there is e.g. a Markdown code block with indentation in the comment). 2. When reducing initial whitespace sequences of comment lines by the remaining margin the algorithm cut off one character too much. This might have been introduced to fix 1. It had a similar effect as 1.
This commit is contained in:
parent
52dc9f2282
commit
d6af9b7cbc
|
@ -6884,7 +6884,8 @@ namespace ts {
|
||||||
let state = JSDocState.SawAsterisk;
|
let state = JSDocState.SawAsterisk;
|
||||||
let margin: number | undefined;
|
let margin: number | undefined;
|
||||||
// + 4 for leading '/** '
|
// + 4 for leading '/** '
|
||||||
let indent = start - Math.max(content.lastIndexOf("\n", start), 0) + 4;
|
// + 1 because the last index of \n is always one index before the first character in the line and coincidentally, if there is no \n before start, it is -1, which is also one index before the first character
|
||||||
|
let indent = start - (content.lastIndexOf("\n", start) + 1) + 4;
|
||||||
function pushComment(text: string) {
|
function pushComment(text: string) {
|
||||||
if (!margin) {
|
if (!margin) {
|
||||||
margin = indent;
|
margin = indent;
|
||||||
|
@ -6940,7 +6941,7 @@ namespace ts {
|
||||||
comments.push(whitespace);
|
comments.push(whitespace);
|
||||||
}
|
}
|
||||||
else if (margin !== undefined && indent + whitespace.length > margin) {
|
else if (margin !== undefined && indent + whitespace.length > margin) {
|
||||||
comments.push(whitespace.slice(margin - indent - 1));
|
comments.push(whitespace.slice(margin - indent));
|
||||||
}
|
}
|
||||||
indent += whitespace.length;
|
indent += whitespace.length;
|
||||||
break;
|
break;
|
||||||
|
|
13
tests/cases/fourslash/jsDocIndentationPreservation1.ts
Normal file
13
tests/cases/fourslash/jsDocIndentationPreservation1.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
///<reference path="fourslash.ts" />
|
||||||
|
// @allowJs: true
|
||||||
|
// @Filename: Foo.js
|
||||||
|
|
||||||
|
/////**
|
||||||
|
//// * Does some stuff.
|
||||||
|
//// * Second line.
|
||||||
|
//// * Third line.
|
||||||
|
//// */
|
||||||
|
////function foo/**/(){}
|
||||||
|
|
||||||
|
goTo.marker();
|
||||||
|
verify.quickInfoIs("function foo(): void", "Does some stuff.\n Second line.\n\tThird line.");
|
13
tests/cases/fourslash/jsDocIndentationPreservation2.ts
Normal file
13
tests/cases/fourslash/jsDocIndentationPreservation2.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
///<reference path="fourslash.ts" />
|
||||||
|
// @allowJs: true
|
||||||
|
// @Filename: Foo.js
|
||||||
|
|
||||||
|
/////**
|
||||||
|
//// Does some stuff.
|
||||||
|
//// Second line.
|
||||||
|
//// Third line.
|
||||||
|
////*/
|
||||||
|
////function foo/**/(){}
|
||||||
|
|
||||||
|
goTo.marker();
|
||||||
|
verify.quickInfoIs("function foo(): void", "Does some stuff.\n Second line.\n\tThird line.");
|
13
tests/cases/fourslash/jsDocIndentationPreservation3.ts
Normal file
13
tests/cases/fourslash/jsDocIndentationPreservation3.ts
Normal file
|
@ -0,0 +1,13 @@
|
||||||
|
///<reference path="fourslash.ts" />
|
||||||
|
// @allowJs: true
|
||||||
|
// @Filename: Foo.js
|
||||||
|
|
||||||
|
/////**
|
||||||
|
//// Does some stuff.
|
||||||
|
//// Second line.
|
||||||
|
//// Third line.
|
||||||
|
////*/
|
||||||
|
////function foo/**/(){}
|
||||||
|
|
||||||
|
goTo.marker();
|
||||||
|
verify.quickInfoIs("function foo(): void", "Does some stuff.\n Second line.\n\tThird line.");
|
Loading…
Reference in a new issue