repl.ts 7.4 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  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';
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 已提交
10
import actions = require('vs/base/common/actions');
E
Erich Gamma 已提交
11 12 13
import builder = require('vs/base/browser/builder');
import dom = require('vs/base/browser/dom');
import platform = require('vs/base/common/platform');
J
Joao Moreno 已提交
14
import tree = require('vs/base/parts/tree/browser/tree');
E
Erich Gamma 已提交
15 16 17
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');
18
import debugactions = require('vs/workbench/parts/debug/electron-browser/debugActions');
I
isidor 已提交
19
import replhistory = require('vs/workbench/parts/debug/common/replHistory');
I
isidor 已提交
20
import { Panel, ClosePanelAction } from 'vs/workbench/browser/panel';
E
Erich Gamma 已提交
21 22 23 24 25 26 27
import { ITelemetryService } from 'vs/platform/telemetry/common/telemetry';
import { IContextViewService, IContextMenuService } from 'vs/platform/contextview/browser/contextView';
import { IInstantiationService, INullService } from 'vs/platform/instantiation/common/instantiation';
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 已提交
28
const $ = dom.emmet;
E
Erich Gamma 已提交
29

I
isidor 已提交
30
const replTreeOptions = {
E
Erich Gamma 已提交
31 32 33 34 35
	indentPixels: 8,
	twistiePixels: 20,
	paddingOnRow: false
};

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

I
isidor 已提交
38
export class Repl extends Panel {
E
Erich Gamma 已提交
39

I
isidor 已提交
40
	public static ID = 'workbench.panel.repl';
41
	private static HALF_WIDTH_TYPICAL = 'n';
E
Erich Gamma 已提交
42 43 44 45 46 47

	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 已提交
48
	private renderer: viewer.ReplExpressionsRenderer;
49
	private characterWidthSurveyor: HTMLElement;
E
Erich Gamma 已提交
50 51 52
	private treeContainer: HTMLElement;
	private replInput: HTMLInputElement;
	private refreshTimeoutHandle: number;
I
isidor 已提交
53
	private actions: actions.IAction[];
E
Erich Gamma 已提交
54 55 56 57 58 59 60 61

	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 已提交
62
		@IStorageService private storageService: IStorageService
E
Erich Gamma 已提交
63 64 65 66
	) {
		super(Repl.ID, telemetryService);

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

I
isidor 已提交
70
	private registerListeners(): void {
E
Erich Gamma 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
		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 已提交
95 96
	public create(parent: builder.Builder): TPromise<void> {
		super.create(parent);
I
isidor 已提交
97 98
		const container = dom.append(parent.getHTMLElement(), $('.repl'));
		// inherit the background color from selected theme.
E
Erich Gamma 已提交
99 100
		dom.addClass(container, 'monaco-editor-background');
		this.treeContainer = dom.append(container, $('.repl-tree'));
I
isidor 已提交
101
		const replInputContainer = dom.append(container, $(platform.isWindows ? '.repl-input-wrapper.windows' : platform.isMacintosh ? '.repl-input-wrapper.mac' : '.repl-input-wrapper.linux'));
E
Erich Gamma 已提交
102 103 104 105 106 107 108 109 110 111
		this.replInput = <HTMLInputElement>dom.append(replInputContainer, $('input.repl-input'));

		dom.addStandardDisposableListener(this.replInput, 'keydown', (e: dom.IKeyboardEvent) => {
			let trimmedValue = this.replInput.value.trim();

			if (e.equals(CommonKeybindings.ENTER) && trimmedValue) {
				this.debugService.addReplExpression(trimmedValue);
				Repl.HISTORY.evaluated(trimmedValue);
				this.replInput.value = '';
			} else if (e.equals(CommonKeybindings.UP_ARROW) || e.equals(CommonKeybindings.DOWN_ARROW)) {
I
isidor 已提交
112
				const historyInput = e.equals(CommonKeybindings.UP_ARROW) ? Repl.HISTORY.previous() : Repl.HISTORY.next();
E
Erich Gamma 已提交
113 114 115
				if (historyInput) {
					Repl.HISTORY.remember(this.replInput.value, e.equals(CommonKeybindings.UP_ARROW));
					this.replInput.value = historyInput;
I
isidor 已提交
116
					// always leave cursor at the end.
E
Erich Gamma 已提交
117 118 119 120 121
					e.preventDefault();
				}
			}
		});

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

I
isidor 已提交
129
		this.renderer = this.instantiationService.createInstance(viewer.ReplExpressionsRenderer);
E
Erich Gamma 已提交
130 131
		this.tree = new treeimpl.Tree(this.treeContainer, {
			dataSource: new viewer.ReplExpressionsDataSource(this.debugService),
I
isidor 已提交
132
			renderer: this.renderer,
E
Erich Gamma 已提交
133 134 135 136 137 138 139
			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 已提交
140
		return this.tree.setInput(this.debugService.getModel());
E
Erich Gamma 已提交
141 142 143 144
	}

	public layout(dimension: builder.Dimension): void {
		if (this.tree) {
145
			this.renderer.setWidth(dimension.width - 20, this.characterWidthSurveyor.clientWidth / this.characterWidthSurveyor.textContent.length);
E
Erich Gamma 已提交
146
			this.tree.layout(this.treeContainer.clientHeight);
I
isidor 已提交
147
			this.tree.refresh().done(null, errors.onUnexpectedError);
E
Erich Gamma 已提交
148 149 150 151 152 153 154 155 156 157 158
		}
	}

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

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

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

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

		return this.actions;
I
isidor 已提交
172 173 174
	}

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

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

		super.dispose();
	}
}