Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(feat) basic rename #172

Merged
merged 15 commits into from
Jun 18, 2020
Merged

(feat) basic rename #172

merged 15 commits into from
Jun 18, 2020

Conversation

dummdidumm
Copy link
Member

@dummdidumm dummdidumm commented Jun 6, 2020

#110

  • rename where everything is inside same svelte file works
  • rename of variable inside svelte which is also in another ts/js file works
  • rename of svelte prop works when doing the rename for prop of component A in component B which uses that prop
  • rename of svelte prop works when doing the rename for prop of component A in component A

Out of scope for this PR:

  • rename in ts file which would affect svelte file -> out of this scope because we would also need to trigger the language server on ts/js files then

@jasonlyu123
Copy link
Member

Found an issue. if I rename a props that is html attribute like disabled. It would do a rename on the html attribute no matter that props exist on the component or not.

@dummdidumm
Copy link
Member Author

Good catch, thank you. There is also a prop rename problem. I think I need to enhance svelte2tsx with some diagnostic capabilities like "what other svelte components does this file import?" or "what props does this file have?".

Simon Holthausen added 2 commits June 17, 2020 08:52
To show a message early that rename is not allowed in certain locations
@dummdidumm
Copy link
Member Author

I ended up using getRenameInfo of the ts language service. The downside is that component properties can only be renamed within that component now. I added a TODO about this, this can be tackled later. I also added prepareRename support so it shows the user early that this cannot be renamed.

@dummdidumm dummdidumm marked this pull request as ready for review June 17, 2020 16:16
@dummdidumm
Copy link
Member Author

@jasonlyu123 could you take one more look? 😃 Also code-wise, I had to jump through some hoops to get it working.

Copy link
Member

@jasonlyu123 jasonlyu123 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, the source map of export { className as class } is wrong. This could be done in another PR

// First find out if it's really the "rename prop inside component with that prop" case
// Use original document for that because only there the `export` is present.
const regex = new RegExp(
`export\\s+(const|let)\\s+${getVariableAtPosition(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Class and function can also be exported. Though I never used it myself, I don't know what it can do 😂
There's also export { className as class } this probably is tricky because it could be multiple lines.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sw-yx do you know about export const, function, class? I only found it to be available in the component instance, if so it probably doesn't needs to be checked.

return getVariableAtOffset(offsetAt(position, text), text);
}

export function getVariableAtOffset(offset: number, text: string) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

also variable.b, variable) and operators without space in between.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found a hack that can be used to find the identifier/symbol in the generated tsx:

const offset = fragment.offsetAt(fragment.getGeneratedPosition(position));
const { start, length } = lang.getSmartSelectionRange(tsDoc.filePath, offset).textSpan;
const identifier = tsDoc.getText(start, start + length);

@jasonlyu123
Copy link
Member

jasonlyu123 commented Jun 18, 2020

<input bind:value />

would be renamed to

<input bind:valuvalues />

Some shorthand attributes would have a problem, maybe it can all be handle in another PR.

@dummdidumm
Copy link
Member Author

This is a source map problem, seems like svelte2tsx mappings are off after conversion. I was able to fix it. I also added your suggestion for the variable retrieval and added a TODO that export {x as y} syntax is currently not supported for rename. The function/class thing ... let's just wait till someone shows up with this request 😄

@dummdidumm dummdidumm merged commit f483271 into sveltejs:master Jun 18, 2020
@dummdidumm dummdidumm deleted the basic-rename branch June 18, 2020 10:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants