From 3f1ade519c9f3f4b061448ad1e92a4d8776013d2 Mon Sep 17 00:00:00 2001 From: Mirco Dotta Date: Mon, 20 Aug 2012 16:34:22 +0200 Subject: [PATCH] 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). --- .../org/scalaide/worksheet/editor/ScriptEditor.scala | 5 +++-- .../src/org/scalaide/worksheet/text/Mixer.scala | 2 +- .../org/scalaide/worksheet/text/SourceInserter.scala | 11 +++++++---- 3 files changed, 11 insertions(+), 7 deletions(-) diff --git a/org.scalaide.worksheet/src/org/scalaide/worksheet/editor/ScriptEditor.scala b/org.scalaide.worksheet/src/org/scalaide/worksheet/editor/ScriptEditor.scala index 4ca44d8..c0cd522 100644 --- a/org.scalaide.worksheet/src/org/scalaide/worksheet/editor/ScriptEditor.scala +++ b/org.scalaide.worksheet/src/org/scalaide/worksheet/editor/ScriptEditor.scala @@ -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 } diff --git a/org.scalaide.worksheet/src/org/scalaide/worksheet/text/Mixer.scala b/org.scalaide.worksheet/src/org/scalaide/worksheet/text/Mixer.scala index 5ad19da..fc0f6af 100644 --- a/org.scalaide.worksheet/src/org/scalaide/worksheet/text/Mixer.scala +++ b/org.scalaide.worksheet/src/org/scalaide/worksheet/text/Mixer.scala @@ -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) diff --git a/org.scalaide.worksheet/src/org/scalaide/worksheet/text/SourceInserter.scala b/org.scalaide.worksheet/src/org/scalaide/worksheet/text/SourceInserter.scala index 87299d1..fc0e79b 100644 --- a/org.scalaide.worksheet/src/org/scalaide/worksheet/text/SourceInserter.scala +++ b/org.scalaide.worksheet/src/org/scalaide/worksheet/text/SourceInserter.scala @@ -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 } }