Skip to content

Plumb formatting to getDocCommentTemplateAtPosition #52349

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/harness/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ export class SessionClient implements LanguageService {
return notImplemented();
}

getDocCommentTemplateAtPosition(_fileName: string, _position: number, _options?: DocCommentTemplateOptions): TextInsertion {
getDocCommentTemplateAtPosition(_fileName: string, _position: number, _options?: DocCommentTemplateOptions, _formatOptions?: FormatCodeSettings): TextInsertion {
return notImplemented();
}

Expand Down
2 changes: 1 addition & 1 deletion src/harness/fourslashImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3249,7 +3249,7 @@ export class TestState {

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

if (expected === undefined) {
if (actual) {
Expand Down
2 changes: 1 addition & 1 deletion src/harness/fourslashInterfaceImpl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ export class Verify extends VerifyNegatable {

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

public noDocCommentTemplateAt(marker: string | FourSlash.Marker) {
Expand Down
4 changes: 2 additions & 2 deletions src/harness/harnessLanguageService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,8 @@ class LanguageServiceShimProxy implements ts.LanguageService {
getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: ts.FormatCodeOptions): ts.TextChange[] {
return unwrapJSONCallResult(this.shim.getFormattingEditsAfterKeystroke(fileName, position, key, JSON.stringify(options)));
}
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: ts.DocCommentTemplateOptions): ts.TextInsertion {
return unwrapJSONCallResult(this.shim.getDocCommentTemplateAtPosition(fileName, position, options));
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: ts.DocCommentTemplateOptions, formatOptions?: ts.FormatCodeSettings): ts.TextInsertion {
return unwrapJSONCallResult(this.shim.getDocCommentTemplateAtPosition(fileName, position, options, formatOptions));
}
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean {
return unwrapJSONCallResult(this.shim.isValidBraceCompletionAtPosition(fileName, position, openingBrace));
Expand Down
2 changes: 1 addition & 1 deletion src/server/session.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2083,7 +2083,7 @@ export class Session<TMessage = string> implements EventSender {
private getDocCommentTemplate(args: protocol.FileLocationRequestArgs) {
const { file, languageService } = this.getFileAndLanguageServiceForSyntacticOperation(args);
const position = this.getPositionInFile(args, file);
return languageService.getDocCommentTemplateAtPosition(file, position, this.getPreferences(file));
return languageService.getDocCommentTemplateAtPosition(file, position, this.getPreferences(file), this.getFormatOptions(file));
}

private getSpanOfEnclosingComment(args: protocol.SpanOfEnclosingCommentRequestArgs) {
Expand Down
5 changes: 3 additions & 2 deletions src/services/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2436,8 +2436,9 @@ export function createLanguageService(
: Promise.reject("Host does not implement `installPackage`");
}

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

function isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean {
Expand Down
6 changes: 3 additions & 3 deletions src/services/shims.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ export interface LanguageServiceShim extends Shim {
/**
* Returns JSON-encoded value of the type TextInsertion.
*/
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): string;
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions, formatOptions?: FormatCodeSettings): string;

/**
* Returns JSON-encoded boolean to indicate whether we should support brace location
Expand Down Expand Up @@ -1091,10 +1091,10 @@ class LanguageServiceShimObject extends ShimBase implements LanguageServiceShim
});
}

public getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): string {
public getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions, formatOptions?: FormatCodeSettings): string {
return this.forwardJSONCall(
`getDocCommentTemplateAtPosition('${fileName}', ${position})`,
() => this.languageService.getDocCommentTemplateAtPosition(fileName, position, options)
() => this.languageService.getDocCommentTemplateAtPosition(fileName, position, options, formatOptions)
);
}

Expand Down
2 changes: 1 addition & 1 deletion src/services/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -604,7 +604,7 @@ export interface LanguageService {
getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];

getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): TextInsertion | undefined;
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions, formatOptions?: FormatCodeSettings): TextInsertion | undefined;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unfortunate that this has to be another optional parameter and not a required one (like other functions here), but I can't see a way to write an overloaded function that can actually detect which one is which.


isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean;
/**
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/tsserverlibrary.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9990,7 +9990,7 @@ declare namespace ts {
getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): TextInsertion | undefined;
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions, formatOptions?: FormatCodeSettings): TextInsertion | undefined;
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean;
/**
* 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.
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/api/typescript.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6088,7 +6088,7 @@ declare namespace ts {
getFormattingEditsForRange(fileName: string, start: number, end: number, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions | FormatCodeSettings): TextChange[];
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions): TextInsertion | undefined;
getDocCommentTemplateAtPosition(fileName: string, position: number, options?: DocCommentTemplateOptions, formatOptions?: FormatCodeSettings): TextInsertion | undefined;
isValidBraceCompletionAtPosition(fileName: string, position: number, openingBrace: number): boolean;
/**
* 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.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference path='fourslash.ts' />

const singleLineOffset = 3;
const multiLineOffset = 12;
const multiLineOffset = 11;


////class C {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference path='fourslash.ts' />

const multiLineOffset = 12;
const multiLineOffset = 11;

////class C {
//// /*0*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference path='fourslash.ts' />

const singleLineOffset = 3;
const multiLineOffset = 12;
const multiLineOffset = 11;


////class C {
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/docCommentTemplateConstructor01.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
//// }
////}

const newTextOffset = 12;
const newTextOffset = 11;
verify.docCommentTemplateAt("0", /*newTextOffset*/ newTextOffset,
`/**
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
//// exports.foo = (a) => {};


verify.docCommentTemplateAt("", 8,
verify.docCommentTemplateAt("", 7,
`/**
*
* @param {any} a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
////const x = /*next*/ function f(p) {}

for (const marker of test.markerNames()) {
verify.docCommentTemplateAt(marker, 8,
verify.docCommentTemplateAt(marker, 7,
`/**
*
* @param p
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

const noIndentScaffolding = "/**\n * \n * @param x\n * @param y\n */";
const oneIndentScaffolding = "/**\n * \n * @param x\n * @param y\n */";
const noIndentOffset = 8;
const noIndentOffset = 7;
const oneIndentOffset = noIndentOffset + 4;

goTo.marker("0");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
/////*0*/
////function f(a, ...b): boolean {}

verify.docCommentTemplateAt("0", 8,
verify.docCommentTemplateAt("0", 7,
`/**
*
* @param {any} a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
//// foo: (a: number, b: string) => void;
////}

verify.docCommentTemplateAt("", 12,
verify.docCommentTemplateAt("", 11,
`/**
*
* @param a
* @param b
* @returns
*/`);

verify.docCommentTemplateAt("", 12,
verify.docCommentTemplateAt("", 11,
`/**
*
* @param a
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ verify.docCommentTemplateAt("interfaceFoo", /*expectedOffset*/ 3,
verify.docCommentTemplateAt("propertybar", /*expectedOffset*/ 3,
"/** */");

verify.docCommentTemplateAt("methodbaz", /*expectedOffset*/ 12,
verify.docCommentTemplateAt("methodbaz", /*expectedOffset*/ 11,
`/**
*
* @param message
Expand All @@ -46,4 +46,4 @@ verify.docCommentTemplateAt("memberOpen", /*expectedOffset*/ 3,
"/** */");

verify.docCommentTemplateAt("memberClosed", /*expectedOffset*/ 3,
"/** */");
"/** */");
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
////const myNamespace = {};
/////*1*/myNamespace.myExport = function(x) {};

verify.docCommentTemplateAt("0", 8,
verify.docCommentTemplateAt("0", 7,
`/**
*
* @param {any} a
*/
`);

verify.docCommentTemplateAt("1", 8,
verify.docCommentTemplateAt("1", 7,
`/**
*
* @param {any} x
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference path='fourslash.ts' />

const multiLineOffset = 12;
const multiLineOffset = 11;

////var x = {
//// /*0*/
Expand Down
2 changes: 1 addition & 1 deletion tests/cases/fourslash/docCommentTemplatePrototypeMethod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
////C.prototype.method = /*next*/ function (p) {}

for (const marker of test.markerNames()) {
verify.docCommentTemplateAt(marker, 8,
verify.docCommentTemplateAt(marker, 7,
`/**
*
* @param {any} p
Expand Down
8 changes: 4 additions & 4 deletions tests/cases/fourslash/docCommentTemplateReturnsTag1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,19 @@

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

verify.docCommentTemplateAt("1", 8,
verify.docCommentTemplateAt("1", 7,
`/**
*
* @returns
*/`);

verify.docCommentTemplateAt("2", 8,
verify.docCommentTemplateAt("2", 7,
`/**
*
* @returns
*/`);

verify.docCommentTemplateAt("3", 8,
verify.docCommentTemplateAt("3", 7,
`/**
*
* @returns
Expand All @@ -49,7 +49,7 @@ verify.docCommentTemplateAt("3", 8,
verify.docCommentTemplateAt("4", 3, "/** */");


verify.docCommentTemplateAt("5", 12,
verify.docCommentTemplateAt("5", 11,
`/**
*
* @returns
Expand Down
4 changes: 2 additions & 2 deletions tests/cases/fourslash/docCommentTemplateReturnsTag2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
//// return 1;
////}

verify.docCommentTemplateAt("0", 8,
verify.docCommentTemplateAt("0", 7,
`/**
*
* @param x
* @param y
* @returns
*/`, { generateReturnInDocTemplate: true });

verify.docCommentTemplateAt("0", 8,
verify.docCommentTemplateAt("0", 7,
`/**
*
* @param x
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ for (const varName of ["a", "b", "c", "d"]) {
"/** */");
}

verify.docCommentTemplateAt("e", /*newTextOffset*/ 8,
verify.docCommentTemplateAt("e", /*newTextOffset*/ 7,
`/**
*
* @param x
Expand All @@ -42,10 +42,10 @@ verify.docCommentTemplateAt("e", /*newTextOffset*/ 8,
* @returns
*/`);

verify.docCommentTemplateAt("f", /*newTextOffset*/ 8,
verify.docCommentTemplateAt("f", /*newTextOffset*/ 7,
`/**
*
* @param a
* @param b
* @param c
*/`);
*/`);
12 changes: 6 additions & 6 deletions tests/cases/fourslash/docCommentTemplateVariableStatements03.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,14 @@
//// }
////}))

verify.docCommentTemplateAt("a", /*newTextOffset*/ 8,
verify.docCommentTemplateAt("a", /*newTextOffset*/ 7,
`/**
*
* @param x
* @returns
*/`);

verify.docCommentTemplateAt("b", /*newTextOffset*/ 8,
verify.docCommentTemplateAt("b", /*newTextOffset*/ 7,
`/**
*
* @param x
Expand All @@ -45,7 +45,7 @@ verify.docCommentTemplateAt("b", /*newTextOffset*/ 8,
* @returns
*/`);

verify.docCommentTemplateAt("c", /*newTextOffset*/ 8,
verify.docCommentTemplateAt("c", /*newTextOffset*/ 7,
`/**
*
* @param x
Expand All @@ -55,7 +55,7 @@ verify.docCommentTemplateAt("c", /*newTextOffset*/ 8,
verify.docCommentTemplateAt("d", /*newTextOffset*/ 3,
"/** */");

verify.docCommentTemplateAt("e", /*newTextOffset*/ 8,
verify.docCommentTemplateAt("e", /*newTextOffset*/ 7,
`/**
*
* @param param0
Expand All @@ -65,8 +65,8 @@ verify.docCommentTemplateAt("e", /*newTextOffset*/ 8,
verify.docCommentTemplateAt("f", /*newTextOffset*/ 3,
"/** */");

verify.docCommentTemplateAt("g", /*newTextOffset*/ 8,
verify.docCommentTemplateAt("g", /*newTextOffset*/ 7,
`/**
*
* @param x
*/`);
*/`);
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/////** Doc/*1*/ */
////function g(p) { return p; }

verify.docCommentTemplateAt("", /*newTextOffset*/ 8,
verify.docCommentTemplateAt("", /*newTextOffset*/ 7,
`/**
*
* @param p
Expand Down