editorBrowser.ts 15.0 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.
 *--------------------------------------------------------------------------------------------*/
'use strict';

J
Johannes Rieken 已提交
7 8 9 10 11
import { IEventEmitter } from 'vs/base/common/eventEmitter';
import { IDisposable } from 'vs/base/common/lifecycle';
import { IKeyboardEvent } from 'vs/base/browser/keyboardEvent';
import { IMouseEvent } from 'vs/base/browser/mouseEvent';
import { IConstructorSignature1 } from 'vs/platform/instantiation/common/instantiation';
A
Alex Dima 已提交
12
import * as editorCommon from 'vs/editor/common/editorCommon';
J
Johannes Rieken 已提交
13 14
import { Position } from 'vs/editor/common/core/position';
import { Range } from 'vs/editor/common/core/range';
E
Erich Gamma 已提交
15

16 17 18
/**
 * @internal
 */
E
Erich Gamma 已提交
19 20 21 22 23
export interface IContentWidgetData {
	widget: IContentWidget;
	position: IContentWidgetPosition;
}

24 25 26
/**
 * @internal
 */
E
Erich Gamma 已提交
27 28 29 30 31
export interface IOverlayWidgetData {
	widget: IOverlayWidget;
	position: IOverlayWidgetPosition;
}

32 33 34
/**
 * @internal
 */
E
Erich Gamma 已提交
35
export interface ICodeEditorHelper {
36
	getScrollWidth(): number;
E
Erich Gamma 已提交
37
	getScrollLeft(): number;
38

E
Erich Gamma 已提交
39
	getScrollHeight(): number;
40 41
	getScrollTop(): number;

J
Johannes Rieken 已提交
42
	setScrollPosition(position: editorCommon.INewScrollPosition): void;
43

J
Johannes Rieken 已提交
44 45 46
	getVerticalOffsetForPosition(lineNumber: number, column: number): number;
	delegateVerticalScrollbarMouseDown(browserEvent: MouseEvent): void;
	getOffsetForColumn(lineNumber: number, column: number): number;
47
	getTargetAtClientPoint(clientX: number, clientY: number): IMouseTarget;
E
Erich Gamma 已提交
48 49
}

50 51 52
/**
 * @internal
 */
A
Alex Dima 已提交
53
export interface IView extends IDisposable {
E
Erich Gamma 已提交
54 55
	domNode: HTMLElement;

A
Alex Dima 已提交
56
	getInternalEventBus(): IEventEmitter;
E
Erich Gamma 已提交
57

J
Johannes Rieken 已提交
58
	createOverviewRuler(cssClassName: string, minimumHeight: number, maximumHeight: number): IOverviewRuler;
E
Erich Gamma 已提交
59 60
	getCodeEditorHelper(): ICodeEditorHelper;

61 62 63 64
	/**
	 * Returns the range of lines in the view port which are completely visible.
	 */
	getCompletelyVisibleLinesRangeInViewport(): Range;
E
Erich Gamma 已提交
65

J
Johannes Rieken 已提交
66
	change(callback: (changeAccessor: IViewZoneChangeAccessor) => any): boolean;
A
Alex Dima 已提交
67
	getWhitespaces(): editorCommon.IEditorWhitespace[];
E
Erich Gamma 已提交
68

J
Johannes Rieken 已提交
69 70
	render(now: boolean, everything: boolean): void;
	setAriaActiveDescendant(id: string): void;
E
Erich Gamma 已提交
71 72 73 74

	focus(): void;
	isFocused(): boolean;

A
Alex Dima 已提交
75
	saveState(): editorCommon.IViewState;
J
Johannes Rieken 已提交
76
	restoreState(state: editorCommon.IViewState): void;
E
Erich Gamma 已提交
77 78 79 80 81 82 83 84 85 86

	addContentWidget(widgetData: IContentWidgetData): void;
	layoutContentWidget(widgetData: IContentWidgetData): void;
	removeContentWidget(widgetData: IContentWidgetData): void;

	addOverlayWidget(widgetData: IOverlayWidgetData): void;
	layoutOverlayWidget(widgetData: IOverlayWidgetData): void;
	removeOverlayWidget(widgetData: IOverlayWidgetData): void;
}

87 88 89
/**
 * @internal
 */
E
Erich Gamma 已提交
90 91
export interface IViewZoneData {
	viewZoneId: number;
J
Johannes Rieken 已提交
92 93
	positionBefore: Position;
	positionAfter: Position;
A
Alex Dima 已提交
94
	position: Position;
E
Erich Gamma 已提交
95 96 97
	afterLineNumber: number;
}

98 99 100
/**
 * @internal
 */
101
export interface IMouseDispatchData {
A
Alex Dima 已提交
102
	position: Position;
103 104 105 106
	/**
	 * Desired mouse column (e.g. when position.column gets clamped to text length -- clicking after text on a line).
	 */
	mouseColumn: number;
107
	startedOnLineNumbers: boolean;
108 109 110

	inSelectionMode: boolean;
	mouseDownCount: number;
A
Alex Dima 已提交
111 112 113 114
	altKey: boolean;
	ctrlKey: boolean;
	metaKey: boolean;
	shiftKey: boolean;
115 116
}

117 118 119
/**
 * @internal
 */
E
Erich Gamma 已提交
120
export interface IViewController {
J
Johannes Rieken 已提交
121
	dispatchMouse(data: IMouseDispatchData);
122

J
Johannes Rieken 已提交
123
	moveTo(source: string, position: Position): void;
124

J
Johannes Rieken 已提交
125
	paste(source: string, text: string, pasteOnNewLine: boolean): void;
E
Erich Gamma 已提交
126
	type(source: string, text: string): void;
J
Johannes Rieken 已提交
127
	replacePreviousChar(source: string, text: string, replaceCharCnt: number): void;
128 129
	compositionStart(source: string): void;
	compositionEnd(source: string): void;
J
Johannes Rieken 已提交
130 131 132 133 134 135 136 137 138
	cut(source: string): void;

	emitKeyDown(e: IKeyboardEvent): void;
	emitKeyUp(e: IKeyboardEvent): void;
	emitContextMenu(e: IEditorMouseEvent): void;
	emitMouseMove(e: IEditorMouseEvent): void;
	emitMouseLeave(e: IEditorMouseEvent): void;
	emitMouseUp(e: IEditorMouseEvent): void;
	emitMouseDown(e: IEditorMouseEvent): void;
E
Erich Gamma 已提交
139 140
}

141 142 143
/**
 * @internal
 */
A
Alex Dima 已提交
144
export const ClassNames = {
E
Erich Gamma 已提交
145 146 147 148 149 150 151 152
	TEXTAREA_COVER: 'textAreaCover',
	TEXTAREA: 'inputarea',
	LINES_CONTENT: 'lines-content',
	OVERFLOW_GUARD: 'overflow-guard',
	VIEW_LINES: 'view-lines',
	VIEW_LINE: 'view-line',
	SCROLLABLE_ELEMENT: 'editor-scrollable',
	CONTENT_WIDGETS: 'contentWidgets',
153
	OVERFLOWING_CONTENT_WIDGETS: 'overflowingContentWidgets',
E
Erich Gamma 已提交
154
	OVERLAY_WIDGETS: 'overlayWidgets',
155
	MARGIN_VIEW_OVERLAYS: 'margin-view-overlays',
156
	MARGIN: 'margin',
E
Erich Gamma 已提交
157 158 159 160 161 162 163
	LINE_NUMBERS: 'line-numbers',
	GLYPH_MARGIN: 'glyph-margin',
	SCROLL_DECORATION: 'scroll-decoration',
	VIEW_CURSORS_LAYER: 'cursors-layer',
	VIEW_ZONES: 'view-zones'
};

164 165 166
/**
 * @internal
 */
E
Erich Gamma 已提交
167
export interface IViewportInfo {
168
	visibleRange: Range;
J
Johannes Rieken 已提交
169 170 171 172
	width: number;
	height: number;
	deltaTop: number;
	deltaLeft: number;
E
Erich Gamma 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185
}

// --- end View Event Handlers & Parts

/**
 * A view zone is a full horizontal rectangle that 'pushes' text down.
 * The editor reserves space for view zones when rendering.
 */
export interface IViewZone {
	/**
	 * The line number after which this zone should appear.
	 * Use 0 to place a view zone before the first line number.
	 */
J
Johannes Rieken 已提交
186
	afterLineNumber: number;
E
Erich Gamma 已提交
187 188 189 190
	/**
	 * The column after which this zone should appear.
	 * If not set, the maxLineColumn of `afterLineNumber` will be used.
	 */
J
Johannes Rieken 已提交
191
	afterColumn?: number;
E
Erich Gamma 已提交
192 193 194 195 196
	/**
	 * Suppress mouse down events.
	 * If set, the editor will attach a mouse down listener to the view zone and .preventDefault on it.
	 * Defaults to false
	 */
J
Johannes Rieken 已提交
197
	suppressMouseDown?: boolean;
E
Erich Gamma 已提交
198 199 200 201 202
	/**
	 * The height in lines of the view zone.
	 * If specified, `heightInPx` will be used instead of this.
	 * If neither `heightInPx` nor `heightInLines` is specified, a default of `heightInLines` = 1 will be chosen.
	 */
J
Johannes Rieken 已提交
203
	heightInLines?: number;
E
Erich Gamma 已提交
204 205 206 207 208 209 210 211 212
	/**
	 * The height in px of the view zone.
	 * If this is set, the editor will give preference to it rather than `heightInLines` above.
	 * If neither `heightInPx` nor `heightInLines` is specified, a default of `heightInLines` = 1 will be chosen.
	 */
	heightInPx?: number;
	/**
	 * The dom node of the view zone
	 */
J
Johannes Rieken 已提交
213
	domNode: HTMLElement;
214 215 216 217
	/**
	 * An optional dom node for the view zone that will be placed in the margin area.
	 */
	marginDomNode?: HTMLElement;
E
Erich Gamma 已提交
218 219 220
	/**
	 * Callback which gives the relative top of the view zone as it appears (taking scrolling into account).
	 */
J
Johannes Rieken 已提交
221
	onDomNodeTop?: (top: number) => void;
E
Erich Gamma 已提交
222 223 224
	/**
	 * Callback which gives the height in pixels of the view zone.
	 */
J
Johannes Rieken 已提交
225
	onComputedHeight?: (height: number) => void;
E
Erich Gamma 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
}
/**
 * An accessor that allows for zones to be added or removed.
 */
export interface IViewZoneChangeAccessor {
	/**
	 * Create a new view zone.
	 * @param zone Zone to create
	 * @return A unique identifier to the view zone.
	 */
	addZone(zone: IViewZone): number;
	/**
	 * Remove a zone
	 * @param id A unique identifier to the view zone, as returned by the `addZone` call.
	 */
	removeZone(id: number): void;
	/**
	 * Change a zone's position.
	 * The editor will rescan the `afterLineNumber` and `afterColumn` properties of a view zone.
	 */
	layoutZone(id: number): void;
}

/**
 * A positioning preference for rendering content widgets.
 */
export enum ContentWidgetPositionPreference {
	/**
	 * Place the content widget exactly at a position
	 */
	EXACT,
	/**
	 * Place the content widget above a position
	 */
	ABOVE,
	/**
	 * Place the content widget below a position
	 */
	BELOW
}
/**
 * A position for rendering content widgets.
 */
export interface IContentWidgetPosition {
	/**
	 * Desired position for the content widget.
	 * `preference` will also affect the placement.
	 */
A
Alex Dima 已提交
274
	position: editorCommon.IPosition;
E
Erich Gamma 已提交
275 276 277 278 279 280 281 282 283 284 285 286 287
	/**
	 * Placement preference for position, in order of preference.
	 */
	preference: ContentWidgetPositionPreference[];
}
/**
 * A content widget renders inline with the text and can be easily placed 'near' an editor position.
 */
export interface IContentWidget {
	/**
	 * Render this content widget in a location where it could overflow the editor's view dom node.
	 */
	allowEditorOverflow?: boolean;
288 289

	suppressMouseDown?: boolean;
E
Erich Gamma 已提交
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
	/**
	 * Get a unique identifier of the content widget.
	 */
	getId(): string;
	/**
	 * Get the dom node of the content widget.
	 */
	getDomNode(): HTMLElement;
	/**
	 * Get the placement of the content widget.
	 * If null is returned, the content widget will be placed off screen.
	 */
	getPosition(): IContentWidgetPosition;
}

/**
 * A positioning preference for rendering overlay widgets.
 */
export enum OverlayWidgetPositionPreference {
	/**
	 * Position the overlay widget in the top right corner
	 */
	TOP_RIGHT_CORNER,

	/**
	 * Position the overlay widget in the bottom right corner
	 */
	BOTTOM_RIGHT_CORNER,

	/**
	 * Position the overlay widget in the top center
	 */
	TOP_CENTER
}
/**
 * A position for rendering overlay widgets.
 */
export interface IOverlayWidgetPosition {
	/**
	 * The position preference for the overlay widget.
	 */
	preference: OverlayWidgetPositionPreference;
}
/**
 * An overlay widgets renders on top of the text.
 */
export interface IOverlayWidget {
	/**
	 * Get a unique identifier of the overlay widget.
	 */
	getId(): string;
	/**
	 * Get the dom node of the overlay widget.
	 */
	getDomNode(): HTMLElement;
	/**
	 * Get the placement of the overlay widget.
	 * If null is returned, the overlay widget is responsible to place itself.
	 */
	getPosition(): IOverlayWidgetPosition;
}

/**
 * Target hit with the mouse in the editor.
 */
export interface IMouseTarget {
	/**
	 * The target element
	 */
359
	readonly element: Element;
E
Erich Gamma 已提交
360 361 362
	/**
	 * The target type
	 */
363
	readonly type: editorCommon.MouseTargetType;
E
Erich Gamma 已提交
364 365 366
	/**
	 * The 'approximate' editor position
	 */
367
	readonly position: Position;
368 369 370
	/**
	 * Desired mouse column (e.g. when position.column gets clamped to text length -- clicking after text on a line).
	 */
371
	readonly mouseColumn: number;
E
Erich Gamma 已提交
372 373 374
	/**
	 * The 'approximate' editor range
	 */
375
	readonly range: Range;
E
Erich Gamma 已提交
376 377 378
	/**
	 * Some extra detail.
	 */
379
	readonly detail: any;
E
Erich Gamma 已提交
380 381 382 383
}
/**
 * A mouse event originating from the editor.
 */
A
Alex Dima 已提交
384
export interface IEditorMouseEvent {
385 386
	readonly event: IMouseEvent;
	readonly target: IMouseTarget;
E
Erich Gamma 已提交
387 388
}

389 390 391
/**
 * @internal
 */
392
export type IEditorContributionCtor = IConstructorSignature1<ICodeEditor, editorCommon.IEditorContribution>;
A
Alex Dima 已提交
393

E
Erich Gamma 已提交
394 395
/**
 * An overview ruler
396
 * @internal
E
Erich Gamma 已提交
397 398 399 400
 */
export interface IOverviewRuler {
	getDomNode(): HTMLElement;
	dispose(): void;
J
Johannes Rieken 已提交
401 402
	setZones(zones: editorCommon.OverviewRulerZone[]): void;
	setLayout(position: editorCommon.OverviewRulerPosition): void;
E
Erich Gamma 已提交
403 404 405 406
}
/**
 * A rich code editor.
 */
A
Alex Dima 已提交
407
export interface ICodeEditor extends editorCommon.ICommonCodeEditor {
A
Alex Dima 已提交
408 409
	/**
	 * An event emitted on a "mouseup".
A
Alex Dima 已提交
410
	 * @event
A
Alex Dima 已提交
411
	 */
J
Johannes Rieken 已提交
412
	onMouseUp(listener: (e: IEditorMouseEvent) => void): IDisposable;
A
Alex Dima 已提交
413 414
	/**
	 * An event emitted on a "mousedown".
A
Alex Dima 已提交
415
	 * @event
A
Alex Dima 已提交
416
	 */
J
Johannes Rieken 已提交
417
	onMouseDown(listener: (e: IEditorMouseEvent) => void): IDisposable;
A
Alex Dima 已提交
418 419
	/**
	 * An event emitted on a "contextmenu".
A
Alex Dima 已提交
420
	 * @event
A
Alex Dima 已提交
421
	 */
J
Johannes Rieken 已提交
422
	onContextMenu(listener: (e: IEditorMouseEvent) => void): IDisposable;
A
Alex Dima 已提交
423 424
	/**
	 * An event emitted on a "mousemove".
A
Alex Dima 已提交
425
	 * @event
A
Alex Dima 已提交
426
	 */
J
Johannes Rieken 已提交
427
	onMouseMove(listener: (e: IEditorMouseEvent) => void): IDisposable;
A
Alex Dima 已提交
428 429
	/**
	 * An event emitted on a "mouseleave".
A
Alex Dima 已提交
430
	 * @event
A
Alex Dima 已提交
431
	 */
J
Johannes Rieken 已提交
432
	onMouseLeave(listener: (e: IEditorMouseEvent) => void): IDisposable;
A
Alex Dima 已提交
433 434
	/**
	 * An event emitted on a "keyup".
A
Alex Dima 已提交
435
	 * @event
A
Alex Dima 已提交
436
	 */
J
Johannes Rieken 已提交
437
	onKeyUp(listener: (e: IKeyboardEvent) => void): IDisposable;
A
Alex Dima 已提交
438 439
	/**
	 * An event emitted on a "keydown".
A
Alex Dima 已提交
440
	 * @event
A
Alex Dima 已提交
441
	 */
J
Johannes Rieken 已提交
442
	onKeyDown(listener: (e: IKeyboardEvent) => void): IDisposable;
A
Alex Dima 已提交
443 444
	/**
	 * An event emitted when the layout of the editor has changed.
A
Alex Dima 已提交
445
	 * @event
A
Alex Dima 已提交
446
	 */
J
Johannes Rieken 已提交
447
	onDidLayoutChange(listener: (e: editorCommon.EditorLayoutInfo) => void): IDisposable;
A
Alex Dima 已提交
448 449
	/**
	 * An event emitted when the scroll in the editor has changed.
A
Alex Dima 已提交
450
	 * @event
A
Alex Dima 已提交
451
	 */
J
Johannes Rieken 已提交
452
	onDidScrollChange(listener: (e: editorCommon.IScrollEvent) => void): IDisposable;
A
Alex Dima 已提交
453

E
Erich Gamma 已提交
454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490
	/**
	 * Returns the editor's dom node
	 */
	getDomNode(): HTMLElement;

	/**
	 * Add a content widget. Widgets must have unique ids, otherwise they will be overwritten.
	 */
	addContentWidget(widget: IContentWidget): void;
	/**
	 * Layout/Reposition a content widget. This is a ping to the editor to call widget.getPosition()
	 * and update appropiately.
	 */
	layoutContentWidget(widget: IContentWidget): void;
	/**
	 * Remove a content widget.
	 */
	removeContentWidget(widget: IContentWidget): void;

	/**
	 * Add an overlay widget. Widgets must have unique ids, otherwise they will be overwritten.
	 */
	addOverlayWidget(widget: IOverlayWidget): void;
	/**
	 * Layout/Reposition an overlay widget. This is a ping to the editor to call widget.getPosition()
	 * and update appropiately.
	 */
	layoutOverlayWidget(widget: IOverlayWidget): void;
	/**
	 * Remove an overlay widget.
	 */
	removeOverlayWidget(widget: IOverlayWidget): void;

	/**
	 * Change the view zones. View zones are lost when a new model is attached to the editor.
	 */
	changeViewZones(callback: (accessor: IViewZoneChangeAccessor) => void): void;
491 492 493 494

	/**
	 * Returns the range that is currently centered in the view port.
	 */
495
	getCenteredRangeInViewport(): Range;
496 497 498

	/**
	 * Get the view zones.
499
	 * @internal
500
	 */
A
Alex Dima 已提交
501
	getWhitespaces(): editorCommon.IEditorWhitespace[];
502

503 504 505 506 507 508 509
	/**
	 * Get the horizontal position (left offset) for the column w.r.t to the beginning of the line.
	 * This method works only if the line `lineNumber` is currently rendered (in the editor's viewport).
	 * Use this method with caution.
	 */
	getOffsetForColumn(lineNumber: number, column: number): number;

510 511 512 513 514
	/**
	 * Force an editor render now.
	 */
	render(): void;

515 516 517 518 519 520 521 522 523 524
	/**
	 * Get the vertical position (top offset) for the line w.r.t. to the first line.
	 */
	getTopForLineNumber(lineNumber: number): number;

	/**
	 * Get the vertical position (top offset) for the position w.r.t. to the first line.
	 */
	getTopForPosition(lineNumber: number, column: number): number;

525 526 527 528 529 530 531 532
	/**
	 * Get the hit test target at coordinates `clientX` and `clientY`.
	 * The coordinates are relative to the top-left of the viewport.
	 *
	 * @returns Hit test target or null if the coordinates fall outside the editor or the editor has no model.
	 */
	getTargetAtClientPoint(clientX: number, clientY: number): IMouseTarget;

533 534 535 536 537 538 539
	/**
	 * Get the visible position for `position`.
	 * The result position takes scrolling into account and is relative to the top left corner of the editor.
	 * Explanation 1: the results of this method will change for the same `position` if the user scrolls the editor.
	 * Explanation 2: the results of this method will not change if the container of the editor gets repositioned.
	 * Warning: the results of this method are innacurate for positions that are outside the current editor viewport.
	 */
A
Alex Dima 已提交
540
	getScrolledVisiblePosition(position: editorCommon.IPosition): { top: number; left: number; height: number; };
M
Martin Aeschlimann 已提交
541 542 543

	/**
	 * Set the model ranges that will be hidden in the view.
544
	 * @internal
M
Martin Aeschlimann 已提交
545
	 */
J
Johannes Rieken 已提交
546
	setHiddenAreas(ranges: editorCommon.IRange[]): void;
A
Alex Dima 已提交
547

548 549 550
	/**
	 * @internal
	 */
J
Johannes Rieken 已提交
551
	setAriaActiveDescendant(id: string): void;
552 553 554 555

	/**
	 * Apply the same font settings as the editor to `target`.
	 */
J
Johannes Rieken 已提交
556
	applyFontInfo(target: HTMLElement): void;
E
Erich Gamma 已提交
557 558 559 560 561
}

/**
 * A rich diff editor.
 */
A
Alex Dima 已提交
562
export interface IDiffEditor extends editorCommon.ICommonDiffEditor {
E
Erich Gamma 已提交
563 564 565 566 567
	/**
	 * @see ICodeEditor.getDomNode
	 */
	getDomNode(): HTMLElement;
}