editorBrowser.ts 14.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.
 *--------------------------------------------------------------------------------------------*/
'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;
E
Erich Gamma 已提交
47 48
}

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

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

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

60
	getCenteredRangeInViewport(): Range;
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[];
J
Johannes Rieken 已提交
68
	renderOnce(callback: () => any): any;
E
Erich Gamma 已提交
69

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

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

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

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

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

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

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

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

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

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

J
Johannes Rieken 已提交
126
	paste(source: string, text: string, pasteOnNewLine: boolean): void;
E
Erich Gamma 已提交
127
	type(source: string, text: string): void;
J
Johannes Rieken 已提交
128
	replacePreviousChar(source: string, text: string, replaceCharCnt: number): void;
129 130
	compositionStart(source: string): void;
	compositionEnd(source: string): void;
J
Johannes Rieken 已提交
131 132 133 134 135 136 137 138 139
	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 已提交
140 141
}

142 143 144
/**
 * @internal
 */
A
Alex Dima 已提交
145
export const ClassNames = {
E
Erich Gamma 已提交
146 147 148 149 150 151 152 153
	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',
154
	OVERFLOWING_CONTENT_WIDGETS: 'overflowingContentWidgets',
E
Erich Gamma 已提交
155
	OVERLAY_WIDGETS: 'overlayWidgets',
156
	MARGIN_VIEW_OVERLAYS: 'margin-view-overlays',
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;
E
Erich Gamma 已提交
214 215 216
	/**
	 * Callback which gives the relative top of the view zone as it appears (taking scrolling into account).
	 */
J
Johannes Rieken 已提交
217
	onDomNodeTop?: (top: number) => void;
E
Erich Gamma 已提交
218 219 220
	/**
	 * Callback which gives the height in pixels of the view zone.
	 */
J
Johannes Rieken 已提交
221
	onComputedHeight?: (height: number) => void;
E
Erich Gamma 已提交
222 223 224 225 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
}
/**
 * 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 已提交
270
	position: editorCommon.IPosition;
E
Erich Gamma 已提交
271 272 273 274 275 276 277 278 279 280 281 282 283
	/**
	 * 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;
284 285

	suppressMouseDown?: boolean;
E
Erich Gamma 已提交
286 287 288 289 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
	/**
	 * 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
	 */
355
	readonly element: Element;
E
Erich Gamma 已提交
356 357 358
	/**
	 * The target type
	 */
359
	readonly type: editorCommon.MouseTargetType;
E
Erich Gamma 已提交
360 361 362
	/**
	 * The 'approximate' editor position
	 */
363
	readonly position: Position;
364 365 366
	/**
	 * Desired mouse column (e.g. when position.column gets clamped to text length -- clicking after text on a line).
	 */
367
	readonly mouseColumn: number;
E
Erich Gamma 已提交
368 369 370
	/**
	 * The 'approximate' editor range
	 */
371
	readonly range: Range;
E
Erich Gamma 已提交
372 373 374
	/**
	 * Some extra detail.
	 */
375
	readonly detail: any;
E
Erich Gamma 已提交
376 377 378 379
}
/**
 * A mouse event originating from the editor.
 */
A
Alex Dima 已提交
380
export interface IEditorMouseEvent {
381 382
	readonly event: IMouseEvent;
	readonly target: IMouseTarget;
E
Erich Gamma 已提交
383 384
}

385 386 387
/**
 * @internal
 */
388
export type IEditorContributionCtor = IConstructorSignature1<ICodeEditor, editorCommon.IEditorContribution>;
A
Alex Dima 已提交
389

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

E
Erich Gamma 已提交
450 451 452 453 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
	/**
	 * 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;
487 488 489 490

	/**
	 * Returns the range that is currently centered in the view port.
	 */
491
	getCenteredRangeInViewport(): Range;
492 493 494

	/**
	 * Get the view zones.
495
	 * @internal
496
	 */
A
Alex Dima 已提交
497
	getWhitespaces(): editorCommon.IEditorWhitespace[];
498

499 500 501 502 503 504 505
	/**
	 * 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;

506 507 508 509 510
	/**
	 * Force an editor render now.
	 */
	render(): void;

511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527
	/**
	 * 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;

	/**
	 * 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 已提交
528
	getScrolledVisiblePosition(position: editorCommon.IPosition): { top: number; left: number; height: number; };
M
Martin Aeschlimann 已提交
529 530 531

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

536 537 538
	/**
	 * @internal
	 */
J
Johannes Rieken 已提交
539
	setAriaActiveDescendant(id: string): void;
540 541 542 543

	/**
	 * Apply the same font settings as the editor to `target`.
	 */
J
Johannes Rieken 已提交
544
	applyFontInfo(target: HTMLElement): void;
E
Erich Gamma 已提交
545 546 547 548 549
}

/**
 * A rich diff editor.
 */
A
Alex Dima 已提交
550
export interface IDiffEditor extends editorCommon.ICommonDiffEditor {
E
Erich Gamma 已提交
551 552 553 554 555
	/**
	 * @see ICodeEditor.getDomNode
	 */
	getDomNode(): HTMLElement;
}