Skip to content

Commit

Permalink
fix #21550
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Apr 21, 2017
1 parent 372ca2e commit 34cdf9f
Showing 1 changed file with 29 additions and 48 deletions.
77 changes: 29 additions & 48 deletions src/vs/editor/contrib/goToDeclaration/browser/goToDeclaration.ts
Expand Up @@ -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';
Expand Down Expand Up @@ -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[];
Expand Down Expand Up @@ -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);
}
if (!ref.object || !ref.object.textEditorModel) {
ref.dispose();
return;
}

source = source.replace(new RegExp(`^([ \\t]{${min}})`, 'gm'), strings.empty);
const { object: { textEditorModel } } = ref;
const { startLineNumber } = result.range;

if (to < editorModel.getLineCount()) {
source += '\n\u2026';
}
if (textEditorModel.getLineMaxColumn(startLineNumber) === 0) {
ref.dispose();
return;
}

const language = this.modeService.getModeIdByFilenameOrFirstLine(editorModel.uri.fsPath);
hoverMessage = {
language,
value: source
};
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;
}
}

ref.dispose();
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), hoverMessage);
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);
Expand Down

0 comments on commit 34cdf9f

Please sign in to comment.