From 6d174a0f18d6406e16dd527fa24481296f610c71 Mon Sep 17 00:00:00 2001 From: Arthur Ozga Date: Mon, 20 Jul 2015 18:31:17 -0700 Subject: [PATCH] Adjust carat after insertion --- src/harness/harnessLanguageService.ts | 2 +- src/services/services.ts | 23 +++++++++++++++++------ src/services/shims.ts | 11 +++++------ 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/harness/harnessLanguageService.ts b/src/harness/harnessLanguageService.ts index c6b472e79e5..bb87fb43609 100644 --- a/src/harness/harnessLanguageService.ts +++ b/src/harness/harnessLanguageService.ts @@ -381,7 +381,7 @@ module Harness.LanguageService { getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: ts.FormatCodeOptions): ts.TextChange[] { return unwrapJSONCallResult(this.shim.getFormattingEditsAfterKeystroke(fileName, position, key, JSON.stringify(options))); } - getDocCommentScaffoldingAtPosition(fileName: string, position: number): string { + getDocCommentScaffoldingAtPosition(fileName: string, position: number): ts.TextInsertion { return unwrapJSONCallResult(this.shim.getDocCommentScaffoldingAtPosition(fileName, position)); } getEmitOutput(fileName: string): ts.EmitOutput { diff --git a/src/services/services.ts b/src/services/services.ts index 058029af9f4..f91cfa10f1b 100644 --- a/src/services/services.ts +++ b/src/services/services.ts @@ -1035,7 +1035,7 @@ namespace ts { getFormattingEditsForDocument(fileName: string, options: FormatCodeOptions): TextChange[]; getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: FormatCodeOptions): TextChange[]; - getDocCommentScaffoldingAtPosition(fileName: string, position: number): string; + getDocCommentScaffoldingAtPosition(fileName: string, position: number): TextInsertion; getEmitOutput(fileName: string): EmitOutput; @@ -1083,6 +1083,11 @@ namespace ts { newText: string; } + export interface TextInsertion { + newText: string; + cursorOffset: number; + } + export interface RenameLocation { textSpan: TextSpan; fileName: string; @@ -6755,9 +6760,9 @@ namespace ts { * @param position The (character-indexed) position in the file where the check should * be performed. */ - function getDocCommentScaffoldingAtPosition(fileName: string, position: number): string { - const nullResult = "/**"; - const emptyCompletion = "/** */"; + function getDocCommentScaffoldingAtPosition(fileName: string, position: number): TextInsertion { + const nullResult = { newText: "/**", cursorOffset: 3 }; + const emptyCompletion = { newText: "/** */", cursorOffset: 3 }; let start = new Date().getTime(); let sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName); log("getDocCommentScaffoldingAtPosition: getCurrentSourceFile: " + (new Date().getTime() - start)); @@ -6783,9 +6788,15 @@ namespace ts { let docParams = parameters.map((p, index) => indentationStr + " * @param " + (p.name.kind === SyntaxKind.Identifier ? (p.name).text : "param" + index.toString()) + "\n"); - let result = "/**\n" + docParams.reduce((prev, cur) => prev + cur, "") + indentationStr + " */"; + let result = + /* opening comment */ "/**\n" + + /* first line for function info */ indentationStr + " * \n" + + /* paramters */ docParams.reduce((prev, cur) => prev + cur, "") + + /* closing comment */ indentationStr + " */"; + + let cursorOffset = /* "/**\n" */ 4 + indentationStr.length + /* " * " */ 3; - return result; + return {newText: result, cursorOffset: cursorOffset }; } function getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[] { diff --git a/src/services/shims.ts b/src/services/shims.ts index 523ca00056c..0a783a256d2 100644 --- a/src/services/shims.ts +++ b/src/services/shims.ts @@ -205,6 +205,9 @@ namespace ts { getFormattingEditsForDocument(fileName: string, options: string/*Services.FormatCodeOptions*/): string; getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, options: string/*Services.FormatCodeOptions*/): string; + /** + * Returns JSON-encoded value of the type TextInsertion. + */ getDocCommentScaffoldingAtPosition(fileName: string, position: number): string; getEmitOutput(fileName: string): string; @@ -816,12 +819,8 @@ namespace ts { public getDocCommentScaffoldingAtPosition(fileName: string, position: number): string { return this.forwardJSONCall( "getDocCommentScaffoldingAtPosition('" + fileName + "', " + position + ")", - () => { - var commentText = this.languageService.getDocCommentScaffoldingAtPosition(fileName, position); - return commentText; - } - - ) + () => this.languageService.getDocCommentScaffoldingAtPosition(fileName, position) + ); } /// NAVIGATE TO