refactoring

This commit is contained in:
Arthur Ozga 2015-07-31 11:58:52 -07:00
parent 082a5ade47
commit 2540905075
3 changed files with 35 additions and 31 deletions

View file

@ -1944,12 +1944,24 @@ module FourSlash {
public verifyDocCommentScaffolding(expected: ts.TextInsertion) {
let actual = this.languageService.getDocCommentScaffoldingAtPosition(this.activeFile.fileName, this.currentCaretPosition);
if (expected === undefined) {
if (actual === undefined) {
return;
}
else {
this.raiseError('verifyDocCommentScaffolding failed - expected no scaffolding but got {newText: \"' + actual.newText + '\" offsetInNewText: ' + actual.offsetInNewText + '}');
}
}
if (expected !== undefined && actual === undefined) {
this.raiseError('verifyDocCommentScaffolding failed - expected the scaffolding {newText: \"' + actual.newText + '\" offsetInNewText: ' + actual.offsetInNewText + '} but got nothin instead');
}
if (actual.newText !== expected.newText) {
this.raiseError('verifyDocCommentScaffolding failed - expected insertion:\n' + expected.newText + '\nactual insertion:\n' + actual.newText);
}
if (actual.cursorOffset !== expected.cursorOffset) {
this.raiseError('verifyDocCommentScaffolding failed - expected cursorOffset: ' + expected.cursorOffset + ',\tactual cursorOffset:' + actual.cursorOffset);
if (actual.offsetInNewText !== expected.offsetInNewText) {
this.raiseError('verifyDocCommentScaffolding failed - expected cursorOffset: ' + expected.offsetInNewText + ',\tactual cursorOffset:' + actual.offsetInNewText);
}
}

View file

@ -1090,7 +1090,7 @@ namespace ts {
export interface TextInsertion {
newText: string;
cursorOffset: number;
offsetInNewText: number;
}
export interface RenameLocation {
@ -6801,7 +6801,7 @@ namespace ts {
* * outside of comments, statements, and expressions, and
* * preceding a function declaration.
*
* In VS, we additionally check that:
* Hosts should ideally check that:
* * The line is all whitespace up to 'position' before performing the insertion.
* * If the keystroke sequence "/\*\*" induced the call, we also check that the next
* non-whitespace character is '*', which (approximately) indicates whether we added
@ -6811,27 +6811,25 @@ namespace ts {
* be performed.
*/
function getDocCommentScaffoldingAtPosition(fileName: string, position: number): TextInsertion {
// Indicates the position is not appropriate to insert a comment (eg: a string or a regex).
const nullResult: TextInsertion = { newText: "", cursorOffset: 0 };
let start = new Date().getTime();
let sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName);
log("getDocCommentScaffoldingAtPosition: getCurrentSourceFile: " + (new Date().getTime() - start));
// Check if in a context where we don't want to perform any insertion
if (isInString(sourceFile, position) || isInComment(sourceFile, position) || hasDocComment(sourceFile, position)) {
return nullResult;
return undefined;
}
let nodeAtPos = getTokenAtPosition(sourceFile, position);
let nodeStart = nodeAtPos.getStart()
if (!nodeAtPos || nodeStart < position) {
return nullResult;
let tokenAtPos = getTokenAtPosition(sourceFile, position);
let tokenStart = tokenAtPos.getStart()
if (!tokenAtPos || tokenStart < position) {
return undefined;
}
let containingFunction = <FunctionDeclaration>getAncestor(nodeAtPos, SyntaxKind.FunctionDeclaration);
let containingFunction = <FunctionDeclaration>getAncestor(tokenAtPos, SyntaxKind.FunctionDeclaration);
if (!containingFunction || containingFunction.getStart() < position) {
return nullResult;
return undefined;
}
let parameters = containingFunction.parameters;
@ -6840,19 +6838,21 @@ namespace ts {
let indentationStr = sourceFile.text.substr(lineStart, posLineAndChar.character).match(/\s*/).toString();
const newLine = host.getNewLine();
let docParams = parameters.map((p, index) =>
indentationStr + " * @param " + (p.name.kind === SyntaxKind.Identifier ? (<Identifier>p.name).text : "param" + index.toString()) + "\n");
indentationStr + " * @param " + (p.name.kind === SyntaxKind.Identifier ? (<Identifier>p.name).text : "param" + index.toString()) + newLine);
let result =
/* opening comment */ "/**\n" +
/* first line for function info */ indentationStr + " * \n" +
/* opening comment */ "/**" + newLine +
/* first line for function info */ indentationStr + " * " + newLine +
/* paramters */ docParams.reduce((prev, cur) => prev + cur, "") +
/* closing comment */ indentationStr + " */" +
/* newline if at decl start */ (nodeStart === position ? "\n" + indentationStr : "");
/* newline if at decl start */ (tokenStart === position ? newLine + indentationStr : "");
let cursorOffset = /* "/**\n" */ 4 + indentationStr.length + /* " * " */ 3;
let cursorOffset = /* "/**" */ 3 + /* newLine */ + newLine.length + indentationStr.length + /* " * " */ 3;
return {newText: result, cursorOffset: cursorOffset };
return {newText: result, offsetInNewText: cursorOffset };
}
function getTodoComments(fileName: string, descriptors: TodoCommentDescriptor[]): TodoComment[] {

View file

@ -379,20 +379,12 @@ module FourSlashInterface {
}
// Will fix in fourslash-referencing
public DocCommentScaffolding(expectedText: string, expectedOffset: number) {
FourSlash.currentTestState.verifyDocCommentScaffolding({ newText: expectedText, cursorOffset: expectedOffset });
public DocCommentScaffolding(expectedText: string, expectedOffset: number, empty?: boolean) {
FourSlash.currentTestState.verifyDocCommentScaffolding(empty ? undefined : { newText: expectedText, cursorOffset: expectedOffset });
}
public noDocCommentScaffolding() {
const expectedText = "";
const expectedOffset = 0;
this.DocCommentScaffolding(expectedText, expectedOffset);
}
public emptyTemplateDocCommentScaffolding() {
const expectedText = "/** */";
const expectedOffset = 3;
this.DocCommentScaffolding(expectedText, expectedOffset);
this.DocCommentScaffolding(/*expectedText*/ undefined, /*expectedOffset*/ undefined, true);
}
public getScriptLexicalStructureListCount(count: number) {