repl.ts 7.6 KB
Newer Older
E
Erich Gamma 已提交
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 'vs/css!./media/repl';
7
import nls = require('vs/nls');
E
Erich Gamma 已提交
8 9 10
import { TPromise, Promise } from 'vs/base/common/winjs.base';
import errors = require('vs/base/common/errors');
import lifecycle = require('vs/base/common/lifecycle');
I
isidor 已提交
11
import actions = require('vs/base/common/actions');
E
Erich Gamma 已提交
12 13 14
import builder = require('vs/base/browser/builder');
import dom = require('vs/base/browser/dom');
import platform = require('vs/base/common/platform');
J
Joao Moreno 已提交
15
import tree = require('vs/base/parts/tree/browser/tree');
E
Erich Gamma 已提交
16 17 18
import treeimpl = require('vs/base/parts/tree/browser/treeImpl');
import viewer = require('vs/workbench/parts/debug/browser/replViewer');
import debug = require('vs/workbench/parts/debug/common/debug');
19
import debugactions = require('vs/workbench/parts/debug/electron-browser/debugActions');
I
isidor 已提交
20
import replhistory = require('vs/workbench/parts/debug/common/replHistory');
I
isidor 已提交
21
import { Panel } from 'vs/workbench/browser/panel';
E
Erich Gamma 已提交
22 23
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IContextViewService, IContextMenuService } from 'vs/platform/contextview/browser/contextView';
I
isidor 已提交
24
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
E
Erich Gamma 已提交
25 26 27 28
import { IWorkspaceContextService } from 'vs/workbench/services/workspace/common/contextService';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { CommonKeybindings } from 'vs/base/common/keyCodes';

I
isidor 已提交
29
const $ = dom.emmet;
E
Erich Gamma 已提交
30

31
const replTreeOptions: tree.ITreeOptions = {
E
Erich Gamma 已提交
32 33
	indentPixels: 8,
	twistiePixels: 20,
34 35
	paddingOnRow: false,
	ariaLabel: nls.localize('replAriaLabel', "Read Eval Print Loop Panel")
E
Erich Gamma 已提交
36 37
};

I
isidor 已提交
38
const HISTORY_STORAGE_KEY = 'debug.repl.history';
E
Erich Gamma 已提交
39

I
isidor 已提交
40
export class Repl extends Panel {
E
Erich Gamma 已提交
41

42
	private static HALF_WIDTH_TYPICAL = 'n';
E
Erich Gamma 已提交
43 44 45 46 47 48

	private static HISTORY: replhistory.ReplHistory;
	private static REFRESH_DELAY = 500; // delay in ms to refresh the repl for new elements to show

	private toDispose: lifecycle.IDisposable[];
	private tree: tree.ITree;
I
isidor 已提交
49
	private renderer: viewer.ReplExpressionsRenderer;
50
	private characterWidthSurveyor: HTMLElement;
E
Erich Gamma 已提交
51 52 53
	private treeContainer: HTMLElement;
	private replInput: HTMLInputElement;
	private refreshTimeoutHandle: number;
I
isidor 已提交
54
	private actions: actions.IAction[];
E
Erich Gamma 已提交
55 56 57 58 59 60 61 62

	constructor(
		@debug.IDebugService private debugService: debug.IDebugService,
		@IContextMenuService private contextMenuService: IContextMenuService,
		@IWorkspaceContextService private contextService: IWorkspaceContextService,
		@ITelemetryService telemetryService: ITelemetryService,
		@IInstantiationService private instantiationService: IInstantiationService,
		@IContextViewService private contextViewService: IContextViewService,
I
isidor 已提交
63
		@IStorageService private storageService: IStorageService
E
Erich Gamma 已提交
64
	) {
I
isidor 已提交
65
		super(debug.REPL_ID, telemetryService);
E
Erich Gamma 已提交
66 67

		this.toDispose = [];
I
isidor 已提交
68
		this.registerListeners();
E
Erich Gamma 已提交
69 70
	}

I
isidor 已提交
71
	private registerListeners(): void {
E
Erich Gamma 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
		this.toDispose.push(this.debugService.getModel().addListener2(debug.ModelEvents.REPL_ELEMENTS_UPDATED, (re: debug.ITreeElement|debug.ITreeElement[]) => {
			this.onReplElementsUpdated(re);
		}));
	}

	private onReplElementsUpdated(re: debug.ITreeElement | debug.ITreeElement[]): void {
		if (this.tree) {
			if (this.refreshTimeoutHandle) {
				return; // refresh already triggered
			}

			this.refreshTimeoutHandle = setTimeout(() => {
				delete this.refreshTimeoutHandle;

				const scrollPosition = this.tree.getScrollPosition();
				this.tree.refresh().then(() => {
					if (scrollPosition === 0 || scrollPosition === 1) {
						return this.tree.setScrollPosition(1); // keep scrolling to the end unless user scrolled up
					}
				}, errors.onUnexpectedError);
			}, Repl.REFRESH_DELAY);
		}
	}

I
isidor 已提交
96 97
	public create(parent: builder.Builder): TPromise<void> {
		super.create(parent);
I
isidor 已提交
98
		const container = dom.append(parent.getHTMLElement(), $('.repl'));
E
Erich Gamma 已提交
99
		this.treeContainer = dom.append(container, $('.repl-tree'));
I
isidor 已提交
100
		const replInputContainer = dom.append(container, $(platform.isWindows ? '.repl-input-wrapper.windows' : platform.isMacintosh ? '.repl-input-wrapper.mac' : '.repl-input-wrapper.linux'));
E
Erich Gamma 已提交
101
		this.replInput = <HTMLInputElement>dom.append(replInputContainer, $('input.repl-input'));
B
Benjamin Pasero 已提交
102
		this.replInput.type = 'text';
E
Erich Gamma 已提交
103

I
isidor 已提交
104
		this.toDispose.push(dom.addStandardDisposableListener(this.replInput, 'keydown', (e: dom.IKeyboardEvent) => {
E
Erich Gamma 已提交
105 106 107 108 109 110
			let trimmedValue = this.replInput.value.trim();

			if (e.equals(CommonKeybindings.ENTER) && trimmedValue) {
				this.debugService.addReplExpression(trimmedValue);
				Repl.HISTORY.evaluated(trimmedValue);
				this.replInput.value = '';
I
isidor 已提交
111
				e.preventDefault();
E
Erich Gamma 已提交
112
			} else if (e.equals(CommonKeybindings.UP_ARROW) || e.equals(CommonKeybindings.DOWN_ARROW)) {
I
isidor 已提交
113
				const historyInput = e.equals(CommonKeybindings.UP_ARROW) ? Repl.HISTORY.previous() : Repl.HISTORY.next();
E
Erich Gamma 已提交
114 115 116
				if (historyInput) {
					Repl.HISTORY.remember(this.replInput.value, e.equals(CommonKeybindings.UP_ARROW));
					this.replInput.value = historyInput;
I
isidor 已提交
117
					// always leave cursor at the end.
E
Erich Gamma 已提交
118 119 120
					e.preventDefault();
				}
			}
I
isidor 已提交
121
		}));
122
		this.toDispose.push(dom.addStandardDisposableListener(this.replInput, dom.EventType.FOCUS, () => dom.addClass(replInputContainer, 'synthetic-focus')));
I
isidor 已提交
123 124
		this.toDispose.push(dom.addStandardDisposableListener(this.replInput, dom.EventType.BLUR, () => dom.removeClass(replInputContainer, 'synthetic-focus')));

125
		this.characterWidthSurveyor = dom.append(container, $('.surveyor'));
126
		this.characterWidthSurveyor.textContent = Repl.HALF_WIDTH_TYPICAL;
127 128 129 130 131
		for (let i = 0; i < 10; i++) {
			this.characterWidthSurveyor.textContent += this.characterWidthSurveyor.textContent;
		}
		this.characterWidthSurveyor.style.fontSize = platform.isMacintosh ? '12px' : '14px';

I
isidor 已提交
132
		this.renderer = this.instantiationService.createInstance(viewer.ReplExpressionsRenderer);
E
Erich Gamma 已提交
133 134
		this.tree = new treeimpl.Tree(this.treeContainer, {
			dataSource: new viewer.ReplExpressionsDataSource(this.debugService),
I
isidor 已提交
135
			renderer: this.renderer,
136
			accessibilityProvider: new viewer.ReplExpressionsAccessibilityProvider(),
E
Erich Gamma 已提交
137 138 139 140 141 142 143
			controller: new viewer.ReplExpressionsController(this.debugService, this.contextMenuService, new viewer.ReplExpressionsActionProvider(this.instantiationService), this.replInput, false)
		}, replTreeOptions);

		if (!Repl.HISTORY) {
			Repl.HISTORY = new replhistory.ReplHistory(JSON.parse(this.storageService.get(HISTORY_STORAGE_KEY, StorageScope.WORKSPACE, '[]')));
		}

I
isidor 已提交
144
		return this.tree.setInput(this.debugService.getModel());
E
Erich Gamma 已提交
145 146 147 148
	}

	public layout(dimension: builder.Dimension): void {
		if (this.tree) {
149
			this.renderer.setWidth(dimension.width - 20, this.characterWidthSurveyor.clientWidth / this.characterWidthSurveyor.textContent.length);
150
			this.tree.layout(dimension.height - 22);
E
Erich Gamma 已提交
151 152 153 154 155 156 157 158 159 160 161
		}
	}

	public focus(): void {
		this.replInput.focus();
	}

	public reveal(element: debug.ITreeElement): Promise {
		return this.tree.reveal(element);
	}

I
isidor 已提交
162
	public getActions(): actions.IAction[] {
I
isidor 已提交
163 164
		if (!this.actions) {
			this.actions = [
I
isidor 已提交
165
				this.instantiationService.createInstance(debugactions.ClearReplAction, debugactions.ClearReplAction.ID, debugactions.ClearReplAction.LABEL)
I
isidor 已提交
166 167 168 169 170 171 172 173
			];

			this.actions.forEach(a => {
				this.toDispose.push(a);
			});
		}

		return this.actions;
I
isidor 已提交
174 175 176
	}

	public shutdown(): void {
E
Erich Gamma 已提交
177 178 179 180
		this.storageService.store(HISTORY_STORAGE_KEY, JSON.stringify(Repl.HISTORY.save()), StorageScope.WORKSPACE);
	}

	public dispose(): void {
I
isidor 已提交
181
		// destroy container
E
Erich Gamma 已提交
182 183 184 185 186
		this.toDispose = lifecycle.disposeAll(this.toDispose);

		super.dispose();
	}
}