"Fossies" - the Fresh Open Source Software Archive

Member "vscode-1.49.1/src/vs/editor/browser/services/abstractCodeEditorService.ts" (16 Sep 2020, 6328 Bytes) of package /linux/misc/vscode-1.49.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) TypeScript source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. See also the last Fossies "Diffs" side-by-side code changes report for "abstractCodeEditorService.ts": 1.46.1_vs_1.47.0.

    1 /*---------------------------------------------------------------------------------------------
    2  *  Copyright (c) Microsoft Corporation. All rights reserved.
    3  *  Licensed under the MIT License. See License.txt in the project root for license information.
    4  *--------------------------------------------------------------------------------------------*/
    5 
    6 import { Emitter, Event } from 'vs/base/common/event';
    7 import { Disposable } from 'vs/base/common/lifecycle';
    8 import { ICodeEditor, IDiffEditor } from 'vs/editor/browser/editorBrowser';
    9 import { ICodeEditorService } from 'vs/editor/browser/services/codeEditorService';
   10 import { IDecorationRenderOptions } from 'vs/editor/common/editorCommon';
   11 import { IModelDecorationOptions, ITextModel } from 'vs/editor/common/model';
   12 import { IResourceEditorInput } from 'vs/platform/editor/common/editor';
   13 import { URI } from 'vs/base/common/uri';
   14 
   15 export abstract class AbstractCodeEditorService extends Disposable implements ICodeEditorService {
   16 
   17     declare readonly _serviceBrand: undefined;
   18 
   19     private readonly _onCodeEditorAdd: Emitter<ICodeEditor> = this._register(new Emitter<ICodeEditor>());
   20     public readonly onCodeEditorAdd: Event<ICodeEditor> = this._onCodeEditorAdd.event;
   21 
   22     private readonly _onCodeEditorRemove: Emitter<ICodeEditor> = this._register(new Emitter<ICodeEditor>());
   23     public readonly onCodeEditorRemove: Event<ICodeEditor> = this._onCodeEditorRemove.event;
   24 
   25     private readonly _onDiffEditorAdd: Emitter<IDiffEditor> = this._register(new Emitter<IDiffEditor>());
   26     public readonly onDiffEditorAdd: Event<IDiffEditor> = this._onDiffEditorAdd.event;
   27 
   28     private readonly _onDiffEditorRemove: Emitter<IDiffEditor> = this._register(new Emitter<IDiffEditor>());
   29     public readonly onDiffEditorRemove: Event<IDiffEditor> = this._onDiffEditorRemove.event;
   30 
   31     private readonly _onDidChangeTransientModelProperty: Emitter<ITextModel> = this._register(new Emitter<ITextModel>());
   32     public readonly onDidChangeTransientModelProperty: Event<ITextModel> = this._onDidChangeTransientModelProperty.event;
   33 
   34 
   35     private readonly _codeEditors: { [editorId: string]: ICodeEditor; };
   36     private readonly _diffEditors: { [editorId: string]: IDiffEditor; };
   37 
   38     constructor() {
   39         super();
   40         this._codeEditors = Object.create(null);
   41         this._diffEditors = Object.create(null);
   42     }
   43 
   44     addCodeEditor(editor: ICodeEditor): void {
   45         this._codeEditors[editor.getId()] = editor;
   46         this._onCodeEditorAdd.fire(editor);
   47     }
   48 
   49     removeCodeEditor(editor: ICodeEditor): void {
   50         if (delete this._codeEditors[editor.getId()]) {
   51             this._onCodeEditorRemove.fire(editor);
   52         }
   53     }
   54 
   55     listCodeEditors(): ICodeEditor[] {
   56         return Object.keys(this._codeEditors).map(id => this._codeEditors[id]);
   57     }
   58 
   59     addDiffEditor(editor: IDiffEditor): void {
   60         this._diffEditors[editor.getId()] = editor;
   61         this._onDiffEditorAdd.fire(editor);
   62     }
   63 
   64     removeDiffEditor(editor: IDiffEditor): void {
   65         if (delete this._diffEditors[editor.getId()]) {
   66             this._onDiffEditorRemove.fire(editor);
   67         }
   68     }
   69 
   70     listDiffEditors(): IDiffEditor[] {
   71         return Object.keys(this._diffEditors).map(id => this._diffEditors[id]);
   72     }
   73 
   74     getFocusedCodeEditor(): ICodeEditor | null {
   75         let editorWithWidgetFocus: ICodeEditor | null = null;
   76 
   77         const editors = this.listCodeEditors();
   78         for (const editor of editors) {
   79 
   80             if (editor.hasTextFocus()) {
   81                 // bingo!
   82                 return editor;
   83             }
   84 
   85             if (editor.hasWidgetFocus()) {
   86                 editorWithWidgetFocus = editor;
   87             }
   88         }
   89 
   90         return editorWithWidgetFocus;
   91     }
   92 
   93     abstract registerDecorationType(key: string, options: IDecorationRenderOptions, parentTypeKey?: string, editor?: ICodeEditor): void;
   94     abstract removeDecorationType(key: string): void;
   95     abstract resolveDecorationOptions(decorationTypeKey: string | undefined, writable: boolean): IModelDecorationOptions;
   96 
   97     private readonly _transientWatchers: { [uri: string]: ModelTransientSettingWatcher; } = {};
   98     private readonly _modelProperties = new Map<string, Map<string, any>>();
   99 
  100     public setModelProperty(resource: URI, key: string, value: any): void {
  101         const key1 = resource.toString();
  102         let dest: Map<string, any>;
  103         if (this._modelProperties.has(key1)) {
  104             dest = this._modelProperties.get(key1)!;
  105         } else {
  106             dest = new Map<string, any>();
  107             this._modelProperties.set(key1, dest);
  108         }
  109 
  110         dest.set(key, value);
  111     }
  112 
  113     public getModelProperty(resource: URI, key: string): any {
  114         const key1 = resource.toString();
  115         if (this._modelProperties.has(key1)) {
  116             const innerMap = this._modelProperties.get(key1)!;
  117             return innerMap.get(key);
  118         }
  119         return undefined;
  120     }
  121 
  122     public setTransientModelProperty(model: ITextModel, key: string, value: any): void {
  123         const uri = model.uri.toString();
  124 
  125         let w: ModelTransientSettingWatcher;
  126         if (this._transientWatchers.hasOwnProperty(uri)) {
  127             w = this._transientWatchers[uri];
  128         } else {
  129             w = new ModelTransientSettingWatcher(uri, model, this);
  130             this._transientWatchers[uri] = w;
  131         }
  132 
  133         w.set(key, value);
  134         this._onDidChangeTransientModelProperty.fire(model);
  135     }
  136 
  137     public getTransientModelProperty(model: ITextModel, key: string): any {
  138         const uri = model.uri.toString();
  139 
  140         if (!this._transientWatchers.hasOwnProperty(uri)) {
  141             return undefined;
  142         }
  143 
  144         return this._transientWatchers[uri].get(key);
  145     }
  146 
  147     public getTransientModelProperties(model: ITextModel): [string, any][] | undefined {
  148         const uri = model.uri.toString();
  149 
  150         if (!this._transientWatchers.hasOwnProperty(uri)) {
  151             return undefined;
  152         }
  153 
  154         return this._transientWatchers[uri].keys().map(key => [key, this._transientWatchers[uri].get(key)]);
  155     }
  156 
  157     _removeWatcher(w: ModelTransientSettingWatcher): void {
  158         delete this._transientWatchers[w.uri];
  159     }
  160 
  161     abstract getActiveCodeEditor(): ICodeEditor | null;
  162     abstract openCodeEditor(input: IResourceEditorInput, source: ICodeEditor | null, sideBySide?: boolean): Promise<ICodeEditor | null>;
  163 }
  164 
  165 export class ModelTransientSettingWatcher {
  166     public readonly uri: string;
  167     private readonly _values: { [key: string]: any; };
  168 
  169     constructor(uri: string, model: ITextModel, owner: AbstractCodeEditorService) {
  170         this.uri = uri;
  171         this._values = {};
  172         model.onWillDispose(() => owner._removeWatcher(this));
  173     }
  174 
  175     public set(key: string, value: any): void {
  176         this._values[key] = value;
  177     }
  178 
  179     public get(key: string): any {
  180         return this._values[key];
  181     }
  182 
  183     public keys(): string[] {
  184         return Object.keys(this._values);
  185     }
  186 }