mirror of
https://github.com/microsoft/monaco-editor.git
synced 2025-12-22 17:25:39 +01:00
Extract DocumentFormattingEditProvider and DocumentRangeFormattingEditProvider
This commit is contained in:
parent
89d05c5889
commit
3cb8acd2e9
5 changed files with 86 additions and 116 deletions
|
|
@ -763,3 +763,73 @@ export class DocumentLinkAdapter<T extends ILanguageWorkerWithDocumentLinks>
|
||||||
}
|
}
|
||||||
|
|
||||||
//#endregion
|
//#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
|
||||||
|
|
|
||||||
|
|
@ -48,11 +48,11 @@ export function setupMode1(defaults: LanguageServiceDefaults): void {
|
||||||
if (languageId === 'html') {
|
if (languageId === 'html') {
|
||||||
languages.registerDocumentFormattingEditProvider(
|
languages.registerDocumentFormattingEditProvider(
|
||||||
languageId,
|
languageId,
|
||||||
new languageFeatures.DocumentFormattingEditProvider(worker)
|
new languageFeatures.HTMLDocumentFormattingEditProvider(worker)
|
||||||
);
|
);
|
||||||
languages.registerDocumentRangeFormattingEditProvider(
|
languages.registerDocumentRangeFormattingEditProvider(
|
||||||
languageId,
|
languageId,
|
||||||
new languageFeatures.DocumentRangeFormattingEditProvider(worker)
|
new languageFeatures.HTMLDocumentRangeFormattingEditProvider(worker)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -135,7 +135,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
|
||||||
providers.push(
|
providers.push(
|
||||||
languages.registerDocumentFormattingEditProvider(
|
languages.registerDocumentFormattingEditProvider(
|
||||||
languageId,
|
languageId,
|
||||||
new languageFeatures.DocumentFormattingEditProvider(worker)
|
new languageFeatures.HTMLDocumentFormattingEditProvider(worker)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -143,7 +143,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
|
||||||
providers.push(
|
providers.push(
|
||||||
languages.registerDocumentRangeFormattingEditProvider(
|
languages.registerDocumentRangeFormattingEditProvider(
|
||||||
languageId,
|
languageId,
|
||||||
new languageFeatures.DocumentRangeFormattingEditProvider(worker)
|
new languageFeatures.HTMLDocumentRangeFormattingEditProvider(worker)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,9 @@ import {
|
||||||
DocumentHighlightAdapter,
|
DocumentHighlightAdapter,
|
||||||
RenameAdapter,
|
RenameAdapter,
|
||||||
DocumentSymbolAdapter,
|
DocumentSymbolAdapter,
|
||||||
DocumentLinkAdapter
|
DocumentLinkAdapter,
|
||||||
|
DocumentFormattingEditProvider,
|
||||||
|
DocumentRangeFormattingEditProvider
|
||||||
} from '../common/lspLanguageFeatures';
|
} from '../common/lspLanguageFeatures';
|
||||||
|
|
||||||
export interface WorkerAccessor {
|
export interface WorkerAccessor {
|
||||||
|
|
@ -46,61 +48,9 @@ export class HTMLDocumentSymbolAdapter extends DocumentSymbolAdapter<HTMLWorker>
|
||||||
|
|
||||||
export class HTMLDocumentLinkAdapter extends DocumentLinkAdapter<HTMLWorker> {}
|
export class HTMLDocumentLinkAdapter extends DocumentLinkAdapter<HTMLWorker> {}
|
||||||
|
|
||||||
function fromFormattingOptions(options: languages.FormattingOptions): lsTypes.FormattingOptions {
|
export class HTMLDocumentFormattingEditProvider extends DocumentFormattingEditProvider<HTMLWorker> {}
|
||||||
return {
|
|
||||||
tabSize: options.tabSize,
|
|
||||||
insertSpaces: options.insertSpaces
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export class DocumentFormattingEditProvider implements languages.DocumentFormattingEditProvider {
|
export class HTMLDocumentRangeFormattingEditProvider extends DocumentRangeFormattingEditProvider<HTMLWorker> {}
|
||||||
constructor(private _worker: WorkerAccessor) {}
|
|
||||||
|
|
||||||
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
|
|
||||||
implements languages.DocumentRangeFormattingEditProvider
|
|
||||||
{
|
|
||||||
constructor(private _worker: WorkerAccessor) {}
|
|
||||||
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class FoldingRangeAdapter implements languages.FoldingRangeProvider {
|
export class FoldingRangeAdapter implements languages.FoldingRangeProvider {
|
||||||
constructor(private _worker: WorkerAccessor) {}
|
constructor(private _worker: WorkerAccessor) {}
|
||||||
|
|
|
||||||
|
|
@ -30,7 +30,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
|
||||||
providers.push(
|
providers.push(
|
||||||
languages.registerDocumentFormattingEditProvider(
|
languages.registerDocumentFormattingEditProvider(
|
||||||
languageId,
|
languageId,
|
||||||
new languageFeatures.DocumentFormattingEditProvider(worker)
|
new languageFeatures.JSONDocumentFormattingEditProvider(worker)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
@ -38,7 +38,7 @@ export function setupMode(defaults: LanguageServiceDefaults): IDisposable {
|
||||||
providers.push(
|
providers.push(
|
||||||
languages.registerDocumentRangeFormattingEditProvider(
|
languages.registerDocumentRangeFormattingEditProvider(
|
||||||
languageId,
|
languageId,
|
||||||
new languageFeatures.DocumentRangeFormattingEditProvider(worker)
|
new languageFeatures.JSONDocumentRangeFormattingEditProvider(worker)
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,9 @@ import {
|
||||||
fromRange,
|
fromRange,
|
||||||
CompletionAdapter,
|
CompletionAdapter,
|
||||||
HoverAdapter,
|
HoverAdapter,
|
||||||
DocumentSymbolAdapter
|
DocumentSymbolAdapter,
|
||||||
|
DocumentFormattingEditProvider,
|
||||||
|
DocumentRangeFormattingEditProvider
|
||||||
} from '../common/lspLanguageFeatures';
|
} from '../common/lspLanguageFeatures';
|
||||||
|
|
||||||
export interface WorkerAccessor {
|
export interface WorkerAccessor {
|
||||||
|
|
@ -62,61 +64,9 @@ export class JSONHoverAdapter extends HoverAdapter<JSONWorker> {}
|
||||||
|
|
||||||
export class JSONDocumentSymbolAdapter extends DocumentSymbolAdapter<JSONWorker> {}
|
export class JSONDocumentSymbolAdapter extends DocumentSymbolAdapter<JSONWorker> {}
|
||||||
|
|
||||||
function fromFormattingOptions(options: languages.FormattingOptions): lsTypes.FormattingOptions {
|
export class JSONDocumentFormattingEditProvider extends DocumentFormattingEditProvider<JSONWorker> {}
|
||||||
return {
|
|
||||||
tabSize: options.tabSize,
|
|
||||||
insertSpaces: options.insertSpaces
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export class DocumentFormattingEditProvider implements languages.DocumentFormattingEditProvider {
|
export class JSONDocumentRangeFormattingEditProvider extends DocumentRangeFormattingEditProvider<JSONWorker> {}
|
||||||
constructor(private _worker: WorkerAccessor) {}
|
|
||||||
|
|
||||||
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
|
|
||||||
implements languages.DocumentRangeFormattingEditProvider
|
|
||||||
{
|
|
||||||
constructor(private _worker: WorkerAccessor) {}
|
|
||||||
|
|
||||||
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);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class DocumentColorAdapter implements languages.DocumentColorProvider {
|
export class DocumentColorAdapter implements languages.DocumentColorProvider {
|
||||||
constructor(private _worker: WorkerAccessor) {}
|
constructor(private _worker: WorkerAccessor) {}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue