11import { Position , WorkspaceEdit , Range } from 'vscode-languageserver' ;
2- import { Document , mapRangeToOriginal , getLineAtPosition } from '../../../lib/documents' ;
2+ import { Document , mapRangeToOriginal , getLineAtPosition , offsetAt } from '../../../lib/documents' ;
33import { filterAsync , isNotNullOrUndefined , pathToUrl } from '../../../utils' ;
44import { RenameProvider } from '../../interfaces' ;
55import {
@@ -15,7 +15,8 @@ import {
1515 isComponentAtPosition ,
1616 isAfterSvelte2TsxPropsReturn ,
1717 isNoTextSpanInGeneratedCode ,
18- SnapshotFragmentMap
18+ SnapshotFragmentMap ,
19+ findContainingNode
1920} from './utils' ;
2021
2122export class RenameProviderImpl implements RenameProvider {
@@ -68,7 +69,13 @@ export class RenameProviderImpl implements RenameProvider {
6869 range : Range ;
6970 }
7071 > = await this . mapAndFilterRenameLocations ( renameLocations , docs ) ;
71- // eslint-disable-next-line max-len
72+
73+ convertedRenameLocations = this . checkShortHandBindingLocation (
74+ lang ,
75+ convertedRenameLocations ,
76+ docs
77+ ) ;
78+
7279 const additionalRenameForPropRenameInsideComponentWithProp =
7380 await this . getAdditionLocationsForRenameOfPropInsideComponentWithProp (
7481 document ,
@@ -237,7 +244,12 @@ export class RenameProviderImpl implements RenameProvider {
237244 const idx = ( match . index || 0 ) + match [ 0 ] . lastIndexOf ( match [ 1 ] ) ;
238245 const replacementsForProp =
239246 lang . findRenameLocations ( updatePropLocation . fileName , idx , false , false ) || [ ] ;
240- return await this . mapAndFilterRenameLocations ( replacementsForProp , fragments ) ;
247+
248+ return this . checkShortHandBindingLocation (
249+ lang ,
250+ await this . mapAndFilterRenameLocations ( replacementsForProp , fragments ) ,
251+ fragments
252+ ) ;
241253 }
242254
243255 // --------> svelte2tsx?
@@ -369,6 +381,88 @@ export class RenameProviderImpl implements RenameProvider {
369381 private getSnapshot ( filePath : string ) {
370382 return this . lsAndTsDocResolver . getSnapshot ( filePath ) ;
371383 }
384+
385+ private checkShortHandBindingLocation (
386+ lang : ts . LanguageService ,
387+ renameLocations : Array < ts . RenameLocation & { range : Range } > ,
388+ fragments : SnapshotFragmentMap
389+ ) : Array < ts . RenameLocation & { range : Range } > {
390+ const bind = 'bind:' ;
391+
392+ return renameLocations . map ( ( location ) => {
393+ const sourceFile = lang . getProgram ( ) ?. getSourceFile ( location . fileName ) ;
394+
395+ if (
396+ ! sourceFile ||
397+ location . fileName !== sourceFile . fileName ||
398+ location . range . start . line < 0 ||
399+ location . range . end . line < 0
400+ ) {
401+ return location ;
402+ }
403+
404+ const fragment = fragments . getFragment ( location . fileName ) ;
405+ if ( ! ( fragment instanceof SvelteSnapshotFragment ) ) {
406+ return location ;
407+ }
408+
409+ const { originalText } = fragment ;
410+
411+ const possibleJsxAttribute = findContainingNode (
412+ sourceFile ,
413+ location . textSpan ,
414+ ts . isJsxAttribute
415+ ) ;
416+ if ( ! possibleJsxAttribute ) {
417+ return location ;
418+ }
419+
420+ const attributeName = possibleJsxAttribute . name . getText ( ) ;
421+ const { initializer } = possibleJsxAttribute ;
422+
423+ // not props={props}
424+ if (
425+ ! initializer ||
426+ ! ts . isJsxExpression ( initializer ) ||
427+ attributeName !== initializer . expression ?. getText ( )
428+ ) {
429+ return location ;
430+ }
431+
432+ const originalStart = offsetAt ( location . range . start , originalText ) ;
433+
434+ const isShortHandBinding =
435+ originalText . substr ( originalStart - bind . length , bind . length ) === bind ;
436+
437+ const directiveName = ( isShortHandBinding ? bind : '' ) + attributeName ;
438+ const prefixText = directiveName + '={' ;
439+
440+ const newRange = mapRangeToOriginal (
441+ fragment ,
442+ convertRange ( fragment , {
443+ start : possibleJsxAttribute . getStart ( ) ,
444+ length : possibleJsxAttribute . getWidth ( )
445+ } )
446+ ) ;
447+
448+ // somehow the mapping is one character before
449+ if (
450+ isShortHandBinding ||
451+ originalText
452+ . substring ( offsetAt ( newRange . start , originalText ) , originalStart )
453+ . trimLeft ( ) === '{'
454+ ) {
455+ newRange . start . character ++ ;
456+ }
457+
458+ return {
459+ ...location ,
460+ prefixText,
461+ suffixText : '}' ,
462+ range : newRange
463+ } ;
464+ } ) ;
465+ }
372466}
373467
374468function unique < T > ( array : T [ ] ) : T [ ] {
0 commit comments