@@ -1339,4 +1339,116 @@ describe('server', () => {
13391339 await lookupAndExpectNpmConfigLoglevelResult ( 'npmloglevel' ) // fuzzy
13401340 } )
13411341 } )
1342+
1343+ describe ( 'onPrepareRename' , ( ) => {
1344+ async function getPrepareRenameResult ( line : LSP . uinteger , character : LSP . uinteger ) {
1345+ const { connection } = await initializeServer ( )
1346+
1347+ return connection . onPrepareRename . mock . calls [ 0 ] [ 0 ] (
1348+ {
1349+ textDocument : {
1350+ uri : FIXTURE_URI . RENAMING ,
1351+ } ,
1352+ position : { line, character } ,
1353+ } ,
1354+ { } as any ,
1355+ )
1356+ }
1357+
1358+ it ( 'returns null when a renamable symbol is not found' , async ( ) => {
1359+ // Shebang comment
1360+ expect ( await getPrepareRenameResult ( 0 , 0 ) ) . toBeNull ( )
1361+ // Empty line
1362+ expect ( await getPrepareRenameResult ( 1 , 0 ) ) . toBeNull ( )
1363+ // Special variable
1364+ expect ( await getPrepareRenameResult ( 2 , 7 ) ) . toBeNull ( )
1365+ // Positional parameter
1366+ expect ( await getPrepareRenameResult ( 3 , 7 ) ) . toBeNull ( )
1367+ // Invalidly named variable
1368+ expect ( await getPrepareRenameResult ( 4 , 0 ) ) . toBeNull ( )
1369+ // if keyword
1370+ expect ( await getPrepareRenameResult ( 11 , 0 ) ) . toBeNull ( )
1371+ // String
1372+ expect ( await getPrepareRenameResult ( 11 , 29 ) ) . toBeNull ( )
1373+ // Regular word
1374+ expect ( await getPrepareRenameResult ( 15 , 11 ) ) . toBeNull ( )
1375+
1376+ // Documents some of tree-sitter-bash's limitations when parsing
1377+ // constructs that affect renaming; these may fail in the future when
1378+ // parsing gets better.
1379+ // Variables inside a C-style for loop arithmetic expression
1380+ expect ( await getPrepareRenameResult ( 19 , 13 ) ) . toBeNull ( )
1381+ expect ( await getPrepareRenameResult ( 19 , 21 ) ) . toBeNull ( )
1382+ // Variable inside an arithmetic expansion
1383+ expect ( await getPrepareRenameResult ( 20 , 11 ) ) . toBeNull ( )
1384+ } )
1385+
1386+ it ( 'returns range when a renamable symbol is found' , async ( ) => {
1387+ // echo builtin command
1388+ expect ( await getPrepareRenameResult ( 2 , 0 ) ) . toMatchInlineSnapshot ( `
1389+ {
1390+ "end": {
1391+ "character": 4,
1392+ "line": 2,
1393+ },
1394+ "start": {
1395+ "character": 0,
1396+ "line": 2,
1397+ },
1398+ }
1399+ ` )
1400+ // ls executable command
1401+ expect ( await getPrepareRenameResult ( 6 , 12 ) ) . toMatchInlineSnapshot ( `
1402+ {
1403+ "end": {
1404+ "character": 13,
1405+ "line": 6,
1406+ },
1407+ "start": {
1408+ "character": 11,
1409+ "line": 6,
1410+ },
1411+ }
1412+ ` )
1413+ // Variable definition
1414+ expect ( await getPrepareRenameResult ( 6 , 3 ) ) . toMatchInlineSnapshot ( `
1415+ {
1416+ "end": {
1417+ "character": 7,
1418+ "line": 6,
1419+ },
1420+ "start": {
1421+ "character": 0,
1422+ "line": 6,
1423+ },
1424+ }
1425+ ` )
1426+ // Function definition
1427+ expect ( await getPrepareRenameResult ( 7 , 10 ) ) . toMatchInlineSnapshot ( `
1428+ {
1429+ "end": {
1430+ "character": 11,
1431+ "line": 7,
1432+ },
1433+ "start": {
1434+ "character": 0,
1435+ "line": 7,
1436+ },
1437+ }
1438+ ` )
1439+ // Function used as command
1440+ expect ( await getPrepareRenameResult ( 11 , 13 ) ) . toMatchInlineSnapshot ( `
1441+ {
1442+ "end": {
1443+ "character": 19,
1444+ "line": 11,
1445+ },
1446+ "start": {
1447+ "character": 8,
1448+ "line": 11,
1449+ },
1450+ }
1451+ ` )
1452+ } )
1453+ } )
13421454} )
0 commit comments