1+ /*
2+ Copyright (c) 2023-2024, Oracle and/or its affiliates.
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ https://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
15+ */
16+ import { LogMessageNotification , MessageType , TelemetryEventNotification } from "vscode-languageclient" ;
17+ import { notificationOrRequestListenerType } from "../types" ;
18+ import { asRanges , ShowStatusMessageParams , StatusMessageRequest , TestProgressNotification , TextEditorDecorationDisposeNotification , TextEditorDecorationSetNotification } from "../protocol" ;
19+ import { commands , window , workspace } from "vscode" ;
20+ import { isNbJavacDisabledHandler , updateConfigurationValue } from "../../configurations/handlers" ;
21+ import { l10n } from "../../localiser" ;
22+ import { configKeys } from "../../configurations/configuration" ;
23+ import { builtInCommands } from "../../commands/commands" ;
24+ import { globalVars , LOGGER } from "../../extension" ;
25+
26+ const checkInstallNbJavac = ( msg : string ) => {
27+ const NO_JAVA_SUPPORT = "Cannot initialize Java support" ;
28+ if ( msg . startsWith ( NO_JAVA_SUPPORT ) ) {
29+ if ( isNbJavacDisabledHandler ( ) ) {
30+ const message = l10n . value ( "jdk.extension.nbjavac.message.supportedVersionRequired" ) ;
31+ const enable = l10n . value ( "jdk.extension.nbjavac.label.enableNbjavac" ) ;
32+ const settings = l10n . value ( "jdk.extension.nbjavac.label.openSettings" ) ;
33+ window . showErrorMessage ( message , enable , settings ) . then ( reply => {
34+ if ( enable === reply ) {
35+ updateConfigurationValue ( configKeys . disableNbJavac , false ) ;
36+ } else if ( settings === reply ) {
37+ commands . executeCommand ( builtInCommands . openSettings , configKeys . jdkHome ) ;
38+ }
39+ } ) ;
40+ }
41+ }
42+ }
43+
44+ const showStatusBarMessageHandler = ( params : ShowStatusMessageParams ) => {
45+ let decorated : string = params . message ;
46+ let defTimeout ;
47+
48+ switch ( params . type ) {
49+ case MessageType . Error :
50+ decorated = '$(error) ' + params . message ;
51+ defTimeout = 0 ;
52+ checkInstallNbJavac ( params . message ) ;
53+ break ;
54+ case MessageType . Warning :
55+ decorated = '$(warning) ' + params . message ;
56+ defTimeout = 0 ;
57+ break ;
58+ default :
59+ defTimeout = 10000 ;
60+ break ;
61+ }
62+ // params.timeout may be defined but 0 -> should be used
63+ const timeout = params . timeout != undefined ? params . timeout : defTimeout ;
64+ if ( timeout > 0 ) {
65+ window . setStatusBarMessage ( decorated , timeout ) ;
66+ } else {
67+ window . setStatusBarMessage ( decorated ) ;
68+ }
69+ }
70+
71+ const logMessageHandler = ( param : any ) => {
72+ LOGGER . log ( param . message ) ;
73+ }
74+
75+ const testProgressHandler = ( param : any ) => {
76+ if ( globalVars . testAdapter ) {
77+ globalVars . testAdapter . testProgress ( param . suite ) ;
78+ }
79+ }
80+
81+ const textEditorSetDecorationHandler = ( param : any ) => {
82+ let decorationType = globalVars . decorations . get ( param . key ) ;
83+ if ( decorationType ) {
84+ let editorsWithUri = window . visibleTextEditors . filter (
85+ editor => editor . document . uri . toString ( ) == param . uri
86+ ) ;
87+ if ( editorsWithUri . length > 0 ) {
88+ editorsWithUri [ 0 ] . setDecorations ( decorationType , asRanges ( param . ranges ) ) ;
89+ globalVars . decorationParamsByUri . set ( editorsWithUri [ 0 ] . document . uri , param ) ;
90+ }
91+ }
92+ }
93+
94+ const textEditorDecorationDisposeHandler = ( param : any ) => {
95+ let decorationType = globalVars . decorations . get ( param ) ;
96+ if ( decorationType ) {
97+ globalVars . decorations . delete ( param ) ;
98+ decorationType . dispose ( ) ;
99+ globalVars . decorationParamsByUri . forEach ( ( value , key , map ) => {
100+ if ( value . key == param ) {
101+ map . delete ( key ) ;
102+ }
103+ } ) ;
104+ }
105+ }
106+
107+
108+ const telemetryEventHandler = ( param : any ) => {
109+ const ls = globalVars . listeners . get ( param ) ;
110+ if ( ls ) {
111+ for ( const listener of ls ) {
112+ commands . executeCommand ( listener ) ;
113+ }
114+ }
115+ }
116+
117+ export const notificationListeners : notificationOrRequestListenerType [ ] = [ {
118+ type : StatusMessageRequest . type ,
119+ handler : showStatusBarMessageHandler
120+ } , {
121+ type : LogMessageNotification . type ,
122+ handler : logMessageHandler
123+ } , {
124+ type : TestProgressNotification . type ,
125+ handler : testProgressHandler
126+ } , {
127+ type : TextEditorDecorationSetNotification . type ,
128+ handler : textEditorSetDecorationHandler
129+ } , {
130+ type : TextEditorDecorationDisposeNotification . type ,
131+ handler : textEditorDecorationDisposeHandler
132+ } , {
133+ type : TelemetryEventNotification . type ,
134+ handler : telemetryEventHandler
135+ } ] ;
0 commit comments