Skip to content

Commit 25f609b

Browse files
authored
Merge pull request #32672 from armanio123/FixNavbarMultiline
Fixed issue for navbar when having multi-line string literals
2 parents 5b59cfb + f76e3b5 commit 25f609b

4 files changed

+174
-21
lines changed

src/services/navigationBar.ts

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ namespace ts.NavigationBar {
1515
*/
1616
const whiteSpaceRegex = /\s+/g;
1717

18+
/**
19+
* Maximum amount of characters to return
20+
* The amount was choosen arbitrarily.
21+
*/
22+
const maxLength = 150;
23+
1824
// Keep sourceFile handy so we don't have to search for it every time we need to call `getText`.
1925
let curCancellationToken: CancellationToken;
2026
let curSourceFile: SourceFile;
@@ -74,7 +80,7 @@ namespace ts.NavigationBar {
7480
}
7581

7682
function nodeText(node: Node): string {
77-
return node.getText(curSourceFile);
83+
return cleanText(node.getText(curSourceFile));
7884
}
7985

8086
function navigationBarNodeKind(n: NavigationBarNode): SyntaxKind {
@@ -194,7 +200,7 @@ namespace ts.NavigationBar {
194200
// Handle named bindings in imports e.g.:
195201
// import * as NS from "mod";
196202
// import {a, b as B} from "mod";
197-
const {namedBindings} = importClause;
203+
const { namedBindings } = importClause;
198204
if (namedBindings) {
199205
if (namedBindings.kind === SyntaxKind.NamespaceImport) {
200206
addLeafNode(namedBindings);
@@ -434,13 +440,13 @@ namespace ts.NavigationBar {
434440

435441
function getItemName(node: Node, name: Node | undefined): string {
436442
if (node.kind === SyntaxKind.ModuleDeclaration) {
437-
return getModuleName(<ModuleDeclaration>node);
443+
return cleanText(getModuleName(<ModuleDeclaration>node));
438444
}
439445

440446
if (name) {
441447
const text = nodeText(name);
442448
if (text.length > 0) {
443-
return text;
449+
return cleanText(text);
444450
}
445451
}
446452

@@ -636,11 +642,11 @@ namespace ts.NavigationBar {
636642
function getFunctionOrClassName(node: FunctionExpression | FunctionDeclaration | ArrowFunction | ClassLikeDeclaration): string {
637643
const { parent } = node;
638644
if (node.name && getFullWidth(node.name) > 0) {
639-
return declarationNameToString(node.name);
645+
return cleanText(declarationNameToString(node.name));
640646
}
641647
// See if it is a var initializer. If so, use the var name.
642648
else if (isVariableDeclaration(parent)) {
643-
return declarationNameToString(parent.name);
649+
return cleanText(declarationNameToString(parent.name));
644650
}
645651
// See if it is of the form "<expr> = function(){...}". If so, use the text from the left-hand side.
646652
else if (isBinaryExpression(parent) && parent.operatorToken.kind === SyntaxKind.EqualsToken) {
@@ -658,9 +664,15 @@ namespace ts.NavigationBar {
658664
return "<class>";
659665
}
660666
else if (isCallExpression(parent)) {
661-
const name = getCalledExpressionName(parent.expression);
667+
let name = getCalledExpressionName(parent.expression);
662668
if (name !== undefined) {
663-
const args = mapDefined(parent.arguments, a => isStringLiteralLike(a) ? a.getText(curSourceFile) : undefined).join(", ");
669+
name = cleanText(name);
670+
671+
if (name.length > maxLength) {
672+
return `${name} callback`;
673+
}
674+
675+
const args = cleanText(mapDefined(parent.arguments, a => isStringLiteralLike(a) ? a.getText(curSourceFile) : undefined).join(", "));
664676
return `${name}(${args}) callback`;
665677
}
666678
}
@@ -691,4 +703,16 @@ namespace ts.NavigationBar {
691703
return false;
692704
}
693705
}
706+
707+
function cleanText(text: string): string {
708+
// Truncate to maximum amount of characters as we don't want to do a big replace operation.
709+
text = text.length > maxLength ? text.substring(0, maxLength) + "..." : text;
710+
711+
// Replaces ECMAScript line terminators and removes the trailing `\` from each line:
712+
// \n - Line Feed
713+
// \r - Carriage Return
714+
// \u2028 - Line separator
715+
// \u2029 - Paragraph separator
716+
return text.replace(/\\(\r?\n|\r|\u2028|\u2029)/g, "");
717+
}
694718
}

tests/cases/fourslash/navigationBarItemsMultilineStringIdentifiers.ts renamed to tests/cases/fourslash/navigationBarItemsMultilineStringIdentifiers1.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
////}
88
////declare module "MultilineMadness" {}
99
////
10+
////declare module "Multiline\
11+
////Madness2" {
12+
////}
13+
////
1014
////interface Foo {
1115
//// "a1\\\r\nb";
1216
//// "a2\
@@ -29,17 +33,17 @@ verify.navigationTree({
2933
"kind": "script",
3034
"childItems": [
3135
{
32-
"text": "\"Multiline\\\nMadness\"",
36+
"text": "\"MultilineMadness\"",
3337
"kind": "module",
3438
"kindModifiers": "declare"
3539
},
3640
{
37-
"text": "\"Multiline\\r\\nMadness\"",
41+
"text": "\"MultilineMadness2\"",
3842
"kind": "module",
3943
"kindModifiers": "declare"
4044
},
4145
{
42-
"text": "\"MultilineMadness\"",
46+
"text": "\"Multiline\\r\\nMadness\"",
4347
"kind": "module",
4448
"kindModifiers": "declare"
4549
},
@@ -52,7 +56,7 @@ verify.navigationTree({
5256
"kind": "property"
5357
},
5458
{
55-
"text": "'a2\\\n \\\n b'",
59+
"text": "'a2 b'",
5660
"kind": "method"
5761
}
5862
]
@@ -66,7 +70,7 @@ verify.navigationTree({
6670
"kind": "property"
6771
},
6872
{
69-
"text": "\"a2\\\n \\\n b\"",
73+
"text": "\"a2 b\"",
7074
"kind": "method"
7175
}
7276
]
@@ -80,17 +84,17 @@ verify.navigationBar([
8084
"kind": "script",
8185
"childItems": [
8286
{
83-
"text": "\"Multiline\\\nMadness\"",
87+
"text": "\"MultilineMadness\"",
8488
"kind": "module",
8589
"kindModifiers": "declare"
8690
},
8791
{
88-
"text": "\"Multiline\\r\\nMadness\"",
92+
"text": "\"MultilineMadness2\"",
8993
"kind": "module",
9094
"kindModifiers": "declare"
9195
},
9296
{
93-
"text": "\"MultilineMadness\"",
97+
"text": "\"Multiline\\r\\nMadness\"",
9498
"kind": "module",
9599
"kindModifiers": "declare"
96100
},
@@ -105,19 +109,19 @@ verify.navigationBar([
105109
]
106110
},
107111
{
108-
"text": "\"Multiline\\\nMadness\"",
112+
"text": "\"MultilineMadness\"",
109113
"kind": "module",
110114
"kindModifiers": "declare",
111115
"indent": 1
112116
},
113117
{
114-
"text": "\"Multiline\\r\\nMadness\"",
118+
"text": "\"MultilineMadness2\"",
115119
"kind": "module",
116120
"kindModifiers": "declare",
117121
"indent": 1
118122
},
119123
{
120-
"text": "\"MultilineMadness\"",
124+
"text": "\"Multiline\\r\\nMadness\"",
121125
"kind": "module",
122126
"kindModifiers": "declare",
123127
"indent": 1
@@ -131,7 +135,7 @@ verify.navigationBar([
131135
"kind": "property"
132136
},
133137
{
134-
"text": "'a2\\\n \\\n b'",
138+
"text": "'a2 b'",
135139
"kind": "method"
136140
}
137141
],
@@ -146,7 +150,7 @@ verify.navigationBar([
146150
"kind": "property"
147151
},
148152
{
149-
"text": "\"a2\\\n \\\n b\"",
153+
"text": "\"a2 b\"",
150154
"kind": "method"
151155
}
152156
],
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
2+
//// function f(p1: () => any, p2: string) { }
3+
//// f(() => { }, `line1\
4+
//// line2\
5+
//// line3`);
6+
////
7+
//// class c1 {
8+
//// const a = ' ''line1\
9+
//// line2';
10+
//// }
11+
12+
verify.navigationTree({
13+
"text": "<global>",
14+
"kind": "script",
15+
"childItems": [
16+
{
17+
"text": "c1",
18+
"kind": "class",
19+
"childItems": [
20+
{
21+
"text": "a",
22+
"kind": "property"
23+
},
24+
{
25+
"text": "'line1 line2'",
26+
"kind": "property"
27+
}
28+
]
29+
},
30+
{
31+
"text": "f",
32+
"kind": "function"
33+
},
34+
{
35+
"text": "f(`line1line2line3`) callback",
36+
"kind": "function"
37+
}
38+
]
39+
});
40+
41+
verify.navigationBar([
42+
{
43+
"text": "<global>",
44+
"kind": "script",
45+
"childItems": [
46+
{
47+
"text": "c1",
48+
"kind": "class"
49+
},
50+
{
51+
"text": "f",
52+
"kind": "function"
53+
},
54+
{
55+
"text": "f(`line1line2line3`) callback",
56+
"kind": "function"
57+
}
58+
]
59+
},
60+
{
61+
"text": "c1",
62+
"kind": "class",
63+
"childItems": [
64+
{
65+
"text": "a",
66+
"kind": "property"
67+
},
68+
{
69+
"text": "'line1 line2'",
70+
"kind": "property"
71+
}
72+
],
73+
"indent": 1
74+
},
75+
{
76+
"text": "f",
77+
"kind": "function",
78+
"indent": 1
79+
},
80+
{
81+
"text": "f(`line1line2line3`) callback",
82+
"kind": "function",
83+
"indent": 1
84+
}
85+
]);
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
2+
//// declare module 'MoreThanOneHundredAndFiftyCharacters\
3+
//// MoreThanOneHundredAndFiftyCharacters\
4+
//// MoreThanOneHundredAndFiftyCharacters\
5+
//// MoreThanOneHundredAndFiftyCharacters\
6+
//// MoreThanOneHundredAndFiftyCharacters\
7+
//// MoreThanOneHundredAndFiftyCharacters\
8+
//// MoreThanOneHundredAndFiftyCharacters' { }
9+
10+
verify.navigationTree({
11+
"text": "<global>",
12+
"kind": "script",
13+
"childItems": [
14+
{
15+
"text": "'MoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharacter...",
16+
"kind": "module",
17+
"kindModifiers": "declare"
18+
}
19+
]
20+
});
21+
22+
verify.navigationBar([
23+
{
24+
"text": "<global>",
25+
"kind": "script",
26+
"childItems": [
27+
{
28+
"text": "'MoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharacter...",
29+
"kind": "module",
30+
"kindModifiers": "declare"
31+
}
32+
]
33+
},
34+
{
35+
"text": "'MoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharactersMoreThanOneHundredAndFiftyCharacter...",
36+
"kind": "module",
37+
"kindModifiers": "declare",
38+
"indent": 1
39+
}
40+
]);

0 commit comments

Comments
 (0)