From f5d0c196c69d06a9dda1bdeb379fa20ecbf75590 Mon Sep 17 00:00:00 2001 From: nlujjawal <164378643+nlujjawal@users.noreply.github.com> Date: Thu, 14 Nov 2024 12:13:34 +0100 Subject: [PATCH] Feat: exposing getter setter for widget manager created using line widgets (#5673) * Feat: exposing getter setter for widget manager from edit_session created using line widgets --- ace-internal.d.ts | 16 +++++++++------- ace.d.ts | 24 +++++++++++++++++++++++- src/edit_session.js | 32 ++++++++++++++++++++++++++++++++ src/editor.js | 9 ++------- src/ext/code_lens.js | 6 ------ src/ext/error_marker.js | 6 ------ 6 files changed, 66 insertions(+), 27 deletions(-) diff --git a/ace-internal.d.ts b/ace-internal.d.ts index 96d51a66eb3..65783e456d7 100644 --- a/ace-internal.d.ts +++ b/ace-internal.d.ts @@ -293,21 +293,23 @@ export namespace Ace { } interface LineWidget { - el: HTMLElement; - rowCount: number; - hidden: boolean; - _inDocument: boolean; + editor?: Editor, + el?: HTMLElement; + rowCount?: number; + hidden?: boolean; + _inDocument?: boolean; column?: number; - row?: number; + row: number; $oldWidget?: LineWidget, - session: EditSession, + session?: EditSession, html?: string, text?: string, className?: string, coverGutter?: boolean, pixelHeight?: number, $fold?: Fold, - editor: Editor, + type?:any, + destroy?:()=>void; coverLine?: boolean, fixedWidth?: boolean, fullWidth?: boolean, diff --git a/ace.d.ts b/ace.d.ts index 88560068eb7..de9f7f75e17 100644 --- a/ace.d.ts +++ b/ace.d.ts @@ -513,7 +513,7 @@ export namespace Ace { name?: string; }; }; - + widgetManager:WidgetManager; // TODO: define BackgroundTokenizer on(name: 'changeFold', @@ -1107,6 +1107,28 @@ export namespace Ace { tryShow(pos: Point, lineHeight: number, anchor: "top" | "bottom" | undefined, forceShow?: boolean): boolean; goTo(where: AcePopupNavigation): void; } + + export interface LineWidget { + el: HTMLElement; + row: number; + rowCount?: number; + hidden: boolean; + editor: Editor, + session: EditSession, + column?: number; + className?: string, + coverGutter?: boolean, + pixelHeight?: number, + fixedWidth?: boolean, + fullWidth?: boolean, + screenWidth?: number, + } + + export class WidgetManager { + constructor(session: EditSession); + addLineWidget(w: LineWidget): LineWidget; + removeLineWidget(w: LineWidget): void; + } } diff --git a/src/edit_session.js b/src/edit_session.js index 7fef86d933d..f9717aa4779 100644 --- a/src/edit_session.js +++ b/src/edit_session.js @@ -6,6 +6,7 @@ * @typedef {import("../ace-internal").Ace.Delta} Delta * @typedef {import("../ace-internal").Ace.IRange} IRange * @typedef {import("../ace-internal").Ace.SyntaxMode} SyntaxMode + * @typedef {import("../ace-internal").Ace.LineWidget} LineWidget */ var oop = require("./lib/oop"); @@ -16,6 +17,7 @@ var EventEmitter = require("./lib/event_emitter").EventEmitter; var Selection = require("./selection").Selection; var TextMode = require("./mode/text").Mode; var Range = require("./range").Range; +var LineWidgets = require("./line_widgets").LineWidgets; var Document = require("./document").Document; var BackgroundTokenizer = require("./background_tokenizer").BackgroundTokenizer; var SearchHighlight = require("./search_highlight").SearchHighlight; @@ -45,6 +47,7 @@ class EditSession { this.$backMarkers = {}; this.$markerId = 1; this.$undoSelect = true; + this.$editor = null; this.prevOp = {}; /** @type {FoldLine[]} */ @@ -187,6 +190,35 @@ class EditSession { return this.doc; } + /** + * Get "widgetManager" from EditSession + * + * @returns {LineWidgets} object + */ + get widgetManager() { + const widgetManager = new LineWidgets(this); + // todo remove the widgetManger assignement from lineWidgets constructor when introducing breaking changes + this.widgetManager = widgetManager; + + if (this.$editor) + widgetManager.attach(this.$editor); + + return widgetManager; + } + + /** + * Set "widgetManager" in EditSession + * + * @returns void + */ + set widgetManager(value) { + Object.defineProperty(this, "widgetManager", { + writable: true, + enumerable: true, + configurable: true, + value: value, + }); + } /** * @param {Number} docRow The row to work with * diff --git a/src/editor.js b/src/editor.js index 2ad6199af47..db888515deb 100644 --- a/src/editor.js +++ b/src/editor.js @@ -23,7 +23,6 @@ var CommandManager = require("./commands/command_manager").CommandManager; var defaultCommands = require("./commands/default_commands").commands; var config = require("./config"); var TokenIterator = require("./token_iterator").TokenIterator; -var LineWidgets = require("./line_widgets").LineWidgets; var GutterKeyboardHandler = require("./keyboard/gutter_handler").GutterKeyboardHandler; var nls = require("./config").nls; @@ -357,7 +356,9 @@ class Editor { this.curOp = null; oldSession && oldSession._signal("changeEditor", {oldEditor: this}); + if (oldSession) oldSession.$editor = null; session && session._signal("changeEditor", {editor: this}); + if (session) session.$editor = this; if (session && !session.destroyed) session.bgTokenizer.scheduleStart(); @@ -1486,10 +1487,6 @@ class Editor { * @param {Point} [position] Position to insert text to */ setGhostText(text, position) { - if (!this.session.widgetManager) { - this.session.widgetManager = new LineWidgets(this.session); - this.session.widgetManager.attach(this); - } this.renderer.setGhostText(text, position); } @@ -1497,8 +1494,6 @@ class Editor { * Removes "ghost" text currently displayed in the editor. */ removeGhostText() { - if (!this.session.widgetManager) return; - this.renderer.removeGhostText(); } diff --git a/src/ext/code_lens.js b/src/ext/code_lens.js index 7098c1e9984..44748bec7b5 100644 --- a/src/ext/code_lens.js +++ b/src/ext/code_lens.js @@ -4,7 +4,6 @@ * @typedef {import("../virtual_renderer").VirtualRenderer & {$textLayer: import("../layer/text").Text &{$lenses: any}}} VirtualRenderer */ -var LineWidgets = require("../line_widgets").LineWidgets; var event = require("../lib/event"); var lang = require("../lib/lang"); var dom = require("../lib/dom"); @@ -157,11 +156,6 @@ function attachToEditor(editor) { var session = editor.session; if (!session) return; - if (!session.widgetManager) { - session.widgetManager = new LineWidgets(session); - session.widgetManager.attach(editor); - } - var providersToWaitNum = editor.codeLensProviders.length; var lenses = []; editor.codeLensProviders.forEach(function(provider) { diff --git a/src/ext/error_marker.js b/src/ext/error_marker.js index 1c4bc5aa7e6..2da3838a7ca 100644 --- a/src/ext/error_marker.js +++ b/src/ext/error_marker.js @@ -1,5 +1,4 @@ "use strict"; -var LineWidgets = require("../line_widgets").LineWidgets; var dom = require("../lib/dom"); var Range = require("../range").Range; var nls = require("../config").nls; @@ -70,11 +69,6 @@ function findAnnotations(session, row, dir) { */ exports.showErrorMarker = function(editor, dir) { var session = editor.session; - if (!session.widgetManager) { - session.widgetManager = new LineWidgets(session); - session.widgetManager.attach(editor); - } - var pos = editor.getCursorPosition(); var row = pos.row; var oldWidget = session.widgetManager.getWidgetsAtRow(row).filter(function(w) {