-
Notifications
You must be signed in to change notification settings - Fork 13k
replaceNode: Always use non-adjusted end #22519
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
Conversation
I believe the adjusted end position includes trailing comments, not just the newline. Also, why would the start and the end not be symmetric? |
Yeah, looks like we should never be adjusting the start position either. |
* If pos\end should be interpreted literally 'useNonAdjustedStartPosition' or 'useNonAdjustedEndPosition' should be set to true | ||
*/ | ||
export type ConfigurableStartEnd = ConfigurableStart & ConfigurableEnd; | ||
export interface ConfigurableStartEnd extends ConfigurableStart, ConfigurableEnd {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Converting to an interface because of #19296
@amcasey Could you re-review? |
This change only partially resolves #21246, correct? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Personally, I think this change feels incomplete - it's neither a targeted fix nor a complete revision.
src/services/textChanges.ts
Outdated
const pos = getAdjustedStartPosition(sourceFile, oldNode, options, Position.Start); | ||
const end = getAdjustedEndPosition(sourceFile, oldNode, options); | ||
return this.replaceRange(sourceFile, { pos, end }, newNode, options); | ||
public replaceNode(sourceFile: SourceFile, oldNode: Node, newNode: Node, options: InsertNodeOptions = {}) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was only expecting the default to change but this looks like it makes the alternative (i.e. adjusting start and end positions) impossible. Was that intentional?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also it seems strange to update this signature without making corresponding changes to (e.g.) replaceNodeRange
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we're likely to have any reason to replace the a node's comments while replacing it -- if we do end up needing that, we can add it back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated replaceNodeRange
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I think of replace(Node | Range | NodeRange)(WithNodes)? as a family of related functions that should have parallel signatures. Notice that the WithNodes variants already default to using non-adjusted positions, which should make things easier.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added the option back where it makes sense, but not for replaceRange
which should only accept InsertNodeOptions
-- it doesn't adjust pos and end since it accepts a range.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Created #22813 which will be easier to review independently, then can merge into this.
this.replaceRange(sourceFile, { pos, end: pos }, createToken(modifier), { suffix: " " }); | ||
} | ||
|
||
public insertCommentBeforeLine(sourceFile: SourceFile, lineNumber: number, position: number, commentText: string): void { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm surprised that we need to compute line numbers for this operation (I would have guessed, perhaps incorrectly, that that was expensive), but I see that this is just a refactoring.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It could probably be done without the line numbers by counting back to the beginning of the line. Though, getting the line number is just a binary search given that sourceFile.lineMap
is cached.
65184f1
to
a6be0a6
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for cleaning this up!
Alternative to #22517 -- there are no remaining uses where we need to adjust an end position with
replaceNode
, so make that the hardcoded behavior. In some cases we were adjusting the end, then adding{ suffix: newLineCharacter }
, which means we remove and then replace the newline.