@@ -13,7 +13,6 @@ import { MarkedString } from 'vs/base/common/htmlContent';
1313import { KeyCode , KeyMod , KeyChord } from 'vs/base/common/keyCodes' ;
1414import * as platform from 'vs/base/common/platform' ;
1515import Severity from 'vs/base/common/severity' ;
16- import * as strings from 'vs/base/common/strings' ;
1716import { TPromise } from 'vs/base/common/winjs.base' ;
1817import * as browser from 'vs/base/browser/browser' ;
1918import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent' ;
@@ -374,7 +373,7 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC
374373 static TRIGGER_MODIFIER = platform . isMacintosh ? 'metaKey' : 'ctrlKey' ;
375374 static TRIGGER_SIDEBYSIDE_KEY_VALUE = KeyCode . Alt ;
376375 static TRIGGER_KEY_VALUE = platform . isMacintosh ? KeyCode . Meta : KeyCode . Ctrl ;
377- static MAX_SOURCE_PREVIEW_LINES = 7 ;
376+ static MAX_SOURCE_PREVIEW_LINES = 8 ;
378377
379378 private editor : ICodeEditor ;
380379 private toUnhook : IDisposable [ ] ;
@@ -474,59 +473,41 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC
474473 }
475474
476475 this . textModelResolverService . createModelReference ( result . uri ) . then ( ref => {
477- const model = ref . object ;
478- let hoverMessage : MarkedString ;
479- if ( model && model . textEditorModel ) {
480- const editorModel = model . textEditorModel ;
481- let from = Math . max ( 1 , result . range . startLineNumber ) ;
482- let to : number ;
483-
484- // if we have a range, take that into consideration for the "to" position, otherwise fallback to MAX_SOURCE_PREVIEW_LINES
485- if ( ! Range . isEmpty ( result . range ) ) {
486- to = Math . min ( result . range . endLineNumber , result . range . startLineNumber + GotoDefinitionWithMouseEditorContribution . MAX_SOURCE_PREVIEW_LINES , editorModel . getLineCount ( ) ) ;
487- } else {
488- to = Math . min ( from + GotoDefinitionWithMouseEditorContribution . MAX_SOURCE_PREVIEW_LINES , editorModel . getLineCount ( ) ) ;
489- }
490476
491- let source = editorModel . getValueInRange ( {
492- startLineNumber : from ,
493- startColumn : 1 ,
494- endLineNumber : to ,
495- endColumn : editorModel . getLineMaxColumn ( to )
496- } ) . trim ( ) ;
497-
498- // remove common leading whitespace
499- let min = Number . MAX_VALUE ,
500- regexp = / ^ [ \t ] * / ,
501- match : RegExpExecArray ,
502- contents : string ;
503-
504- while ( from <= to && min > 0 ) {
505- contents = editorModel . getLineContent ( from ++ ) ;
506- if ( contents . trim ( ) . length === 0 ) {
507- // empty or whitespace only
508- continue ;
509- }
510- match = regexp . exec ( contents ) ;
511- min = Math . min ( min , match [ 0 ] . length ) ;
512- }
477+ if ( ! ref . object || ! ref . object . textEditorModel ) {
478+ ref . dispose ( ) ;
479+ return ;
480+ }
513481
514- source = source . replace ( new RegExp ( `^([ \\t]{${ min } })` , 'gm' ) , strings . empty ) ;
482+ const { object : { textEditorModel } } = ref ;
483+ const { startLineNumber } = result . range ;
515484
516- if ( to < editorModel . getLineCount ( ) ) {
517- source += '\n\u2026' ;
518- }
485+ if ( textEditorModel . getLineMaxColumn ( startLineNumber ) === 0 ) {
486+ ref . dispose ( ) ;
487+ return ;
488+ }
519489
520- const language = this . modeService . getModeIdByFilenameOrFirstLine ( editorModel . uri . fsPath ) ;
521- hoverMessage = {
522- language,
523- value : source
524- } ;
490+ const startIndent = textEditorModel . getLineFirstNonWhitespaceColumn ( startLineNumber ) ;
491+ const maxLineNumber = Math . min ( textEditorModel . getLineCount ( ) , startLineNumber + GotoDefinitionWithMouseEditorContribution . MAX_SOURCE_PREVIEW_LINES ) ;
492+ let endLineNumber = startLineNumber + 1 ;
493+ let minIndent = startIndent ;
494+
495+ for ( ; endLineNumber < maxLineNumber ; endLineNumber ++ ) {
496+ let endIndent = textEditorModel . getLineFirstNonWhitespaceColumn ( endLineNumber ) ;
497+ minIndent = Math . min ( minIndent , endIndent ) ;
498+ if ( startIndent === endIndent ) {
499+ break ;
500+ }
525501 }
526502
527- ref . dispose ( ) ;
503+ const previewRange = new Range ( startLineNumber , 1 , endLineNumber + 1 , 1 ) ;
504+ const value = textEditorModel . getValueInRange ( previewRange ) . replace ( new RegExp ( `^\\s{${ minIndent - 1 } }` , 'gm' ) , '' ) . trim ( ) ;
528505
529- this . addDecoration ( new Range ( position . lineNumber , word . startColumn , position . lineNumber , word . endColumn ) , hoverMessage ) ;
506+ this . addDecoration ( new Range ( position . lineNumber , word . startColumn , position . lineNumber , word . endColumn ) , {
507+ language : this . modeService . getModeIdByFilenameOrFirstLine ( textEditorModel . uri . fsPath ) ,
508+ value
509+ } ) ;
510+ ref . dispose ( ) ;
530511 } ) ;
531512 }
532513 } ) . done ( undefined , onUnexpectedError ) ;
0 commit comments