| 19 | } |
| 20 | |
| 21 | export function activate(context: vscode.ExtensionContext) { |
| 22 | // A `CommentController` is able to provide comments for documents. |
| 23 | const commentController = vscode.comments.createCommentController('comment-sample', 'Comment API Sample'); |
| 24 | context.subscriptions.push(commentController); |
| 25 | |
| 26 | // A `CommentingRangeProvider` controls where gutter decorations that allow adding comments are shown |
| 27 | commentController.commentingRangeProvider = { |
| 28 | provideCommentingRanges: (document: vscode.TextDocument, _token: vscode.CancellationToken) => { |
| 29 | const lineCount = document.lineCount; |
| 30 | return [new vscode.Range(0, 0, lineCount - 1, 0)]; |
| 31 | } |
| 32 | }; |
| 33 | |
| 34 | context.subscriptions.push(vscode.commands.registerCommand('mywiki.createNote', (reply: vscode.CommentReply) => { |
| 35 | replyNote(reply); |
| 36 | })); |
| 37 | |
| 38 | context.subscriptions.push(vscode.commands.registerCommand('mywiki.replyNote', (reply: vscode.CommentReply) => { |
| 39 | replyNote(reply); |
| 40 | })); |
| 41 | |
| 42 | context.subscriptions.push(vscode.commands.registerCommand('mywiki.startDraft', (reply: vscode.CommentReply) => { |
| 43 | const thread = reply.thread; |
| 44 | thread.contextValue = 'draft'; |
| 45 | const newComment = new NoteComment(reply.text, vscode.CommentMode.Preview, { name: 'vscode' }, thread); |
| 46 | newComment.label = 'pending'; |
| 47 | thread.comments = [...thread.comments, newComment]; |
| 48 | })); |
| 49 | |
| 50 | context.subscriptions.push(vscode.commands.registerCommand('mywiki.finishDraft', (reply: vscode.CommentReply) => { |
| 51 | const thread = reply.thread; |
| 52 | |
| 53 | if (!thread) { |
| 54 | return; |
| 55 | } |
| 56 | |
| 57 | thread.contextValue = undefined; |
| 58 | thread.collapsibleState = vscode.CommentThreadCollapsibleState.Collapsed; |
| 59 | if (reply.text) { |
| 60 | const newComment = new NoteComment(reply.text, vscode.CommentMode.Preview, { name: 'vscode' }, thread); |
| 61 | thread.comments = [...thread.comments, newComment].map(comment => { |
| 62 | comment.label = undefined; |
| 63 | return comment; |
| 64 | }); |
| 65 | } |
| 66 | })); |
| 67 | |
| 68 | context.subscriptions.push(vscode.commands.registerCommand('mywiki.deleteNoteComment', (comment: NoteComment) => { |
| 69 | const thread = comment.parent; |
| 70 | if (!thread) { |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | thread.comments = thread.comments.filter(cmt => (cmt as NoteComment).id !== comment.id); |
| 75 | |
| 76 | if (thread.comments.length === 0) { |
| 77 | thread.dispose(); |
| 78 | } |