Skip to content

Commit

Permalink
First attempt to integrate merge view for auto-format changes
Browse files Browse the repository at this point in the history
- relates #10, #14
  • Loading branch information
zz85 committed Jan 1, 2015
1 parent 1b288f0 commit 7a2ade5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
29 changes: 26 additions & 3 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ <h1>Is your code style approved by <a href="https://twitter.com/mrdoob">mrdoob</

}
</textarea>
<div id="merge"></div>

<div id="loading">Loading</div>

Expand All @@ -124,6 +125,7 @@ <h1>Is your code style approved by <a href="https://twitter.com/mrdoob">mrdoob</
<script src="jscs-browser.js"></script>
<link rel="stylesheet" href="bower_components/codemirror/lib/codemirror.css">
<link rel="stylesheet" href="bower_components/codemirror/addon/lint/lint.css">
<link rel="stylesheet" href="bower_components/codemirror/addon/merge/merge.css">

<script src="bower_components/codemirror/lib/codemirror.js"></script>
<script src="bower_components/codemirror/mode/javascript/javascript.js"></script>
Expand All @@ -135,6 +137,8 @@ <h1>Is your code style approved by <a href="https://twitter.com/mrdoob">mrdoob</
<script src="bower_components/codemirror/addon/lint/lint.js"></script>
<script src="mdcs-lint.js"></script>

<script src="bower_components/google-diff-match-patch-js/diff_match_patch.js"></script>
<script src="bower_components/codemirror/addon/merge/merge.js"></script>

<button onclick="check()">Check</button>
Auto-check <input type="checkbox" id="autocheck" checked />
Expand All @@ -156,9 +160,9 @@ <h1>Is your code style approved by <a href="https://twitter.com/mrdoob">mrdoob</
var answer = document.getElementById('answer');
var hints = document.getElementById('hints');
var autocheck = document.getElementById('autocheck');
var merge = document.getElementById('merge');

var deferredValidator;

var cmOptions = {
value: textarea.value,
mode: 'javascript',
Expand All @@ -177,7 +181,14 @@ <h1>Is your code style approved by <a href="https://twitter.com/mrdoob">mrdoob</

if (autocheck.checked) validator(text);
}
}
},
// merge plugin - for our case "orginal" means our "formatted" version.
origRight: '',
// connect: 'align',
collapseIdentical: true,
allowEditingOriginals: false,
showDifferences: true,
revertButtons: true
};

var checker, codeEditor;
Expand All @@ -202,7 +213,10 @@ <h1>Is your code style approved by <a href="https://twitter.com/mrdoob">mrdoob</
/*
* Init Code CodeMirror
*/
codeEditor = CodeMirror.fromTextArea( textarea, cmOptions );
// codeEditor = CodeMirror.fromTextArea( textarea, cmOptions );
mergeView = CodeMirror.MergeView(merge, cmOptions);
codeEditor = mergeView.editor();
formatEditor = mergeView.rightOriginal();

}

Expand Down Expand Up @@ -277,10 +291,19 @@ <h1>Is your code style approved by <a href="https://twitter.com/mrdoob">mrdoob</
var code = codeEditor.getValue();

try {
console.time('jscs format');
formatEditor.setValue( checker.formatString(code + '\n') );

/*
// Reverse approach
codeEditor.setValue( checker.formatString(code + '\n') );
formatEditor.setValue( code );
*/
} catch (e) {
alert('oops, i cant format that right now...')
throw(e);
} finally {
console.timeEnd('jscs format');
}
}
</script>
Expand Down
2 changes: 2 additions & 0 deletions mdcs-lint.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

function validator(text, options) {
var jscsErrors = jscsCheck( text );
if (!jscsErrors) return [];

var errorList = jscsErrors.getErrorList();

var hintErrors;
Expand Down

0 comments on commit 7a2ade5

Please sign in to comment.