debugSource.ts 2.1 KB
Newer Older
I
isidor 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  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';
import paths = require('vs/base/common/paths');
8
import { IModel } from 'vs/workbench/parts/debug/common/debug';
I
isidor 已提交
9 10 11 12 13 14 15 16

export class Source {

	public uri: uri;
	public available: boolean;

	private static INTERNAL_URI_PREFIX = 'debug://internal/';

17
	constructor(public raw: DebugProtocol.Source) {
18
		this.uri = raw.path ? uri.file(raw.path) : uri.parse(Source.INTERNAL_URI_PREFIX + raw.name);
I
isidor 已提交
19 20 21
		this.available = true;
	}

22 23 24 25 26 27 28 29 30 31 32 33
	public get name() {
		return this.raw.name;
	}

	public get origin() {
		return this.raw.origin;
	}

	public get reference() {
		return this.raw.sourceReference;
	}

34 35 36 37 38
	public get inMemory() {
		return Source.isInMemory(this.uri);
	}

	public static toRawSource(uri: uri, model: IModel): DebugProtocol.Source {
39 40 41 42
		if (model) {
			// first try to find the raw source amongst the stack frames - since that represenation has more data (source reference),
			const threads = model.getThreads();
			for (let threadId in threads) {
43 44
				if (threads.hasOwnProperty(threadId) && threads[threadId].getCachedCallStack()) {
					const found = threads[threadId].getCachedCallStack().filter(sf => sf.source.uri.toString() === uri.toString()).pop();
45 46 47
					if (found) {
						return found.source.raw;
					}
48 49 50 51
				}
			}
		}

I
isidor 已提交
52
		// did not find the raw source amongst the stack frames, construct the raw stack frame from the limited data you have.
53 54
		return Source.isInMemory(uri) ? { name: Source.getName(uri) } :
			{ path: paths.normalize(uri.fsPath, true) };
I
isidor 已提交
55 56
	}

57
	private static getName(uri: uri): string {
I
isidor 已提交
58
		const uriStr = uri.toString();
59 60 61 62 63
		return uriStr.substr(uriStr.lastIndexOf('/') + 1);
	}

	private static isInMemory(uri: uri): boolean {
		return uri.toString().indexOf(Source.INTERNAL_URI_PREFIX) === 0;
I
isidor 已提交
64 65
	}
}