-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Format on type deletes valid range #3084
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,6 +21,21 @@ module ts.server { | |
} | ||
return spaceCache[n]; | ||
} | ||
|
||
export function generateIndentString(n: number, editorOptions: EditorOptions): string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we already have code for that, check getIndentationString |
||
if (editorOptions.ConvertTabsToSpaces) { | ||
return generateSpaces(n); | ||
} else { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
var result = ""; | ||
for (var i = 0; i < Math.floor(n / editorOptions.TabSize); i++) { | ||
result += "\t"; | ||
} | ||
for (var i = 0; i < n % editorOptions.TabSize; i++) { | ||
result += " "; | ||
} | ||
return result; | ||
} | ||
} | ||
|
||
interface FileStart { | ||
file: string; | ||
|
@@ -541,31 +556,29 @@ module ts.server { | |
var editorOptions: ts.EditorOptions = { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do we really need to copy formatOptions? it should be assignable to EditorOptions so original formatOptions can be used directly (assuming that nobody changes them) |
||
IndentSize: formatOptions.IndentSize, | ||
TabSize: formatOptions.TabSize, | ||
NewLineCharacter: "\n", | ||
NewLineCharacter: formatOptions.NewLineCharacter, | ||
ConvertTabsToSpaces: formatOptions.ConvertTabsToSpaces, | ||
}; | ||
var indentPosition = | ||
compilerService.languageService.getIndentationAtPosition(file, position, editorOptions); | ||
var preferredIndent = compilerService.languageService.getIndentationAtPosition(file, position, editorOptions); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should already have code that determines first non-whitespace column\character in the line, check SmartIndenter.findFirstNonWhitespaceCharacterAndColumn |
||
var hasIndent = 0; | ||
for (var i = 0, len = lineText.length; i < len; i++) { | ||
if (lineText.charAt(i) == " ") { | ||
indentPosition--; | ||
hasIndent++; | ||
} | ||
else if (lineText.charAt(i) == "\t") { | ||
indentPosition -= editorOptions.IndentSize; | ||
hasIndent += editorOptions.TabSize; | ||
} | ||
else { | ||
break; | ||
} | ||
} | ||
if (indentPosition > 0) { | ||
var spaces = generateSpaces(indentPosition); | ||
edits.push({ span: ts.createTextSpanFromBounds(position, position), newText: spaces }); | ||
} | ||
else if (indentPosition < 0) { | ||
edits.push({ | ||
span: ts.createTextSpanFromBounds(position, position - indentPosition), | ||
newText: "" | ||
}); | ||
// i points to the first non whitespace character | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm finding this quite worrying how much extra logic is here in the server to handle this sort of conversion. It seems like far too much complexit in this layer. |
||
if (preferredIndent !== hasIndent) { | ||
var firstNoWhiteSpacePosition = lineInfo.offset + i; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
edits.push({ | ||
span: ts.createTextSpanFromBounds(lineInfo.offset, firstNoWhiteSpacePosition), | ||
newText: generateIndentString(preferredIndent, editorOptions) | ||
}); | ||
} | ||
} | ||
} | ||
|
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.
Why is this changing as part of a formatting change?
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 agree this needs to be in a separate change.
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.
agree