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,138 @@
'use strict'
const { expect } = require('chai')
const CommentList = require('../lib/file_data/comment_list')
const Comment = require('../lib/file_data/comment')
const Range = require('../lib/file_data/range')
describe('commentList', function () {
it('checks if toRaw() returns a correct comment list', function () {
const commentList = new CommentList(
new Map([
['comm1', new Comment([new Range(5, 10)])],
['comm2', new Comment([new Range(20, 5)])],
['comm3', new Comment([new Range(30, 15)])],
])
)
expect(commentList.getComments()).to.eql([
{ id: 'comm1', ranges: [{ pos: 5, length: 10 }], resolved: false },
{ id: 'comm2', ranges: [{ pos: 20, length: 5 }], resolved: false },
{
id: 'comm3',
ranges: [{ pos: 30, length: 15 }],
resolved: false,
},
])
})
it('should get a comment by id', function () {
const commentList = new CommentList(
new Map([
['comm1', new Comment([new Range(5, 10)])],
['comm3', new Comment([new Range(30, 15)])],
['comm2', new Comment([new Range(20, 5)])],
])
)
const comment = commentList.getComment('comm2')
expect(comment?.toRaw()).to.eql({
ranges: [
{
pos: 20,
length: 5,
},
],
resolved: false,
})
})
it('should add new comment to the list', function () {
const commentList = new CommentList(
new Map([
['comm1', new Comment([new Range(5, 10)])],
['comm2', new Comment([new Range(20, 5)])],
['comm3', new Comment([new Range(30, 15)])],
])
)
commentList.add('comm4', new Comment([new Range(40, 10)]))
expect(commentList.getComments()).to.eql([
{ id: 'comm1', ranges: [{ pos: 5, length: 10 }], resolved: false },
{ id: 'comm2', ranges: [{ pos: 20, length: 5 }], resolved: false },
{
id: 'comm3',
ranges: [{ pos: 30, length: 15 }],
resolved: false,
},
{
id: 'comm4',
ranges: [{ pos: 40, length: 10 }],
resolved: false,
},
])
})
it('should override existing if a new comment has the same id', function () {
const commentList = new CommentList(
new Map([
['comm1', new Comment([new Range(5, 10)])],
['comm2', new Comment([new Range(20, 5)])],
['comm3', new Comment([new Range(30, 15)])],
])
)
commentList.add('comm2', new Comment([new Range(40, 10)]))
expect(commentList.getComments()).to.eql([
{ id: 'comm1', ranges: [{ pos: 5, length: 10 }], resolved: false },
{
id: 'comm2',
ranges: [{ pos: 40, length: 10 }],
resolved: false,
},
{
id: 'comm3',
ranges: [{ pos: 30, length: 15 }],
resolved: false,
},
])
})
it('should delete a comment from the list', function () {
const commentList = new CommentList(
new Map([
['comm1', new Comment([new Range(5, 10)])],
['comm2', new Comment([new Range(20, 5)])],
['comm3', new Comment([new Range(30, 15)])],
])
)
commentList.delete('comm3')
expect(commentList.getComments()).to.eql([
{ id: 'comm1', ranges: [{ pos: 5, length: 10 }], resolved: false },
{ id: 'comm2', ranges: [{ pos: 20, length: 5 }], resolved: false },
])
})
it('should not throw an error if comment id does not exist', function () {
const commentList = new CommentList(
new Map([
['comm1', new Comment([new Range(5, 10)])],
['comm2', new Comment([new Range(20, 5)])],
['comm3', new Comment([new Range(30, 15)])],
])
)
commentList.delete('comm5')
expect(commentList.getComments()).to.eql([
{ id: 'comm1', ranges: [{ pos: 5, length: 10 }], resolved: false },
{ id: 'comm2', ranges: [{ pos: 20, length: 5 }], resolved: false },
{
id: 'comm3',
ranges: [{ pos: 30, length: 15 }],
resolved: false,
},
])
})
})
@@ -88,4 +88,9 @@ describe('File', function () {
expect(clone.getStringLength()).to.equal(0)
})
})
it('getComments() returns an empty comment list', function () {
const file = File.fromString('foo')
expect(file.getComments()).to.eql([])
})
})
@@ -34,4 +34,70 @@ describe('StringFileData', function () {
expect(fileData.getByteLength()).to.equal(longString.length - 1)
expect(fileData.getStringLength()).to.equal(longString.length - 1)
})
it('getComments() should return an empty array', function () {
const fileData = new StringFileData('test')
expect(fileData.getComments()).to.eql([])
})
it('creates StringFileData with comments', function () {
const fileData = new StringFileData('test', [
{
id: 'comm1',
ranges: [
{
pos: 5,
length: 10,
},
],
},
{
id: 'comm2',
ranges: [
{
pos: 20,
length: 5,
},
],
},
])
expect(fileData.getComments()).to.eql([
{ id: 'comm1', ranges: [{ pos: 5, length: 10 }], resolved: false },
{ id: 'comm2', ranges: [{ pos: 20, length: 5 }], resolved: false },
])
})
it('fromRaw() should create StringFileData with comments', function () {
const fileData = StringFileData.fromRaw({
content: 'test',
comments: [
{
id: 'comm1',
ranges: [
{
pos: 5,
length: 10,
},
],
resolved: false,
},
{
id: 'comm2',
ranges: [
{
pos: 20,
length: 5,
},
],
resolved: true,
},
],
})
expect(fileData.getComments()).to.eql([
{ id: 'comm1', ranges: [{ pos: 5, length: 10 }], resolved: false },
{ id: 'comm2', ranges: [{ pos: 20, length: 5 }], resolved: true },
])
})
})