Merge pull request #5033 from Microsoft/dirkb/fix_5032
Fixed #5032: tsserver: Format on type broken
This commit is contained in:
Коммит
5234bf698c
|
@ -21,6 +21,21 @@ namespace ts.server {
|
|||
return spaceCache[n];
|
||||
}
|
||||
|
||||
export function generateIndentString(n: number, editorOptions: EditorOptions): string {
|
||||
if (editorOptions.ConvertTabsToSpaces) {
|
||||
return generateSpaces(n);
|
||||
} else {
|
||||
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;
|
||||
start: ILineInfo;
|
||||
|
@ -608,27 +623,25 @@ namespace ts.server {
|
|||
ConvertTabsToSpaces: formatOptions.ConvertTabsToSpaces,
|
||||
IndentStyle: ts.IndentStyle.Smart,
|
||||
};
|
||||
var indentPosition =
|
||||
compilerService.languageService.getIndentationAtPosition(file, position, editorOptions);
|
||||
var preferredIndent = compilerService.languageService.getIndentationAtPosition(file, position, editorOptions);
|
||||
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) {
|
||||
// i points to the first non whitespace character
|
||||
if (preferredIndent !== hasIndent) {
|
||||
var firstNoWhiteSpacePosition = lineInfo.offset + i;
|
||||
edits.push({
|
||||
span: ts.createTextSpanFromBounds(position, position - indentPosition),
|
||||
newText: ""
|
||||
span: ts.createTextSpanFromBounds(lineInfo.offset, firstNoWhiteSpacePosition),
|
||||
newText: generateIndentString(preferredIndent, editorOptions)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
Загрузка…
Ссылка в новой задаче