Merge "Use user setting for opt out from suggest code while commenting"
diff --git a/polygerrit-ui/app/constants/constants.ts b/polygerrit-ui/app/constants/constants.ts
index 24fb5c0..0fa58f4 100644
--- a/polygerrit-ui/app/constants/constants.ts
+++ b/polygerrit-ui/app/constants/constants.ts
@@ -263,6 +263,7 @@
     email_strategy: EmailStrategy.ATTENTION_SET_ONLY,
     default_base_for_merges: DefaultBase.AUTO_MERGE,
     allow_browser_notifications: false,
+    allow_suggest_code_while_commenting: true,
     diff_page_sidebar: 'NONE',
   };
 }
diff --git a/polygerrit-ui/app/elements/settings/gr-settings-view/gr-settings-view.ts b/polygerrit-ui/app/elements/settings/gr-settings-view/gr-settings-view.ts
index 4d4834e..29ece75 100644
--- a/polygerrit-ui/app/elements/settings/gr-settings-view/gr-settings-view.ts
+++ b/polygerrit-ui/app/elements/settings/gr-settings-view/gr-settings-view.ts
@@ -71,6 +71,8 @@
 import {navigationToken} from '../../core/gr-navigation/gr-navigation';
 import {getDocUrl, rootUrl} from '../../../utils/url-util';
 import {configModelToken} from '../../../models/config/config-model';
+import {SuggestionsProvider} from '../../../api/suggestions';
+import {pluginLoaderToken} from '../../shared/gr-js-api-interface/gr-plugin-loader';
 
 const HTTP_AUTH = ['HTTP', 'HTTP_LDAP'];
 
@@ -118,6 +120,9 @@
   @query('#allowBrowserNotifications')
   allowBrowserNotifications?: HTMLInputElement;
 
+  @query('#allowSuggestCodeWhileCommenting')
+  allowSuggestCodeWhileCommenting?: HTMLInputElement;
+
   @query('#disableKeyboardShortcuts')
   disableKeyboardShortcuts!: HTMLInputElement;
 
@@ -196,6 +201,9 @@
 
   @state() private docsBaseUrl = '';
 
+  @state()
+  suggestionsProvider?: SuggestionsProvider;
+
   // private but used in test
   public _testOnly_loadingPromise?: Promise<void>;
 
@@ -212,6 +220,8 @@
 
   private readonly getConfigModel = resolve(this, configModelToken);
 
+  private readonly getPluginLoader = resolve(this, pluginLoaderToken);
+
   constructor() {
     super();
     subscribe(
@@ -265,6 +275,14 @@
     // we need to manually calling scrollIntoView when hash changed
     document.addEventListener('location-change', this.handleLocationChange);
     fireTitleChange('Settings');
+    this.getPluginLoader()
+      .awaitPluginsLoaded()
+      .then(() => {
+        const suggestionsPlugins =
+          this.getPluginLoader().pluginsModel.getState().suggestionsPlugins;
+        // We currently support results from only 1 provider.
+        this.suggestionsProvider = suggestionsPlugins?.[0]?.provider;
+      });
   }
 
   override firstUpdated() {
@@ -451,6 +469,7 @@
             ${this.renderTheme()} ${this.renderChangesPerPages()}
             ${this.renderDateTimeFormat()} ${this.renderEmailNotification()}
             ${this.renderEmailFormat()} ${this.renderBrowserNotifications()}
+            ${this.renderGenerateSuggestionWhenCommenting()}
             ${this.renderDefaultBaseForMerges()}
             ${this.renderRelativeDateInChangeTable()} ${this.renderDiffView()}
             ${this.renderShowSizeBarsInFileList()}
@@ -867,6 +886,45 @@
     `;
   }
 
+  private renderGenerateSuggestionWhenCommenting() {
+    if (
+      !this.flagsService.isEnabled(KnownExperimentId.ML_SUGGESTED_EDIT) ||
+      !this.suggestionsProvider
+    )
+      return nothing;
+    return html`
+      <section id="allowSuggestCodeWhileCommentingSection">
+        <div class="title">
+          <label for="allowSuggestCodeWhileCommenting"
+            >Allow generating suggestions while commenting</label
+          >
+          <a
+            href=${getDocUrl(
+              this.docsBaseUrl,
+              'user-suggest-edits.html#_generate_suggestion'
+            )}
+            target="_blank"
+            rel="noopener noreferrer"
+          >
+            <gr-icon icon="help" title="read documentation"></gr-icon>
+          </a>
+        </div>
+        <span class="value">
+          <input
+            id="allowSuggestCodeWhileCommenting"
+            type="checkbox"
+            ?checked=${this.localPrefs.allow_suggest_code_while_commenting}
+            @change=${() => {
+              this.localPrefs.allow_suggest_code_while_commenting =
+                this.allowSuggestCodeWhileCommenting!.checked;
+              this.prefsChanged = true;
+            }}
+          />
+        </span>
+      </section>
+    `;
+  }
+
   private renderDefaultBaseForMerges() {
     if (!this.localPrefs.default_base_for_merges) return nothing;
     return nothing;
diff --git a/polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts b/polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts
index 3e341b8..9b83f3e 100644
--- a/polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts
+++ b/polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts
@@ -377,6 +377,19 @@
           this.generateSuggestEdit();
         }
       );
+      subscribe(
+        this,
+        () => this.getUserModel().preferences$,
+        prefs => {
+          if (
+            this.generateSuggestion !==
+            !!prefs.allow_suggest_code_while_commenting
+          ) {
+            this.generateSuggestion =
+              !!prefs.allow_suggest_code_while_commenting;
+          }
+        }
+      );
     }
   }
 
diff --git a/polygerrit-ui/app/test/test-data-generators.ts b/polygerrit-ui/app/test/test-data-generators.ts
index 0ac20ac..cba3a05 100644
--- a/polygerrit-ui/app/test/test-data-generators.ts
+++ b/polygerrit-ui/app/test/test-data-generators.ts
@@ -709,6 +709,7 @@
     changes_per_page: 10,
     email_strategy: EmailStrategy.ENABLED,
     allow_browser_notifications: true,
+    allow_suggest_code_while_commenting: true,
   };
 }
 
diff --git a/polygerrit-ui/app/types/common.ts b/polygerrit-ui/app/types/common.ts
index 9992f8b..4f23dbd 100644
--- a/polygerrit-ui/app/types/common.ts
+++ b/polygerrit-ui/app/types/common.ts
@@ -1335,6 +1335,7 @@
   // The email_format doesn't mentioned in doc, but exists in Java class GeneralPreferencesInfo
   email_format?: EmailFormat;
   allow_browser_notifications?: boolean;
+  allow_suggest_code_while_commenting?: boolean;
   diff_page_sidebar?: DiffPageSidebar;
 }