Permalink
Browse files

Fixes auto correct can destroy content. Closes #321

Auto correct on save processes the content from start to end using the
original string and a regexp matcher find approach. The returned offset
needed to be adjusted for already made replacements. This fix simply
keeps track of diff in offset up to the point of replacement and makes
use of that when making the next replacement.
  • Loading branch information...
1 parent b18897e commit 57facf2f914b6bbce1bbf52ee09e6246ab020721 @hlindberg hlindberg committed May 10, 2012
@@ -83,34 +83,37 @@ public void perform(IResource r, IXtextDocument document) {
Matcher matcher = trimPattern.matcher(content);
boolean mustRefetch = false;
;
+ int lengthAdjustment = 0;
while(matcher.find()) {
int offset = matcher.start();
int length = matcher.end() - offset;
try {
- document.replace(offset, length, matcher.group(1));
+ String replacement = matcher.group(1);
+ document.replace(offset - lengthAdjustment, length, replacement);
+ lengthAdjustment += (length - replacement.length());
mustRefetch = true;
}
catch(BadLocationException e) {
// ignore
}
}
- // content = trimPattern.matcher(content).replaceAll("$1");
if(mustRefetch)
content = document.get();
}
if(replaceFunkySpace) {
Matcher matcher = funkySpacePattern.matcher(content);
+ int lengthAdjustment = 0;
while(matcher.find()) {
int offset = matcher.start();
int length = matcher.end() - offset;
try {
- document.replace(offset, length, " ");
+ document.replace(offset - lengthAdjustment, length, " ");
+ lengthAdjustment += length - 1;
}
catch(BadLocationException e) {
// ignore
}
}
- content = funkySpacePattern.matcher(content).replaceAll(" ");
}
}
// // USE THIS IF SEMANTIC CHANGES ARE NEEDED LATER

0 comments on commit 57facf2

Please sign in to comment.