Skip to content

Commit 1094188

Browse files
authored
Plumb formatting to getDocCommentTemplateAtPosition (#52349)
Fixes #52348
1 parent 3d38971 commit 1094188

28 files changed

+47
-46
lines changed

src/harness/client.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -727,7 +727,7 @@ export class SessionClient implements LanguageService {
727727
return notImplemented();
728728
}
729729

730-
getDocCommentTemplateAtPosition(_fileName: string, _position: number, _options?: DocCommentTemplateOptions): TextInsertion {
730+
getDocCommentTemplateAtPosition(_fileName: string, _position: number, _options?: DocCommentTemplateOptions, _formatOptions?: FormatCodeSettings): TextInsertion {
731731
return notImplemented();
732732
}
733733

src/harness/fourslashImpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3249,7 +3249,7 @@ export class TestState {
32493249

32503250
public verifyDocCommentTemplate(expected: ts.TextInsertion | undefined, options?: ts.DocCommentTemplateOptions) {
32513251
const name = "verifyDocCommentTemplate";
3252-
const actual = this.languageService.getDocCommentTemplateAtPosition(this.activeFile.fileName, this.currentCaretPosition, options || { generateReturnInDocTemplate: true })!;
3252+
const actual = this.languageService.getDocCommentTemplateAtPosition(this.activeFile.fileName, this.currentCaretPosition, options || { generateReturnInDocTemplate: true }, this.formatCodeSettings)!;
32533253

32543254
if (expected === undefined) {
32553255
if (actual) {

src/harness/fourslashInterfaceImpl.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ export class Verify extends VerifyNegatable {
452452

453453
public docCommentTemplateAt(marker: string | FourSlash.Marker, expectedOffset: number, expectedText: string, options?: ts.DocCommentTemplateOptions) {
454454
this.state.goToMarker(marker);
455-
this.state.verifyDocCommentTemplate({ newText: expectedText.replace(/\r?\n/g, "\r\n"), caretOffset: expectedOffset }, options);
455+
this.state.verifyDocCommentTemplate({ newText: expectedText.replace(/\r?\n/g, ts.testFormatSettings.newLineCharacter!), caretOffset: expectedOffset }, options);
456456
}
457457

458458
public noDocCommentTemplateAt(marker: string | FourSlash.Marker) {

src/harness/harnessLanguageService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -587,8 +587,8 @@ class LanguageServiceShimProxy implements ts.LanguageService {
587587
getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: ts.FormatCodeOptions): ts.TextChange[] {
588588
return unwrapJSONCallResult(this.shim.getFormattingEditsAfterKeystroke(fileName, position, key, JSON.stringify(options)));
589589
}
590-
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: ts.DocCommentTemplateOptions): ts.TextInsertion {
591-
return unwrapJSONCallResult(this.shim.getDocCommentTemplateAtPosition(fileName, position, options));
590+
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: ts.DocCommentTemplateOptions, formatOptions?: ts.FormatCodeSettings): ts.TextInsertion {
591+
return unwrapJSONCallResult(this.shim.getDocCommentTemplateAtPosition(fileName, position, options, formatOptions));
592592
}
593593
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean {
594594
return unwrapJSONCallResult(this.shim.isValidBraceCompletionAtPosition(fileName, position, openingBrace));

src/server/session.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2083,7 +2083,7 @@ export class Session<TMessage = string> implements EventSender {
20832083
private getDocCommentTemplate(args: protocol.FileLocationRequestArgs) {
20842084
const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args);
20852085
const position = this.getPositionInFile(args, file);
2086-
return languageService.getDocCommentTemplateAtPosition(file, position, this.getPreferences(file));
2086+
return languageService.getDocCommentTemplateAtPosition(file, position, this.getPreferences(file), this.getFormatOptions(file));
20872087
}
20882088

20892089
private getSpanOfEnclosingComment(args: protocol.SpanOfEnclosingCommentRequestArgs) {

src/services/services.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2436,8 +2436,9 @@ export function createLanguageService(
24362436
: Promise.reject("Host does not implement `installPackage`");
24372437
}
24382438

2439-
function getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): TextInsertion | undefined {
2440-
return JsDoc.getDocCommentTemplateAtPosition(getNewLineOrDefaultFromHost(host, /*formatSettings*/ undefined), syntaxTreeCache.getCurrentSourceFile(fileName), position, options);
2439+
function getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions, formatOptions?: FormatCodeSettings): TextInsertion | undefined {
2440+
const formatSettings = formatOptions ? formatting.getFormatContext(formatOptions, host).options : undefined;
2441+
return JsDoc.getDocCommentTemplateAtPosition(getNewLineOrDefaultFromHost(host, formatSettings), syntaxTreeCache.getCurrentSourceFile(fileName), position, options);
24412442
}
24422443

24432444
function isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean {

src/services/shims.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ export interface LanguageServiceShim extends Shim {
349349
/**
350350
* Returns JSON-encoded value of the type TextInsertion.
351351
*/
352-
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): string;
352+
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions, formatOptions?: FormatCodeSettings): string;
353353

354354
/**
355355
* Returns JSON-encoded boolean to indicate whether we should support brace location
@@ -1091,10 +1091,10 @@ class LanguageServiceShimObject extends ShimBase implements LanguageServiceShim
10911091
});
10921092
}
10931093

1094-
public getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): string {
1094+
public getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions, formatOptions?: FormatCodeSettings): string {
10951095
return this.forwardJSONCall(
10961096
`getDocCommentTemplateAtPosition('${fileName}', ${position})`,
1097-
() => this.languageService.getDocCommentTemplateAtPosition(fileName, position, options)
1097+
() => this.languageService.getDocCommentTemplateAtPosition(fileName, position, options, formatOptions)
10981098
);
10991099
}
11001100

src/services/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ export interface LanguageService {
604604
getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
605605
getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
606606

607-
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): TextInsertion | undefined;
607+
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions, formatOptions?: FormatCodeSettings): TextInsertion | undefined;
608608

609609
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean;
610610
/**

tests/baselines/reference/api/tsserverlibrary.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9990,7 +9990,7 @@ declare namespace ts {
99909990
getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
99919991
getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
99929992
getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
9993-
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): TextInsertion | undefined;
9993+
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions, formatOptions?: FormatCodeSettings): TextInsertion | undefined;
99949994
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean;
99959995
/**
99969996
* This will return a defined result if the position is after the `>` of the opening tag, or somewhere in the text, of a JSXElement with no closing tag.

tests/baselines/reference/api/typescript.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6088,7 +6088,7 @@ declare namespace ts {
60886088
getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
60896089
getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
60906090
getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
6091-
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): TextInsertion | undefined;
6091+
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions, formatOptions?: FormatCodeSettings): TextInsertion | undefined;
60926092
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean;
60936093
/**
60946094
* This will return a defined result if the position is after the `>` of the opening tag, or somewhere in the text, of a JSXElement with no closing tag.

tests/cases/fourslash/docCommentTemplateClassDeclMethods01.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path='fourslash.ts' />
22

33
const singleLineOffset = 3;
4-
const multiLineOffset = 12;
4+
const multiLineOffset = 11;
55

66

77
////class C {

tests/cases/fourslash/docCommentTemplateClassDeclMethods02.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference path='fourslash.ts' />
22

3-
const multiLineOffset = 12;
3+
const multiLineOffset = 11;
44

55
////class C {
66
//// /*0*/

tests/cases/fourslash/docCommentTemplateClassDeclProperty01.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/// <reference path='fourslash.ts' />
22

33
const singleLineOffset = 3;
4-
const multiLineOffset = 12;
4+
const multiLineOffset = 11;
55

66

77
////class C {

tests/cases/fourslash/docCommentTemplateConstructor01.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//// }
1414
////}
1515

16-
const newTextOffset = 12;
16+
const newTextOffset = 11;
1717
verify.docCommentTemplateAt("0", /*newTextOffset*/ newTextOffset,
1818
`/**
1919
*

tests/cases/fourslash/docCommentTemplateExportAssignmentJS.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
//// exports.foo = (a) => {};
99

1010

11-
verify.docCommentTemplateAt("", 8,
11+
verify.docCommentTemplateAt("", 7,
1212
`/**
1313
*
1414
* @param {any} a

tests/cases/fourslash/docCommentTemplateFunctionExpression.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
////const x = /*next*/ function f(p) {}
55

66
for (const marker of test.markerNames()) {
7-
verify.docCommentTemplateAt(marker, 8,
7+
verify.docCommentTemplateAt(marker, 7,
88
`/**
99
*
1010
* @param p

tests/cases/fourslash/docCommentTemplateFunctionWithParameters.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
const noIndentScaffolding = "/**\n * \n * @param x\n * @param y\n */";
99
const oneIndentScaffolding = "/**\n * \n * @param x\n * @param y\n */";
10-
const noIndentOffset = 8;
10+
const noIndentOffset = 7;
1111
const oneIndentOffset = noIndentOffset + 4;
1212

1313
goTo.marker("0");

tests/cases/fourslash/docCommentTemplateFunctionWithParameters_js.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
/////*0*/
55
////function f(a, ...b): boolean {}
66

7-
verify.docCommentTemplateAt("0", 8,
7+
verify.docCommentTemplateAt("0", 7,
88
`/**
99
*
1010
* @param {any} a

tests/cases/fourslash/docCommentTemplateInterfacePropertyFunctionType.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
//// foo: (a: number, b: string) => void;
66
////}
77

8-
verify.docCommentTemplateAt("", 12,
8+
verify.docCommentTemplateAt("", 11,
99
`/**
1010
*
1111
* @param a
1212
* @param b
1313
* @returns
1414
*/`);
1515

16-
verify.docCommentTemplateAt("", 12,
16+
verify.docCommentTemplateAt("", 11,
1717
`/**
1818
*
1919
* @param a

tests/cases/fourslash/docCommentTemplateInterfacesEnumsAndTypeAliases.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ verify.docCommentTemplateAt("interfaceFoo", /*expectedOffset*/ 3,
3030
verify.docCommentTemplateAt("propertybar", /*expectedOffset*/ 3,
3131
"/** */");
3232

33-
verify.docCommentTemplateAt("methodbaz", /*expectedOffset*/ 12,
33+
verify.docCommentTemplateAt("methodbaz", /*expectedOffset*/ 11,
3434
`/**
3535
*
3636
* @param message
@@ -46,4 +46,4 @@ verify.docCommentTemplateAt("memberOpen", /*expectedOffset*/ 3,
4646
"/** */");
4747

4848
verify.docCommentTemplateAt("memberClosed", /*expectedOffset*/ 3,
49-
"/** */");
49+
"/** */");

tests/cases/fourslash/docCommentTemplateJsSpecialPropertyAssignment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
////const myNamespace = {};
66
/////*1*/myNamespace.myExport = function(x) {};
77

8-
verify.docCommentTemplateAt("0", 8,
8+
verify.docCommentTemplateAt("0", 7,
99
`/**
1010
*
1111
* @param {any} a
1212
*/
1313
`);
1414

15-
verify.docCommentTemplateAt("1", 8,
15+
verify.docCommentTemplateAt("1", 7,
1616
`/**
1717
*
1818
* @param {any} x

tests/cases/fourslash/docCommentTemplateObjectLiteralMethods01.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
/// <reference path='fourslash.ts' />
22

3-
const multiLineOffset = 12;
3+
const multiLineOffset = 11;
44

55
////var x = {
66
//// /*0*/

tests/cases/fourslash/docCommentTemplatePrototypeMethod.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
////C.prototype.method = /*next*/ function (p) {}
88

99
for (const marker of test.markerNames()) {
10-
verify.docCommentTemplateAt(marker, 8,
10+
verify.docCommentTemplateAt(marker, 7,
1111
`/**
1212
*
1313
* @param {any} p

tests/cases/fourslash/docCommentTemplateReturnsTag1.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,19 +28,19 @@
2828

2929
verify.docCommentTemplateAt("0", 3, "/** */");
3030

31-
verify.docCommentTemplateAt("1", 8,
31+
verify.docCommentTemplateAt("1", 7,
3232
`/**
3333
*
3434
* @returns
3535
*/`);
3636

37-
verify.docCommentTemplateAt("2", 8,
37+
verify.docCommentTemplateAt("2", 7,
3838
`/**
3939
*
4040
* @returns
4141
*/`);
4242

43-
verify.docCommentTemplateAt("3", 8,
43+
verify.docCommentTemplateAt("3", 7,
4444
`/**
4545
*
4646
* @returns
@@ -49,7 +49,7 @@ verify.docCommentTemplateAt("3", 8,
4949
verify.docCommentTemplateAt("4", 3, "/** */");
5050

5151

52-
verify.docCommentTemplateAt("5", 12,
52+
verify.docCommentTemplateAt("5", 11,
5353
`/**
5454
*
5555
* @returns

tests/cases/fourslash/docCommentTemplateReturnsTag2.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,15 @@
55
//// return 1;
66
////}
77

8-
verify.docCommentTemplateAt("0", 8,
8+
verify.docCommentTemplateAt("0", 7,
99
`/**
1010
*
1111
* @param x
1212
* @param y
1313
* @returns
1414
*/`, { generateReturnInDocTemplate: true });
1515

16-
verify.docCommentTemplateAt("0", 8,
16+
verify.docCommentTemplateAt("0", 7,
1717
`/**
1818
*
1919
* @param x

tests/cases/fourslash/docCommentTemplateVariableStatements01.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ for (const varName of ["a", "b", "c", "d"]) {
3333
"/** */");
3434
}
3535

36-
verify.docCommentTemplateAt("e", /*newTextOffset*/ 8,
36+
verify.docCommentTemplateAt("e", /*newTextOffset*/ 7,
3737
`/**
3838
*
3939
* @param x
@@ -42,10 +42,10 @@ verify.docCommentTemplateAt("e", /*newTextOffset*/ 8,
4242
* @returns
4343
*/`);
4444

45-
verify.docCommentTemplateAt("f", /*newTextOffset*/ 8,
45+
verify.docCommentTemplateAt("f", /*newTextOffset*/ 7,
4646
`/**
4747
*
4848
* @param a
4949
* @param b
5050
* @param c
51-
*/`);
51+
*/`);

tests/cases/fourslash/docCommentTemplateVariableStatements03.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
//// }
3030
////}))
3131

32-
verify.docCommentTemplateAt("a", /*newTextOffset*/ 8,
32+
verify.docCommentTemplateAt("a", /*newTextOffset*/ 7,
3333
`/**
3434
*
3535
* @param x
3636
* @returns
3737
*/`);
3838

39-
verify.docCommentTemplateAt("b", /*newTextOffset*/ 8,
39+
verify.docCommentTemplateAt("b", /*newTextOffset*/ 7,
4040
`/**
4141
*
4242
* @param x
@@ -45,7 +45,7 @@ verify.docCommentTemplateAt("b", /*newTextOffset*/ 8,
4545
* @returns
4646
*/`);
4747

48-
verify.docCommentTemplateAt("c", /*newTextOffset*/ 8,
48+
verify.docCommentTemplateAt("c", /*newTextOffset*/ 7,
4949
`/**
5050
*
5151
* @param x
@@ -55,7 +55,7 @@ verify.docCommentTemplateAt("c", /*newTextOffset*/ 8,
5555
verify.docCommentTemplateAt("d", /*newTextOffset*/ 3,
5656
"/** */");
5757

58-
verify.docCommentTemplateAt("e", /*newTextOffset*/ 8,
58+
verify.docCommentTemplateAt("e", /*newTextOffset*/ 7,
5959
`/**
6060
*
6161
* @param param0
@@ -65,8 +65,8 @@ verify.docCommentTemplateAt("e", /*newTextOffset*/ 8,
6565
verify.docCommentTemplateAt("f", /*newTextOffset*/ 3,
6666
"/** */");
6767

68-
verify.docCommentTemplateAt("g", /*newTextOffset*/ 8,
68+
verify.docCommentTemplateAt("g", /*newTextOffset*/ 7,
6969
`/**
7070
*
7171
* @param x
72-
*/`);
72+
*/`);

tests/cases/fourslash/docCommentTemplate_insideEmptyComment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/////** Doc/*1*/ */
77
////function g(p) { return p; }
88

9-
verify.docCommentTemplateAt("", /*newTextOffset*/ 8,
9+
verify.docCommentTemplateAt("", /*newTextOffset*/ 7,
1010
`/**
1111
*
1212
* @param p

0 commit comments

Comments
 (0)