Handle linebreaks consistently in code fixes and refactorings#21158
Conversation
| synchronizeHostData(); | ||
| Debug.assert(scope.type === "file"); | ||
| const sourceFile = getValidSourceFile(scope.fileName); | ||
| const newLineCharacter = getNewLineOrDefaultFromHost(host); |
There was a problem hiding this comment.
we should mark this function as deprecated, and replace all its uses as well.
There was a problem hiding this comment.
It appears to be internal. Can we just delete it?
There was a problem hiding this comment.
Actually, the remaining callers look pretty legitimate. Is there another method they should call instead?
There was a problem hiding this comment.
removign it should be fine then.
There was a problem hiding this comment.
I'm confused. This method appears to have callers. Why are we deprecating/removing it?
There was a problem hiding this comment.
looking at this again, seems like in every call to this method, we have an insufficient public API. for instance, in getDocCommentTemplateAtPosition, seems like the public API should take a FormatCodeSettings just like getCodeFixesAtPosition does.
I do not think this is related to this PR per se, but this is something that we should fix for the long term. unfortunately we can not remove the getNewLineOrDefaultFromHost as you noted without breaking our public API, but we should add optional FormatCodeSettings in places where we use it to allow the host to override the value.
|
Name suggestions for Edit: moot |
|
I'm also hoping there's a more idiomatic way to express the conversion from Edit: moot |
| @@ -24,8 +24,8 @@ verify.applyCodeActionFromCompletion("b", { | |||
| source: "/a", | |||
| description: `Import 'foo' from module "./a"`, | |||
| // TODO: GH#18445 | |||
|
|
||
| export function getNewLineFromContext(context: RefactorOrCodeFixContext) { | ||
| const formatSettings = context.formatContext.options; | ||
| return formatSettings ? formatSettings.newLineCharacter : context.host.getNewLine(); |
There was a problem hiding this comment.
The newLineCharacter property is declared optional, so this should return context.host.getNewLine() if that's undefined.
Although it looks like getNewLine() is also optional -- see getNewLineOrDefaultFromHost.
| } | ||
|
|
||
| const changeTracker = textChanges.ChangeTracker.fromContext(context); | ||
| const changeTracker = textChanges.ChangeTracker.fromContext(toTextChangesContext(context)); |
There was a problem hiding this comment.
It looks like every call to fromContext calls toTextChangesContext first -- would be easier to just do that in fromContext, and probably inline toTextChangesContext.
This would mean we don't need a TextChangesContext type, just RefactorOrCodeFixContext -- and you could rename RefactorOrCodeFixContext to TextChangesContext.
It's already in the EditorSettings and the LanguageServiceHost. Fixes microsoft#18291 Fixes microsoft#18445
f46e0cd to
db3f7c5
Compare
|
Straight rebase to resolve conflicts. Will address comments in subsequent commits. |
| interface SymbolContext { | ||
| sourceFile: SourceFile; | ||
| symbolName: string; | ||
| formatContext: ts.formatting.FormatContext; |
There was a problem hiding this comment.
Looks like this could be moved to ImportCodeFixContext.
There was a problem hiding this comment.
It looks like every consumer of SymbolContext ands it with TextChangesContext anyway, so I'll just make it a subtype.
There was a problem hiding this comment.
Do you know why SymbolContext exists? Is it to prevent certain functions from accessing certain members of ImportCodeFixContext?
There was a problem hiding this comment.
It's just there to indicate that we don't need everything from ImportCodeFixContext.
There was a problem hiding this comment.
That seems excessive to me, but not worth changing at the moment.
Don't store it on the context; do look in the
EditorSettingsbefore falling back on theLanguageServiceHost.There's probably a bunch of renaming to do before this goes in.
NOTE: The code change is in the first commit.
Fixes #18291
Fixes #18445