Add CommentList to StringFileData (#16568)

* Add CommentList to StringFileData

* added more types

* use toRaw

* using Map rather than array for comments

* using Range class

* Comment with ranges:Range[]

* Revert "Comment with ranges:Range[]"

This reverts commit 0783b1837562600637db03cc70c620129061c797.

* Comment with ranges:Range[]

* remove isDeleted

* commentList.toRaw()

* using toRaw

* commentId to id

* Revert "using toRaw"

This reverts commit 0c04ca5836f3befd5ec027bad5bf722e8b27f36c.

* fix merge

* make comment map internal to CommentList

* remove unused type

* fix parameter name in StringFileData

* import types more consistently

* more consistent type def

GitOrigin-RevId: 2be2225819d8e8ebcf90d08def280377205cb9ec
This commit is contained in:
Domagoj Kriskovic
2024-01-24 09:04:15 +00:00
committed by Copybot
parent 3de9efb1bb
commit fc90db231c
10 changed files with 390 additions and 3 deletions
@@ -0,0 +1,36 @@
const Range = require('./range')
/**
* @typedef {import("../types").CommentRawData} CommentRawData
*/
class Comment {
/**
* @param {Range[]} ranges
* @param {boolean} [resolved]
*/
constructor(ranges, resolved = false) {
this.ranges = ranges
this.resolved = resolved
}
toRaw() {
return {
resolved: this.resolved,
ranges: this.ranges.map(range => range.toRaw()),
}
}
/**
* @param {CommentRawData} rawComment
* @returns {Comment}
*/
static fromRaw(rawComment) {
return new Comment(
rawComment.ranges.map(range => Range.fromRaw(range)),
rawComment.resolved
)
}
}
module.exports = Comment
@@ -0,0 +1,68 @@
const Comment = require('./comment')
/**
* @typedef {import("../types").CommentRawData} CommentRawData
*/
class CommentList {
/**
* @param {Map<string, Comment>} comments
*/
constructor(comments) {
this.comments = comments
}
/**
* @returns {CommentRawData[]}
*/
getComments() {
return Array.from(this.comments).map(([commentId, comment]) => {
return {
id: commentId,
...comment.toRaw(),
}
})
}
/**
* @param {string} id
* @returns {Comment | undefined}
*/
getComment(id) {
return this.comments.get(id)
}
/**
* @param {string} id
* @param {Comment} newComment
*/
add(id, newComment) {
const existing = this.getComment(id)
if (existing) {
// todo: merge/split ranges
existing.ranges = newComment.ranges
} else {
this.comments.set(id, newComment)
}
}
/**
* @param {string} id
*/
delete(id) {
return this.comments.delete(id)
}
/**
* @param {CommentRawData[]} rawComments
*/
static fromRaw(rawComments) {
const comments = new Map()
for (const rawComment of rawComments) {
comments.set(rawComment.id, Comment.fromRaw(rawComment))
}
return new CommentList(comments)
}
}
module.exports = CommentList
@@ -15,6 +15,7 @@ let StringFileData = null
/**
* @typedef {import("../types").BlobStore} BlobStore
* @typedef {import("../types").CommentRawData} CommentRawData
*/
/**
@@ -147,6 +148,16 @@ class FileData {
async store(blobStore) {
throw new Error('store not implemented for ' + JSON.stringify(this))
}
/**
* @see File#getComments
* @function
* @return {CommentRawData[]}
* @abstract
*/
getComments() {
throw new Error('getComments not implemented for ' + JSON.stringify(this))
}
}
module.exports = FileData
@@ -0,0 +1,23 @@
class Range {
/**
* @param {number} pos
* @param {number} length
*/
constructor(pos, length) {
this.pos = pos
this.length = length
}
toRaw() {
return {
pos: this.pos,
length: this.length
}
}
static fromRaw(raw) {
return new Range(raw.pos, raw.length)
}
}
module.exports = Range
@@ -3,23 +3,31 @@
const assert = require('check-types').assert
const FileData = require('./')
const CommentList = require('./comment_list')
/**
* @typedef {import("../types").StringFileRawData} StringFileRawData
* @typedef {import("../types").CommentRawData} CommentRawData
*/
class StringFileData extends FileData {
/**
* @param {string} content
* @param {CommentRawData[]} [rawComments]
*/
constructor(content) {
constructor(content, rawComments = []) {
super()
assert.string(content)
this.content = content
this.comments = CommentList.fromRaw(rawComments)
}
/**
* @param {StringFileRawData} raw
* @returns {StringFileData}
*/
static fromRaw(raw) {
return new StringFileData(raw.content)
return new StringFileData(raw.content, raw.comments || [])
}
/**
@@ -27,7 +35,14 @@ class StringFileData extends FileData {
* @returns {StringFileRawData}
*/
toRaw() {
return { content: this.content }
const raw = { content: this.content }
const comments = this.getComments()
if (comments.length) {
raw.comments = comments
}
return raw
}
/** @inheritdoc */
@@ -55,6 +70,11 @@ class StringFileData extends FileData {
this.content = textOperation.apply(this.content)
}
/** @inheritdoc */
getComments() {
return this.comments.getComments()
}
/** @inheritdoc */
async toEager() {
return this