Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat: exposing getter setter for widget manager created using line widgets #5673

Merged
merged 4 commits into from
Nov 14, 2024

Conversation

nlujjawal
Copy link
Contributor

@nlujjawal nlujjawal commented Nov 12, 2024

Description of changes:
Exposing getter and setter for widget manager created using line widgets.
So now ace consumers can use the getter to access the widgetManager which can be used to create line widgets.

  1. Added and setter which creates widgetManger(an instance of line_widgets) link it with editor.session.widgetManager and attached it the editor.
  2. Added and getter which returns the widgetManger(an instance of line_widgets) attached with the edit_session and not with editor because session can be created without editor with some line_widgets so session.widgetManager should work without editor. And that session can be linked with editor later.
  3. Updated the ace.d.ts types to have the setWidgetManager and getWidgetManager methods in Editor type.

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

Pull Request Checklist:

@nlujjawal nlujjawal requested a review from akoreman November 12, 2024 15:38
Copy link

One of the public type files has been updated, plase make sure there are no backwards incompatible changes done in the PR.

src/editor.js Outdated
* Set "widgetManager" in editor.session
* @returns void
*/
setWidgetManager() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

calling this setWidgetManager might lead to the assumption that it accepts a parameter which gets set, maybe something like attachWidgetManager makes sense?

ace.d.ts Outdated
@@ -953,6 +953,8 @@ export namespace Ace {
splitLine(): void;
setGhostText(text: string, position: Point): void;
removeGhostText(): void;
setWidgetManager(): void;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to expose this function? consumers will want to access the widget manager (which they can do through getWidgetManager) and when it's not defined yet that will be handled by this getter. Do you see an use-case where someone wants to attach a widget manager but not use it afterwards?

ace.d.ts Outdated
@@ -953,6 +953,8 @@ export namespace Ace {
splitLine(): void;
setGhostText(text: string, position: Point): void;
removeGhostText(): void;
setWidgetManager(): void;
getWidgetManager(): Ace.LineWidgets;
Copy link
Contributor

@akoreman akoreman Nov 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ace.LineWidgets is only exported from the internal type declaration file at the moment right? we should include it in this declaration file as well if we're exposing this function. (this is probably also what's breaking the CI build atm)

@akoreman
Copy link
Contributor

We also use the widget manager for the ghost text here, does it make sense to convert this instance as well to the new functions?

@nightwing
Copy link
Member

The reason widgetManager was not defined here, was that initially we wanted to keep lineWidgets as an optional feature that could be excluded from the bundle. Since we have added import of lineWidgets to the editor itself, i'd suggest to convert session.widgetManager to a getter, so that this.session.widgetManager will always return something.

diff --git a/src/edit_session.js b/src/edit_session.js
index 718105762..af93087e5 100644
--- a/src/edit_session.js
+++ b/src/edit_session.js
@@ -12,6 +12,7 @@ var oop = require("./lib/oop");
 var lang = require("./lib/lang");
 var BidiHandler = require("./bidihandler").BidiHandler;
 var config = require("./config");
+var { LineWidgets } = require("./line_widgets");
 var EventEmitter = require("./lib/event_emitter").EventEmitter;
 var Selection = require("./selection").Selection;
 var TextMode = require("./mode/text").Mode;
@@ -2386,6 +2387,23 @@ class EditSession {
         return currentLine[pos.column - 1];
     }
 
+    get widgetManager() {
+        var widgetManager = new LineWidgets(this);
+        
+        if (this.$editor)
+            widgetManager.attach(this.$editor);
+
+        return widgetManager;
+    }
+    set widgetManager(value) {
+        Object.defineProperty(this, "widgetManager", {
+            writable: true, 
+            enumerable: true,
+            configurable: true,
+            value: value,
+        });
+    }
+
     destroy() {
         if (!this.destroyed) {
             this.bgTokenizer.setDocument(null);
diff --git a/src/editor.js b/src/editor.js
index c9d2bf7a6..c9df18860 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;
 
@@ -372,7 +371,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();
@@ -1502,10 +1503,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);
     }
 
@@ -1513,8 +1510,6 @@ class Editor {
      * Removes "ghost" text currently displayed in the editor.
      */
     removeGhostText() {
-        if (!this.session.widgetManager) return;
-
         this.renderer.removeGhostText();
     }
 

@nlujjawal nlujjawal force-pushed the expose_widget_manager branch from ec97825 to c381585 Compare November 13, 2024 16:43
Copy link

One of the public type files has been updated, plase make sure there are no backwards incompatible changes done in the PR.

Copy link

codecov bot commented Nov 13, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 87.01%. Comparing base (74e95d1) to head (f1d6816).
Report is 2 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5673      +/-   ##
==========================================
- Coverage   87.01%   87.01%   -0.01%     
==========================================
  Files         598      598              
  Lines       43681    43680       -1     
  Branches     7205     7204       -1     
==========================================
- Hits        38010    38009       -1     
  Misses       5671     5671              
Flag Coverage Δ
unittests 87.01% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@nlujjawal nlujjawal force-pushed the expose_widget_manager branch from c381585 to 52436cd Compare November 13, 2024 16:46
Copy link

One of the public type files has been updated, plase make sure there are no backwards incompatible changes done in the PR.

@nlujjawal nlujjawal force-pushed the expose_widget_manager branch from 52436cd to f0bed9b Compare November 13, 2024 16:50
Copy link

One of the public type files has been updated, plase make sure there are no backwards incompatible changes done in the PR.

@nlujjawal nlujjawal force-pushed the expose_widget_manager branch from f0bed9b to c08b103 Compare November 13, 2024 16:58
Copy link

One of the public type files has been updated, plase make sure there are no backwards incompatible changes done in the PR.

@nlujjawal nlujjawal force-pushed the expose_widget_manager branch from c08b103 to fdc468c Compare November 13, 2024 17:02
Copy link

One of the public type files has been updated, plase make sure there are no backwards incompatible changes done in the PR.

Copy link

One of the public type files has been updated, plase make sure there are no backwards incompatible changes done in the PR.

* @returns {LineWidgets} object
*/
get widgetManager() {
const widgetManager = new LineWidgets(this);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how does this work when both consumers want to use editor.widgetManager and we want to use it internally? Doesn't this mean that when a consumer calls the widgetManager all the existing widgets currently in the editor get deleted?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice catch. Will be adding the check to only create widgetManager if it doesn't exists.

Copy link

One of the public type files has been updated, plase make sure there are no backwards incompatible changes done in the PR.

/**
* Get "widgetManager" from EditSession
*
* @returns {LineWidgets} object
Copy link
Contributor

@akoreman akoreman Nov 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This gives session.widgetManager the LineWidgets type, which I think means that session.widgetManager.addLineWidget(...) will result in a typescript error right? Should this return type be WidgetManager?

Copy link

One of the public type files has been updated, plase make sure there are no backwards incompatible changes done in the PR.

@nlujjawal nlujjawal merged commit f5d0c19 into master Nov 14, 2024
4 checks passed
@nlujjawal nlujjawal deleted the expose_widget_manager branch November 14, 2024 11:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants