selection-spec.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. const TextEditor = require('../src/text-editor');
  2. describe('Selection', () => {
  3. let buffer, editor, selection;
  4. beforeEach(() => {
  5. buffer = atom.project.bufferForPathSync('sample.js');
  6. editor = new TextEditor({ buffer, tabLength: 2 });
  7. selection = editor.getLastSelection();
  8. });
  9. afterEach(() => buffer.destroy());
  10. describe('.deleteSelectedText()', () => {
  11. describe('when nothing is selected', () => {
  12. it('deletes nothing', () => {
  13. selection.setBufferRange([[0, 3], [0, 3]]);
  14. selection.deleteSelectedText();
  15. expect(buffer.lineForRow(0)).toBe('var quicksort = function () {');
  16. });
  17. });
  18. describe('when one line is selected', () => {
  19. it('deletes selected text and clears the selection', () => {
  20. selection.setBufferRange([[0, 4], [0, 14]]);
  21. selection.deleteSelectedText();
  22. expect(buffer.lineForRow(0)).toBe('var = function () {');
  23. const endOfLine = buffer.lineForRow(0).length;
  24. selection.setBufferRange([[0, 0], [0, endOfLine]]);
  25. selection.deleteSelectedText();
  26. expect(buffer.lineForRow(0)).toBe('');
  27. expect(selection.isEmpty()).toBeTruthy();
  28. });
  29. });
  30. describe('when multiple lines are selected', () => {
  31. it('deletes selected text and clears the selection', () => {
  32. selection.setBufferRange([[0, 1], [2, 39]]);
  33. selection.deleteSelectedText();
  34. expect(buffer.lineForRow(0)).toBe('v;');
  35. expect(selection.isEmpty()).toBeTruthy();
  36. });
  37. });
  38. describe('when the cursor precedes the tail', () => {
  39. it('deletes selected text and clears the selection', () => {
  40. selection.cursor.setScreenPosition([0, 13]);
  41. selection.selectToScreenPosition([0, 4]);
  42. selection.delete();
  43. expect(buffer.lineForRow(0)).toBe('var = function () {');
  44. expect(selection.isEmpty()).toBeTruthy();
  45. });
  46. });
  47. });
  48. describe('.isReversed()', () => {
  49. it('returns true if the cursor precedes the tail', () => {
  50. selection.cursor.setScreenPosition([0, 20]);
  51. selection.selectToScreenPosition([0, 10]);
  52. expect(selection.isReversed()).toBeTruthy();
  53. selection.selectToScreenPosition([0, 25]);
  54. expect(selection.isReversed()).toBeFalsy();
  55. });
  56. });
  57. describe('.selectLine(row)', () => {
  58. describe('when passed a row', () => {
  59. it('selects the specified row', () => {
  60. selection.setBufferRange([[2, 4], [3, 4]]);
  61. selection.selectLine(5);
  62. expect(selection.getBufferRange()).toEqual([[5, 0], [6, 0]]);
  63. });
  64. });
  65. describe('when not passed a row', () => {
  66. it('selects all rows spanned by the selection', () => {
  67. selection.setBufferRange([[2, 4], [3, 4]]);
  68. selection.selectLine();
  69. expect(selection.getBufferRange()).toEqual([[2, 0], [4, 0]]);
  70. });
  71. });
  72. });
  73. describe("when the selection's range is moved", () => {
  74. it('notifies ::onDidChangeRange observers', () => {
  75. selection.setBufferRange([[2, 0], [2, 10]]);
  76. const changeScreenRangeHandler = jasmine.createSpy(
  77. 'changeScreenRangeHandler'
  78. );
  79. selection.onDidChangeRange(changeScreenRangeHandler);
  80. buffer.insert([2, 5], 'abc');
  81. expect(changeScreenRangeHandler).toHaveBeenCalled();
  82. expect(
  83. changeScreenRangeHandler.mostRecentCall.args[0]
  84. ).not.toBeUndefined();
  85. });
  86. });
  87. describe("when only the selection's tail is moved (regression)", () => {
  88. it('notifies ::onDidChangeRange observers', () => {
  89. selection.setBufferRange([[2, 0], [2, 10]], { reversed: true });
  90. const changeScreenRangeHandler = jasmine.createSpy(
  91. 'changeScreenRangeHandler'
  92. );
  93. selection.onDidChangeRange(changeScreenRangeHandler);
  94. buffer.insert([2, 5], 'abc');
  95. expect(changeScreenRangeHandler).toHaveBeenCalled();
  96. expect(
  97. changeScreenRangeHandler.mostRecentCall.args[0]
  98. ).not.toBeUndefined();
  99. });
  100. });
  101. describe('when the selection is destroyed', () => {
  102. it('destroys its marker', () => {
  103. selection.setBufferRange([[2, 0], [2, 10]]);
  104. const { marker } = selection;
  105. selection.destroy();
  106. expect(marker.isDestroyed()).toBeTruthy();
  107. });
  108. });
  109. describe('.insertText(text, options)', () => {
  110. it('allows pasting white space only lines when autoIndent is enabled', () => {
  111. selection.setBufferRange([[0, 0], [0, 0]]);
  112. selection.insertText(' \n \n\n', { autoIndent: true });
  113. expect(buffer.lineForRow(0)).toBe(' ');
  114. expect(buffer.lineForRow(1)).toBe(' ');
  115. expect(buffer.lineForRow(2)).toBe('');
  116. });
  117. it('auto-indents if only a newline is inserted', () => {
  118. selection.setBufferRange([[2, 0], [3, 0]]);
  119. selection.insertText('\n', { autoIndent: true });
  120. expect(buffer.lineForRow(2)).toBe(' ');
  121. });
  122. it('auto-indents if only a carriage return + newline is inserted', () => {
  123. selection.setBufferRange([[2, 0], [3, 0]]);
  124. selection.insertText('\r\n', { autoIndent: true });
  125. expect(buffer.lineForRow(2)).toBe(' ');
  126. });
  127. it('does not adjust the indent of trailing lines if preserveTrailingLineIndentation is true', () => {
  128. selection.setBufferRange([[5, 0], [5, 0]]);
  129. selection.insertText(' foo\n bar\n', {
  130. preserveTrailingLineIndentation: true,
  131. indentBasis: 1
  132. });
  133. expect(buffer.lineForRow(6)).toBe(' bar');
  134. });
  135. });
  136. describe('.fold()', () => {
  137. it('folds the buffer range spanned by the selection', () => {
  138. selection.setBufferRange([[0, 3], [1, 6]]);
  139. selection.fold();
  140. expect(selection.getScreenRange()).toEqual([[0, 4], [0, 4]]);
  141. expect(selection.getBufferRange()).toEqual([[1, 6], [1, 6]]);
  142. expect(editor.lineTextForScreenRow(0)).toBe(
  143. `var${editor.displayLayer.foldCharacter}sort = function(items) {`
  144. );
  145. expect(editor.isFoldedAtBufferRow(0)).toBe(true);
  146. });
  147. it("doesn't create a fold when the selection is empty", () => {
  148. selection.setBufferRange([[0, 3], [0, 3]]);
  149. selection.fold();
  150. expect(selection.getScreenRange()).toEqual([[0, 3], [0, 3]]);
  151. expect(selection.getBufferRange()).toEqual([[0, 3], [0, 3]]);
  152. expect(editor.lineTextForScreenRow(0)).toBe(
  153. 'var quicksort = function () {'
  154. );
  155. expect(editor.isFoldedAtBufferRow(0)).toBe(false);
  156. });
  157. });
  158. describe('within a read-only editor', () => {
  159. beforeEach(() => {
  160. editor.setReadOnly(true);
  161. selection.setBufferRange([[0, 0], [0, 13]]);
  162. });
  163. const modifications = [
  164. {
  165. name: 'insertText',
  166. op: opts => selection.insertText('yes', opts)
  167. },
  168. {
  169. name: 'backspace',
  170. op: opts => selection.backspace(opts)
  171. },
  172. {
  173. name: 'deleteToPreviousWordBoundary',
  174. op: opts => selection.deleteToPreviousWordBoundary(opts)
  175. },
  176. {
  177. name: 'deleteToNextWordBoundary',
  178. op: opts => selection.deleteToNextWordBoundary(opts)
  179. },
  180. {
  181. name: 'deleteToBeginningOfWord',
  182. op: opts => selection.deleteToBeginningOfWord(opts)
  183. },
  184. {
  185. name: 'deleteToBeginningOfLine',
  186. op: opts => selection.deleteToBeginningOfLine(opts)
  187. },
  188. {
  189. name: 'delete',
  190. op: opts => selection.delete(opts)
  191. },
  192. {
  193. name: 'deleteToEndOfLine',
  194. op: opts => selection.deleteToEndOfLine(opts)
  195. },
  196. {
  197. name: 'deleteToEndOfWord',
  198. op: opts => selection.deleteToEndOfWord(opts)
  199. },
  200. {
  201. name: 'deleteToBeginningOfSubword',
  202. op: opts => selection.deleteToBeginningOfSubword(opts)
  203. },
  204. {
  205. name: 'deleteToEndOfSubword',
  206. op: opts => selection.deleteToEndOfSubword(opts)
  207. },
  208. {
  209. name: 'deleteSelectedText',
  210. op: opts => selection.deleteSelectedText(opts)
  211. },
  212. {
  213. name: 'deleteLine',
  214. op: opts => selection.deleteLine(opts)
  215. },
  216. {
  217. name: 'joinLines',
  218. op: opts => selection.joinLines(opts)
  219. },
  220. {
  221. name: 'outdentSelectedRows',
  222. op: opts => selection.outdentSelectedRows(opts)
  223. },
  224. {
  225. name: 'autoIndentSelectedRows',
  226. op: opts => selection.autoIndentSelectedRows(opts)
  227. },
  228. {
  229. name: 'toggleLineComments',
  230. op: opts => selection.toggleLineComments(opts)
  231. },
  232. {
  233. name: 'cutToEndOfLine',
  234. op: opts => selection.cutToEndOfLine(false, opts)
  235. },
  236. {
  237. name: 'cutToEndOfBufferLine',
  238. op: opts => selection.cutToEndOfBufferLine(false, opts)
  239. },
  240. {
  241. name: 'cut',
  242. op: opts => selection.cut(false, false, opts.bypassReadOnly)
  243. },
  244. {
  245. name: 'indent',
  246. op: opts => selection.indent(opts)
  247. },
  248. {
  249. name: 'indentSelectedRows',
  250. op: opts => selection.indentSelectedRows(opts)
  251. }
  252. ];
  253. describe('without bypassReadOnly', () => {
  254. for (const { name, op } of modifications) {
  255. it(`throws an error on ${name}`, () => {
  256. expect(op).toThrow();
  257. });
  258. }
  259. });
  260. describe('with bypassReadOnly', () => {
  261. for (const { name, op } of modifications) {
  262. it(`permits ${name}`, () => {
  263. op({ bypassReadOnly: true });
  264. });
  265. }
  266. });
  267. });
  268. });