blob: bde64c5a03fae40743316624f88ad13fc8d37585 [file] [log] [blame] [raw]
Patrick Quistd4411162020-02-27 23:27:57 +01001// 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 Mortonde85aec2020-09-27 00:20:19 -040024
Markus7a754a32021-10-05 05:49:16 +020025import _ from 'underscore';
26import * as monaco from 'monaco-editor';
Patrick Quistd4411162020-02-27 23:27:57 +010027
Markus7a754a32021-10-05 05:49:16 +020028interface RegisteredCodeLens {
29 compilerId: number;
30 editorModel: monaco.editor.ITextModel;
31 lenses: monaco.languages.CodeLens[];
32}
Patrick Quistd4411162020-02-27 23:27:57 +010033
Markus7a754a32021-10-05 05:49:16 +020034let registeredCodelenses: RegisteredCodeLens[] = [];
Rubén Rincón Blanco42c7b2b2022-02-03 18:04:50 +010035const providersPerLanguage: Record<string, monaco.IDisposable> = {};
Patrick Quistd4411162020-02-27 23:27:57 +010036
Rubén Rincón Blanco42c7b2b2022-02-03 18:04:50 +010037export function registerLensesForCompiler(
38 compilerId: number,
39 editorModel: monaco.editor.ITextModel,
Jeremy Rifkin384c2972023-02-26 12:21:35 -050040 lenses: monaco.languages.CodeLens[],
Mats Larsenc199b542022-04-20 22:39:10 +020041): void {
Rubén Rincón Blancodb229b92022-02-12 02:41:01 +010042 const item = _.find(registeredCodelenses, (item: RegisteredCodeLens): boolean => {
Patrick Quistd4411162020-02-27 23:27:57 +010043 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 Blancoccff4b92020-08-04 22:39:02 +020052 lenses: lenses,
Patrick Quistd4411162020-02-27 23:27:57 +010053 });
54 }
55}
56
Markus7a754a32021-10-05 05:49:16 +020057function provide(model: monaco.editor.ITextModel): monaco.languages.CodeLensList {
Rubén Rincón Blancodb229b92022-02-12 02:41:01 +010058 const item = _.find(registeredCodelenses, (item: RegisteredCodeLens): boolean => {
Patrick Quistd4411162020-02-27 23:27:57 +010059 return item.editorModel === model;
60 });
61
62 if (item) {
63 return {
64 lenses: item.lenses,
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020065 dispose: function () {},
Patrick Quistd4411162020-02-27 23:27:57 +010066 };
67 } else {
68 return {
69 lenses: [],
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020070 dispose: function () {},
Patrick Quistd4411162020-02-27 23:27:57 +010071 };
72 }
73}
74
Markus7a754a32021-10-05 05:49:16 +020075export function unregister(compilerId: number): void {
Rubén Rincón Blancodb229b92022-02-12 02:41:01 +010076 const item = _.find(registeredCodelenses, (item: RegisteredCodeLens): boolean => {
Patrick Quistd4411162020-02-27 23:27:57 +010077 return item.compilerId === compilerId;
78 });
79
80 if (item) {
81 registeredCodelenses = _.without(registeredCodelenses, item);
82 }
83}
84
Markus7a754a32021-10-05 05:49:16 +020085export function registerProviderForLanguage(language: string): void {
Jeremy Rifkin0bffd4e2022-04-15 18:36:00 -040086 if (!(language in providersPerLanguage)) {
Patrick Quistd4411162020-02-27 23:27:57 +010087 providersPerLanguage[language] = monaco.languages.registerCodeLensProvider(language, {
Rubén Rincón Blancoccff4b92020-08-04 22:39:02 +020088 provideCodeLenses: provide,
Patrick Quistd4411162020-02-27 23:27:57 +010089 });
90 }
91}