src/vs/workbench/services/search/common/searchHelpers.ts/addContextToEditorMatches
export function addContextToEditorMatches(matches: ITextSearchMatch[], model: ITextModel, query: ITextQuery): ITextSearchResult[] {
	const results: ITextSearchResult[] = [];

	let prevLine = -1;
	for (let i = 0; i < matches.length; i++) {
		const { start: matchStartLine, end: matchEndLine } = getMatchStartEnd(matches[i]);
		if (typeof query.beforeContext === 'number' && query.beforeContext > 0) {
			const beforeContextStartLine = Math.max(prevLine + 1, matchStartLine - query.beforeContext);
			for (let b = beforeContextStartLine; b < matchStartLine; b++) {
				results.push(<ITextSearchContext>{
					text: model.getLineContent(b + 1),
					lineNumber: b
				});
			}
		}

		results.push(matches[i]);

		const nextMatch = matches[i + 1];
		const nextMatchStartLine = nextMatch ? getMatchStartEnd(nextMatch).start : Number.MAX_VALUE;
		if (typeof query.afterContext === 'number' && query.afterContext > 0) {
			const afterContextToLine = Math.min(nextMatchStartLine - 1, matchEndLine + query.afterContext, model.getLineCount() - 1);
			for (let a = matchEndLine + 1; a <= afterContextToLine; a++) {
				results.push(<ITextSearchContext>{
					text: model.getLineContent(a + 1),
					lineNumber: a
				});
			}
		}

		prevLine = matchEndLine;
	}

	return results;
}