extension.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. // si texte selectionné et non formatté: entoure avec le symbole
  156. // si texte selectionné et déjà formatté: retire les symbole
  157. // si curseur entouré d'espaces: ajoute un double symbole et place le curseur au centre
  158. extendSelection(/\S+/);
  159. toggleSurrounding('*');
  160. }
  161. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleItalic', toggleItalic));
  162. // ctrl+b
  163. function toggleBold() {
  164. extendSelection(/\S+/);
  165. toggleSurrounding('**');
  166. }
  167. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleBold', toggleBold));
  168. // ctrl+b ctrl+i
  169. function toggleCombinedEmphasis() {
  170. vscode.window.showInformationMessage('combined');
  171. }
  172. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleCombinedEmphasis', toggleCombinedEmphasis));
  173. // ctrl+alt+e
  174. function toggleStrikethrough() {
  175. extendSelection(/\S+/);
  176. toggleSurrounding('~~');
  177. }
  178. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleStrikethrough', toggleStrikethrough));
  179. // ctrl+l
  180. function toggleLink() {
  181. vscode.window.showInformationMessage('link');
  182. }
  183. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleLink', toggleLink));
  184. // ctrl+num
  185. function toggleNumRefLink() {
  186. vscode.window.showInformationMessage('ref-link');
  187. }
  188. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleNumRefLink', toggleNumRefLink));
  189. // ctrl+g
  190. function toggleImageLink() {
  191. vscode.window.showInformationMessage('img-link');
  192. }
  193. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleImageLink', toggleImageLink));
  194. // ctrl+q
  195. function toggleBlockquote() {
  196. vscode.window.showInformationMessage('quote');
  197. }
  198. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleBlockquote', toggleBlockquote));
  199. // ctrl+r
  200. function insertHRule() {
  201. vscode.window.showInformationMessage('hrule');
  202. }
  203. context.subscriptions.push(vscode.commands.registerCommand('fastmd.insertHRule', insertHRule));
  204. // ctrl+k
  205. function toggleCodeblock() {
  206. vscode.window.showInformationMessage('codeblock');
  207. }
  208. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleCodeblock', toggleCodeblock));
  209. // none
  210. function togglePyCodeblock() {
  211. vscode.window.showInformationMessage('codeblock');
  212. }
  213. context.subscriptions.push(vscode.commands.registerCommand('fastmd.togglePyCodeblock', togglePyCodeblock));
  214. // none
  215. function toggleJsCodeblock() {
  216. vscode.window.showInformationMessage('codeblock');
  217. }
  218. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleJsCodeblock', toggleJsCodeblock));
  219. /// ctrl+u
  220. function toggleUList() {
  221. vscode.window.showInformationMessage('list');
  222. }
  223. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleUList', toggleUList));
  224. // ctrl+o
  225. function toggleOList() {
  226. vscode.window.showInformationMessage('num-list');
  227. }
  228. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleOList', toggleOList));
  229. // ctrl+t
  230. function insertTable() {
  231. vscode.window.showInformationMessage('table');
  232. }
  233. context.subscriptions.push(vscode.commands.registerCommand('fastmd.insertTable', insertTable));
  234. // ctrl+right
  235. function tableAddCol() {
  236. vscode.window.showInformationMessage('add-col');
  237. }
  238. context.subscriptions.push(vscode.commands.registerCommand('fastmd.tableAddCol', tableAddCol));
  239. // ctrl+bottom
  240. function tableAddRow() {
  241. vscode.window.showInformationMessage('add-row');
  242. }
  243. context.subscriptions.push(vscode.commands.registerCommand('fastmd.tableAddRow', tableAddRow));
  244. // ctrl+alt+p
  245. function togglePreview() {
  246. vscode.window.showInformationMessage('preview');
  247. }
  248. context.subscriptions.push(vscode.commands.registerCommand('fastmd.togglePreview', togglePreview));
  249. }
  250. exports.activate = activate;
  251. function deactivate() {}
  252. module.exports = {
  253. activate,
  254. deactivate
  255. }