Patrick Quist | d441116 | 2020-02-27 23:27:57 +0100 | [diff] [blame] | 1 | // Copyright (c) 2020, Compiler Explorer Authors |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are met: |
| 6 | // |
| 7 | // * Redistributions of source code must retain the above copyright notice, |
| 8 | // this list of conditions and the following disclaimer. |
| 9 | // * Redistributions in binary form must reproduce the above copyright |
| 10 | // notice, this list of conditions and the following disclaimer in the |
| 11 | // documentation and/or other materials provided with the distribution. |
| 12 | // |
| 13 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 14 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 17 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 23 | // POSSIBILITY OF SUCH DAMAGE. |
Austin Morton | de85aec | 2020-09-27 00:20:19 -0400 | [diff] [blame] | 24 | |
Markus | 7a754a3 | 2021-10-05 05:49:16 +0200 | [diff] [blame] | 25 | import _ from 'underscore'; |
| 26 | import * as monaco from 'monaco-editor'; |
Patrick Quist | d441116 | 2020-02-27 23:27:57 +0100 | [diff] [blame] | 27 | |
Markus | 7a754a3 | 2021-10-05 05:49:16 +0200 | [diff] [blame] | 28 | interface RegisteredCodeLens { |
| 29 | compilerId: number; |
| 30 | editorModel: monaco.editor.ITextModel; |
| 31 | lenses: monaco.languages.CodeLens[]; |
| 32 | } |
Patrick Quist | d441116 | 2020-02-27 23:27:57 +0100 | [diff] [blame] | 33 | |
Markus | 7a754a3 | 2021-10-05 05:49:16 +0200 | [diff] [blame] | 34 | let registeredCodelenses: RegisteredCodeLens[] = []; |
Rubén Rincón Blanco | 42c7b2b | 2022-02-03 18:04:50 +0100 | [diff] [blame] | 35 | const providersPerLanguage: Record<string, monaco.IDisposable> = {}; |
Patrick Quist | d441116 | 2020-02-27 23:27:57 +0100 | [diff] [blame] | 36 | |
Rubén Rincón Blanco | 42c7b2b | 2022-02-03 18:04:50 +0100 | [diff] [blame] | 37 | export function registerLensesForCompiler( |
| 38 | compilerId: number, |
| 39 | editorModel: monaco.editor.ITextModel, |
Jeremy Rifkin | 384c297 | 2023-02-26 12:21:35 -0500 | [diff] [blame] | 40 | lenses: monaco.languages.CodeLens[], |
Mats Larsen | c199b54 | 2022-04-20 22:39:10 +0200 | [diff] [blame] | 41 | ): void { |
Rubén Rincón Blanco | db229b9 | 2022-02-12 02:41:01 +0100 | [diff] [blame] | 42 | const item = _.find(registeredCodelenses, (item: RegisteredCodeLens): boolean => { |
Patrick Quist | d441116 | 2020-02-27 23:27:57 +0100 | [diff] [blame] | 43 | return item.compilerId === compilerId; |
| 44 | }); |
| 45 | |
| 46 | if (item) { |
| 47 | item.lenses = lenses; |
| 48 | } else { |
| 49 | registeredCodelenses.push({ |
| 50 | compilerId: compilerId, |
| 51 | editorModel: editorModel, |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 52 | lenses: lenses, |
Patrick Quist | d441116 | 2020-02-27 23:27:57 +0100 | [diff] [blame] | 53 | }); |
| 54 | } |
| 55 | } |
| 56 | |
Markus | 7a754a3 | 2021-10-05 05:49:16 +0200 | [diff] [blame] | 57 | function provide(model: monaco.editor.ITextModel): monaco.languages.CodeLensList { |
Rubén Rincón Blanco | db229b9 | 2022-02-12 02:41:01 +0100 | [diff] [blame] | 58 | const item = _.find(registeredCodelenses, (item: RegisteredCodeLens): boolean => { |
Patrick Quist | d441116 | 2020-02-27 23:27:57 +0100 | [diff] [blame] | 59 | return item.editorModel === model; |
| 60 | }); |
| 61 | |
| 62 | if (item) { |
| 63 | return { |
| 64 | lenses: item.lenses, |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 65 | dispose: function () {}, |
Patrick Quist | d441116 | 2020-02-27 23:27:57 +0100 | [diff] [blame] | 66 | }; |
| 67 | } else { |
| 68 | return { |
| 69 | lenses: [], |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 70 | dispose: function () {}, |
Patrick Quist | d441116 | 2020-02-27 23:27:57 +0100 | [diff] [blame] | 71 | }; |
| 72 | } |
| 73 | } |
| 74 | |
Markus | 7a754a3 | 2021-10-05 05:49:16 +0200 | [diff] [blame] | 75 | export function unregister(compilerId: number): void { |
Rubén Rincón Blanco | db229b9 | 2022-02-12 02:41:01 +0100 | [diff] [blame] | 76 | const item = _.find(registeredCodelenses, (item: RegisteredCodeLens): boolean => { |
Patrick Quist | d441116 | 2020-02-27 23:27:57 +0100 | [diff] [blame] | 77 | return item.compilerId === compilerId; |
| 78 | }); |
| 79 | |
| 80 | if (item) { |
| 81 | registeredCodelenses = _.without(registeredCodelenses, item); |
| 82 | } |
| 83 | } |
| 84 | |
Markus | 7a754a3 | 2021-10-05 05:49:16 +0200 | [diff] [blame] | 85 | export function registerProviderForLanguage(language: string): void { |
Jeremy Rifkin | 0bffd4e | 2022-04-15 18:36:00 -0400 | [diff] [blame] | 86 | if (!(language in providersPerLanguage)) { |
Patrick Quist | d441116 | 2020-02-27 23:27:57 +0100 | [diff] [blame] | 87 | providersPerLanguage[language] = monaco.languages.registerCodeLensProvider(language, { |
Rubén Rincón Blanco | ccff4b9 | 2020-08-04 22:39:02 +0200 | [diff] [blame] | 88 | provideCodeLenses: provide, |
Patrick Quist | d441116 | 2020-02-27 23:27:57 +0100 | [diff] [blame] | 89 | }); |
| 90 | } |
| 91 | } |