summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMilutin Kristofic <milutin@google.com>2023-08-23 09:12:37 +0000
committerGerrit Code Review <noreply-gerritcodereview@google.com>2023-08-23 09:12:37 +0000
commit122c0c9eac1a12c3e3c38b65d9900d292cda93ae (patch)
tree79f0051b1a8d91c842945ba31ff5b6b406b7b303
parent8a8c85aaf25d2f129c4359e889bf3167cc781719 (diff)
parentb59aeeb67e5d93a29855888a74ad3a098c2d2f3f (diff)
Merge "ML Suggested Edits - Suggestions Provider improvement"
-rw-r--r--polygerrit-ui/app/api/plugin.ts2
-rw-r--r--polygerrit-ui/app/api/suggestions.ts2
-rw-r--r--polygerrit-ui/app/elements/plugins/gr-suggestions-api/gr-suggestions-api.ts48
-rw-r--r--polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts5
-rw-r--r--polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.ts5
5 files changed, 60 insertions, 2 deletions
diff --git a/polygerrit-ui/app/api/plugin.ts b/polygerrit-ui/app/api/plugin.ts
index 4aa3aaad36..684429a666 100644
--- a/polygerrit-ui/app/api/plugin.ts
+++ b/polygerrit-ui/app/api/plugin.ts
@@ -15,6 +15,7 @@ import {ChangeActionsPluginApi} from './change-actions';
import {RestPluginApi} from './rest';
import {HookApi, RegisterOptions} from './hook';
import {StylePluginApi} from './styles';
+import {SuggestionsPluginApi} from './suggestions';
export enum TargetElement {
CHANGE_ACTIONS = 'changeactions',
@@ -58,6 +59,7 @@ export declare interface PluginApi {
changeActions(): ChangeActionsPluginApi;
changeReply(): ChangeReplyPluginApi;
checks(): ChecksPluginApi;
+ suggestions(): SuggestionsPluginApi;
eventHelper(element: Node): EventHelperPluginApi;
getPluginName(): string;
hook<T extends HTMLElement>(
diff --git a/polygerrit-ui/app/api/suggestions.ts b/polygerrit-ui/app/api/suggestions.ts
index f75f6e0636..4038102150 100644
--- a/polygerrit-ui/app/api/suggestions.ts
+++ b/polygerrit-ui/app/api/suggestions.ts
@@ -19,7 +19,7 @@ export declare interface SuggestCodeRequest {
patchsetNumber: RevisionPatchSetNum;
filePath: string;
range?: CommentRange;
- lineNumber?: Number;
+ lineNumber?: number;
}
export declare interface SuggestionsProvider {
diff --git a/polygerrit-ui/app/elements/plugins/gr-suggestions-api/gr-suggestions-api.ts b/polygerrit-ui/app/elements/plugins/gr-suggestions-api/gr-suggestions-api.ts
new file mode 100644
index 0000000000..400684900e
--- /dev/null
+++ b/polygerrit-ui/app/elements/plugins/gr-suggestions-api/gr-suggestions-api.ts
@@ -0,0 +1,48 @@
+/**
+ * @license
+ * Copyright 2023 Google LLC
+ * SPDX-License-Identifier: Apache-2.0
+ */
+import {PluginApi} from '../../../api/plugin';
+import {ReportingService} from '../../../services/gr-reporting/gr-reporting';
+import {PluginsModel} from '../../../models/plugins/plugins-model';
+import {
+ SuggestionsPluginApi,
+ SuggestionsProvider,
+} from '../../../api/suggestions';
+
+enum State {
+ NOT_REGISTERED,
+ REGISTERED,
+}
+
+/**
+ * Plugin API for suggestions.
+ *
+ * This object is returned to plugins that want to provide suggestions data.
+ * Plugins normally just call register() once at startup and then wait for
+ * suggestCode() being called on the provider interface.
+ */
+export class GrSuggestionsApi implements SuggestionsPluginApi {
+ private state = State.NOT_REGISTERED;
+
+ constructor(
+ private readonly reporting: ReportingService,
+ private readonly pluginsModel: PluginsModel,
+ readonly plugin: PluginApi
+ ) {
+ this.reporting.trackApi(this.plugin, 'suggestions', 'constructor');
+ }
+
+ register(provider: SuggestionsProvider): void {
+ this.reporting.trackApi(this.plugin, 'suggestions', 'register');
+ if (this.state === State.REGISTERED) {
+ throw new Error('Only one provider can be registered per plugin.');
+ }
+ this.state = State.REGISTERED;
+ this.pluginsModel.suggestionsRegister({
+ pluginName: this.plugin.getPluginName(),
+ provider,
+ });
+ }
+}
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 e2229b5ce7..6ca47022c1 100644
--- a/polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts
+++ b/polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts
@@ -889,7 +889,10 @@ export class GrComment extends LitElement {
});
const replacement = suggestion.suggestions?.[0].replacement;
if (!replacement) return;
- this.messageText += `${USER_SUGGESTION_START_PATTERN}${suggestion}${'\n```'}`;
+ const addNewLine = this.messageText.length !== 0;
+ this.messageText += `${
+ addNewLine ? '\n' : ''
+ }${'```\n'}${replacement}${'\n```'}`;
}
private renderRobotActions() {
diff --git a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.ts b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.ts
index 832b97efee..d3dea78cc4 100644
--- a/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.ts
+++ b/polygerrit-ui/app/elements/shared/gr-js-api-interface/gr-public-js-api.ts
@@ -37,6 +37,7 @@ import {RestApiService} from '../../../services/gr-rest-api/gr-rest-api';
import {PluginsModel} from '../../../models/plugins/plugins-model';
import {GrPluginStyleApi} from './gr-plugin-style-api';
import {StylePluginApi} from '../../../api/styles';
+import {GrSuggestionsApi} from '../../plugins/gr-suggestions-api/gr-suggestions-api';
const PLUGIN_NAME_NOT_SET = 'NULL';
@@ -214,6 +215,10 @@ export class Plugin implements PluginApi {
return new GrChecksApi(this.report, this.pluginsModel, this);
}
+ suggestions(): GrSuggestionsApi {
+ return new GrSuggestionsApi(this.report, this.pluginsModel, this);
+ }
+
reporting(): ReportingPluginApi {
return new GrReportingJsApi(this.report, this);
}