Skip to content

Commit

Permalink
fix: coping \r\n (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
moshfeu authored Feb 12, 2021
1 parent 0d5422d commit b713a70
Show file tree
Hide file tree
Showing 11 changed files with 435 additions and 28 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"name": "diff-merge",
"displayName": "Diff & Merge",
"description": "Show diffs and merge",
"version": "0.4.2",
"version": "0.4.3",
"repository": {
"type": "git",
"url": "https://github.com/moshfeu/vscode-diff-merge"
Expand Down
3 changes: 2 additions & 1 deletion resources/monaco/jsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"checkJs": true
"checkJs": true,
"target": "es2015",
},
"exclude": ["node_modules"]
}
4 changes: 4 additions & 0 deletions resources/monaco/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@
"scripts": {
"start": "http-server -p 12345",
"build": "webpack",
"mocha": "mocha dist/test.bundle.js",
"test:unit": "webpack --config webpack.config.test.js && yarn mocha",
"test:unit:dev": "webpack --config webpack.config.test.dev.js",
"build:dev": "webpack --config webpack.config.dev.js --watch"
},
"dependencies": {
Expand All @@ -15,6 +18,7 @@
"css-loader": "^3.2.1",
"file-loader": "^5.0.2",
"http-server": "^0.12.0",
"mocha": "^8.3.0",
"style-loader": "^1.0.1",
"webpack": "^4.41.2",
"webpack-cli": "^3.3.10",
Expand Down
19 changes: 19 additions & 0 deletions resources/monaco/src/tests/utils.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const assert = require('assert');
const { getChangeOriginalValue } = require('../utils');

describe('getChangeOriginalValue', () => {
it('\\r + \\n', () => {
const originalValue = getChangeOriginalValue(
{
originalStartLineNumber: 1,
originalEndLineNumber: 2,
},
{
originalEditor: {
getValue: () => `<?php\r\ndefine('SUSPECT_THRESHOLD', 0.3);`,
},
}
);
assert.strictEqual(originalValue.includes('\r'), false);
});
});
9 changes: 8 additions & 1 deletion resources/monaco/src/utils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/// <reference path="../node_modules/monaco-editor/monaco.d.ts" />

let cacheActionsLines = [];
let diffActionsNode,
diffEditor,
Expand Down Expand Up @@ -51,6 +53,10 @@ export function swap() {
setEditorValue(modifiedEditor, leftValue);
}

/**
* @param {string} path
* @param {string} qs
*/
function generateMonacoFakeUri(path, qs) {
if (path) {
const prefixPath = path.startsWith('/') ? '' : '/';
Expand Down Expand Up @@ -150,9 +156,10 @@ function applyOriginalLines(originalLines, replacer, diffEditor) {
diffEditor.modifiedEditor.executeEdits('diff-merge', [diff]);
}

function getChangeOriginalValue(change, diffEditor) {
export function getChangeOriginalValue(change, diffEditor) {
return diffEditor.originalEditor
.getValue()
.replace(/\r\n/g, '\n')
.split(/(?<=[\n\r])/gm)
.slice(change.originalStartLineNumber - 1, change.originalEndLineNumber)
.join('');
Expand Down
4 changes: 2 additions & 2 deletions resources/monaco/webpack.config.dev.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ const merge = require('webpack-merge');
module.exports = merge(config, {
mode: 'development',
watch: true,
devtool: 'eval-source-map'
});
devtool: 'inline-source-map',
});
18 changes: 18 additions & 0 deletions resources/monaco/webpack.config.test.dev.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
const { spawn } = require('child_process');
const config = require('./webpack.config.test');
const { scripts } = require('./package.json');

module.exports = {
...config,
watch: true,
plugins: [
{
apply: (compiler) => {
compiler.hooks.afterCompile.tap('jest', () => {
const [command, ...args] = scripts.mocha.split(/ /g);
spawn(command, args, { stdio: 'inherit' });
});
},
},
],
};
11 changes: 11 additions & 0 deletions resources/monaco/webpack.config.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const config = require('./webpack.config.dev');

module.exports = {
...config,
mode: 'development',
watch: false,
devtool: 'inline-source-map',
entry: {
test: './src/tests/utils.test.js',
},
};
Loading

0 comments on commit b713a70

Please sign in to comment.