extension.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. // ctrl+h 1
  119. function toggleH1() {
  120. vscode.window.showInformationMessage('h1');
  121. }
  122. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleH1', toggleH1));
  123. // ctrl+h 2
  124. function toggleH2() {
  125. vscode.window.showInformationMessage('h2');
  126. }
  127. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleH2', toggleH2));
  128. // ctrl+h 3
  129. function toggleH3() {
  130. vscode.window.showInformationMessage('h3');
  131. }
  132. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleH3', toggleH3));
  133. // ctrl+h 4
  134. function toggleH4() {
  135. vscode.window.showInformationMessage('h4');
  136. }
  137. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleH4', toggleH4));
  138. // ctrl+h 5
  139. function toggleH5() {
  140. vscode.window.showInformationMessage('h5');
  141. }
  142. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleH5', toggleH5));
  143. // ctrl+i
  144. function toggleItalic() {
  145. // permettre de choisir le symbole dans les params
  146. // si texte selectionné et non formatté: entoure avec le symbole
  147. // si texte selectionné et déjà formatté: retire les symbole
  148. // si curseur entouré d'espaces: ajoute un double symbole et place le curseur au centre
  149. extendSelection(/\S+/);
  150. toggleSurrounding('*');
  151. }
  152. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleItalic', toggleItalic));
  153. // ctrl+b
  154. function toggleBold() {
  155. extendSelection(/\S+/);
  156. toggleSurrounding('**');
  157. }
  158. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleBold', toggleBold));
  159. // ctrl+b ctrl+i
  160. function toggleCombinedEmphasis() {
  161. vscode.window.showInformationMessage('combined');
  162. }
  163. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleCombinedEmphasis', toggleCombinedEmphasis));
  164. // ctrl+alt+e
  165. function toggleStrikethrough() {
  166. extendSelection(/\S+/);
  167. toggleSurrounding('~~');
  168. }
  169. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleStrikethrough', toggleStrikethrough));
  170. // ctrl+l
  171. function toggleLink() {
  172. vscode.window.showInformationMessage('link');
  173. }
  174. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleLink', toggleLink));
  175. // ctrl+num
  176. function toggleNumRefLink() {
  177. vscode.window.showInformationMessage('ref-link');
  178. }
  179. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleNumRefLink', toggleNumRefLink));
  180. // ctrl+g
  181. function toggleImageLink() {
  182. vscode.window.showInformationMessage('img-link');
  183. }
  184. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleImageLink', toggleImageLink));
  185. // ctrl+q
  186. function toggleBlockquote() {
  187. vscode.window.showInformationMessage('quote');
  188. }
  189. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleBlockquote', toggleBlockquote));
  190. // ctrl+r
  191. function insertHRule() {
  192. vscode.window.showInformationMessage('hrule');
  193. }
  194. context.subscriptions.push(vscode.commands.registerCommand('fastmd.insertHRule', insertHRule));
  195. // ctrl+k
  196. function toggleCodeblock() {
  197. vscode.window.showInformationMessage('codeblock');
  198. }
  199. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleCodeblock', toggleCodeblock));
  200. // none
  201. function togglePyCodeblock() {
  202. vscode.window.showInformationMessage('codeblock');
  203. }
  204. context.subscriptions.push(vscode.commands.registerCommand('fastmd.togglePyCodeblock', togglePyCodeblock));
  205. // none
  206. function toggleJsCodeblock() {
  207. vscode.window.showInformationMessage('codeblock');
  208. }
  209. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleJsCodeblock', toggleJsCodeblock));
  210. /// ctrl+u
  211. function toggleUList() {
  212. vscode.window.showInformationMessage('list');
  213. }
  214. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleUList', toggleUList));
  215. // ctrl+o
  216. function toggleOList() {
  217. vscode.window.showInformationMessage('num-list');
  218. }
  219. context.subscriptions.push(vscode.commands.registerCommand('fastmd.toggleOList', toggleOList));
  220. // ctrl+t
  221. function insertTable() {
  222. vscode.window.showInformationMessage('table');
  223. }
  224. context.subscriptions.push(vscode.commands.registerCommand('fastmd.insertTable', insertTable));
  225. // ctrl+right
  226. function tableAddCol() {
  227. vscode.window.showInformationMessage('add-col');
  228. }
  229. context.subscriptions.push(vscode.commands.registerCommand('fastmd.tableAddCol', tableAddCol));
  230. // ctrl+bottom
  231. function tableAddRow() {
  232. vscode.window.showInformationMessage('add-row');
  233. }
  234. context.subscriptions.push(vscode.commands.registerCommand('fastmd.tableAddRow', tableAddRow));
  235. // ctrl+alt+p
  236. function togglePreview() {
  237. vscode.window.showInformationMessage('preview');
  238. }
  239. context.subscriptions.push(vscode.commands.registerCommand('fastmd.togglePreview', togglePreview));
  240. }
  241. exports.activate = activate;
  242. function deactivate() {}
  243. module.exports = {
  244. activate,
  245. deactivate
  246. }