add proper new line handling including new line character detection
This commit is contained in:
parent
c34324fe70
commit
10e35ef507
2 changed files with 82 additions and 3 deletions
|
|
@ -3,7 +3,7 @@ ace.provide("ace.Document");
|
||||||
ace.Document = function(text, mode) {
|
ace.Document = function(text, mode) {
|
||||||
this.$initEvents();
|
this.$initEvents();
|
||||||
|
|
||||||
this.lines = this.$split(text);
|
this.lines = [];
|
||||||
this.modified = true;
|
this.modified = true;
|
||||||
this.selection = new ace.Selection(this);
|
this.selection = new ace.Selection(this);
|
||||||
|
|
||||||
|
|
@ -11,6 +11,8 @@ ace.Document = function(text, mode) {
|
||||||
if (mode) {
|
if (mode) {
|
||||||
this.setMode(mode);
|
this.setMode(mode);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
this.insert({row: 0, column: 0}, text);
|
||||||
};
|
};
|
||||||
|
|
||||||
(function() {
|
(function() {
|
||||||
|
|
@ -22,7 +24,7 @@ ace.Document = function(text, mode) {
|
||||||
};
|
};
|
||||||
|
|
||||||
this.toString = function() {
|
this.toString = function() {
|
||||||
return this.lines.join("\n");
|
return this.lines.join(this.$getNewLineCharacter());
|
||||||
};
|
};
|
||||||
|
|
||||||
this.getSelection = function() {
|
this.getSelection = function() {
|
||||||
|
|
@ -68,6 +70,41 @@ ace.Document = function(text, mode) {
|
||||||
return this.$tabSize;
|
return this.$tabSize;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
this.$detectNewLine = function(text) {
|
||||||
|
var match = text.match(/^.*?(\r?\n)/m);
|
||||||
|
console.log(match);
|
||||||
|
if (match) {
|
||||||
|
this.$autoNewLine = match[1];
|
||||||
|
} else {
|
||||||
|
this.$autoNewLine = "\n";
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
this.$getNewLineCharacter = function() {
|
||||||
|
switch (this.$newLineMode) {
|
||||||
|
case "windows":
|
||||||
|
return "\r\n";
|
||||||
|
|
||||||
|
case "unix":
|
||||||
|
return "\n";
|
||||||
|
|
||||||
|
case "auto":
|
||||||
|
return this.$autoNewLine;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
|
||||||
|
this.$autoNewLine = "\n";
|
||||||
|
this.$newLineMode = "auto";
|
||||||
|
this.setNewLineMode = function(newLineMode) {
|
||||||
|
if (this.$newLineMode === newLineMode) return;
|
||||||
|
|
||||||
|
this.$newLineMode = newLineMode;
|
||||||
|
};
|
||||||
|
|
||||||
|
this.getNewLineMode = function() {
|
||||||
|
return this.$newLineMode;
|
||||||
|
};
|
||||||
|
|
||||||
this.$mode = null;
|
this.$mode = null;
|
||||||
this.setMode = function(mode) {
|
this.setMode = function(mode) {
|
||||||
if (this.$mode === mode) return;
|
if (this.$mode === mode) return;
|
||||||
|
|
@ -127,7 +164,7 @@ ace.Document = function(text, mode) {
|
||||||
lines.push(this.lines[range.start.row].substring(range.start.column));
|
lines.push(this.lines[range.start.row].substring(range.start.column));
|
||||||
lines.push.apply(lines, this.getLines(range.start.row+1, range.end.row-1));
|
lines.push.apply(lines, this.getLines(range.start.row+1, range.end.row-1));
|
||||||
lines.push(this.lines[range.end.row].substring(0, range.end.column));
|
lines.push(this.lines[range.end.row].substring(0, range.end.column));
|
||||||
return lines.join("\n");
|
return lines.join(this.$getNewLineCharacter());
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -242,6 +279,9 @@ ace.Document = function(text, mode) {
|
||||||
|
|
||||||
this.$insert = function(position, text) {
|
this.$insert = function(position, text) {
|
||||||
this.modified = true;
|
this.modified = true;
|
||||||
|
if (this.lines.length <= 1) {
|
||||||
|
this.$detectNewLine(text);
|
||||||
|
}
|
||||||
|
|
||||||
var newLines = this.$split(text);
|
var newLines = this.$split(text);
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -84,5 +84,44 @@ var TextDocumentTest = new TestCase("TextDocumentTest", {
|
||||||
|
|
||||||
doc.duplicateLines(0, 0);
|
doc.duplicateLines(0, 0);
|
||||||
assertEquals(["1", "1", "2", "3"].join("\n"), doc.toString());
|
assertEquals(["1", "1", "2", "3"].join("\n"), doc.toString());
|
||||||
|
},
|
||||||
|
|
||||||
|
"test: should handle unix style new lines" : function() {
|
||||||
|
var doc = new ace.Document(["1", "2", "3"].join("\n"));
|
||||||
|
assertEquals(["1", "2", "3"].join("\n"), doc.toString());
|
||||||
|
},
|
||||||
|
|
||||||
|
"test: should handle windows style new lines" : function() {
|
||||||
|
var doc = new ace.Document(["1", "2", "3"].join("\r\n"));
|
||||||
|
doc.setNewLineMode("unix");
|
||||||
|
assertEquals(["1", "2", "3"].join("\n"), doc.toString());
|
||||||
|
},
|
||||||
|
|
||||||
|
"test: set new line mode to 'windows' should use '\r\n' as new lines": function() {
|
||||||
|
var doc = new ace.Document(["1", "2", "3"].join("\n"));
|
||||||
|
doc.setNewLineMode("windows");
|
||||||
|
assertEquals(["1", "2", "3"].join("\r\n"), doc.toString());
|
||||||
|
},
|
||||||
|
|
||||||
|
"test: set new line mode to 'unix' should use '\n' as new lines": function() {
|
||||||
|
var doc = new ace.Document(["1", "2", "3"].join("\r\n"));
|
||||||
|
doc.setNewLineMode("unix");
|
||||||
|
assertEquals(["1", "2", "3"].join("\n"), doc.toString());
|
||||||
|
},
|
||||||
|
|
||||||
|
"test: set new line mode to 'auto' should use detect the incoming nl type": function() {
|
||||||
|
var doc = new ace.Document(["1", "2", "3"].join("\n"));
|
||||||
|
doc.setNewLineMode("auto");
|
||||||
|
assertEquals(["1", "2", "3"].join("\n"), doc.toString());
|
||||||
|
|
||||||
|
var doc = new ace.Document(["1", "2", "3"].join("\r\n"));
|
||||||
|
doc.setNewLineMode("auto");
|
||||||
|
assertEquals(["1", "2", "3"].join("\r\n"), doc.toString());
|
||||||
|
|
||||||
|
doc.replace({
|
||||||
|
start: {row: 0, column: 0},
|
||||||
|
end: {row: 2, column: 1}
|
||||||
|
}, ["4", "5", "6"].join("\n"));
|
||||||
|
assertEquals(["4", "5", "6"].join("\n"), doc.toString());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
Loading…
Add table
Add a link
Reference in a new issue