'use strict' ; import * as vscode from 'vscode' ; let commentId = 1 ; class NoteComment implements vscode . Comment { id : number ; label : string | undefined ; constructor ( public body , public mode , public author , public parent ? , public contextValue ? ) { this . id = ++ commentId ; } } export function activate ( context ) { const commentController = vscode . comments . createCommentController ( 'comment-sample' , 'Comment API Sample' ) ; context . subscriptions . push ( commentController ) ; commentController . commentingRangeProvider = { provideCommentingRanges : ( document , token ) => { const lineCount = document . lineCount ; return [ new vscode . Range ( 0 , 0 , lineCount - 1 , 0 ) ] ; } } ; context . subscriptions . push ( vscode . commands . registerCommand ( 'mywiki.createNote' , ( reply ) => { replyNote ( reply ) ; } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'mywiki.replyNote' , ( reply ) => { replyNote ( reply ) ; } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'mywiki.startDraft' , ( reply ) => { const thread = reply . thread ; thread . contextValue = 'draft' ; const newComment = new NoteComment ( reply . text , vscode . CommentMode . Preview , { name : 'vscode' } , thread ) ; newComment . label = 'pending' ; thread . comments = [ ... thread . comments , newComment ] ; } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'mywiki.finishDraft' , ( reply ) => { const thread = reply . thread ; if ( ! thread ) { return ; } thread . contextValue = undefined ; thread . collapsibleState = vscode . CommentThreadCollapsibleState . Collapsed ; if ( reply . text ) { const newComment = new NoteComment ( reply . text , vscode . CommentMode . Preview , { name : 'vscode' } , thread ) ; thread . comments = [ ... thread . comments , newComment ] . map ( comment => { comment . label = undefined ; return comment ; } ) ; } } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'mywiki.deleteNoteComment' , ( comment ) => { const thread = comment . parent ; if ( ! thread ) { return ; } thread . comments = thread . comments . filter ( cmt => ( cmt as NoteComment ) . id !== comment . id ) ; if ( thread . comments . length === 0 ) { thread . dispose ( ) ; } } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'mywiki.deleteNote' , ( thread ) => { thread . dispose ( ) ; } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'mywiki.cancelsaveNote' , ( comment ) => { if ( ! comment . parent ) { return ; } comment . parent . comments = comment . parent . comments . map ( cmt => { if ( ( cmt as NoteComment ) . id === comment . id ) { cmt . mode = vscode . CommentMode . Preview ; } return cmt ; } ) ; } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'mywiki.saveNote' , ( comment ) => { if ( ! comment . parent ) { return ; } comment . parent . comments = comment . parent . comments . map ( cmt => { if ( ( cmt as NoteComment ) . id === comment . id ) { cmt . mode = vscode . CommentMode . Preview ; } return cmt ; } ) ; } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'mywiki.editNote' , ( comment ) => { if ( ! comment . parent ) { return ; } comment . parent . comments = comment . parent . comments . map ( cmt => { if ( ( cmt as NoteComment ) . id === comment . id ) { cmt . mode = vscode . CommentMode . Editing ; } return cmt ; } ) ; } ) ) ; context . subscriptions . push ( vscode . commands . registerCommand ( 'mywiki.dispose' , ( ) => { commentController . dispose ( ) ; } ) ) ; function replyNote ( reply ) { const thread = reply . thread ; const newComment = new NoteComment ( reply . text , vscode . CommentMode . Preview , { name : 'vscode' } , thread , thread . comments . length ? 'canDelete' : undefined ) ; if ( thread . contextValue === 'draft' ) { newComment . label = 'pending' ; } thread . comments = [ ... thread . comments , newComment ] ; } }