|
| 1 | +import { Disposable, DisposableCollection } from '@theia/core'; |
| 2 | +import URI from '@theia/core/lib/common/uri'; |
| 3 | +import { inject, injectable } from '@theia/core/shared/inversify'; |
| 4 | +import { |
| 5 | + Location, |
| 6 | + Range, |
| 7 | +} from '@theia/core/shared/vscode-languageserver-protocol'; |
| 8 | +import { CoreError } from '../../common/protocol/core-service'; |
| 9 | +import { Contribution } from './contribution'; |
| 10 | +import { CoreErrorHandler } from './core-error-handler'; |
| 11 | +import { EditorManager } from '@theia/editor/lib/browser/editor-manager'; |
| 12 | +import { EditorWidget } from '@theia/editor/lib/browser'; |
| 13 | +import { ApplicationShell, FrontendApplication } from '@theia/core/lib/browser'; |
| 14 | +import { |
| 15 | + EditorDecoration, |
| 16 | + TrackedRangeStickiness, |
| 17 | +} from '@theia/editor/lib/browser/decorations/editor-decoration'; |
| 18 | + |
| 19 | +@injectable() |
| 20 | +export class EditorDecorations extends Contribution { |
| 21 | + @inject(EditorManager) |
| 22 | + private readonly editorManager: EditorManager; |
| 23 | + |
| 24 | + @inject(CoreErrorHandler) |
| 25 | + private readonly coreErrorHandler: CoreErrorHandler; |
| 26 | + |
| 27 | + private shell: ApplicationShell | undefined; |
| 28 | + |
| 29 | + private readonly toDisposeOnCompilerErrorDidChange = |
| 30 | + new DisposableCollection(); |
| 31 | + |
| 32 | + override onStart(app: FrontendApplication): void { |
| 33 | + this.shell = app.shell; |
| 34 | + this.coreErrorHandler.onCompilerErrorsDidChange((errors) => |
| 35 | + this.handleCompilerErrorsDidChange(errors) |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + private async handleCompilerErrorsDidChange( |
| 40 | + errors: CoreError.Compiler[] |
| 41 | + ): Promise<void> { |
| 42 | + this.toDisposeOnCompilerErrorDidChange.dispose(); |
| 43 | + this.toDisposeOnCompilerErrorDidChange.pushAll( |
| 44 | + await Promise.all([ |
| 45 | + this.decorateEditors(errors), |
| 46 | + this.registerCodeLens(errors), |
| 47 | + ]) |
| 48 | + ); |
| 49 | + const first = errors[0]; |
| 50 | + if (first) { |
| 51 | + await this.revealLocationInEditor(first.location); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + private async decorateEditors( |
| 56 | + errors: CoreError.Compiler[] |
| 57 | + ): Promise<Disposable> { |
| 58 | + return new DisposableCollection( |
| 59 | + ...(await Promise.all( |
| 60 | + [ |
| 61 | + ...errors |
| 62 | + .reduce((acc, curr) => { |
| 63 | + const { |
| 64 | + location: { uri }, |
| 65 | + } = curr; |
| 66 | + let errors = acc.get(uri); |
| 67 | + if (!errors) { |
| 68 | + errors = []; |
| 69 | + acc.set(uri, errors); |
| 70 | + } |
| 71 | + errors.push(curr); |
| 72 | + return acc; |
| 73 | + }, new Map<string, CoreError.Compiler[]>()) |
| 74 | + .entries(), |
| 75 | + ].map(([uri, errors]) => this.decorateEditor(uri, errors)) |
| 76 | + )) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + private async decorateEditor( |
| 81 | + uri: string, |
| 82 | + errors: CoreError.Compiler[] |
| 83 | + ): Promise<Disposable> { |
| 84 | + const editor = await this.editorManager.getByUri(new URI(uri)); |
| 85 | + if (!editor) { |
| 86 | + return Disposable.NULL; |
| 87 | + } |
| 88 | + const oldDecorations = editor.editor.deltaDecorations({ |
| 89 | + oldDecorations: [], |
| 90 | + newDecorations: errors.map((error) => |
| 91 | + this.compilerErrorDecoration(error.location.range) |
| 92 | + ), |
| 93 | + }); |
| 94 | + return Disposable.create(() => { |
| 95 | + this.editorManager.getByUri(new URI(uri)).then((e) => { |
| 96 | + if (e) { |
| 97 | + e.editor.deltaDecorations({ oldDecorations, newDecorations: [] }); |
| 98 | + } |
| 99 | + }); |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + private compilerErrorDecoration(range: Range): EditorDecoration { |
| 104 | + return { |
| 105 | + range, |
| 106 | + options: { |
| 107 | + isWholeLine: true, |
| 108 | + className: 'core-error', |
| 109 | + stickiness: TrackedRangeStickiness.NeverGrowsWhenTypingAtEdges, |
| 110 | + }, |
| 111 | + }; |
| 112 | + } |
| 113 | + |
| 114 | + private async registerCodeLens( |
| 115 | + errors: CoreError.Compiler[] |
| 116 | + ): Promise<Disposable> { |
| 117 | + return new DisposableCollection(); |
| 118 | + } |
| 119 | + |
| 120 | + // The double editor activation logic is required: https://github.com/eclipse-theia/theia/issues/11284 |
| 121 | + private async revealLocationInEditor( |
| 122 | + location: Location |
| 123 | + ): Promise<EditorWidget | undefined> { |
| 124 | + const { uri, range: selection } = location; |
| 125 | + const editor = await this.editorManager.getByUri(new URI(uri), { |
| 126 | + mode: 'activate', |
| 127 | + selection, |
| 128 | + }); |
| 129 | + if (editor && this.shell) { |
| 130 | + const activeWidget = await this.shell.activateWidget(editor.id); |
| 131 | + if (!activeWidget) { |
| 132 | + console.warn( |
| 133 | + `editor widget activation has failed. editor widget ${editor.id} expected to be the active one.` |
| 134 | + ); |
| 135 | + return editor; |
| 136 | + } |
| 137 | + if (editor !== activeWidget) { |
| 138 | + console.warn( |
| 139 | + `active widget was not the same as previously activated editor. editor widget ID ${editor.id}, active widget ID: ${activeWidget.id}` |
| 140 | + ); |
| 141 | + } |
| 142 | + return editor; |
| 143 | + } |
| 144 | + console.warn(`could not found editor widget for URI: ${uri}`); |
| 145 | + return undefined; |
| 146 | + } |
| 147 | +} |
0 commit comments