debugSource.ts 2.2 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');
J
Johannes Rieken 已提交
8
import { IModel, DEBUG_SCHEME } from 'vs/workbench/parts/debug/common/debug';
I
isidor 已提交
9 10 11 12 13 14

export class Source {

	public uri: uri;
	public available: boolean;

I
isidor 已提交
15
	private static INTERNAL_URI_PREFIX = `${DEBUG_SCHEME}://internal/`;
I
isidor 已提交
16

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

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
		if (model) {
			// first try to find the raw source amongst the stack frames - since that represenation has more data (source reference),
41 42
			const processes = model.getProcesses();
			for (let i = 0; i < processes.length; i++) {
I
isidor 已提交
43
				const threads = process[i].getThreads();
44 45 46 47 48 49
				for (let threadId in threads) {
					if (threads.hasOwnProperty(threadId) && threads[threadId].getCachedCallStack()) {
						const found = threads[threadId].getCachedCallStack().filter(sf => sf.source.uri.toString() === uri.toString()).pop();
						if (found) {
							return found.source.raw;
						}
50
					}
51 52 53 54
				}
			}
		}

I
isidor 已提交
55
		// did not find the raw source amongst the stack frames, construct the raw stack frame from the limited data you have.
I
isidor 已提交
56 57
		return Source.isInMemory(uri) ? { name: paths.basename(uri.toString()) } :
			{ path: paths.normalize(uri.fsPath, true), name: paths.basename(uri.fsPath) };
58 59 60 61
	}

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