extension.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  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. editor().edit((edit) => {
  87. edit.insert(selection().end, ' ');
  88. } )
  89. }
  90. context.subscriptions.push(vscode.commands.registerCommand('fastmd.addLinebreak', addLinebreak));
  91. // ctrl+/
  92. // auto-escape on formatted text?
  93. function escape() {
  94. return replaceSelection(mdEscape(selectedText()));
  95. }
  96. context.subscriptions.push(vscode.commands.registerCommand('fastmd.escape', escape));
  97. // ctrl+h
  98. function headerUp() {
  99. var line = editor().document.lineAt(position().line);
  100. if (!line.text.startsWith('#####')){
  101. editor().edit((edit) => {
  102. return edit.insert(new vscode.Position(position().line, 0), (line.text.startsWith('#') ? '#' : '# '));
  103. } )
  104. }
  105. }
  106. context.subscriptions.push(vscode.commands.registerCommand('fastmd.headerUp', headerUp));
  107. // ctrl+shift+h
  108. function headerDown() {
  109. var line = editor().document.lineAt(position().line);
  110. if (line.text.startsWith('#')){
  111. editor().edit((edit) => {
  112. return edit.delete(new vscode.Range(new vscode.Position(position().line, 0),
  113. new vscode.Position(position().line, (line.text.startsWith('# ') ? 2 : 1))));
  114. } )
  115. }
  116. }
  117. context.subscriptions.push(vscode.commands.registerCommand('fastmd.headerDown', headerDown));
  118. function setHeader(level) {
  119. var line = editor().document.lineAt(position().line);
  120. editor().edit((edit) => {
  121. return edit.replace(new vscode.Range(new vscode.Position(position().line, 0),
  122. new vscode.Position(position().line,
  123. line.text.search(/[^\s#]/))),
  124. '#'.repeat(level) + ' ');
  125. });
  126. }
  127. // ctrl+shift+1
  128. function setH1() {
  129. setHeader(1);
  130. }
  131. context.subscriptions.push(vscode.commands.registerCommand('fastmd.setH1', setH1));
  132. // ctrl+h 2
  133. function setH2() {
  134. setHeader(2);
  135. }
  136. context.subscriptions.push(vscode.commands.registerCommand('fastmd.setH2', setH2));
  137. // ctrl+h 3
  138. function setH3() {
  139. setHeader(3);
  140. }
  141. context.subscriptions.push(vscode.commands.registerCommand('fastmd.setH3', setH3));
  142. // ctrl+h 4
  143. function setH4() {
  144. setHeader(4);
  145. }
  146. context.subscriptions.push(vscode.commands.registerCommand('fastmd.setH4', setH4));
  147. // ctrl+h 5
  148. function setH5() {
  149. setHeader(5);
  150. }
  151. context.subscriptions.push(vscode.commands.registerCommand('fastmd.setH5', setH5));
  152. // ctrl+i
  153. function toggleItalic() {
  154. // permettre de choisir le symbole dans les params
  155. // voir à mieux prendre en compte les paramètres spéciaux (inclure ou exclure selon les cas)
  156. extendSelection(/\S+/);
  157. toggleSurrounding('*');
  158. }
  159. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleItalic', toggleItalic));
  160. // ctrl+b
  161. function toggleBold() {
  162. // voir à mieux prendre en compte les paramètres spéciaux (inclure ou exclure selon les cas)
  163. extendSelection(/\S+/);
  164. toggleSurrounding('**');
  165. }
  166. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleBold', toggleBold));
  167. // ctrl+alt+s
  168. function toggleStrikethrough() {
  169. extendSelection(/\S+/);
  170. toggleSurrounding('~~');
  171. }
  172. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleStrikethrough', toggleStrikethrough));
  173. // ctrl+l
  174. function toggleLink() {
  175. vscode.window.showInformationMessage('link');
  176. }
  177. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleLink', toggleLink));
  178. // ctrl+num
  179. function toggleNumRefLink() {
  180. vscode.window.showInformationMessage('ref-link');
  181. }
  182. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleNumRefLink', toggleNumRefLink));
  183. // ctrl+g
  184. function toggleImageLink() {
  185. vscode.window.showInformationMessage('img-link');
  186. }
  187. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleImageLink', toggleImageLink));
  188. // ctrl+q
  189. function toggleBlockquote() {
  190. vscode.window.showInformationMessage('quote');
  191. }
  192. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleBlockquote', toggleBlockquote));
  193. // ctrl+r
  194. function insertHRule() {
  195. vscode.window.showInformationMessage('hrule');
  196. }
  197. context.subscriptions.push(vscode.commands.registerCommand('fastmd.insertHRule', insertHRule));
  198. // ctrl+k
  199. function toggleCodeblock() {
  200. vscode.window.showInformationMessage('codeblock');
  201. }
  202. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleCodeblock', toggleCodeblock));
  203. // none
  204. function togglePyCodeblock() {
  205. vscode.window.showInformationMessage('codeblock');
  206. }
  207. context.subscriptions.push(vscode.commands.registerCommand('fastmd.togglePyCodeblock', togglePyCodeblock));
  208. // none
  209. function toggleJsCodeblock() {
  210. vscode.window.showInformationMessage('codeblock');
  211. }
  212. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleJsCodeblock', toggleJsCodeblock));
  213. /// ctrl+u
  214. function toggleUList() {
  215. vscode.window.showInformationMessage('list');
  216. }
  217. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleUList', toggleUList));
  218. // ctrl+o
  219. function toggleOList() {
  220. vscode.window.showInformationMessage('num-list');
  221. }
  222. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleOList', toggleOList));
  223. // ctrl++
  224. function toggleChecklist() {
  225. vscode.window.showInformationMessage('checklist');
  226. }
  227. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleChecklist', toggleChecklist));
  228. // alt+c
  229. function check() {
  230. vscode.window.showInformationMessage('check');
  231. }
  232. context.subscriptions.push(vscode.commands.registerCommand('fastmd.check', check));
  233. // ctrl+t
  234. function insertTable() {
  235. vscode.window.showInformationMessage('table');
  236. }
  237. context.subscriptions.push(vscode.commands.registerCommand('fastmd.insertTable', insertTable));
  238. // ctrl+right
  239. function tableAddCol() {
  240. vscode.window.showInformationMessage('add-col');
  241. }
  242. context.subscriptions.push(vscode.commands.registerCommand('fastmd.tableAddCol', tableAddCol));
  243. // ctrl+bottom
  244. function tableAddRow() {
  245. vscode.window.showInformationMessage('add-row');
  246. }
  247. context.subscriptions.push(vscode.commands.registerCommand('fastmd.tableAddRow', tableAddRow));
  248. // ctrl+alt+p
  249. function togglePreview() {
  250. vscode.window.showInformationMessage('preview');
  251. }
  252. context.subscriptions.push(vscode.commands.registerCommand('fastmd.togglePreview', togglePreview));
  253. }
  254. exports.activate = activate;
  255. function deactivate() {}
  256. module.exports = {
  257. activate,
  258. deactivate
  259. }