summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMilutin Kristofic <milutin@google.com>2023-08-25 22:07:21 +0200
committerMilutin Kristofic <milutin@google.com>2023-08-28 11:15:44 +0200
commit11c05afae8e570d5a9093b2ef23d1aef0a536db2 (patch)
tree7c50393c7ba0f9a906afbd8edc0f9e6503c5d6b5
parent7e0c8d6cf5fbdfd6b6f45022c5cc4828bb0d389a (diff)
Change Range by Suggestionupstream/change-384100
-rw-r--r--polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.ts2
-rw-r--r--polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts68
-rw-r--r--polygerrit-ui/app/embed/diff/gr-diff/gr-diff.ts1
3 files changed, 53 insertions, 18 deletions
diff --git a/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.ts b/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.ts
index 24777fb692..b9c3dbb7cf 100644
--- a/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.ts
+++ b/polygerrit-ui/app/elements/shared/gr-comment-thread/gr-comment-thread.ts
@@ -617,6 +617,7 @@ export class GrCommentThread extends LitElement {
this.unresolved = this.getLastComment()?.unresolved ?? true;
this.diff = this.computeDiff();
+ // TODO(milutin): Check how we calculate range
this.highlightRange = this.computeHighlightRange();
}
@@ -710,6 +711,7 @@ export class GrCommentThread extends LitElement {
});
}
+ // TODO(milutin): change range
private computeHighlightRange() {
const comment = this.getFirstComment();
if (!comment) return undefined;
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 6ca47022c1..5f070a0da6 100644
--- a/polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts
+++ b/polygerrit-ui/app/elements/shared/gr-comment/gr-comment.ts
@@ -62,7 +62,7 @@ import {CommentSide, SpecialFilePath} from '../../../constants/constants';
import {Subject} from 'rxjs';
import {debounceTime} from 'rxjs/operators';
import {changeModelToken} from '../../../models/change/change-model';
-import {isBase64FileContent} from '../../../api/rest-api';
+import {CommentRange, isBase64FileContent} from '../../../api/rest-api';
import {createDiffUrl} from '../../../models/views/change';
import {userModelToken} from '../../../models/user/user-model';
import {modalStyles} from '../../../styles/gr-modal-styles';
@@ -214,6 +214,9 @@ export class GrComment extends LitElement {
@state()
isOwner = false;
+ @state()
+ newCommentRange?: CommentRange;
+
private readonly restApiService = getAppContext().restApiService;
private readonly reporting = getAppContext().reportingService;
@@ -877,22 +880,40 @@ export class GrComment extends LitElement {
const suggestionsPlugins =
this.getPluginLoader().pluginsModel.getState().suggestionsPlugins;
if (suggestionsPlugins.length === 0) return;
- if (!this.changeNum || !this.comment?.patch_set || !this.comments?.[0].path)
+ const firstComment = this.comments?.[0];
+ if (!firstComment) return;
+ if (!this.changeNum || !this.comment?.patch_set || !firstComment.path)
return;
const suggestion = await suggestionsPlugins[0].provider.suggestCode({
prompt: this.messageText,
changeNumber: this.changeNum,
patchsetNumber: this.comment?.patch_set,
- filePath: this.comments?.[0].path,
- range: this.comments?.[0].range,
- lineNumber: this.comments?.[0].line,
+ filePath: firstComment.path,
+ range: firstComment.range,
+ lineNumber: firstComment.line,
});
- const replacement = suggestion.suggestions?.[0].replacement;
+ let replacement = suggestion.suggestions?.[0].replacement;
if (!replacement) return;
+ if (firstComment.line === 45) {
+ replacement = ' fontStyles,\n diffStyles,\n css`';
+ this.newCommentRange = {
+ end_character: 16,
+ end_line: 46,
+ start_character: 6,
+ start_line: 45,
+ };
+ }
const addNewLine = this.messageText.length !== 0;
this.messageText += `${
addNewLine ? '\n' : ''
- }${'```\n'}${replacement}${'\n```'}`;
+ }${'```suggestion\n'}${replacement}${'\n```'}`;
+ // lineNumber : 45
+ // const range = {
+ // end_character: 16,
+ // end_line: 45,
+ // start_character: 6,
+ // start_line: 45,
+ // };
}
private renderRobotActions() {
@@ -1032,6 +1053,7 @@ export class GrComment extends LitElement {
// comment changes.
fire(this, 'comment-text-changed', {value: this.messageText});
}
+ // TODO(milutin): maybe fire update on range.
}
private handlePortedMessageClick() {
@@ -1248,11 +1270,17 @@ export class GrComment extends LitElement {
convertToCommentInput(): CommentInput | undefined {
if (!this.somethingToSave() || !this.comment) return;
- return convertToCommentInput({
+ const newComment = {
...this.comment,
message: this.messageText.trimEnd(),
unresolved: this.unresolved,
- });
+ };
+ if (this.newCommentRange) {
+ newComment.range = this.newCommentRange;
+ newComment.line = this.newCommentRange.end_line;
+ this.newCommentRange = undefined;
+ }
+ return convertToCommentInput(newComment);
}
async save() {
@@ -1289,7 +1317,8 @@ export class GrComment extends LitElement {
return (
isError(this.comment) ||
this.messageText.trimEnd() !== this.comment?.message ||
- this.unresolved !== this.comment.unresolved
+ this.unresolved !== this.comment.unresolved ||
+ (this.newCommentRange && this.newCommentRange !== this.comment.range)
);
}
@@ -1297,14 +1326,17 @@ export class GrComment extends LitElement {
private rawSave(options: {showToast: boolean}) {
assert(isDraft(this.comment), 'only drafts are editable');
assert(!isSaving(this.comment), 'saving already in progress');
- return this.getCommentsModel().saveDraft(
- {
- ...this.comment,
- message: this.messageText.trimEnd(),
- unresolved: this.unresolved,
- },
- options.showToast
- );
+ const newDraft = {
+ ...this.comment,
+ message: this.messageText.trimEnd(),
+ unresolved: this.unresolved,
+ };
+ if (this.newCommentRange) {
+ newDraft.range = this.newCommentRange;
+ newDraft.line = this.newCommentRange.end_line;
+ this.newCommentRange = undefined;
+ }
+ return this.getCommentsModel().saveDraft(newDraft, options.showToast);
}
private handleToggleResolved() {
diff --git a/polygerrit-ui/app/embed/diff/gr-diff/gr-diff.ts b/polygerrit-ui/app/embed/diff/gr-diff/gr-diff.ts
index b3c7dad688..eb4c5a41a1 100644
--- a/polygerrit-ui/app/embed/diff/gr-diff/gr-diff.ts
+++ b/polygerrit-ui/app/embed/diff/gr-diff/gr-diff.ts
@@ -671,6 +671,7 @@ export class GrDiff extends LitElement implements GrDiffApi {
this.nodeObserver = new MutationObserver(() => this.processNodes());
this.nodeObserver.observe(this, {childList: true});
// Process existing comment widgets before the first observed change.
+ // TODO(milutin): Comment was not added but changed range. We need to trigger this
this.processNodes();
}