editorBrowser.ts 14.3 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';

A
Alex Dima 已提交
7
import {IEventEmitter} from 'vs/base/common/eventEmitter';
A
Alex Dima 已提交
8
import {IDisposable} from 'vs/base/common/lifecycle';
A
Cleanup  
Alex Dima 已提交
9
import {IKeyboardEvent} from 'vs/base/browser/keyboardEvent';
A
Alex Dima 已提交
10
import {IMouseEvent} from 'vs/base/browser/mouseEvent';
11
import {IConstructorSignature1} from 'vs/platform/instantiation/common/instantiation';
A
Alex Dima 已提交
12
import * as editorCommon from 'vs/editor/common/editorCommon';
A
Alex Dima 已提交
13
import {Position} from 'vs/editor/common/core/position';
14
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 42 43
	getScrollTop(): number;

	setScrollPosition(position:editorCommon.INewScrollPosition): void;

E
Erich Gamma 已提交
44 45 46 47 48
	getVerticalOffsetForPosition(lineNumber:number, column:number): number;
	delegateVerticalScrollbarMouseDown(browserEvent:MouseEvent): void;
	getOffsetForColumn(lineNumber:number, column:number): number;
}

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 57 58 59

	createOverviewRuler(cssClassName:string, minimumHeight:number, maximumHeight:number): IOverviewRuler;
	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 66

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

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

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

A
Alex Dima 已提交
76 77
	saveState(): editorCommon.IViewState;
	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;
A
Alex Dima 已提交
93 94 95
	positionBefore:Position;
	positionAfter:Position;
	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 {
122 123
	dispatchMouse(data:IMouseDispatchData);

A
Alex Dima 已提交
124
	moveTo(source:string, position:Position): void;
125

E
Erich Gamma 已提交
126 127
	paste(source:string, text:string, pasteOnNewLine:boolean): void;
	type(source: string, text: string): void;
128
	replacePreviousChar(source: string, text: string, replaceCharCnt:number): void;
129 130
	compositionStart(source: string): void;
	compositionEnd(source: string): void;
E
Erich Gamma 已提交
131 132
	cut(source:string): void;

A
Cleanup  
Alex Dima 已提交
133 134
	emitKeyDown(e:IKeyboardEvent): void;
	emitKeyUp(e:IKeyboardEvent): void;
A
Alex Dima 已提交
135 136 137 138 139
	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
 */
E
Erich Gamma 已提交
145 146 147 148 149 150 151 152 153
export var ClassNames = {
	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;
E
Erich Gamma 已提交
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 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
	width:number;
	height:number;
	deltaTop:number;
	deltaLeft:number;
}

// --- 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.
	 */
	afterLineNumber:number;
	/**
	 * The column after which this zone should appear.
	 * If not set, the maxLineColumn of `afterLineNumber` will be used.
	 */
	afterColumn?:number;
	/**
	 * 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
	 */
	suppressMouseDown?:boolean;
	/**
	 * 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.
	 */
	heightInLines?:number;
	/**
	 * 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
	 */
	domNode:HTMLElement;
	/**
	 * Callback which gives the relative top of the view zone as it appears (taking scrolling into account).
	 */
	onDomNodeTop?:(top: number) =>void;
	/**
	 * Callback which gives the height in pixels of the view zone.
	 */
	onComputedHeight?:(height: number) =>void;
}
/**
 * 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 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
	 */
	element: Element;
	/**
	 * The target type
	 */
A
Alex Dima 已提交
359
	type: editorCommon.MouseTargetType;
E
Erich Gamma 已提交
360 361 362
	/**
	 * The 'approximate' editor position
	 */
A
Alex Dima 已提交
363
	position: Position;
364 365 366 367
	/**
	 * Desired mouse column (e.g. when position.column gets clamped to text length -- clicking after text on a line).
	 */
	mouseColumn: number;
E
Erich Gamma 已提交
368 369 370
	/**
	 * The 'approximate' editor range
	 */
371
	range: Range;
E
Erich Gamma 已提交
372 373 374 375 376 377 378 379
	/**
	 * Some extra detail.
	 */
	detail: any;
}
/**
 * A mouse event originating from the editor.
 */
A
Alex Dima 已提交
380 381
export interface IEditorMouseEvent {
	event: IMouseEvent;
E
Erich Gamma 已提交
382 383 384
	target: IMouseTarget;
}

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;
397
	setZones(zones:editorCommon.OverviewRulerZone[]): void;
398
	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 406
	/**
	 * An event emitted on a "mouseup".
	 */
A
Alex Dima 已提交
407
	onMouseUp(listener: (e:IEditorMouseEvent)=>void): IDisposable;
A
Alex Dima 已提交
408 409 410
	/**
	 * An event emitted on a "mousedown".
	 */
A
Alex Dima 已提交
411
	onMouseDown(listener: (e:IEditorMouseEvent)=>void): IDisposable;
A
Alex Dima 已提交
412 413 414
	/**
	 * An event emitted on a "contextmenu".
	 */
A
Alex Dima 已提交
415
	onContextMenu(listener: (e:IEditorMouseEvent)=>void): IDisposable;
A
Alex Dima 已提交
416 417 418
	/**
	 * An event emitted on a "mousemove".
	 */
A
Alex Dima 已提交
419
	onMouseMove(listener: (e:IEditorMouseEvent)=>void): IDisposable;
A
Alex Dima 已提交
420 421 422
	/**
	 * An event emitted on a "mouseleave".
	 */
A
Alex Dima 已提交
423
	onMouseLeave(listener: (e:IEditorMouseEvent)=>void): IDisposable;
A
Alex Dima 已提交
424 425 426
	/**
	 * An event emitted on a "keyup".
	 */
A
Alex Dima 已提交
427
	onKeyUp(listener: (e:IKeyboardEvent)=>void): IDisposable;
A
Alex Dima 已提交
428 429 430
	/**
	 * An event emitted on a "keydown".
	 */
A
Alex Dima 已提交
431
	onKeyDown(listener: (e:IKeyboardEvent)=>void): IDisposable;
A
Alex Dima 已提交
432 433 434
	/**
	 * An event emitted when the layout of the editor has changed.
	 */
A
Alex Dima 已提交
435
	onDidLayoutChange(listener: (e:editorCommon.EditorLayoutInfo)=>void): IDisposable;
A
Alex Dima 已提交
436 437 438
	/**
	 * An event emitted when the scroll in the editor has changed.
	 */
A
Alex Dima 已提交
439 440
	onDidScrollChange(listener: (e:editorCommon.IScrollEvent)=>void): IDisposable;

E
Erich Gamma 已提交
441 442 443 444 445 446 447 448 449 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
	/**
	 * 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;
478 479 480 481

	/**
	 * Returns the range that is currently centered in the view port.
	 */
482
	getCenteredRangeInViewport(): Range;
483 484 485

	/**
	 * Get the view zones.
486
	 * @internal
487
	 */
A
Alex Dima 已提交
488
	getWhitespaces(): editorCommon.IEditorWhitespace[];
489

490 491 492 493 494 495 496
	/**
	 * 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;

497 498 499 500 501
	/**
	 * Force an editor render now.
	 */
	render(): void;

502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
	/**
	 * 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 已提交
519
	getScrolledVisiblePosition(position: editorCommon.IPosition): { top: number; left: number; height: number; };
M
Martin Aeschlimann 已提交
520 521 522

	/**
	 * Set the model ranges that will be hidden in the view.
523
	 * @internal
M
Martin Aeschlimann 已提交
524
	 */
A
Alex Dima 已提交
525
	setHiddenAreas(ranges:editorCommon.IRange[]): void;
A
Alex Dima 已提交
526

527 528 529
	/**
	 * @internal
	 */
A
Alex Dima 已提交
530
	setAriaActiveDescendant(id:string): void;
531 532 533 534 535

	/**
	 * Apply the same font settings as the editor to `target`.
	 */
	applyFontInfo(target:HTMLElement): void;
E
Erich Gamma 已提交
536 537 538 539 540
}

/**
 * A rich diff editor.
 */
A
Alex Dima 已提交
541
export interface IDiffEditor extends editorCommon.ICommonDiffEditor {
E
Erich Gamma 已提交
542 543 544 545 546
	/**
	 * @see ICodeEditor.getDomNode
	 */
	getDomNode(): HTMLElement;
}