Skip to content

Commit bbab0c4

Browse files
committed
Revert removing check and update onPrepareRename tests
1 parent 4f7bf1e commit bbab0c4

File tree

3 files changed

+54
-31
lines changed

3 files changed

+54
-31
lines changed

server/src/__tests__/server.test.ts

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,96 +1356,112 @@ describe('server', () => {
13561356
}
13571357

13581358
it('returns null when a renamable symbol is not found', async () => {
1359-
// Shebang comment
1360-
expect(await getPrepareRenameResult(0, 0)).toBeNull()
13611359
// Empty line
13621360
expect(await getPrepareRenameResult(1, 0)).toBeNull()
1363-
// Special variable
1364-
expect(await getPrepareRenameResult(2, 7)).toBeNull()
1365-
// Positional parameter
1361+
// Comment
1362+
expect(await getPrepareRenameResult(2, 9)).toBeNull()
1363+
// Special variables
13661364
expect(await getPrepareRenameResult(3, 7)).toBeNull()
1367-
// Invalidly named variable
13681365
expect(await getPrepareRenameResult(4, 0)).toBeNull()
1366+
// Positional parameters
1367+
expect(await getPrepareRenameResult(6, 7)).toBeNull()
1368+
expect(await getPrepareRenameResult(7, 0)).toBeNull()
1369+
// Invalidly named variables
1370+
expect(await getPrepareRenameResult(9, 2)).toBeNull()
1371+
expect(await getPrepareRenameResult(10, 0)).toBeNull()
13691372
// if keyword
1370-
expect(await getPrepareRenameResult(11, 0)).toBeNull()
1373+
expect(await getPrepareRenameResult(17, 1)).toBeNull()
13711374
// String
1372-
expect(await getPrepareRenameResult(11, 29)).toBeNull()
1375+
expect(await getPrepareRenameResult(17, 29)).toBeNull()
13731376
// Regular word
1374-
expect(await getPrepareRenameResult(15, 11)).toBeNull()
1377+
expect(await getPrepareRenameResult(21, 11)).toBeNull()
13751378

13761379
// Documents some of tree-sitter-bash's limitations when parsing
13771380
// constructs that affect renaming; these may fail in the future when
13781381
// parsing gets better.
13791382
// Variables inside a C-style for loop arithmetic expression
1380-
expect(await getPrepareRenameResult(19, 13)).toBeNull()
1381-
expect(await getPrepareRenameResult(19, 21)).toBeNull()
1383+
expect(await getPrepareRenameResult(25, 13)).toBeNull()
1384+
expect(await getPrepareRenameResult(25, 21)).toBeNull()
13821385
// Variable inside an arithmetic expansion
1383-
expect(await getPrepareRenameResult(20, 11)).toBeNull()
1386+
expect(await getPrepareRenameResult(26, 11)).toBeNull()
13841387
})
13851388

13861389
it('returns range when a renamable symbol is found', async () => {
13871390
// echo builtin command
1388-
expect(await getPrepareRenameResult(2, 0)).toMatchInlineSnapshot(`
1391+
expect(await getPrepareRenameResult(3, 2)).toMatchInlineSnapshot(`
13891392
{
13901393
"end": {
13911394
"character": 4,
1392-
"line": 2,
1395+
"line": 3,
13931396
},
13941397
"start": {
13951398
"character": 0,
1396-
"line": 2,
1399+
"line": 3,
13971400
},
13981401
}
13991402
`)
14001403
// ls executable command
1401-
expect(await getPrepareRenameResult(6, 12)).toMatchInlineSnapshot(`
1404+
expect(await getPrepareRenameResult(12, 12)).toMatchInlineSnapshot(`
14021405
{
14031406
"end": {
14041407
"character": 13,
1405-
"line": 6,
1408+
"line": 12,
14061409
},
14071410
"start": {
14081411
"character": 11,
1409-
"line": 6,
1412+
"line": 12,
14101413
},
14111414
}
14121415
`)
14131416
// Variable definition
1414-
expect(await getPrepareRenameResult(6, 3)).toMatchInlineSnapshot(`
1417+
expect(await getPrepareRenameResult(12, 0)).toMatchInlineSnapshot(`
14151418
{
14161419
"end": {
14171420
"character": 7,
1418-
"line": 6,
1421+
"line": 12,
14191422
},
14201423
"start": {
14211424
"character": 0,
1422-
"line": 6,
1425+
"line": 12,
1426+
},
1427+
}
1428+
`)
1429+
// Expanded variable
1430+
expect(await getPrepareRenameResult(18, 13)).toMatchInlineSnapshot(`
1431+
{
1432+
"end": {
1433+
"character": 15,
1434+
"line": 18,
1435+
},
1436+
"start": {
1437+
"character": 8,
1438+
"line": 18,
14231439
},
14241440
}
14251441
`)
14261442
// Function definition
1427-
expect(await getPrepareRenameResult(7, 10)).toMatchInlineSnapshot(`
1443+
expect(await getPrepareRenameResult(13, 10)).toMatchInlineSnapshot(`
14281444
{
14291445
"end": {
14301446
"character": 11,
1431-
"line": 7,
1447+
"line": 13,
14321448
},
14331449
"start": {
14341450
"character": 0,
1435-
"line": 7,
1451+
"line": 13,
14361452
},
14371453
}
14381454
`)
14391455
// Function used as command
1440-
expect(await getPrepareRenameResult(11, 13)).toMatchInlineSnapshot(`
1456+
expect(await getPrepareRenameResult(17, 13)).toMatchInlineSnapshot(`
14411457
{
14421458
"end": {
14431459
"character": 19,
1444-
"line": 11,
1460+
"line": 17,
14451461
},
14461462
"start": {
14471463
"character": 8,
1448-
"line": 11,
1464+
"line": 17,
14491465
},
14501466
}
14511467
`)

server/src/server.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -731,7 +731,8 @@ export default class BashServer {
731731

732732
if (
733733
!symbol ||
734-
(symbol.kind === LSP.SymbolKind.Variable && !/^[a-z_][\w]*$/i.test(symbol.word))
734+
(symbol.kind === LSP.SymbolKind.Variable &&
735+
(symbol.word === '_' || !/^[a-z_][\w]*$/i.test(symbol.word)))
735736
) {
736737
return null
737738
}

testing/fixtures/renaming.sh

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
#!/usr/bin/env bash
22

3-
echo "$_" # Special variable
4-
echo "$1" # Positional parameter
5-
1abc="1abc" # Invalidly named variable
3+
# Special variables
4+
echo "$_"
5+
_="reassign"
6+
# Positional parameters
7+
echo "$1"
8+
2="reassign"
9+
# Invalidly named variables
10+
1abc="1abc"
11+
ab%c="ab%c"
612

713
somevar="$(ls somedir)"
814
somecommand() {

0 commit comments

Comments
 (0)