Skip to content

Commit

Permalink
modification for handling :paredit-[forward|backward]-delete function…
Browse files Browse the repository at this point in the history
…ality
  • Loading branch information
laurentpetit committed Feb 7, 2010
1 parent ab7eddf commit 0ffc000
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 16 deletions.
46 changes: 41 additions & 5 deletions ccw.core/src/ccw/editors/antlrbased/AntlrBasedClojureEditor.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@
import org.eclipse.ui.texteditor.SourceViewerDecorationSupport; import org.eclipse.ui.texteditor.SourceViewerDecorationSupport;
import org.eclipse.ui.views.contentoutline.IContentOutlinePage; import org.eclipse.ui.views.contentoutline.IContentOutlinePage;


import ccw.ClojureCore;
import ccw.CCWPlugin; import ccw.CCWPlugin;
import ccw.ClojureCore;
import ccw.debug.ClojureClient; import ccw.debug.ClojureClient;
import ccw.editors.outline.ClojureOutlinePage; import ccw.editors.outline.ClojureOutlinePage;
import ccw.editors.rulesbased.ClojureDocumentProvider; import ccw.editors.rulesbased.ClojureDocumentProvider;
Expand Down Expand Up @@ -86,7 +86,7 @@ public AntlrBasedClojureEditor() {
CCWPlugin.registerEditorColors(preferenceStore); CCWPlugin.registerEditorColors(preferenceStore);
setSourceViewerConfiguration(new ClojureSourceViewerConfiguration(preferenceStore, this)); setSourceViewerConfiguration(new ClojureSourceViewerConfiguration(preferenceStore, this));
setPreferenceStore(preferenceStore); setPreferenceStore(preferenceStore);
setDocumentProvider(new ClojureDocumentProvider()); setDocumentProvider(new ClojureDocumentProvider());
} }


@Override @Override
Expand Down Expand Up @@ -317,7 +317,7 @@ protected IDocument getDocument() {
* Asserts document != null. * Asserts document != null.
* @return * @return
*/ */
private int getSourceCaretOffset() { public int getSourceCaretOffset() {
IRegion selection= getSignedSelection(getSourceViewer()); IRegion selection= getSignedSelection(getSourceViewer());
return selection.getOffset() + selection.getLength(); return selection.getOffset() + selection.getLength();
} }
Expand Down Expand Up @@ -486,7 +486,10 @@ private boolean matchChar(char c, char[] charsToMatch) {
* @param sourceViewer the source viewer * @param sourceViewer the source viewer
* @return a region denoting the current signed selection, for a resulting RtoL selections length is < 0 * @return a region denoting the current signed selection, for a resulting RtoL selections length is < 0
*/ */
protected IRegion getSignedSelection(ISourceViewer sourceViewer) { public IRegion getSignedSelection(ISourceViewer sourceViewer) {
if (sourceViewer == null) {
sourceViewer = getSourceViewer();
}
StyledText text= sourceViewer.getTextWidget(); StyledText text= sourceViewer.getTextWidget();
Point selection= text.getSelectionRange(); Point selection= text.getSelectionRange();


Expand All @@ -500,6 +503,22 @@ protected IRegion getSignedSelection(ISourceViewer sourceViewer) {
return new Region(selection.x, selection.y); return new Region(selection.x, selection.y);
} }


/**
* Returns the signed current selection.
* The length will be negative if the resulting selection
* is right-to-left (RtoL).
* <p>
* The selection offset is model based.
* </p>
*
* @param sourceViewer the source viewer
* @return a region denoting the current signed selection, for a resulting RtoL selections length is < 0
*/
public IRegion getSignedSelection() {
return getSignedSelection(null);
}


/** /**
* Returns the unsigned current selection. * Returns the unsigned current selection.
* The length will always be positive. * The length will always be positive.
Expand All @@ -510,7 +529,10 @@ protected IRegion getSignedSelection(ISourceViewer sourceViewer) {
* @param sourceViewer the source viewer * @param sourceViewer the source viewer
* @return a region denoting the current unsigned selection * @return a region denoting the current unsigned selection
*/ */
protected IRegion getUnSignedSelection(ISourceViewer sourceViewer) { public IRegion getUnSignedSelection(ISourceViewer sourceViewer) {
if (sourceViewer == null) {
sourceViewer = getSourceViewer();
}
StyledText text= sourceViewer.getTextWidget(); StyledText text= sourceViewer.getTextWidget();
Point selection= text.getSelectionRange(); Point selection= text.getSelectionRange();


Expand All @@ -519,6 +541,20 @@ protected IRegion getUnSignedSelection(ISourceViewer sourceViewer) {
return new Region(selection.x, selection.y); return new Region(selection.x, selection.y);
} }


/**
* Returns the unsigned current selection.
* The length will always be positive.
* <p>
* The selection offset is model based.
* </p>
*
* @param sourceViewer the source viewer
* @return a region denoting the current unsigned selection
*/
public IRegion getUnSignedSelection() {
return getUnSignedSelection(null);
}

/* /*
* @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#initializeKeyBindingScopes() * @see org.eclipse.ui.texteditor.AbstractDecoratedTextEditor#initializeKeyBindingScopes()
*/ */
Expand Down
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ public IReconciler getReconciler(ISourceViewer sourceViewer) {
@Override @Override
public IAutoEditStrategy[] getAutoEditStrategies( public IAutoEditStrategy[] getAutoEditStrategies(
ISourceViewer sourceViewer, final String contentType) { ISourceViewer sourceViewer, final String contentType) {

return new IAutoEditStrategy[] { return new IAutoEditStrategy[] {
new PareditAutoEditStrategy(fPreferenceStore) new PareditAutoEditStrategy(editor, fPreferenceStore)
, new org.eclipse.jface.text.DefaultIndentLineAutoEditStrategy() }; , new org.eclipse.jface.text.DefaultIndentLineAutoEditStrategy() };
} }


Expand Down
25 changes: 15 additions & 10 deletions ccw.core/src/ccw/editors/antlrbased/PareditAutoEditStrategy.clj
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
(:import (:import
[org.eclipse.jface.text IAutoEditStrategy [org.eclipse.jface.text IAutoEditStrategy
IDocument IDocument
DocumentCommand]) DocumentCommand]
[ccw.editors.antlrbased AntlrBasedClojureEditor])
(:gen-class (:gen-class
:implements [org.eclipse.jface.text.IAutoEditStrategy] :implements [org.eclipse.jface.text.IAutoEditStrategy]
:constructors {[org.eclipse.jface.preference.IPreferenceStore] []} :constructors {[ccw.editors.antlrbased.AntlrBasedClojureEditor org.eclipse.jface.preference.IPreferenceStore] []}
:init init :init init
:state state)) :state state))


(defn- -init (defn- -init
[preference-store] [[] (ref {:prefs-store preference-store})]) [editor preference-store] [[] (ref {:editor editor :prefs-store preference-store})])


; TODO move this into paredit itself ... ; TODO move this into paredit itself ...
(def *one-char-command* (def *one-char-command*
Expand All @@ -26,18 +27,20 @@


(defn- call-paredit [command document-text] (defn- call-paredit [command document-text]
(cond (cond
(and (zero? (:length command)) (and (zero? (:length command)) ; TODO enhance to also handle the replace of a bunch of text
(contains? *one-char-command* (:text command))) (contains? *one-char-command* (:text command)))
(paredit (get *one-char-command* (:text command)) (paredit (get *one-char-command* (:text command))
{:text document-text {:text (:text document-text)
:offset (:offset command) :offset (:offset command)
:length 0}) :length 0})
(and (zero? (-> command :text .length)) (and (zero? (-> command :text .length))
(= 1 (:length command))) (= 1 (:length command)))
(paredit :paredit-forward-delete ; TODO how to distinguish from forward delete and backward delete ? (let [paredit-command (if (= (:offset command) (:caret-offset document-text))
{:text document-text :paredit-forward-delete :paredit-backward-delete)]
:offset (:offset command) (paredit paredit-command
:length 1}))) {:text (:text document-text)
:offset (if (= paredit-command :paredit-backward-delete) (inc (:offset command)) (:offset command))
:length 1}))))




(defn paredit-enabled? (defn paredit-enabled?
Expand All @@ -48,7 +51,9 @@
[#^IAutoEditStrategy this, #^IDocument document, #^DocumentCommand command] [#^IAutoEditStrategy this, #^IDocument document, #^DocumentCommand command]
(when (and (paredit-enabled? (-> this .state deref :prefs-store)) (when (and (paredit-enabled? (-> this .state deref :prefs-store))
(.doit command)) (.doit command))
(let [document-text (.get document)
(let [signed-selection (bean (-> this .state deref :editor .getSignedSelection))
document-text {:text (.get document) :caret-offset (+ (:offset signed-selection) (:length signed-selection)) :selection-length (:length signed-selection)}
par-command {:text (.text command) :offset (.offset command) :length (.length command)} par-command {:text (.text command) :offset (.offset command) :length (.length command)}
result (call-paredit par-command document-text)] result (call-paredit par-command document-text)]
(when (and result (not= :ko (-> result :parser-state))) (when (and result (not= :ko (-> result :parser-state)))
Expand Down

0 comments on commit 0ffc000

Please sign in to comment.