Files
vscode-extension-samples/comment-sample/src/extension.ts

110 lines
3.6 KiB
TypeScript
Raw Normal View History

'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;
}
}
export function activate(context: vscode.ExtensionContext) {
// A `CommentController` is able to provide comments for documents.
2019-05-21 12:03:32 -07:00
const commentController = vscode.comments.createCommentController('comment-sample', 'Comment API Sample');
context.subscriptions.push(commentController);
2019-05-17 18:39:54 -07:00
vscode.commands.executeCommand('setContext', 'inDraft', false);
// 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-21 12:03:32 -07:00
2019-05-17 18:31:33 -07:00
context.subscriptions.push(vscode.commands.registerCommand('mywiki.createNote', (reply: vscode.CommentReply) => {
2019-05-17 18:52:59 -07:00
replyNote(reply);
2019-05-17 18:31:33 -07:00
}));
2019-05-21 12:03:32 -07:00
2019-05-17 18:31:33 -07:00
context.subscriptions.push(vscode.commands.registerCommand('mywiki.replyNote', (reply: vscode.CommentReply) => {
2019-05-17 18:52:59 -07:00
replyNote(reply);
2019-05-17 18:31:33 -07:00
}));
2019-05-21 12:03:32 -07:00
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-21 12:03:32 -07:00
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-21 12:03:32 -07:00
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-21 12:03:32 -07:00
2019-05-17 16:40:30 -07:00
if (thread.comments.length === 0) {
thread.dispose();
}
}));
context.subscriptions.push(vscode.commands.registerCommand('mywiki.deleteNote', (thread: vscode.CommentThread) => {
thread.dispose();
}));
2019-05-21 12:03:32 -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) => {
2019-05-21 12:03:32 -07:00
if (cmt.id === comment.id) {
2019-05-17 18:31:33 -07:00
cmt.mode = vscode.CommentMode.Preview;
}
2019-05-21 12:03:32 -07:00
2019-05-17 18:31:33 -07:00
return cmt;
});
}));
2019-05-21 12:03:32 -07:00
2019-05-17 18:31:33 -07:00
context.subscriptions.push(vscode.commands.registerCommand('mywiki.saveNote', (comment: NoteComment) => {
comment.parent.comments = comment.parent.comments.map((cmt: NoteComment) => {
2019-05-21 12:03:32 -07:00
if (cmt.id === comment.id) {
2019-05-17 18:31:33 -07:00
cmt.mode = vscode.CommentMode.Preview;
}
2019-05-21 12:03:32 -07:00
2019-05-17 18:31:33 -07:00
return cmt;
});
}));
2019-05-21 12:03:32 -07:00
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) => {
2019-05-21 12:03:32 -07:00
if (cmt.id === comment.id) {
2019-05-17 16:40:30 -07:00
cmt.mode = vscode.CommentMode.Editing;
}
2019-05-21 12:03:32 -07:00
2019-05-17 16:40:30 -07:00
return cmt;
});
}));
2019-05-17 18:52:59 -07:00
function 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];
}
}