mirror of
https://github.com/microsoft/TypeScript.git
synced 2026-02-06 02:33:53 -06:00
support both old and new set of settings in the formatting API
This commit is contained in:
parent
80bca64844
commit
6041fceb52
@ -67,7 +67,11 @@ namespace ts.formatting {
|
||||
delta: number;
|
||||
}
|
||||
|
||||
export function formatOnEnter(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] {
|
||||
export function formatOnEnter(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings | FormatCodeOptions): TextChange[] {
|
||||
return formatOnEnterWorker(position, sourceFile, rulesProvider, toEditorSettings(options));
|
||||
}
|
||||
|
||||
export function formatOnEnterWorker(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] {
|
||||
const line = sourceFile.getLineAndCharacterOfPosition(position).line;
|
||||
if (line === 0) {
|
||||
return [];
|
||||
@ -96,15 +100,27 @@ namespace ts.formatting {
|
||||
return formatSpan(span, sourceFile, options, rulesProvider, FormattingRequestKind.FormatOnEnter);
|
||||
}
|
||||
|
||||
export function formatOnSemicolon(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] {
|
||||
export function formatOnSemicolon(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings | FormatCodeOptions): TextChange[] {
|
||||
return formatOnSemicolonWorker(position, sourceFile, rulesProvider, toEditorSettings(options));
|
||||
}
|
||||
|
||||
export function formatOnSemicolonWorker(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] {
|
||||
return formatOutermostParent(position, SyntaxKind.SemicolonToken, sourceFile, options, rulesProvider, FormattingRequestKind.FormatOnSemicolon);
|
||||
}
|
||||
|
||||
export function formatOnClosingCurly(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] {
|
||||
export function formatOnClosingCurly(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings | FormatCodeOptions): TextChange[] {
|
||||
return formatOnClosingCurlyWorker(position, sourceFile, rulesProvider, toEditorSettings(options));
|
||||
}
|
||||
|
||||
export function formatOnClosingCurlyWorker(position: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] {
|
||||
return formatOutermostParent(position, SyntaxKind.CloseBraceToken, sourceFile, options, rulesProvider, FormattingRequestKind.FormatOnClosingCurlyBrace);
|
||||
}
|
||||
|
||||
export function formatDocument(sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] {
|
||||
export function formatDocument(sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings | FormatCodeOptions): TextChange[] {
|
||||
return formatDocumentWorker(sourceFile, rulesProvider, toEditorSettings(options));
|
||||
}
|
||||
|
||||
export function formatDocumentWorker(sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] {
|
||||
const span = {
|
||||
pos: 0,
|
||||
end: sourceFile.text.length
|
||||
@ -112,7 +128,11 @@ namespace ts.formatting {
|
||||
return formatSpan(span, sourceFile, options, rulesProvider, FormattingRequestKind.FormatDocument);
|
||||
}
|
||||
|
||||
export function formatSelection(start: number, end: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] {
|
||||
export function formatSelection(start: number, end: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings | FormatCodeOptions): TextChange[] {
|
||||
return formatSelectionWorker(start, end, sourceFile, rulesProvider, toEditorSettings(options));
|
||||
}
|
||||
|
||||
export function formatSelectionWorker(start: number, end: number, sourceFile: SourceFile, rulesProvider: RulesProvider, options: FormatCodeSettings): TextChange[] {
|
||||
// format from the beginning of the line
|
||||
const span = {
|
||||
pos: getLineStartPositionForPosition(start, sourceFile),
|
||||
|
||||
@ -24,7 +24,11 @@ namespace ts.formatting {
|
||||
return this.rulesMap;
|
||||
}
|
||||
|
||||
public ensureUpToDate(options: ts.FormatCodeSettings) {
|
||||
public ensureUpToDate(options: ts.FormatCodeSettings | ts.FormatCodeOptions) {
|
||||
this.ensureUpToDateWorker(toEditorSettings(options));
|
||||
}
|
||||
|
||||
public ensureUpToDateWorker(options: ts.FormatCodeSettings) {
|
||||
if (!this.options || !ts.compareDataObjects(this.options, options)) {
|
||||
const activeRules = this.createActiveRules(options);
|
||||
const rulesMap = RulesMap.create(activeRules);
|
||||
|
||||
@ -3164,7 +3164,7 @@ namespace ts {
|
||||
ruleProvider = new formatting.RulesProvider();
|
||||
}
|
||||
|
||||
ruleProvider.ensureUpToDate(options);
|
||||
ruleProvider.ensureUpToDateWorker(options);
|
||||
return ruleProvider;
|
||||
}
|
||||
|
||||
@ -8509,13 +8509,13 @@ namespace ts {
|
||||
function getFormattingEditsForRange(fileName: string, start: number, end: number, optionsOrSettings: FormatCodeOptions | FormatCodeSettings): TextChange[] {
|
||||
const settings = toEditorSettings(optionsOrSettings);
|
||||
const sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName);
|
||||
return formatting.formatSelection(start, end, sourceFile, getRuleProvider(settings), settings);
|
||||
return formatting.formatSelectionWorker(start, end, sourceFile, getRuleProvider(settings), settings);
|
||||
}
|
||||
|
||||
function getFormattingEditsForDocument(fileName: string, optionsOrSettings: FormatCodeOptions | FormatCodeSettings): TextChange[] {
|
||||
const settings = toEditorSettings(optionsOrSettings);
|
||||
const sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName);
|
||||
return formatting.formatDocument(sourceFile, getRuleProvider(settings), settings);
|
||||
return formatting.formatDocumentWorker(sourceFile, getRuleProvider(settings), settings);
|
||||
}
|
||||
|
||||
function getFormattingEditsAfterKeystroke(fileName: string, position: number, key: string, optionsOrSettings: FormatCodeOptions | FormatCodeSettings): TextChange[] {
|
||||
@ -8523,13 +8523,13 @@ namespace ts {
|
||||
const sourceFile = syntaxTreeCache.getCurrentSourceFile(fileName);
|
||||
|
||||
if (key === "}") {
|
||||
return formatting.formatOnClosingCurly(position, sourceFile, getRuleProvider(settings), settings);
|
||||
return formatting.formatOnClosingCurlyWorker(position, sourceFile, getRuleProvider(settings), settings);
|
||||
}
|
||||
else if (key === ";") {
|
||||
return formatting.formatOnSemicolon(position, sourceFile, getRuleProvider(settings), settings);
|
||||
return formatting.formatOnSemicolonWorker(position, sourceFile, getRuleProvider(settings), settings);
|
||||
}
|
||||
else if (key === "\n") {
|
||||
return formatting.formatOnEnter(position, sourceFile, getRuleProvider(settings), settings);
|
||||
return formatting.formatOnEnterWorker(position, sourceFile, getRuleProvider(settings), settings);
|
||||
}
|
||||
|
||||
return [];
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user