@@ -93,23 +93,29 @@ export class HoverCM extends CodeMirrorIntegration {
9393 }
9494
9595 protected restore_from_cache (
96- root_position : IRootPosition ,
9796 document : VirtualDocument ,
9897 virtual_position : IVirtualPosition
9998 ) : IResponseData | null {
99+ const { line, ch } = virtual_position ;
100100 const matching_items = this . cache . data . filter ( cache_item => {
101101 if ( cache_item . document !== document ) {
102102 return false ;
103103 }
104104 let range = cache_item . response . range ;
105- let { line, ch } = virtual_position ;
106105 return (
107106 line >= range . start . line &&
108107 line <= range . end . line &&
109108 ( line != range . start . line || ch >= range . start . character ) &&
110109 ( line != range . end . line || ch <= range . end . character )
111110 ) ;
112111 } ) ;
112+ if ( matching_items . length > 1 ) {
113+ console . warn (
114+ 'LSP: Potential hover cache malfunction: ' ,
115+ virtual_position ,
116+ matching_items
117+ ) ;
118+ }
113119 return matching_items . length != 0 ? matching_items [ 0 ] : null ;
114120 }
115121
@@ -157,16 +163,16 @@ export class HoverCM extends CodeMirrorIntegration {
157163 getModifierState ( event , this . modifierKey ) &&
158164 typeof this . last_hover_character !== 'undefined'
159165 ) {
166+ // does not need to be shown if it is already visible (otherwise we would be creating an identical tooltip again!)
167+ if ( this . tooltip && this . tooltip . isVisible && ! this . tooltip . isDisposed ) {
168+ return ;
169+ }
160170 const document = this . virtual_editor . document_at_root_position (
161171 this . last_hover_character
162172 ) ;
163- const virtual_position = this . virtual_editor . root_position_to_virtual_position (
164- this . last_hover_character
165- ) ;
166173 let response_data = this . restore_from_cache (
167- this . last_hover_character ,
168174 document ,
169- virtual_position
175+ this . virtual_position
170176 ) ;
171177 if ( response_data == null ) {
172178 return ;
@@ -312,6 +318,13 @@ export class HoverCM extends CodeMirrorIntegration {
312318 protected async _updateUnderlineAndTooltip (
313319 event : MouseEvent
314320 ) : Promise < boolean > {
321+ const target = event . target as HTMLElement ;
322+
323+ // if over an empty space in a line (and not over a token) then not worth checking
324+ if ( target . classList . contains ( 'CodeMirror-line' ) ) {
325+ return ;
326+ }
327+
315328 const show_tooltip = getModifierState ( event , this . modifierKey ) ;
316329
317330 // currently the events are coming from notebook panel; ideally these would be connected to individual cells,
@@ -330,9 +343,6 @@ export class HoverCM extends CodeMirrorIntegration {
330343 let token = this . virtual_editor . getTokenAt ( root_position ) ;
331344
332345 let document = this . virtual_editor . document_at_root_position ( root_position ) ;
333- let virtual_position = this . virtual_editor . root_position_to_virtual_position (
334- root_position
335- ) ;
336346
337347 if (
338348 this . is_token_empty ( token ) ||
@@ -344,14 +354,13 @@ export class HoverCM extends CodeMirrorIntegration {
344354 }
345355
346356 if ( ! is_equal ( root_position , this . last_hover_character ) ) {
357+ let virtual_position = this . virtual_editor . root_position_to_virtual_position (
358+ root_position
359+ ) ;
347360 this . virtual_position = virtual_position ;
348361 this . last_hover_character = root_position ;
349362
350- let response_data = this . restore_from_cache (
351- root_position ,
352- document ,
353- virtual_position
354- ) ;
363+ let response_data = this . restore_from_cache ( document , virtual_position ) ;
355364
356365 if ( response_data == null ) {
357366 let response = await this . debounced_get_hover . invoke ( ) ;
@@ -470,15 +479,19 @@ export class HoverCM extends CodeMirrorIntegration {
470479 ...response ,
471480 range : {
472481 start : PositionConverter . cm_to_lsp (
473- this . virtual_editor . transform_from_editor_to_root (
474- ce_editor ,
475- editor_range . start
482+ this . virtual_editor . root_position_to_virtual_position (
483+ this . virtual_editor . transform_from_editor_to_root (
484+ ce_editor ,
485+ editor_range . start
486+ )
476487 )
477488 ) ,
478489 end : PositionConverter . cm_to_lsp (
479- this . virtual_editor . transform_from_editor_to_root (
480- ce_editor ,
481- editor_range . end
490+ this . virtual_editor . root_position_to_virtual_position (
491+ this . virtual_editor . transform_from_editor_to_root (
492+ ce_editor ,
493+ editor_range . end
494+ )
482495 )
483496 )
484497 }
0 commit comments