debugContentProvider.ts 3.8 KB
Newer Older
1 2 3 4 5 6
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

import uri from 'vs/base/common/uri';
7
import { localize } from 'vs/nls';
8
import { TPromise } from 'vs/base/common/winjs.base';
I
isidor 已提交
9
import { guessMimeTypes, MIME_TEXT } from 'vs/base/common/mime';
10 11 12
import { IModel } from 'vs/editor/common/editorCommon';
import { IModelService } from 'vs/editor/common/services/modelService';
import { IModeService } from 'vs/editor/common/services/modeService';
13
import { ITextModelService, ITextModelContentProvider } from 'vs/editor/common/services/resolverService';
14
import { IWorkbenchContribution } from 'vs/workbench/common/contributions';
15
import { DEBUG_SCHEME, IDebugService, IProcess } from 'vs/workbench/parts/debug/common/debug';
16

A
Andre Weinand 已提交
17 18 19 20 21 22 23 24 25 26 27 28 29
/**
 * Debug URI format
 *
 * a debug URI represents a Source object and the debug session where the Source comes from.
 *
 *       debug:arbitrary_path?session=123e4567-e89b-12d3-a456-426655440000&ref=1016
 *       \___/ \____________/ \__________________________________________/ \______/
 *         |          |                             |                          |
 *      scheme   source.path                    session id            source.referencequery
 *
 * the arbitrary_path and the session id are encoded with 'encodeURIComponent'
 *
 */
30 31 32
export class DebugContentProvider implements IWorkbenchContribution, ITextModelContentProvider {

	constructor(
33
		@ITextModelService textModelResolverService: ITextModelService,
34 35 36 37
		@IDebugService private debugService: IDebugService,
		@IModelService private modelService: IModelService,
		@IModeService private modeService: IModeService
	) {
38
		textModelResolverService.registerTextModelContentProvider(DEBUG_SCHEME, this);
39 40 41 42 43 44 45
	}

	public getId(): string {
		return 'debug.contentprovider';
	}

	public provideTextContent(resource: uri): TPromise<IModel> {
46 47

		let process: IProcess;
48 49
		let sourceRef: number;

50 51 52 53
		if (resource.query) {
			const keyvalues = resource.query.split('&');
			for (let keyvalue of keyvalues) {
				const pair = keyvalue.split('=');
54 55 56 57 58
				if (pair.length === 2) {
					switch (pair[0]) {
						case 'session':
							process = this.debugService.findProcessByUUID(decodeURIComponent(pair[1]));
							break;
59
						case 'ref':
60 61 62
							sourceRef = parseInt(pair[1]);
							break;
					}
63 64 65 66 67
				}
			}
		}

		if (!process) {
68
			// fallback: use focused process
69 70
			process = this.debugService.getViewModel().focusedProcess;
		}
71

72
		if (!process) {
73
			return TPromise.wrapError<IModel>(new Error(localize('unable', "Unable to resolve the resource without a debug session")));
74
		}
75
		const source = process.sources.get(resource.toString());
R
Rob Lourens 已提交
76 77 78
		let rawSource: DebugProtocol.Source;
		if (source) {
			rawSource = source.raw;
79 80 81
			if (!sourceRef) {
				sourceRef = source.reference;
			}
R
Rob Lourens 已提交
82
		} else {
83 84 85 86 87
			// create a Source
			rawSource = {
				path: resource.with({ scheme: '', query: '' }).toString(true),	// Remove debug: scheme
				sourceReference: sourceRef
			};
R
Rob Lourens 已提交
88
		}
89

90 91
		return process.session.source({ sourceReference: sourceRef, source: rawSource }).then(response => {

92 93
			const mime = response.body.mimeType || guessMimeTypes(resource.toString())[0];
			const modePromise = this.modeService.getOrCreateMode(mime);
94
			const model = this.modelService.createModel(response.body.content, modePromise, resource);
95

96
			return model;
I
isidor 已提交
97
		}, (err: DebugProtocol.ErrorResponse) => {
98

99
			this.debugService.sourceIsNotAvailable(resource);
I
isidor 已提交
100 101 102 103
			const modePromise = this.modeService.getOrCreateMode(MIME_TEXT);
			const model = this.modelService.createModel(err.message, modePromise, resource);

			return model;
104 105 106
		});
	}
}