Truncate to 150 chars and added unit tests
This commit is contained in:
parent
ce133ec2dd
commit
98729b4462
|
@ -15,6 +15,12 @@ namespace ts.NavigationBar {
|
||||||
*/
|
*/
|
||||||
const whiteSpaceRegex = /\s+/g;
|
const whiteSpaceRegex = /\s+/g;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Maximum amount of characters to return
|
||||||
|
* The amount was choosen arbitrarily.
|
||||||
|
*/
|
||||||
|
const maxLength = 150;
|
||||||
|
|
||||||
// Keep sourceFile handy so we don't have to search for it every time we need to call `getText`.
|
// Keep sourceFile handy so we don't have to search for it every time we need to call `getText`.
|
||||||
let curCancellationToken: CancellationToken;
|
let curCancellationToken: CancellationToken;
|
||||||
let curSourceFile: SourceFile;
|
let curSourceFile: SourceFile;
|
||||||
|
@ -74,7 +80,7 @@ namespace ts.NavigationBar {
|
||||||
}
|
}
|
||||||
|
|
||||||
function nodeText(node: Node): string {
|
function nodeText(node: Node): string {
|
||||||
return node.getText(curSourceFile);
|
return cleanText(node.getText(curSourceFile));
|
||||||
}
|
}
|
||||||
|
|
||||||
function navigationBarNodeKind(n: NavigationBarNode): SyntaxKind {
|
function navigationBarNodeKind(n: NavigationBarNode): SyntaxKind {
|
||||||
|
@ -434,13 +440,13 @@ namespace ts.NavigationBar {
|
||||||
|
|
||||||
function getItemName(node: Node, name: Node | undefined): string {
|
function getItemName(node: Node, name: Node | undefined): string {
|
||||||
if (node.kind === SyntaxKind.ModuleDeclaration) {
|
if (node.kind === SyntaxKind.ModuleDeclaration) {
|
||||||
return getModuleName(<ModuleDeclaration>node);
|
return cleanText(getModuleName(<ModuleDeclaration>node));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (name) {
|
if (name) {
|
||||||
const text = nodeText(name);
|
const text = nodeText(name);
|
||||||
if (text.length > 0) {
|
if (text.length > 0) {
|
||||||
return text;
|
return cleanText(text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -636,11 +642,11 @@ namespace ts.NavigationBar {
|
||||||
function getFunctionOrClassName(node: FunctionExpression | FunctionDeclaration | ArrowFunction | ClassLikeDeclaration): string {
|
function getFunctionOrClassName(node: FunctionExpression | FunctionDeclaration | ArrowFunction | ClassLikeDeclaration): string {
|
||||||
const { parent } = node;
|
const { parent } = node;
|
||||||
if (node.name && getFullWidth(node.name) > 0) {
|
if (node.name && getFullWidth(node.name) > 0) {
|
||||||
return declarationNameToString(node.name);
|
return cleanText(declarationNameToString(node.name));
|
||||||
}
|
}
|
||||||
// See if it is a var initializer. If so, use the var name.
|
// See if it is a var initializer. If so, use the var name.
|
||||||
else if (isVariableDeclaration(parent)) {
|
else if (isVariableDeclaration(parent)) {
|
||||||
return declarationNameToString(parent.name);
|
return cleanText(declarationNameToString(parent.name));
|
||||||
}
|
}
|
||||||
// See if it is of the form "<expr> = function(){...}". If so, use the text from the left-hand side.
|
// See if it is of the form "<expr> = function(){...}". If so, use the text from the left-hand side.
|
||||||
else if (isBinaryExpression(parent) && parent.operatorToken.kind === SyntaxKind.EqualsToken) {
|
else if (isBinaryExpression(parent) && parent.operatorToken.kind === SyntaxKind.EqualsToken) {
|
||||||
|
@ -658,18 +664,15 @@ namespace ts.NavigationBar {
|
||||||
return "<class>";
|
return "<class>";
|
||||||
}
|
}
|
||||||
else if (isCallExpression(parent)) {
|
else if (isCallExpression(parent)) {
|
||||||
const name = getCalledExpressionName(parent.expression);
|
let name = getCalledExpressionName(parent.expression);
|
||||||
if (name !== undefined) {
|
if (name !== undefined) {
|
||||||
const args = mapDefined(parent.arguments, a => {
|
name = cleanText(name);
|
||||||
if (isStringLiteralLike(a)) {
|
|
||||||
const line = curSourceFile.getLineAndCharacterOfPosition(a.pos).line;
|
|
||||||
const endOfLine = getEndLinePosition(line, curSourceFile);
|
|
||||||
|
|
||||||
return a.getText(curSourceFile).substring(0, endOfLine - a.pos);
|
if (name.length > maxLength) {
|
||||||
}
|
return `${name} callback`;
|
||||||
|
}
|
||||||
|
|
||||||
return undefined;
|
const args = cleanText(mapDefined(parent.arguments, a => isStringLiteralLike(a) ? a.getText(curSourceFile) : undefined).join(", "));
|
||||||
}).join(", ");
|
|
||||||
return `${name}(${args}) callback`;
|
return `${name}(${args}) callback`;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -700,4 +703,16 @@ namespace ts.NavigationBar {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cleanText(text: string): string {
|
||||||
|
// Truncate to maximum amount of characters as we don't want to do a big replace operation.
|
||||||
|
text = text.length > maxLength ? text.substring(0, maxLength) + "..." : text;
|
||||||
|
|
||||||
|
// Replaces ECMAScript line terminators and removes the trailing `\` from each line:
|
||||||
|
// \n - Line Feed
|
||||||
|
// \r - Carriage Return
|
||||||
|
// \u2028 - Line separator
|
||||||
|
// \u2029 - Paragraph separator
|
||||||
|
return text.replace(/\\?(\r?\n|\u2028|\u2029)/g, '');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,10 @@
|
||||||
////}
|
////}
|
||||||
////declare module "MultilineMadness" {}
|
////declare module "MultilineMadness" {}
|
||||||
////
|
////
|
||||||
|
////declare module "Multiline\
|
||||||
|
////Madness2" {
|
||||||
|
////}
|
||||||
|
////
|
||||||
////interface Foo {
|
////interface Foo {
|
||||||
//// "a1\\\r\nb";
|
//// "a1\\\r\nb";
|
||||||
//// "a2\
|
//// "a2\
|
||||||
|
@ -29,7 +33,12 @@ verify.navigationTree({
|
||||||
"kind": "script",
|
"kind": "script",
|
||||||
"childItems": [
|
"childItems": [
|
||||||
{
|
{
|
||||||
"text": "\"Multiline\\\nMadness\"",
|
"text": "\"MultilineMadness\"",
|
||||||
|
"kind": "module",
|
||||||
|
"kindModifiers": "declare"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "\"MultilineMadness2\"",
|
||||||
"kind": "module",
|
"kind": "module",
|
||||||
"kindModifiers": "declare"
|
"kindModifiers": "declare"
|
||||||
},
|
},
|
||||||
|
@ -38,11 +47,6 @@ verify.navigationTree({
|
||||||
"kind": "module",
|
"kind": "module",
|
||||||
"kindModifiers": "declare"
|
"kindModifiers": "declare"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"text": "\"MultilineMadness\"",
|
|
||||||
"kind": "module",
|
|
||||||
"kindModifiers": "declare"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"text": "Bar",
|
"text": "Bar",
|
||||||
"kind": "class",
|
"kind": "class",
|
||||||
|
@ -52,7 +56,7 @@ verify.navigationTree({
|
||||||
"kind": "property"
|
"kind": "property"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"text": "'a2\\\n \\\n b'",
|
"text": "'a2 b'",
|
||||||
"kind": "method"
|
"kind": "method"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -66,7 +70,7 @@ verify.navigationTree({
|
||||||
"kind": "property"
|
"kind": "property"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"text": "\"a2\\\n \\\n b\"",
|
"text": "\"a2 b\"",
|
||||||
"kind": "method"
|
"kind": "method"
|
||||||
}
|
}
|
||||||
]
|
]
|
||||||
|
@ -80,7 +84,12 @@ verify.navigationBar([
|
||||||
"kind": "script",
|
"kind": "script",
|
||||||
"childItems": [
|
"childItems": [
|
||||||
{
|
{
|
||||||
"text": "\"Multiline\\\nMadness\"",
|
"text": "\"MultilineMadness\"",
|
||||||
|
"kind": "module",
|
||||||
|
"kindModifiers": "declare"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "\"MultilineMadness2\"",
|
||||||
"kind": "module",
|
"kind": "module",
|
||||||
"kindModifiers": "declare"
|
"kindModifiers": "declare"
|
||||||
},
|
},
|
||||||
|
@ -89,11 +98,6 @@ verify.navigationBar([
|
||||||
"kind": "module",
|
"kind": "module",
|
||||||
"kindModifiers": "declare"
|
"kindModifiers": "declare"
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"text": "\"MultilineMadness\"",
|
|
||||||
"kind": "module",
|
|
||||||
"kindModifiers": "declare"
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"text": "Bar",
|
"text": "Bar",
|
||||||
"kind": "class"
|
"kind": "class"
|
||||||
|
@ -105,7 +109,13 @@ verify.navigationBar([
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"text": "\"Multiline\\\nMadness\"",
|
"text": "\"MultilineMadness\"",
|
||||||
|
"kind": "module",
|
||||||
|
"kindModifiers": "declare",
|
||||||
|
"indent": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "\"MultilineMadness2\"",
|
||||||
"kind": "module",
|
"kind": "module",
|
||||||
"kindModifiers": "declare",
|
"kindModifiers": "declare",
|
||||||
"indent": 1
|
"indent": 1
|
||||||
|
@ -116,12 +126,6 @@ verify.navigationBar([
|
||||||
"kindModifiers": "declare",
|
"kindModifiers": "declare",
|
||||||
"indent": 1
|
"indent": 1
|
||||||
},
|
},
|
||||||
{
|
|
||||||
"text": "\"MultilineMadness\"",
|
|
||||||
"kind": "module",
|
|
||||||
"kindModifiers": "declare",
|
|
||||||
"indent": 1
|
|
||||||
},
|
|
||||||
{
|
{
|
||||||
"text": "Bar",
|
"text": "Bar",
|
||||||
"kind": "class",
|
"kind": "class",
|
||||||
|
@ -131,7 +135,7 @@ verify.navigationBar([
|
||||||
"kind": "property"
|
"kind": "property"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"text": "'a2\\\n \\\n b'",
|
"text": "'a2 b'",
|
||||||
"kind": "method"
|
"kind": "method"
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
|
@ -146,7 +150,7 @@ verify.navigationBar([
|
||||||
"kind": "property"
|
"kind": "property"
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"text": "\"a2\\\n \\\n b\"",
|
"text": "\"a2 b\"",
|
||||||
"kind": "method"
|
"kind": "method"
|
||||||
}
|
}
|
||||||
],
|
],
|
|
@ -0,0 +1,85 @@
|
||||||
|
|
||||||
|
//// function f(p1: () => any, p2: string) { }
|
||||||
|
//// f(() => { }, `line1\
|
||||||
|
//// line2\
|
||||||
|
//// line3`);
|
||||||
|
////
|
||||||
|
//// class c1 {
|
||||||
|
//// const a = ' ''line1\
|
||||||
|
//// line2';
|
||||||
|
//// }
|
||||||
|
|
||||||
|
verify.navigationTree({
|
||||||
|
"text": "<global>",
|
||||||
|
"kind": "script",
|
||||||
|
"childItems": [
|
||||||
|
{
|
||||||
|
"text": "c1",
|
||||||
|
"kind": "class",
|
||||||
|
"childItems": [
|
||||||
|
{
|
||||||
|
"text": "a",
|
||||||
|
"kind": "property"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "'line1 line2'",
|
||||||
|
"kind": "property"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "f",
|
||||||
|
"kind": "function"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "f(`line1line2line3`) callback",
|
||||||
|
"kind": "function"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
verify.navigationBar([
|
||||||
|
{
|
||||||
|
"text": "<global>",
|
||||||
|
"kind": "script",
|
||||||
|
"childItems": [
|
||||||
|
{
|
||||||
|
"text": "c1",
|
||||||
|
"kind": "class"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "f",
|
||||||
|
"kind": "function"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "f(`line1line2line3`) callback",
|
||||||
|
"kind": "function"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "c1",
|
||||||
|
"kind": "class",
|
||||||
|
"childItems": [
|
||||||
|
{
|
||||||
|
"text": "a",
|
||||||
|
"kind": "property"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "'line1 line2'",
|
||||||
|
"kind": "property"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"indent": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "f",
|
||||||
|
"kind": "function",
|
||||||
|
"indent": 1
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "f(`line1line2line3`) callback",
|
||||||
|
"kind": "function",
|
||||||
|
"indent": 1
|
||||||
|
}
|
||||||
|
]);
|
|
@ -0,0 +1,40 @@
|
||||||
|
|
||||||
|
//// declare module 'MoreThanOneHundredAndFiftyCharacters\
|
||||||
|
//// MoreThanOneHundredAndFiftyCharacters\
|
||||||
|
//// MoreThanOneHundredAndFiftyCharacters\
|
||||||
|
//// MoreThanOneHundredAndFiftyCharacters\
|
||||||
|
//// MoreThanOneHundredAndFiftyCharacters\
|
||||||
|
//// MoreThanOneHundredAndFiftyCharacters\
|
||||||
|
//// MoreThanOneHundredAndFiftyCharacters' { }
|
||||||
|
|
||||||
|
verify.navigationTree({
|
||||||
|
"text": "<global>",
|
||||||
|
"kind": "script",
|
||||||
|
"childItems": [
|
||||||
|
{
|
||||||
|
"text": "'MoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharacter...",
|
||||||
|
"kind": "module",
|
||||||
|
"kindModifiers": "declare"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
verify.navigationBar([
|
||||||
|
{
|
||||||
|
"text": "<global>",
|
||||||
|
"kind": "script",
|
||||||
|
"childItems": [
|
||||||
|
{
|
||||||
|
"text": "'MoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharacter...",
|
||||||
|
"kind": "module",
|
||||||
|
"kindModifiers": "declare"
|
||||||
|
}
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"text": "'MoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharacter...",
|
||||||
|
"kind": "module",
|
||||||
|
"kindModifiers": "declare",
|
||||||
|
"indent": 1
|
||||||
|
}
|
||||||
|
]);
|
Loading…
Reference in a new issue