extension.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. // The module 'vscode' contains the VS Code extensibility API
  2. // Import the module and reference it with the alias vscode in your code below
  3. const vscode = require('vscode');
  4. // this method is called when your extension is activated
  5. // your extension is activated the very first time the command is executed
  6. /**
  7. * @param {vscode.ExtensionContext} context
  8. */
  9. function activate(context) {
  10. // return the current text editor
  11. function editor() { return vscode.window.activeTextEditor }
  12. // return the current selection
  13. function selection() { return editor().selection }
  14. // return the current selected text
  15. function selectedText() { return editor().document.getText(selection()) }
  16. // return the current position of the cursor
  17. function position() { return selection().active }
  18. // set a new position for the cursor
  19. function setPosition(l, c) {
  20. var newPosition = position().with(l, c);
  21. var newSelection = new vscode.Selection(newPosition, newPosition);
  22. editor().selection = newSelection;
  23. return newPosition;
  24. }
  25. // move the current position for the cursor
  26. function movePosition(dl, dc) {
  27. var pos = position();
  28. return setPosition(Math.max(pos.line+dl, 0), Math.max(pos.character+dc, 0));
  29. }
  30. function extendedSelection(pattern) {
  31. var range = editor().document.getWordRangeAtPosition(selection().active, pattern);
  32. return range == null ? null : new vscode.Selection(range.start, range.end);
  33. }
  34. function extendSelection(pattern) {
  35. var newSel = extendedSelection(pattern);
  36. if (newSel) {
  37. editor().selection = newSel;
  38. };
  39. }
  40. // return the text with special characters regex escaped
  41. function reEscape(text) {
  42. return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\\$&');
  43. }
  44. // return the text with special characters markdown escaped
  45. function mdEscape(text) {
  46. return text.replace(/[-[\]{}()*+.\\#`_!]/g, '\\$&');
  47. }
  48. function replaceSelection(with_str) {
  49. editor().edit(edit => edit.replace(selection(), (() => with_str)()));
  50. }
  51. // return true if the selection is surrounded by 'with_str'
  52. function isSurroundedWith(with_str) {
  53. var rx = new RegExp('^' + reEscape(with_str) + '(.*)' + reEscape(with_str) + '$');
  54. return rx.test(selectedText())
  55. }
  56. // surround the current selection with 'with_str'
  57. function surround(with_str) {
  58. var selected = selectedText()
  59. if (selected.length > 0) {
  60. return replaceSelection(with_str + selected + with_str);
  61. }
  62. else {
  63. var newPos = movePosition(0, with_str.length);
  64. return editor().edit((edit) => {
  65. edit.insert(selection().start, with_str + with_str);
  66. } ).then(() => {
  67. editor().selection = new vscode.Selection(newPos, newPos)
  68. } )
  69. }
  70. }
  71. // remove the 'with_str' that is surrounding the selection
  72. function removeSurrounding(with_str) {
  73. var rx = new RegExp('^' + reEscape(with_str) + '(.*)' + reEscape(with_str) + '$');
  74. return replaceSelection(selectedText().match(rx)[1]);
  75. }
  76. function toggleSurrounding(with_str) {
  77. if (isSurroundedWith(with_str)) {
  78. return removeSurrounding(with_str)
  79. }
  80. else {
  81. return surround(with_str);
  82. }
  83. }
  84. // ctrl+enter
  85. function addLinebreak() {
  86. replaceSelection(selectedText() + ' ');
  87. movePosition(0,2);
  88. }
  89. context.subscriptions.push(vscode.commands.registerCommand('fastmd.addLinebreak', addLinebreak));
  90. // ctrl+/
  91. // auto-escape on formatted text?
  92. function escape() {
  93. return replaceSelection(mdEscape(selectedText()));
  94. }
  95. context.subscriptions.push(vscode.commands.registerCommand('fastmd.escape', escape));
  96. // ctrl+h
  97. function headerUp() {
  98. vscode.window.showInformationMessage('up');
  99. }
  100. context.subscriptions.push(vscode.commands.registerCommand('fastmd.headerUp', headerUp));
  101. // ctrl+shift+h
  102. function headerDown() {
  103. vscode.window.showInformationMessage('down');
  104. }
  105. context.subscriptions.push(vscode.commands.registerCommand('fastmd.headerDown', headerDown));
  106. // ctrl+h 1
  107. function toggleH1() {
  108. vscode.window.showInformationMessage('h1');
  109. }
  110. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleH1', toggleH1));
  111. // ctrl+h 2
  112. function toggleH2() {
  113. vscode.window.showInformationMessage('h2');
  114. }
  115. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleH2', toggleH2));
  116. // ctrl+h 3
  117. function toggleH3() {
  118. vscode.window.showInformationMessage('h3');
  119. }
  120. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleH3', toggleH3));
  121. // ctrl+h 4
  122. function toggleH4() {
  123. vscode.window.showInformationMessage('h4');
  124. }
  125. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleH4', toggleH4));
  126. // ctrl+h 5
  127. function toggleH5() {
  128. vscode.window.showInformationMessage('h5');
  129. }
  130. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleH5', toggleH5));
  131. // ctrl+i
  132. function toggleItalic() {
  133. // permettre de choisir le symbole dans les params
  134. // si texte selectionné et non formatté: entoure avec le symbole
  135. // si texte selectionné et déjà formatté: retire les symbole
  136. // si curseur entouré d'espaces: ajoute un double symbole et place le curseur au centre
  137. extendSelection(/\S+/);
  138. toggleSurrounding('*');
  139. }
  140. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleItalic', toggleItalic));
  141. // ctrl+b
  142. function toggleBold() {
  143. extendSelection(/\S+/);
  144. toggleSurrounding('**');
  145. }
  146. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleBold', toggleBold));
  147. // ctrl+b ctrl+i
  148. function toggleCombinedEmphasis() {
  149. vscode.window.showInformationMessage('combined');
  150. }
  151. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleCombinedEmphasis', toggleCombinedEmphasis));
  152. // ctrl+alt+e
  153. function toggleStrikethrough() {
  154. extendSelection(/\S+/);
  155. toggleSurrounding('~~');
  156. }
  157. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleStrikethrough', toggleStrikethrough));
  158. // ctrl+l
  159. function toggleLink() {
  160. vscode.window.showInformationMessage('link');
  161. }
  162. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleLink', toggleLink));
  163. // ctrl+num
  164. function toggleNumRefLink() {
  165. vscode.window.showInformationMessage('ref-link');
  166. }
  167. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleNumRefLink', toggleNumRefLink));
  168. // ctrl+g
  169. function toggleImageLink() {
  170. vscode.window.showInformationMessage('img-link');
  171. }
  172. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleImageLink', toggleImageLink));
  173. // ctrl+q
  174. function toggleBlockquote() {
  175. vscode.window.showInformationMessage('quote');
  176. }
  177. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleBlockquote', toggleBlockquote));
  178. // ctrl+r
  179. function insertHRule() {
  180. vscode.window.showInformationMessage('hrule');
  181. }
  182. context.subscriptions.push(vscode.commands.registerCommand('fastmd.insertHRule', insertHRule));
  183. // ctrl+k
  184. function toggleCodeblock() {
  185. vscode.window.showInformationMessage('codeblock');
  186. }
  187. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleCodeblock', toggleCodeblock));
  188. // none
  189. function togglePyCodeblock() {
  190. vscode.window.showInformationMessage('codeblock');
  191. }
  192. context.subscriptions.push(vscode.commands.registerCommand('fastmd.togglePyCodeblock', togglePyCodeblock));
  193. // none
  194. function toggleJsCodeblock() {
  195. vscode.window.showInformationMessage('codeblock');
  196. }
  197. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleJsCodeblock', toggleJsCodeblock));
  198. /// ctrl+u
  199. function toggleUList() {
  200. vscode.window.showInformationMessage('list');
  201. }
  202. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleUList', toggleUList));
  203. // ctrl+o
  204. function toggleOList() {
  205. vscode.window.showInformationMessage('num-list');
  206. }
  207. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleOList', toggleOList));
  208. // ctrl+t
  209. function insertTable() {
  210. vscode.window.showInformationMessage('table');
  211. }
  212. context.subscriptions.push(vscode.commands.registerCommand('fastmd.insertTable', insertTable));
  213. // ctrl+right
  214. function tableAddCol() {
  215. vscode.window.showInformationMessage('add-col');
  216. }
  217. context.subscriptions.push(vscode.commands.registerCommand('fastmd.tableAddCol', tableAddCol));
  218. // ctrl+bottom
  219. function tableAddRow() {
  220. vscode.window.showInformationMessage('add-row');
  221. }
  222. context.subscriptions.push(vscode.commands.registerCommand('fastmd.tableAddRow', tableAddRow));
  223. // ctrl+alt+p
  224. function togglePreview() {
  225. vscode.window.showInformationMessage('preview');
  226. }
  227. context.subscriptions.push(vscode.commands.registerCommand('fastmd.togglePreview', togglePreview));
  228. }
  229. exports.activate = activate;
  230. function deactivate() {}
  231. module.exports = {
  232. activate,
  233. deactivate
  234. }