2019-03-29 19:31:06 +01:00
|
|
|
'use strict';
|
|
|
|
|
|
|
|
|
|
import * as vscode from 'vscode';
|
|
|
|
|
|
2019-05-17 16:40:30 -07:00
|
|
|
let commentId = 1;
|
|
|
|
|
|
|
|
|
|
class NoteComment implements vscode.Comment {
|
|
|
|
|
id: number;
|
2019-05-17 18:36:36 -07:00
|
|
|
label: string | undefined;
|
2019-05-17 16:40:30 -07:00
|
|
|
constructor(
|
|
|
|
|
public body: string | vscode.MarkdownString,
|
|
|
|
|
public mode: vscode.CommentMode,
|
|
|
|
|
public author: vscode.CommentAuthorInformation,
|
|
|
|
|
public parent?: vscode.CommentThread
|
|
|
|
|
) {
|
|
|
|
|
this.id = ++commentId;
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-03-29 19:31:06 +01:00
|
|
|
|
|
|
|
|
export function activate(context: vscode.ExtensionContext) {
|
|
|
|
|
// A `CommentController` is able to provide comments for documents.
|
|
|
|
|
const commentController = vscode.comment.createCommentController('comment-sample', 'Comment API Sample');
|
|
|
|
|
context.subscriptions.push(commentController);
|
2019-05-17 18:39:54 -07:00
|
|
|
vscode.commands.executeCommand('setContext', 'inDraft', false);
|
2019-03-29 19:31:06 +01:00
|
|
|
|
|
|
|
|
// commenting range provider
|
|
|
|
|
commentController.commentingRangeProvider = {
|
|
|
|
|
provideCommentingRanges: (document: vscode.TextDocument, token: vscode.CancellationToken) => {
|
|
|
|
|
let lineCount = document.lineCount;
|
|
|
|
|
return [new vscode.Range(0, 0, lineCount - 1, 0)];
|
2019-04-30 15:08:11 -07:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
2019-05-17 18:31:33 -07:00
|
|
|
context.subscriptions.push(vscode.commands.registerCommand('mywiki.createNote', (reply: vscode.CommentReply) => {
|
2019-05-17 16:40:30 -07:00
|
|
|
let thread = reply.thread;
|
|
|
|
|
let newComment = new NoteComment(reply.text, vscode.CommentMode.Preview, { name: 'vscode' }, thread);
|
|
|
|
|
thread.comments = [...thread.comments, newComment];
|
2019-05-17 18:31:33 -07:00
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
context.subscriptions.push(vscode.commands.registerCommand('mywiki.replyNote', (reply: vscode.CommentReply) => {
|
|
|
|
|
let thread = reply.thread;
|
|
|
|
|
let newComment = new NoteComment(reply.text, vscode.CommentMode.Preview, { name: 'vscode' }, thread);
|
|
|
|
|
thread.comments = [...thread.comments, newComment];
|
|
|
|
|
}));
|
|
|
|
|
|
2019-05-17 18:36:36 -07:00
|
|
|
context.subscriptions.push(vscode.commands.registerCommand('mywiki.startDraft', (reply: vscode.CommentReply) => {
|
2019-05-17 18:39:54 -07:00
|
|
|
vscode.commands.executeCommand('setContext', 'inDraft', true);
|
2019-05-17 18:36:36 -07:00
|
|
|
let thread = reply.thread;
|
|
|
|
|
let newComment = new NoteComment(reply.text, vscode.CommentMode.Preview, { name: 'vscode' }, thread);
|
|
|
|
|
newComment.label = 'pending';
|
|
|
|
|
thread.comments = [...thread.comments, newComment];
|
|
|
|
|
}));
|
|
|
|
|
|
2019-05-17 18:39:54 -07:00
|
|
|
context.subscriptions.push(vscode.commands.registerCommand('mywiki.finishDraft', (reply: vscode.CommentReply) => {
|
|
|
|
|
vscode.commands.executeCommand('setContext', 'inDraft', false);
|
|
|
|
|
|
|
|
|
|
let thread = reply.thread;
|
|
|
|
|
let newComment = new NoteComment(reply.text, vscode.CommentMode.Preview, { name: 'vscode' }, thread);
|
|
|
|
|
thread.comments = [...thread.comments, newComment].map(comment => {
|
|
|
|
|
comment.label = undefined;
|
|
|
|
|
return comment;
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
2019-05-17 16:40:30 -07:00
|
|
|
context.subscriptions.push(vscode.commands.registerCommand('mywiki.deleteNoteComment', (comment: NoteComment) => {
|
|
|
|
|
let thread = comment.parent;
|
2019-05-17 18:31:33 -07:00
|
|
|
thread.comments = thread.comments.filter((cmt: NoteComment) => cmt.id !== comment.id);
|
2019-05-17 16:40:30 -07:00
|
|
|
|
|
|
|
|
if (thread.comments.length === 0) {
|
|
|
|
|
thread.dispose();
|
2019-03-29 19:31:06 +01:00
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
context.subscriptions.push(vscode.commands.registerCommand('mywiki.deleteNote', (thread: vscode.CommentThread) => {
|
|
|
|
|
thread.dispose();
|
|
|
|
|
}));
|
2019-05-17 16:40:30 -07:00
|
|
|
|
2019-05-17 18:31:33 -07:00
|
|
|
context.subscriptions.push(vscode.commands.registerCommand('mywiki.cancelsaveNote', (comment: NoteComment) => {
|
|
|
|
|
comment.parent.comments = comment.parent.comments.map((cmt: NoteComment) => {
|
|
|
|
|
if (cmt.id === comment.id) {
|
|
|
|
|
cmt.mode = vscode.CommentMode.Preview;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cmt;
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
context.subscriptions.push(vscode.commands.registerCommand('mywiki.saveNote', (comment: NoteComment) => {
|
|
|
|
|
comment.parent.comments = comment.parent.comments.map((cmt: NoteComment) => {
|
|
|
|
|
if (cmt.id === comment.id) {
|
|
|
|
|
cmt.mode = vscode.CommentMode.Preview;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cmt;
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
2019-05-17 16:40:30 -07:00
|
|
|
context.subscriptions.push(vscode.commands.registerCommand('mywiki.editNote', (comment: NoteComment) => {
|
|
|
|
|
comment.parent.comments = comment.parent.comments.map((cmt: NoteComment) => {
|
|
|
|
|
if (cmt.id === comment.id) {
|
|
|
|
|
cmt.mode = vscode.CommentMode.Editing;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return cmt;
|
|
|
|
|
});
|
|
|
|
|
}));
|
2019-03-29 19:31:06 +01:00
|
|
|
}
|