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.

0 commit comments

Comments
 (0)