diff --git a/src/vs/editor/contrib/goToDeclaration/browser/goToDeclaration.ts b/src/vs/editor/contrib/goToDeclaration/browser/goToDeclaration.ts index de061b1cd96..a7acd090914 100644 --- a/src/vs/editor/contrib/goToDeclaration/browser/goToDeclaration.ts +++ b/src/vs/editor/contrib/goToDeclaration/browser/goToDeclaration.ts @@ -13,7 +13,6 @@ import { MarkedString } from 'vs/base/common/htmlContent'; import { KeyCode, KeyMod, KeyChord } from 'vs/base/common/keyCodes'; import * as platform from 'vs/base/common/platform'; import Severity from 'vs/base/common/severity'; -import * as strings from 'vs/base/common/strings'; import { TPromise } from 'vs/base/common/winjs.base'; import * as browser from 'vs/base/browser/browser'; import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent'; @@ -374,7 +373,7 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC static TRIGGER_MODIFIER = platform.isMacintosh ? 'metaKey' : 'ctrlKey'; static TRIGGER_SIDEBYSIDE_KEY_VALUE = KeyCode.Alt; static TRIGGER_KEY_VALUE = platform.isMacintosh ? KeyCode.Meta : KeyCode.Ctrl; - static MAX_SOURCE_PREVIEW_LINES = 7; + static MAX_SOURCE_PREVIEW_LINES = 8; private editor: ICodeEditor; private toUnhook: IDisposable[]; @@ -474,59 +473,41 @@ class GotoDefinitionWithMouseEditorContribution implements editorCommon.IEditorC } this.textModelResolverService.createModelReference(result.uri).then(ref => { - const model = ref.object; - let hoverMessage: MarkedString; - if (model && model.textEditorModel) { - const editorModel = model.textEditorModel; - let from = Math.max(1, result.range.startLineNumber); - let to: number; - // if we have a range, take that into consideration for the "to" position, otherwise fallback to MAX_SOURCE_PREVIEW_LINES - if (!Range.isEmpty(result.range)) { - to = Math.min(result.range.endLineNumber, result.range.startLineNumber + GotoDefinitionWithMouseEditorContribution.MAX_SOURCE_PREVIEW_LINES, editorModel.getLineCount()); - } else { - to = Math.min(from + GotoDefinitionWithMouseEditorContribution.MAX_SOURCE_PREVIEW_LINES, editorModel.getLineCount()); - } - - let source = editorModel.getValueInRange({ - startLineNumber: from, - startColumn: 1, - endLineNumber: to, - endColumn: editorModel.getLineMaxColumn(to) - }).trim(); - - // remove common leading whitespace - let min = Number.MAX_VALUE, - regexp = /^[ \t]*/, - match: RegExpExecArray, - contents: string; - - while (from <= to && min > 0) { - contents = editorModel.getLineContent(from++); - if (contents.trim().length === 0) { - // empty or whitespace only - continue; - } - match = regexp.exec(contents); - min = Math.min(min, match[0].length); - } - - source = source.replace(new RegExp(`^([ \\t]{${min}})`, 'gm'), strings.empty); - - if (to < editorModel.getLineCount()) { - source += '\n\u2026'; - } - - const language = this.modeService.getModeIdByFilenameOrFirstLine(editorModel.uri.fsPath); - hoverMessage = { - language, - value: source - }; + if (!ref.object || !ref.object.textEditorModel) { + ref.dispose(); + return; } - ref.dispose(); + const { object: { textEditorModel } } = ref; + const { startLineNumber } = result.range; - this.addDecoration(new Range(position.lineNumber, word.startColumn, position.lineNumber, word.endColumn), hoverMessage); + if (textEditorModel.getLineMaxColumn(startLineNumber) === 0) { + ref.dispose(); + return; + } + + const startIndent = textEditorModel.getLineFirstNonWhitespaceColumn(startLineNumber); + const maxLineNumber = Math.min(textEditorModel.getLineCount(), startLineNumber + GotoDefinitionWithMouseEditorContribution.MAX_SOURCE_PREVIEW_LINES); + let endLineNumber = startLineNumber + 1; + let minIndent = startIndent; + + for (; endLineNumber < maxLineNumber; endLineNumber++) { + let endIndent = textEditorModel.getLineFirstNonWhitespaceColumn(endLineNumber); + minIndent = Math.min(minIndent, endIndent); + if (startIndent === endIndent) { + break; + } + } + + const previewRange = new Range(startLineNumber, 1, endLineNumber + 1, 1); + const value = textEditorModel.getValueInRange(previewRange).replace(new RegExp(`^\\s{${minIndent - 1}}`, 'gm'), '').trim(); + + this.addDecoration(new Range(position.lineNumber, word.startColumn, position.lineNumber, word.endColumn), { + language: this.modeService.getModeIdByFilenameOrFirstLine(textEditorModel.uri.fsPath), + value + }); + ref.dispose(); }); } }).done(undefined, onUnexpectedError);