|
| 1 | +import { MaybePromise } from '@theia/core'; |
| 2 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 3 | +import * as monaco from '@theia/monaco-editor-core'; |
| 4 | +import { Formatter } from '../../common/protocol/formatter'; |
| 5 | +import { Contribution, URI } from './contribution'; |
| 6 | + |
| 7 | +@injectable() |
| 8 | +export class Format |
| 9 | + extends Contribution |
| 10 | + implements |
| 11 | + monaco.languages.DocumentRangeFormattingEditProvider, |
| 12 | + monaco.languages.DocumentFormattingEditProvider |
| 13 | +{ |
| 14 | + @inject(Formatter) |
| 15 | + private readonly formatter: Formatter; |
| 16 | + |
| 17 | + override onStart(): MaybePromise<void> { |
| 18 | + const selector = this.selectorOf('ino', 'c', 'cpp', 'h', 'hpp', 'pde'); |
| 19 | + monaco.languages.registerDocumentRangeFormattingEditProvider( |
| 20 | + selector, |
| 21 | + this |
| 22 | + ); |
| 23 | + monaco.languages.registerDocumentFormattingEditProvider(selector, this); |
| 24 | + } |
| 25 | + async provideDocumentRangeFormattingEdits( |
| 26 | + model: monaco.editor.ITextModel, |
| 27 | + range: monaco.Range, |
| 28 | + options: monaco.languages.FormattingOptions, |
| 29 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 30 | + _token: monaco.CancellationToken |
| 31 | + ): Promise<monaco.languages.TextEdit[]> { |
| 32 | + const text = await this.format(model, range, options); |
| 33 | + return [{ range, text }]; |
| 34 | + } |
| 35 | + |
| 36 | + async provideDocumentFormattingEdits( |
| 37 | + model: monaco.editor.ITextModel, |
| 38 | + options: monaco.languages.FormattingOptions, |
| 39 | + // eslint-disable-next-line @typescript-eslint/no-unused-vars |
| 40 | + _token: monaco.CancellationToken |
| 41 | + ): Promise<monaco.languages.TextEdit[]> { |
| 42 | + const range = this.fullRange(model); |
| 43 | + const text = await this.format(model, range, options); |
| 44 | + return [{ range, text }]; |
| 45 | + } |
| 46 | + |
| 47 | + private fullRange(model: monaco.editor.ITextModel): monaco.Range { |
| 48 | + const lastLine = model.getLineCount(); |
| 49 | + const lastLineMaxColumn = model.getLineMaxColumn(lastLine); |
| 50 | + const end = new monaco.Position(lastLine, lastLineMaxColumn); |
| 51 | + return monaco.Range.fromPositions(new monaco.Position(1, 1), end); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * From the currently opened workspaces (IDE2 has always one), it calculates all possible |
| 56 | + * folder locations where the `.clang-format` file could be. |
| 57 | + */ |
| 58 | + private formatterConfigFolderUris(model: monaco.editor.ITextModel): string[] { |
| 59 | + const editorUri = new URI(model.uri.toString()); |
| 60 | + return this.workspaceService |
| 61 | + .tryGetRoots() |
| 62 | + .map(({ resource }) => resource) |
| 63 | + .filter((workspaceUri) => workspaceUri.isEqualOrParent(editorUri)) |
| 64 | + .map((uri) => uri.toString()); |
| 65 | + } |
| 66 | + |
| 67 | + private format( |
| 68 | + model: monaco.editor.ITextModel, |
| 69 | + range: monaco.Range, |
| 70 | + options: monaco.languages.FormattingOptions |
| 71 | + ): Promise<string> { |
| 72 | + console.info( |
| 73 | + `Formatting ${model.uri.toString()} [Range: ${JSON.stringify( |
| 74 | + range.toJSON() |
| 75 | + )}]` |
| 76 | + ); |
| 77 | + const content = model.getValueInRange(range); |
| 78 | + const formatterConfigFolderUris = this.formatterConfigFolderUris(model); |
| 79 | + return this.formatter.format({ |
| 80 | + content, |
| 81 | + formatterConfigFolderUris, |
| 82 | + options, |
| 83 | + }); |
| 84 | + } |
| 85 | + |
| 86 | + private selectorOf( |
| 87 | + ...languageId: string[] |
| 88 | + ): monaco.languages.LanguageSelector { |
| 89 | + return languageId.map((language) => ({ |
| 90 | + language, |
| 91 | + exclusive: true, // <-- this should make sure the custom formatter has higher precedence over the LS formatter. |
| 92 | + })); |
| 93 | + } |
| 94 | +} |
0 commit comments