Extract DocumentFormattingEditProvider and DocumentRangeFormattingEditProvider

This commit is contained in:
Alex Dima 2021-11-17 14:13:05 +01:00
parent 89d05c5889
commit 3cb8acd2e9
No known key found for this signature in database
GPG key ID: 39563C1504FDD0C9
5 changed files with 86 additions and 116 deletions

View file

@ -763,3 +763,73 @@ export class DocumentLinkAdapter<T extends ILanguageWorkerWithDocumentLinks>
}
//#endregion
//#region DocumentFormattingEditProvider, DocumentRangeFormattingEditProvider
export interface ILanguageWorkerWithFormat {
format(
uri: string,
range: lsTypes.Range | null,
options: lsTypes.FormattingOptions
): Promise<lsTypes.TextEdit[]>;
}
export class DocumentFormattingEditProvider<T extends ILanguageWorkerWithFormat>
implements languages.DocumentFormattingEditProvider
{
constructor(private _worker: WorkerAccessor<T>) {}
public provideDocumentFormattingEdits(
model: editor.IReadOnlyModel,
options: languages.FormattingOptions,
token: CancellationToken
): Promise<languages.TextEdit[] | undefined> {
const resource = model.uri;
return this._worker(resource).then((worker) => {
return worker
.format(resource.toString(), null, fromFormattingOptions(options))
.then((edits) => {
if (!edits || edits.length === 0) {
return;
}
return edits.map<languages.TextEdit>(toTextEdit);
});
});
}
}
export class DocumentRangeFormattingEditProvider<T extends ILanguageWorkerWithFormat>
implements languages.DocumentRangeFormattingEditProvider
{
constructor(private _worker: WorkerAccessor<T>) {}
public provideDocumentRangeFormattingEdits(
model: editor.IReadOnlyModel,
range: Range,
options: languages.FormattingOptions,
token: CancellationToken
): Promise<languages.TextEdit[] | undefined> {
const resource = model.uri;
return this._worker(resource).then((worker) => {
return worker
.format(resource.toString(), fromRange(range), fromFormattingOptions(options))
.then((edits) => {
if (!edits || edits.length === 0) {
return;
}
return edits.map<languages.TextEdit>(toTextEdit);
});
});
}
}
function fromFormattingOptions(options: languages.FormattingOptions): lsTypes.FormattingOptions {
return {
tabSize: options.tabSize,
insertSpaces: options.insertSpaces
};
}
//#endregion