Skip to content

Commit

Permalink
Merge pull request atom#19016 from atom/wl-clipboard-line-endings
Browse files Browse the repository at this point in the history
Convert line endings when copy and pasting
  • Loading branch information
sadick254 authored Sep 30, 2021
2 parents c24056e + c8f3f86 commit 38f403a
Showing 3 changed files with 65 additions and 21 deletions.
34 changes: 29 additions & 5 deletions spec/clipboard-spec.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
describe('Clipboard', () =>
describe('write(text, metadata) and read()', function() {
it('writes and reads text to/from the native clipboard', function() {
describe('Clipboard', () => {
describe('write(text, metadata) and read()', () => {
it('writes and reads text to/from the native clipboard', () => {
expect(atom.clipboard.read()).toBe('initial clipboard content');
atom.clipboard.write('next');
expect(atom.clipboard.read()).toBe('next');
});

return it('returns metadata if the item on the native clipboard matches the last written item', function() {
it('returns metadata if the item on the native clipboard matches the last written item', () => {
atom.clipboard.write('next', { meta: 'data' });
expect(atom.clipboard.read()).toBe('next');
expect(atom.clipboard.readWithMetadata().text).toBe('next');
expect(atom.clipboard.readWithMetadata().metadata).toEqual({
meta: 'data'
});
});
}));
});

describe('line endings', () => {
let originalPlatform = process.platform;

const eols = new Map([
['win32', '\r\n'],
['darwin', '\n'],
['linux', '\n']
]);
for (let [platform, eol] of eols) {
it(`converts line endings to the OS's native line endings on ${platform}`, () => {
Object.defineProperty(process, 'platform', { value: platform });

atom.clipboard.write('next\ndone\r\n\n', { meta: 'data' });
expect(atom.clipboard.readWithMetadata()).toEqual({
text: `next${eol}done${eol}${eol}`,
metadata: { meta: 'data' }
});

Object.defineProperty(process, 'platform', { value: originalPlatform });
});
}
});
});
50 changes: 34 additions & 16 deletions spec/text-editor-spec.js
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ const path = require('path');
const temp = require('temp').track();
const dedent = require('dedent');
const { clipboard } = require('electron');
const os = require('os');
const TextEditor = require('../src/text-editor');
const TextBuffer = require('text-buffer');
const TextMateLanguageMode = require('../src/text-mate-language-mode');
@@ -5051,7 +5052,7 @@ describe('TextEditor', () => {
editor.cutSelectedText();
expect(buffer.lineForRow(0)).toBe('var = function () {');
expect(buffer.lineForRow(1)).toBe(' var = function(items) {');
expect(clipboard.readText()).toBe('quicksort\nsort');
expect(clipboard.readText()).toBe(['quicksort', 'sort'].join(os.EOL));
});

describe('when no text is selected', () => {
@@ -5068,13 +5069,14 @@ describe('TextEditor', () => {
expect(buffer.lineForRow(4)).toBe(
' current < pivot ? left.push(current) : right.push(current);'
);

expect(atom.clipboard.read()).toEqual(
[
'var quicksort = function () {',
'',
' current = items.shift();',
''
].join('\n')
].join(os.EOL)
);
});
});
@@ -5087,7 +5089,9 @@ describe('TextEditor', () => {
[[1, 6], [1, 10]]
]);
editor.cutSelectedText();
expect(atom.clipboard.read()).toEqual(`quicksort\nsort\nitems`);
expect(atom.clipboard.read()).toEqual(
['quicksort', 'sort', 'items'].join(os.EOL)
);
});
});
});
@@ -5115,7 +5119,9 @@ describe('TextEditor', () => {
expect(buffer.lineForRow(2)).toBe(' if (items.length');
expect(buffer.lineForRow(3)).toBe(' var pivot = item');
expect(atom.clipboard.read()).toBe(
' <= 1) return items;\ns.shift(), current, left = [], right = [];'
` <= 1) return items;${
os.EOL
}s.shift(), current, left = [], right = [];`
);
}));

@@ -5131,7 +5137,7 @@ describe('TextEditor', () => {
);
expect(buffer.lineForRow(3)).toBe(' var pivot = item');
expect(atom.clipboard.read()).toBe(
' <= 1) ret\ns.shift(), current, left = [], right = [];'
` <= 1) ret${os.EOL}s.shift(), current, left = [], right = [];`
);
}));
});
@@ -5151,7 +5157,9 @@ describe('TextEditor', () => {
expect(buffer.lineForRow(2)).toBe(' if (items.length');
expect(buffer.lineForRow(3)).toBe(' var pivot = item');
expect(atom.clipboard.read()).toBe(
' <= 1) return items;\ns.shift(), current, left = [], right = [];'
` <= 1) return items;${
os.EOL
}s.shift(), current, left = [], right = [];`
);
});
});
@@ -5166,7 +5174,7 @@ describe('TextEditor', () => {
expect(buffer.lineForRow(2)).toBe(' if (items.lengthurn items;');
expect(buffer.lineForRow(3)).toBe(' var pivot = item');
expect(atom.clipboard.read()).toBe(
' <= 1) ret\ns.shift(), current, left = [], right = [];'
` <= 1) ret${os.EOL}s.shift(), current, left = [], right = [];`
);
});
});
@@ -5186,8 +5194,12 @@ describe('TextEditor', () => {
expect(buffer.lineForRow(2)).toBe(
' if (items.length <= 1) return items;'
);
expect(clipboard.readText()).toBe('quicksort\nsort\nitems');
expect(atom.clipboard.read()).toEqual('quicksort\nsort\nitems');
expect(clipboard.readText()).toBe(
['quicksort', 'sort', 'items'].join(os.EOL)
);
expect(atom.clipboard.read()).toEqual(
['quicksort', 'sort', 'items'].join(os.EOL)
);
});

describe('when no text is selected', () => {
@@ -5202,9 +5214,9 @@ describe('TextEditor', () => {
editor.copySelectedText();
expect(atom.clipboard.read()).toEqual(
[
' var sort = function(items) {\n',
' current = items.shift();\n'
].join('\n')
` var sort = function(items) {${os.EOL}`,
` current = items.shift();${os.EOL}`
].join(os.EOL)
);
expect(editor.getSelectedBufferRanges()).toEqual([
[[1, 5], [1, 5]],
@@ -5221,7 +5233,9 @@ describe('TextEditor', () => {
[[1, 6], [1, 10]]
]);
editor.copySelectedText();
expect(atom.clipboard.read()).toEqual(`quicksort\nsort\nitems`);
expect(atom.clipboard.read()).toEqual(
['quicksort', 'sort', 'items'].join(os.EOL)
);
});
});
});
@@ -5241,8 +5255,12 @@ describe('TextEditor', () => {
expect(buffer.lineForRow(2)).toBe(
' if (items.length <= 1) return items;'
);
expect(clipboard.readText()).toBe('quicksort\nsort\nitems');
expect(atom.clipboard.read()).toEqual(`quicksort\nsort\nitems`);
expect(clipboard.readText()).toBe(
['quicksort', 'sort', 'items'].join(os.EOL)
);
expect(atom.clipboard.read()).toEqual(
['quicksort', 'sort', 'items'].join(os.EOL)
);
});
});

@@ -5540,7 +5558,7 @@ describe('TextEditor', () => {

expect(editor.lineTextForBufferRow(5)).toBe(' a(x);');
expect(editor.lineTextForBufferRow(6)).toBe(' b(x);');
expect(editor.buffer.lineEndingForRow(6)).toBe('\r\n');
expect(editor.buffer.lineEndingForRow(6)).toBe(os.EOL);
expect(editor.lineTextForBufferRow(7)).toBe('c(x);');
expect(editor.lineTextForBufferRow(8)).toBe(
' current = items.shift();'
2 changes: 2 additions & 0 deletions src/clipboard.js
Original file line number Diff line number Diff line change
@@ -42,6 +42,8 @@ module.exports = class Clipboard {
// * `text` The {String} to store.
// * `metadata` (optional) The additional info to associate with the text.
write(text, metadata) {
text = text.replace(/\r?\n/g, process.platform === 'win32' ? '\r\n' : '\n');

this.signatureForMetadata = this.md5(text);
this.metadata = metadata;
clipboard.writeText(text);

0 comments on commit 38f403a

Please sign in to comment.