Skip to content

Commit

Permalink
visual feedback (editor background in gray+tooltip message) when unpa…
Browse files Browse the repository at this point in the history
…rseable state of the editor
  • Loading branch information
laurentpetit committed Dec 4, 2010
1 parent 6d48b84 commit 0ff0360
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
34 changes: 32 additions & 2 deletions ccw.core/src/ccw/editors/antlrbased/ClojureSourceViewer.java
Expand Up @@ -21,6 +21,7 @@
import org.eclipse.jface.text.ITextInputListener;
import org.eclipse.jface.text.Region;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.IOverviewRuler;
import org.eclipse.jface.text.source.IVerticalRuler;
import org.eclipse.jface.text.source.SourceViewerConfiguration;
Expand All @@ -42,6 +43,7 @@
import ccw.CCWPlugin;
import ccw.ClojureCore;
import ccw.repl.REPLView;
import ccw.util.DisplayUtil;

public abstract class ClojureSourceViewer extends ProjectionViewer implements
IClojureEditor, IPropertyChangeListener {
Expand Down Expand Up @@ -287,7 +289,11 @@ public void documentChanged(DocumentEvent event) {
};

private void updateParseRef (String text) {
boolean firstTime = (parseRef == null);
parseRef = EditorSupport.updateParseRef(text, parseRef);
if (firstTime) {
EditorSupport.startWatchParseRef(parseRef, this);
}
}

public Object getParsed () {
Expand All @@ -297,6 +303,21 @@ public Object getParsed () {
return EditorSupport.getParser(getDocument().get(), parseRef);
}

private boolean structuralEditionPossible = true;
public void setStructuralEditionPossible(final boolean state) {
structuralEditionPossible = state;
syncWithStructuralEditionPossibleState();
}
private void syncWithStructuralEditionPossibleState() {
DisplayUtil.asyncExec(new Runnable() {
public void run() {
getTextWidget().setBackground(
structuralEditionPossible ? null : Display.getCurrent().getSystemColor(SWT.COLOR_GRAY));
getTextWidget().setToolTipText(structuralEditionPossible ? null : "Unparseable source code. Structural Edition temporarily disabled.");
}
});
}

public IRegion getSignedSelection () {
StyledText text = getTextWidget();
Point selection = text.getSelectionRange();
Expand Down Expand Up @@ -345,7 +366,16 @@ public REPLView getCorrespondingREPL () {
return null;
}

public void setStructuralEditingPossible (boolean possible) {}

public void updateTabsToSpacesConverter () {}

// TODO get rid of this way of handling document initialization
@Override
public void setDocument(IDocument document,
IAnnotationModel annotationModel, int modelRangeOffset,
int modelRangeLength) {
super.setDocument(document, annotationModel, modelRangeOffset, modelRangeLength);
if (document != null) {
updateParseRef(document.get());
}
}
}
10 changes: 9 additions & 1 deletion ccw.core/src/ccw/editors/antlrbased/EditorSupport.clj
Expand Up @@ -13,7 +13,8 @@
(require [paredit.parser :as p])
(:gen-class
:methods [^{:static true} [updateParseRef [String Object] Object]
^{:static true} [getParser [String Object] Object]]))
^{:static true} [getParser [String Object] Object]
^{:static true} [startWatchParseRef [Object Object] Object]]))

; TODO move in a utility namespace, or remove
(defprotocol Cancellable (isCancelled [this]) (cancel [this]))
Expand All @@ -36,6 +37,13 @@
(ref-set r {:text text :parser (timed-delay 800 #(try (p/parse text) (catch Exception _ nil)))}))
r))

(defn -startWatchParseRef [r editor]
(add-watch r :track-state (fn [_key _r _old-state new-state]
(.setStructuralEditionPossible editor
(let [state (not (nil? @(:parser new-state)))
state (if (.isEmpty (:text new-state)) true state)]
state)))))

(defn -getParser [text r]
(let [rv @r]
(if (= text (:text rv))
Expand Down
2 changes: 0 additions & 2 deletions ccw.core/src/ccw/editors/antlrbased/IClojureEditor.java
Expand Up @@ -71,8 +71,6 @@ public interface IClojureEditor {
*/
public REPLView getCorrespondingREPL ();

public void setStructuralEditingPossible (boolean possible);

public void updateTabsToSpacesConverter ();

public IDocument getDocument();
Expand Down
Expand Up @@ -116,4 +116,4 @@
(when-not (zero? (:length result))
;(println (str "result:" result))
(.selectAndReveal editor (:offset result) (:length result))))
(.setStructuralEditingPossible editor (true? (and result (not= :ko (-> result :parser-state)))))))))
#_(.setStructuralEditingPossible editor (true? (and result (not= :ko (-> result :parser-state)))))))))

0 comments on commit 0ff0360

Please sign in to comment.