Permalink
Browse files

Fix #25 - Caret is kept at the same position after evaluation

* Empty lines are no longer stripped, the reasons are:
    + Keep correct indentation of empty lines.
    + Correct handling of caret position (i.e., if the caret is
      in the middle of an empty line, it should stay there after
      evaluation).
* The new caret position can never be after the old caret position
  (this should motivate the change in Mixer.scala)

This fixes the issue with correctly updating the caret position in
most of the situations, but not all of them. Honestly, I don't think
we should spend more time with trying to improve this, because messing
with the user's document is just wrong. Instead, we should split the
editor into two views: a left one used by the user to write code, and
a right to show the evaluation result (#70).
  • Loading branch information...
1 parent 2dd53f9 commit 3f1ade519c9f3f4b061448ad1e92a4d8776013d2 @dotta committed Aug 20, 2012
@@ -74,8 +74,9 @@ class ScriptEditor extends TextEditor with SelectionTracker with ISourceViewerEd
override def caretOffset: Int = {
var offset = -1
- runInUi { //FIXME: Can I make this non-blocking!?
- offset = getSourceViewer().getTextWidget().getCaretOffset()
+ SWTUtils.syncExec {
+ // Read the comment in `runInUi`
+ if(!disposed) offset = getSourceViewer().getTextWidget().getCaretOffset()
}
offset
}
@@ -82,7 +82,7 @@ class Mixer {
align()
insert(sep: _*)
insert(cs: _*)
- if (written < caret) newcaret = caret + inserted
+ if (written < caret) newcaret = scala.math.min(caret + inserted, caret)
}
mixed ++= source.view(written, source.length)
(mixed.toArray, newcaret)
@@ -12,11 +12,14 @@ object SourceInserter {
def leftPart(str: String) =
(str split """//>|//\|""").head
def isContinuation(str: String) =
- ((str contains "//>") || (str contains "//|")) && (leftPart(str) forall Character.isWhitespace)
- def stripTrailingWS(str: String) =
- str take (str lastIndexWhere (!Character.isWhitespace(_))) + 1
+ ((str contains "//>") || (str contains "//|")) && isEmpty(leftPart(str))
+ def isEmpty(str: String) = str forall Character.isWhitespace
+ def stripTrailingWS(str: String) = str take (str lastIndexWhere (!Character.isWhitespace(_))) + 1
+ def stripTrailingWSIfNonEmpty(str: String) =
+ if(isEmpty(str)) str
+ else stripTrailingWS(str)
val prefixes =
- lines filterNot isContinuation map leftPart map stripTrailingWS
+ lines filterNot isContinuation map leftPart map stripTrailingWSIfNonEmpty
(prefixes mkString "\n").toArray
}
}

0 comments on commit 3f1ade5

Please sign in to comment.