Path: blob/main/src/vs/platform/editor/common/editor.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { equals } from '../../../base/common/arrays.js';6import { IDisposable } from '../../../base/common/lifecycle.js';7import { URI } from '../../../base/common/uri.js';8import { IUriIdentityService } from '../../uriIdentity/common/uriIdentity.js';9import { IRectangle } from '../../window/common/window.js';1011export interface IResolvableEditorModel extends IDisposable {1213/**14* Resolves the model.15*/16resolve(): Promise<void>;1718/**19* Find out if the editor model was resolved or not.20*/21isResolved(): boolean;22}2324export function isResolvedEditorModel(model: IDisposable | undefined | null): model is IResolvableEditorModel {25const candidate = model as IResolvableEditorModel | undefined | null;2627return typeof candidate?.resolve === 'function'28&& typeof candidate?.isResolved === 'function';29}3031export interface IBaseUntypedEditorInput {3233/**34* Optional options to use when opening the input.35*/36options?: IEditorOptions;3738/**39* Label to show for the input.40*/41readonly label?: string;4243/**44* Description to show for the input.45*/46readonly description?: string;47}4849export interface IBaseResourceEditorInput extends IBaseUntypedEditorInput {5051/**52* Hint to indicate that this input should be treated as a53* untitled file.54*55* Without this hint, the editor service will make a guess by56* looking at the scheme of the resource(s).57*58* Use `forceUntitled: true` when you pass in a `resource` that59* does not use the `untitled` scheme. The `resource` will then60* be used as associated path when saving the untitled file.61*/62readonly forceUntitled?: boolean;63}6465export interface IBaseTextResourceEditorInput extends IBaseResourceEditorInput {6667/**68* Optional options to use when opening the text input.69*/70options?: ITextEditorOptions;7172/**73* The contents of the text input if known. If provided,74* the input will not attempt to load the contents from75* disk and may appear dirty.76*/77contents?: string;7879/**80* The encoding of the text input if known.81*/82encoding?: string;8384/**85* The identifier of the language id of the text input86* if known to use when displaying the contents.87*/88languageId?: string;89}9091export interface IResourceEditorInput extends IBaseResourceEditorInput {9293/**94* The resource URI of the resource to open.95*/96readonly resource: URI;97}9899export interface ITextResourceEditorInput extends IResourceEditorInput, IBaseTextResourceEditorInput {100101/**102* Optional options to use when opening the text input.103*/104options?: ITextEditorOptions;105}106107/**108* This identifier allows to uniquely identify an editor with a109* resource, type and editor identifier.110*/111export interface IResourceEditorInputIdentifier {112113/**114* The type of the editor.115*/116readonly typeId: string;117118/**119* The identifier of the editor if provided.120*/121readonly editorId: string | undefined;122123/**124* The resource URI of the editor.125*/126readonly resource: URI;127}128129export enum EditorActivation {130131/**132* Activate the editor after it opened. This will automatically restore133* the editor if it is minimized.134*/135ACTIVATE = 1,136137/**138* Only restore the editor if it is minimized but do not activate it.139*140* Note: will only work in combination with the `preserveFocus: true` option.141* Otherwise, if focus moves into the editor, it will activate and restore142* automatically.143*/144RESTORE,145146/**147* Preserve the current active editor.148*149* Note: will only work in combination with the `preserveFocus: true` option.150* Otherwise, if focus moves into the editor, it will activate and restore151* automatically.152*/153PRESERVE154}155156export enum EditorResolution {157158/**159* Displays a picker and allows the user to decide which editor to use.160*/161PICK,162163/**164* Only exclusive editors are considered.165*/166EXCLUSIVE_ONLY167}168169export enum EditorOpenSource {170171/**172* Default: the editor is opening via a programmatic call173* to the editor service API.174*/175API,176177/**178* Indicates that a user action triggered the opening, e.g.179* via mouse or keyboard use.180*/181USER182}183184export interface IEditorOptions {185186/**187* Tells the editor to not receive keyboard focus when the editor is being opened.188*189* Will also not activate the group the editor opens in unless the group is already190* the active one. This behaviour can be overridden via the `activation` option.191*/192preserveFocus?: boolean;193194/**195* This option is only relevant if an editor is opened into a group that is not active196* already and allows to control if the inactive group should become active, restored197* or preserved.198*199* By default, the editor group will become active unless `preserveFocus` or `inactive`200* is specified.201*/202activation?: EditorActivation;203204/**205* Tells the editor to reload the editor input in the editor even if it is identical to the one206* already showing. By default, the editor will not reload the input if it is identical to the207* one showing.208*/209forceReload?: boolean;210211/**212* Will reveal the editor if it is already opened and visible in any of the opened editor groups.213*214* Note that this option is just a hint that might be ignored if the user wants to open an editor explicitly215* to the side of another one or into a specific editor group.216*/217revealIfVisible?: boolean;218219/**220* Will reveal the editor if it is already opened (even when not visible) in any of the opened editor groups.221*222* Note that this option is just a hint that might be ignored if the user wants to open an editor explicitly223* to the side of another one or into a specific editor group.224*/225revealIfOpened?: boolean;226227/**228* An editor that is pinned remains in the editor stack even when another editor is being opened.229* An editor that is not pinned will always get replaced by another editor that is not pinned.230*/231pinned?: boolean;232233/**234* An editor that is sticky moves to the beginning of the editors list within the group and will remain235* there unless explicitly closed. Operations such as "Close All" will not close sticky editors.236*/237sticky?: boolean;238239/**240* The index in the document stack where to insert the editor into when opening.241*/242index?: number;243244/**245* An active editor that is opened will show its contents directly. Set to true to open an editor246* in the background without loading its contents.247*248* Will also not activate the group the editor opens in unless the group is already249* the active one. This behaviour can be overridden via the `activation` option.250*/251inactive?: boolean;252253/**254* In case of an error opening the editor, will not present this error to the user (e.g. by showing255* a generic placeholder in the editor area). So it is up to the caller to provide error information256* in that case.257*258* By default, an error when opening an editor will result in a placeholder editor that shows the error.259* In certain cases a modal dialog may be presented to ask the user for further action.260*/261ignoreError?: boolean;262263/**264* Allows to override the editor that should be used to display the input:265* - `undefined`: let the editor decide for itself266* - `string`: specific override by id267* - `EditorResolution`: specific override handling268*/269override?: string | EditorResolution;270271/**272* A optional hint to signal in which context the editor opens.273*274* If configured to be `EditorOpenSource.USER`, this hint can be275* used in various places to control the experience. For example,276* if the editor to open fails with an error, a notification could277* inform about this in a modal dialog. If the editor opened through278* some background task, the notification would show in the background,279* not as a modal dialog.280*/281source?: EditorOpenSource;282283/**284* An optional property to signal that certain view state should be285* applied when opening the editor.286*/287viewState?: object;288289/**290* A transient editor will attempt to appear as preview and certain components291* (such as history tracking) may decide to ignore the editor when it becomes292* active.293* This option is meant to be used only when the editor is used for a short294* period of time, for example when opening a preview of the editor from a295* picker control in the background while navigating through results of the picker.296*297* Note: an editor that is already opened in a group that is not transient, will298* not turn transient.299*/300transient?: boolean;301302/**303* Options that only apply when `AUX_WINDOW_GROUP` is used for opening.304*/305auxiliary?: {306307/**308* Define the bounds of the editor window.309*/310bounds?: Partial<IRectangle>;311312/**313* Show editor compact, hiding unnecessary elements.314*/315compact?: boolean;316317/**318* Show the editor always on top of other windows.319*/320alwaysOnTop?: boolean;321};322}323324export interface ITextEditorSelection {325readonly startLineNumber: number;326readonly startColumn: number;327readonly endLineNumber?: number;328readonly endColumn?: number;329}330331export const enum TextEditorSelectionRevealType {332/**333* Option to scroll vertically or horizontally as necessary and reveal a range centered vertically.334*/335Center = 0,336337/**338* Option to scroll vertically or horizontally as necessary and reveal a range centered vertically only if it lies outside the viewport.339*/340CenterIfOutsideViewport = 1,341342/**343* Option to scroll vertically or horizontally as necessary and reveal a range close to the top of the viewport, but not quite at the top.344*/345NearTop = 2,346347/**348* Option to scroll vertically or horizontally as necessary and reveal a range close to the top of the viewport, but not quite at the top.349* Only if it lies outside the viewport350*/351NearTopIfOutsideViewport = 3,352}353354export const enum TextEditorSelectionSource {355356/**357* Programmatic source indicates a selection change that358* was not triggered by the user via keyboard or mouse359* but through text editor APIs.360*/361PROGRAMMATIC = 'api',362363/**364* Navigation source indicates a selection change that365* was caused via some command or UI component such as366* an outline tree.367*/368NAVIGATION = 'code.navigation',369370/**371* Jump source indicates a selection change that372* was caused from within the text editor to another373* location in the same or different text editor such374* as "Go to definition".375*/376JUMP = 'code.jump'377}378379export interface ITextEditorOptions extends IEditorOptions {380381/**382* Text editor selection.383*/384selection?: ITextEditorSelection;385386/**387* Option to control the text editor selection reveal type.388* Defaults to TextEditorSelectionRevealType.Center389*/390selectionRevealType?: TextEditorSelectionRevealType;391392/**393* Source of the call that caused the selection.394*/395selectionSource?: TextEditorSelectionSource | string;396}397398export type ITextEditorChange = [399originalStartLineNumber: number,400originalEndLineNumberExclusive: number,401modifiedStartLineNumber: number,402modifiedEndLineNumberExclusive: number403];404405export interface ITextEditorDiffInformation {406readonly documentVersion: number;407readonly original: URI | undefined;408readonly modified: URI;409readonly changes: readonly ITextEditorChange[];410}411412export function isTextEditorDiffInformationEqual(413uriIdentityService: IUriIdentityService,414diff1: ITextEditorDiffInformation | undefined,415diff2: ITextEditorDiffInformation | undefined): boolean {416return diff1?.documentVersion === diff2?.documentVersion &&417uriIdentityService.extUri.isEqual(diff1?.original, diff2?.original) &&418uriIdentityService.extUri.isEqual(diff1?.modified, diff2?.modified) &&419equals<ITextEditorChange>(diff1?.changes, diff2?.changes, (a, b) => {420return a[0] === b[0] && a[1] === b[1] && a[2] === b[2] && a[3] === b[3];421});422}423424425